Skip to content

Commit

Permalink
Use ctx.hasError instead of manually tracking ctx errors
Browse files Browse the repository at this point in the history
  • Loading branch information
TizzySaurus committed Oct 26, 2024
1 parent aa81782 commit de4c7cc
Showing 1 changed file with 23 additions and 37 deletions.
60 changes: 23 additions & 37 deletions ark/jsonschema/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand All @@ -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()
}
}

Expand Down Expand Up @@ -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) ||
Expand All @@ -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
}

Expand All @@ -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()
}
}

Expand Down

0 comments on commit de4c7cc

Please sign in to comment.