-
Notifications
You must be signed in to change notification settings - Fork 935
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
#2043 introduce ValidationErrorNoStack class to improve error creation performance #2142
Changes from 1 commit
5852a92
4e363f7
425e6e5
320ac86
5beeddf
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import ValidationError from './ValidationError'; | ||
import printValue from './util/printValue'; | ||
import toArray from './util/toArray'; | ||
|
||
let strReg = /\$\{\s*(\w+)\s*\}/g; | ||
|
||
type Params = Record<string, unknown>; | ||
|
||
export default class ValidationErrorNoStack implements ValidationError { | ||
name: string; | ||
message: string; | ||
stack?: string | undefined; | ||
value: any; | ||
path?: string; | ||
type?: string; | ||
errors: string[]; | ||
|
||
params?: Params; | ||
|
||
inner: ValidationError[]; | ||
|
||
static formatError( | ||
message: string | ((params: Params) => string) | unknown, | ||
params: Params, | ||
) { | ||
const path = params.label || params.path || 'this'; | ||
if (path !== params.path) params = { ...params, path }; | ||
|
||
if (typeof message === 'string') | ||
return message.replace(strReg, (_, key) => printValue(params[key])); | ||
if (typeof message === 'function') return message(params); | ||
|
||
return message; | ||
} | ||
|
||
constructor( | ||
errorOrErrors: string | ValidationError | readonly ValidationError[], | ||
value?: any, | ||
field?: string, | ||
type?: string, | ||
) { | ||
this.name = 'ValidationError'; | ||
this.value = value; | ||
this.path = field; | ||
this.type = type; | ||
|
||
this.errors = []; | ||
this.inner = []; | ||
|
||
toArray(errorOrErrors).forEach((err) => { | ||
if (ValidationError.isError(err)) { | ||
this.errors.push(...err.errors); | ||
const innerErrors = err.inner.length ? err.inner : [err]; | ||
this.inner.push(...innerErrors); | ||
} else { | ||
this.errors.push(err); | ||
} | ||
}); | ||
|
||
this.message = | ||
this.errors.length > 1 | ||
? `${this.errors.length} errors occurred` | ||
: this.errors[0]; | ||
} | ||
[Symbol.toStringTag] = 'Error'; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ import isAbsent from './util/isAbsent'; | |
import type { Flags, Maybe, ResolveFlags, _ } from './util/types'; | ||
import toArray from './util/toArray'; | ||
import cloneDeep from './util/cloneDeep'; | ||
import ValidationErrorNoStack from './ValidationErrorNoStack'; | ||
|
||
export type SchemaSpec<TDefault> = { | ||
coerce: boolean; | ||
|
@@ -573,13 +574,14 @@ export default abstract class Schema< | |
(errors, validated) => { | ||
if (errors.length) | ||
reject( | ||
new ValidationError( | ||
errors!, | ||
validated, | ||
undefined, | ||
undefined, | ||
disableStackTrace, | ||
), | ||
disableStackTrace | ||
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. Lets not do this ever place someone needs an error. I think we can do a combo of the original thought here, and pass the disableStackTrace option into the error constructor and then do class ValidationError extends Error {
constructor(
errorOrErrors: string | ValidationError | readonly ValidationError[],
value?: any,
field?: string,
type?: string,
disableStack?: boolean
) {
if (disableStack) {
return new ValidationErrorNoStack()
}
...
}
} 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. I think I can't go this way. If I use a return at the beginning of the constructor, I get typescript errors for all non-nullable members, for example: If you don't like selecting which ValidationError version to construct everywhere, I could define a factory function that receives all args and returns ValidationError or ValidationErrorNoStack depending on disableStack arg. What do you think? |
||
? new ValidationErrorNoStack( | ||
errors!, | ||
validated, | ||
undefined, | ||
undefined, | ||
) | ||
: new ValidationError(errors!, validated, undefined, undefined), | ||
); | ||
else resolve(validated as this['__outputType']); | ||
}, | ||
|
@@ -605,13 +607,9 @@ export default abstract class Schema< | |
}, | ||
(errors, validated) => { | ||
if (errors.length) | ||
throw new ValidationError( | ||
errors!, | ||
value, | ||
undefined, | ||
undefined, | ||
disableStackTrace, | ||
); | ||
throw disableStackTrace | ||
? new ValidationErrorNoStack(errors!, value, undefined, undefined) | ||
: new ValidationError(errors!, value, undefined, undefined); | ||
result = validated; | ||
}, | ||
); | ||
|
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.
Can we DRY this up by moving it to a function, and just put the implementation of ValidationErrorNoStack in this file
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.
I'm trying to figure out how to resolve this comment, but don't find the way!
If I move the initialization of class members to a function, outside the constructor, typescript complains that non-nullable members aren't initialized:
"Property 'errors' has no initializer and is not definitely assigned in the constructor"
Do you have any idea in mind to put me on track to solve this issue?