Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: surface deepest error from failed union validation #779

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/errors/errors.ts
Expand Up @@ -513,14 +513,21 @@ function* FromUndefined(schema: TUndefined, references: TSchema[], path: string,
if (!IsUndefined(value)) yield Create(ValueErrorType.Undefined, schema, path, value)
}
function* FromUnion(schema: TUnion, references: TSchema[], path: string, value: any): IterableIterator<ValueError> {
let count = 0
let unionErrors: ValueError[] | undefined
for (const subschema of schema.anyOf) {
const errors = [...Visit(subschema, references, path, value)]
if (errors.length === 0) return // matched
count += errors.length
if (unionErrors) {
for (const error of errors) unionErrors.push(error)
} else {
unionErrors = errors
}
}
if (count > 0) {
yield Create(ValueErrorType.Union, schema, path, value)
if (unionErrors) {
const pathDepth = (error: ValueError) => error.path.split('/').length
yield unionErrors.reduce(
(deepestError, error) => (!deepestError || pathDepth(error) > pathDepth(deepestError) ? error : deepestError)
)
}
}
function* FromUint8Array(schema: TUint8Array, references: TSchema[], path: string, value: any): IterableIterator<ValueError> {
Expand Down