From 0b10dee3eedd74fb04bf7e96b85cc5851eb684be Mon Sep 17 00:00:00 2001 From: "Michael B. Gale" Date: Fri, 10 Jul 2026 14:24:33 +0100 Subject: [PATCH] Propagate `CheckSchemaOptions` to allow `failFast` to work as intended --- lib/entry-points.js | 21 ++++++++++++--------- src/json/index.ts | 29 +++++++++++++++++++---------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/lib/entry-points.js b/lib/entry-points.js index 122685839..8ef11482f 100644 --- a/lib/entry-points.js +++ b/lib/entry-points.js @@ -144526,7 +144526,7 @@ function array(validator) { }; return { validate, - check: (val, path29) => { + check: (val, opts, path29) => { const result = successfulCheckSchema(); if (!isArray(val)) { result.valid = false; @@ -144535,12 +144535,15 @@ function array(validator) { let index2 = 0; for (const e of val) { const elementPath = `${path29}[${index2}]`; - const eResult = validator.check(e, `${elementPath}`); + const eResult = validator.check(e, opts, `${elementPath}`); result.unknownKeys.push(...eResult.unknownKeys); index2++; if (!eResult.valid) { result.valid = false; result.invalidKeys.push(elementPath); + if (opts.failFast) { + return result; + } continue; } } @@ -144554,11 +144557,11 @@ function object(schema) { validate: (val) => { return isObject(val) && validateSchema(schema, val); }, - check: (val, path29) => { + check: (val, opts, path29) => { if (!isObject(val)) { return invalidCheckSchema(); } - return checkSchema(schema, val, {}, path29); + return checkSchema(schema, val, opts, path29); }, required: true }; @@ -144568,11 +144571,11 @@ function optionalOrNull(validator) { validate: (val) => { return val === void 0 || val === null || validator.validate(val); }, - check: (val, path29) => { + check: (val, opts, path29) => { if (val === void 0 || val === null) { return successfulCheckSchema(); } - return validator.check(val, path29); + return validator.check(val, opts, path29); }, required: false }; @@ -144582,11 +144585,11 @@ function optional(validator) { validate: (val) => { return val === void 0 || validator.validate(val); }, - check: (val, path29) => { + check: (val, opts, path29) => { if (val === void 0) { return successfulCheckSchema(); } - return validator.check(val, path29); + return validator.check(val, opts, path29); }, required: false }; @@ -144632,7 +144635,7 @@ function checkSchema(schema, obj, options = {}, path29 = "") { continue; } if (hasKey) { - const checkResult = validator.check(obj[key], `${path29}.${key}`); + const checkResult = validator.check(obj[key], options, `${path29}.${key}`); result.unknownKeys.push(...checkResult.unknownKeys); result.invalidKeys.push(...checkResult.invalidKeys); if (checkResult.invalidKeys.length > 0) { diff --git a/src/json/index.ts b/src/json/index.ts index a5c89f722..590a1eb6b 100644 --- a/src/json/index.ts +++ b/src/json/index.ts @@ -48,7 +48,11 @@ export function isStringOrUndefined( */ export type Validator = { validate: (val: unknown) => val is T; - check: (val: unknown, path: string) => CheckSchemaResult; + check: ( + val: unknown, + opts: CheckSchemaOptions, + path: string, + ) => CheckSchemaResult; required: boolean; }; @@ -85,7 +89,7 @@ export function array(validator: Validator) { }; return { validate, - check: (val: unknown, path: string) => { + check: (val: unknown, opts: CheckSchemaOptions, path: string) => { const result: CheckSchemaResult = successfulCheckSchema(); // The value must be an array. @@ -98,7 +102,7 @@ export function array(validator: Validator) { let index = 0; for (const e of val) { const elementPath = `${path}[${index}]`; - const eResult = validator.check(e, `${elementPath}`); + const eResult = validator.check(e, opts, `${elementPath}`); result.unknownKeys.push(...eResult.unknownKeys); index++; @@ -106,6 +110,11 @@ export function array(validator: Validator) { if (!eResult.valid) { result.valid = false; result.invalidKeys.push(elementPath); + + if (opts.failFast) { + return result; + } + continue; } } @@ -125,11 +134,11 @@ export function object< validate: (val: unknown) => { return isObject(val) && validateSchema(schema, val); }, - check: (val, path) => { + check: (val, opts, path) => { if (!isObject(val)) { return invalidCheckSchema(); } - return checkSchema(schema, val, {}, path); + return checkSchema(schema, val, opts, path); }, required: true, } as const satisfies Validator; @@ -144,11 +153,11 @@ export function optionalOrNull(validator: Validator) { validate: (val: unknown) => { return val === undefined || val === null || validator.validate(val); }, - check: (val, path) => { + check: (val, opts, path) => { if (val === undefined || val === null) { return successfulCheckSchema(); } - return validator.check(val, path); + return validator.check(val, opts, path); }, required: false, } as const satisfies Validator; @@ -163,11 +172,11 @@ export function optional(validator: Validator) { validate: (val: unknown): val is T | undefined => { return val === undefined || validator.validate(val); }, - check: (val, path) => { + check: (val, opts, path) => { if (val === undefined) { return successfulCheckSchema(); } - return validator.check(val, path); + return validator.check(val, opts, path); }, required: false, } as const satisfies Validator; @@ -284,7 +293,7 @@ export function checkSchema( // If the property is present, validate it. if (hasKey) { - const checkResult = validator.check(obj[key], `${path}.${key}`); + const checkResult = validator.check(obj[key], options, `${path}.${key}`); result.unknownKeys.push(...checkResult.unknownKeys); result.invalidKeys.push(...checkResult.invalidKeys);