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: performance improvement (#2043) #2044

Merged
merged 4 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yup",
"version": "1.2.0",
"version": "1.2.0-dev6",
tedeschia marked this conversation as resolved.
Show resolved Hide resolved
"description": "Dead simple Object schema validation",
"main": "lib/index.js",
"module": "lib/index.esm.js",
Expand Down
14 changes: 9 additions & 5 deletions src/ValidationError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ let strReg = /\$\{\s*(\w+)\s*\}/g;

type Params = Record<string, unknown>;

export default class ValidationError extends Error {
export default class ValidationError implements Error {
name: string;
message: string;
stack?: string | undefined;
value: any;
path?: string;
type?: string;
Expand Down Expand Up @@ -38,9 +41,8 @@ export default class ValidationError extends Error {
value?: any,
field?: string,
type?: string,
disableStack?: boolean,
) {
super();

this.name = 'ValidationError';
this.value = value;
this.path = field;
Expand All @@ -52,7 +54,8 @@ export default class ValidationError extends Error {
toArray(errorOrErrors).forEach((err) => {
if (ValidationError.isError(err)) {
this.errors.push(...err.errors);
this.inner = this.inner.concat(err.inner.length ? err.inner : err);
const innerErrors = err.inner.length ? err.inner : [err];
this.inner.splice(this.inner.length, 0, ...innerErrors);
tedeschia marked this conversation as resolved.
Show resolved Hide resolved
} else {
this.errors.push(err);
}
Expand All @@ -63,6 +66,7 @@ export default class ValidationError extends Error {
? `${this.errors.length} errors occurred`
: this.errors[0];

if (Error.captureStackTrace) Error.captureStackTrace(this, ValidationError);
if (!disableStack && Error.captureStackTrace)
Error.captureStackTrace(this, ValidationError);
}
}
34 changes: 31 additions & 3 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type SchemaSpec<TDefault> = {
strip?: boolean;
strict?: boolean;
recursive?: boolean;
disableStackTrace?: boolean;
label?: string | undefined;
meta?: SchemaMetadata;
};
Expand Down Expand Up @@ -191,6 +192,7 @@ export default abstract class Schema<
strict: false,
abortEarly: true,
recursive: true,
disableStackTrace: false,
nullable: false,
optional: true,
coerce: true,
Expand Down Expand Up @@ -345,6 +347,8 @@ export default abstract class Schema<
strict: options.strict ?? this.spec.strict,
abortEarly: options.abortEarly ?? this.spec.abortEarly,
recursive: options.recursive ?? this.spec.recursive,
disableStackTrace:
options.disableStackTrace ?? this.spec.disableStackTrace,
};
}

Expand Down Expand Up @@ -499,7 +503,11 @@ export default abstract class Schema<

test(args!, panicOnce, function finishTestRun(err) {
if (err) {
nestedErrors = nestedErrors.concat(err);
nestedErrors.splice(
nestedErrors.length,
0,
...(Symbol.iterator in err ? err : [err]),
);
tedeschia marked this conversation as resolved.
Show resolved Hide resolved
}
if (--count <= 0) {
nextOnce(nestedErrors);
Expand Down Expand Up @@ -553,6 +561,8 @@ export default abstract class Schema<
options?: ValidateOptions<TContext>,
): Promise<this['__outputType']> {
let schema = this.resolve({ ...options, value });
let { disableStackTrace = this.spec.disableStackTrace } =
options ?? this.spec;
tedeschia marked this conversation as resolved.
Show resolved Hide resolved

return new Promise((resolve, reject) =>
schema._validate(
Expand All @@ -563,7 +573,16 @@ export default abstract class Schema<
reject(error);
},
(errors, validated) => {
if (errors.length) reject(new ValidationError(errors!, validated));
if (errors.length)
reject(
new ValidationError(
errors!,
validated,
undefined,
undefined,
disableStackTrace,
),
);
else resolve(validated as this['__outputType']);
},
),
Expand All @@ -576,6 +595,8 @@ export default abstract class Schema<
): this['__outputType'] {
let schema = this.resolve({ ...options, value });
let result: any;
let { disableStackTrace = this.spec.disableStackTrace } =
options ?? this.spec;
tedeschia marked this conversation as resolved.
Show resolved Hide resolved

schema._validate(
value,
Expand All @@ -585,7 +606,14 @@ export default abstract class Schema<
throw error;
},
(errors, validated) => {
if (errors.length) throw new ValidationError(errors!, value);
if (errors.length)
throw new ValidationError(
errors!,
value,
undefined,
undefined,
disableStackTrace,
);
result = validated;
},
);
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export interface ValidateOptions<TContext = {}> {
* When false validations will not descend into nested schema (relevant for objects or arrays). Default - true
*/
recursive?: boolean;
/**
* When true ValidationError instance won't include stack trace information. Default - false
*/
disableStackTrace?: boolean;
/**
* Any context needed for validating schema conditions (see: when())
*/
Expand Down
9 changes: 8 additions & 1 deletion src/util/createValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type CreateErrorOptions = {
message?: Message<any>;
params?: ExtraParams;
type?: string;
disableStackTrace?: boolean;
};

export type TestContext<TContext = {}> = {
Expand Down Expand Up @@ -79,7 +80,12 @@ export default function createValidation(config: {
next: NextCallback,
) {
const { name, test, params, message, skipAbsent } = config;
let { parent, context, abortEarly = schema.spec.abortEarly } = options;
let {
parent,
context,
abortEarly = schema.spec.abortEarly,
disableStackTrace = schema.spec.disableStackTrace,
} = options;

function resolve<T>(item: T | Reference<T>) {
return Ref.isRef(item) ? item.getValue(value, parent, context) : item;
Expand All @@ -105,6 +111,7 @@ export default function createValidation(config: {
value,
nextParams.path,
overrides.type || name,
overrides.disableStackTrace ?? disableStackTrace,
);
error.params = nextParams;
return error;
Expand Down