-
-
Notifications
You must be signed in to change notification settings - Fork 363
fix: handle standard schema validators in ValidationError.all #1483
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -431,7 +431,33 @@ export class ValidationError extends Error { | |||||||||||||||||||||||||
Object.setPrototypeOf(this, ValidationError.prototype) | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
get all() { | ||||||||||||||||||||||||||
get all(): MapValueError[] { | ||||||||||||||||||||||||||
// Handle standard schema validators (Zod, Valibot, etc.) | ||||||||||||||||||||||||||
if ( | ||||||||||||||||||||||||||
// @ts-ignore | ||||||||||||||||||||||||||
this.validator?.provider === 'standard' || | ||||||||||||||||||||||||||
'~standard' in this.validator || | ||||||||||||||||||||||||||
// @ts-ignore | ||||||||||||||||||||||||||
('schema' in this.validator && this.validator.schema && '~standard' in this.validator.schema) | ||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||
const standard = // @ts-ignore | ||||||||||||||||||||||||||
('~standard' in this.validator | ||||||||||||||||||||||||||
? this.validator | ||||||||||||||||||||||||||
: // @ts-ignore | ||||||||||||||||||||||||||
this.validator.schema)['~standard'] | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
const issues = standard.validate(this.value).issues | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Map standard schema issues to the expected format | ||||||||||||||||||||||||||
return issues?.map((issue: any) => ({ | ||||||||||||||||||||||||||
summary: issue.message, | ||||||||||||||||||||||||||
path: issue.path?.join('.') || 'root', | ||||||||||||||||||||||||||
message: issue.message, | ||||||||||||||||||||||||||
value: this.value | ||||||||||||||||||||||||||
Comment on lines
+452
to
+456
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Value assignment is misleading. Line 456 assigns For standard validators, either:
Consider extracting field-specific values: // Map standard schema issues to the expected format
return issues?.map((issue: any) => ({
summary: issue.message,
path: issue.path?.join('.') || 'root',
message: issue.message,
- value: this.value
+ value: issue.path?.reduce((obj: any, key: string) => obj?.[key], this.value) ?? this.value
})) || [] This traverses the path to extract the specific field's value. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||
})) || [] | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// Handle TypeBox validators | ||||||||||||||||||||||||||
return 'Errors' in this.validator | ||||||||||||||||||||||||||
? [...this.validator.Errors(this.value)].map(mapValueError) | ||||||||||||||||||||||||||
: // @ts-ignore | ||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-validation on every access degrades performance.
Line 449 calls
standard.validate(this.value)
every time theall
getter is accessed. This re-validates the entire value on each call, which is expensive. The validation already occurred in the constructor (line 292), and the issues could be cached.Consider caching the validation issues in the constructor and reusing them in the getter to avoid redundant validation:
valueError?: ValueError expected?: unknown customError?: string + private cachedIssues?: any[]
Then in the constructor's standard validator branch (after line 292):
const _errors = errors ?? standard.validate(value).issues + this.cachedIssues = _errors
And in the getter:
📝 Committable suggestion
🤖 Prompt for AI Agents