forked from jquense/yup
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jquense#2043 add a ValidationErrorNoStack class to be used when no st…
…ack trace is required
- Loading branch information
Showing
5 changed files
with
122 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
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: ValidationErrorNoStack[]; | ||
|
||
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 | ||
| ValidationErrorNoStack | ||
| readonly ValidationErrorNoStack[], | ||
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters