From aec5441507d8875bdf068af4b4d8a25728f8ac06 Mon Sep 17 00:00:00 2001 From: "Michael B. Gale" Date: Fri, 10 Jul 2026 13:18:45 +0100 Subject: [PATCH] Add convenience functions to produce `CheckSchemaResult` values --- lib/entry-points.js | 10 ++++++++-- src/json/index.ts | 30 +++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/lib/entry-points.js b/lib/entry-points.js index 191e25b23..e8b673085 100644 --- a/lib/entry-points.js +++ b/lib/entry-points.js @@ -144527,7 +144527,7 @@ function optionalOrNull(validator) { }, check: (val, path29) => { if (val === void 0 || val === null) { - return { valid: true, unknownKeys: [] }; + return successfulCheckSchema(); } return validator.check(val, path29); }, @@ -144538,8 +144538,14 @@ function validateSchema(schema, obj) { const result = checkSchema(schema, obj, { failFast: true }); return result.valid; } +function successfulCheckSchema() { + return { + valid: true, + unknownKeys: [] + }; +} function checkSchema(schema, obj, options = {}, path29 = "") { - const result = { valid: true, unknownKeys: [] }; + const result = successfulCheckSchema(); const inputKeys = new Set(Object.keys(obj)); for (const [key, validator] of Object.entries(schema)) { const hasKey = key in obj; diff --git a/src/json/index.ts b/src/json/index.ts index b5c965098..7bc23271e 100644 --- a/src/json/index.ts +++ b/src/json/index.ts @@ -86,7 +86,7 @@ export function array(validator: Validator) { return { validate, check: (val: unknown, path: string) => { - const result: CheckSchemaResult = { valid: true, unknownKeys: [] }; + const result: CheckSchemaResult = successfulCheckSchema(); if (!isArray(val)) { result.valid = false; @@ -123,7 +123,7 @@ export function object< }, check: (val, path) => { if (!isObject(val)) { - return { valid: false, unknownKeys: [] }; + return invalidCheckSchema(); } return checkSchema(schema, val, {}, path); }, @@ -142,7 +142,7 @@ export function optionalOrNull(validator: Validator) { }, check: (val, path) => { if (val === undefined || val === null) { - return { valid: true, unknownKeys: [] }; + return successfulCheckSchema(); } return validator.check(val, path); }, @@ -161,7 +161,7 @@ export function optional(validator: Validator) { }, check: (val, path) => { if (val === undefined) { - return { valid: true, unknownKeys: [] }; + return successfulCheckSchema(); } return validator.check(val, path); }, @@ -214,13 +214,33 @@ export interface CheckSchemaResult { unknownKeys: string[]; } +/** + * Convenience function to produce a `CheckSchemaResult` where `valid: true`. + */ +function successfulCheckSchema(): CheckSchemaResult { + return { + valid: true, + unknownKeys: [], + }; +} + +/** + * Convenience function to produce a `CheckSchemaResult` where `valid: false`. + */ +function invalidCheckSchema(): CheckSchemaResult { + return { + valid: false, + unknownKeys: [], + }; +} + export function checkSchema( schema: S, obj: UnvalidatedObject, options: CheckSchemaOptions = {}, path: string = "", ): CheckSchemaResult { - const result: CheckSchemaResult = { valid: true, unknownKeys: [] }; + const result: CheckSchemaResult = successfulCheckSchema(); const inputKeys = new Set(Object.keys(obj)); for (const [key, validator] of Object.entries(schema)) {