From de4c7ccfab05f6100779f5e60b37e42a18e664f3 Mon Sep 17 00:00:00 2001 From: TizzySaurus Date: Sat, 26 Oct 2024 12:18:07 +0100 Subject: [PATCH] Use ctx.hasError instead of manually tracking ctx errors --- ark/jsonschema/object.ts | 60 +++++++++++++++------------------------- 1 file changed, 23 insertions(+), 37 deletions(-) diff --git a/ark/jsonschema/object.ts b/ark/jsonschema/object.ts index 591a8a60f..1b1797420 100644 --- a/ark/jsonschema/object.ts +++ b/ark/jsonschema/object.ts @@ -82,22 +82,18 @@ const parsePatternProperties = ( // NB: We don't validate compatability of schemas for overlapping patternProperties // since getting the intersection of regexes is inherently non-trivial. return (data: object, ctx: TraversalContext) => { - const errors: false[] = [] - Object.entries(data).forEach(([dataKey, dataValue]) => { patternProperties.forEach(([pattern, parsedJsonSchema]) => { if (pattern.test(dataKey) && !parsedJsonSchema.allows(dataValue)) { - errors.push( - ctx.reject({ - path: [dataKey], - expected: `${parsedJsonSchema.description}, as property ${dataKey} matches patternProperty ${pattern}`, - actual: printable(dataValue) - }) - ) + ctx.reject({ + path: [dataKey], + expected: `${parsedJsonSchema.description}, as property ${dataKey} matches patternProperty ${pattern}`, + actual: printable(dataValue) + }) } }) }) - return errors.length === 0 + return ctx.hasError() } } @@ -121,20 +117,16 @@ const parsePropertyNames = ( } return (data: object, ctx: TraversalContext) => { - const errors: false[] = [] - Object.keys(data).forEach(key => { if (!propertyNamesValidator.allows(key)) { - errors.push( - ctx.reject({ - path: [key], - expected: `a key adhering to the propertyNames schema of ${propertyNamesValidator.description}`, - actual: key - }) - ) + ctx.reject({ + path: [key], + expected: `a key adhering to the propertyNames schema of ${propertyNamesValidator.description}`, + actual: key + }) } }) - return errors.length === 0 + return ctx.hasError() } } @@ -201,8 +193,6 @@ const parseAdditionalProperties = (jsonSchema: JsonSchema.ObjectSchema) => { if (additionalPropertiesSchema === true) return return (data: object, ctx: TraversalContext) => { - const errors: false[] = [] - Object.keys(data).forEach(key => { if ( properties.includes(key) || @@ -212,13 +202,11 @@ const parseAdditionalProperties = (jsonSchema: JsonSchema.ObjectSchema) => { return if (additionalPropertiesSchema === false) { - errors.push( - ctx.reject({ - expected: - "an object with no additional keys, since provided additionalProperties JSON Schema doesn't allow it", - actual: `an additional key (${key})` - }) - ) + ctx.reject({ + expected: + "an object with no additional keys, since provided additionalProperties JSON Schema doesn't allow it", + actual: `an additional key (${key})` + }) return } @@ -228,16 +216,14 @@ const parseAdditionalProperties = (jsonSchema: JsonSchema.ObjectSchema) => { const value = data[key as keyof typeof data] if (!additionalPropertyValidator.allows(value)) { - errors.push( - ctx.reject({ - path: [key], - expected: `${additionalPropertyValidator.description}, since ${key} is an additional property.`, - actual: printable(value) - }) - ) + ctx.reject({ + path: [key], + expected: `${additionalPropertyValidator.description}, since ${key} is an additional property.`, + actual: printable(value) + }) } }) - return errors.length === 0 + return ctx.hasError() } }