Skip to content

Commit

Permalink
Merge branch '2043-performance-improvement' of https://github.com/ted…
Browse files Browse the repository at this point in the history
…eschia/yup into 2043-performance-improvement
  • Loading branch information
tedeschia committed Mar 4, 2024
2 parents 425e6e5 + 4e363f7 commit 320ac86
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 92 deletions.
96 changes: 72 additions & 24 deletions src/ValidationError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,59 @@ let strReg = /\$\{\s*(\w+)\s*\}/g;

type Params = Record<string, unknown>;

export default class ValidationError extends Error {
class ValidationErrorNoStack implements ValidationError {
name: string;
message: string;

value: any;
path?: string;
type?: string;
params?: Params;

errors: string[];
inner: ValidationError[];

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';
}

export default class ValidationError extends Error {
value: any;
path?: string;
type?: string;
params?: Params;

inner: ValidationError[];
errors: string[] = [];
inner: ValidationError[] = [];

static formatError(
message: string | ((params: Params) => string) | unknown,
Expand All @@ -40,33 +84,37 @@ export default class ValidationError extends Error {
type?: string,
disableStack?: boolean,
) {
super();

this.name = 'ValidationError';
this.value = value;
this.path = field;
this.type = type;
const errorNoStack = new ValidationErrorNoStack(
errorOrErrors,
value,
field,
type,
);

this.errors = [];
this.inner = [];
if (disableStack) {
return errorNoStack;
}

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);
}
});
super();

this.message =
this.errors.length > 1
? `${this.errors.length} errors occurred`
: this.errors[0];
this.name = errorNoStack.name;
this.message = errorNoStack.message;
this.value = errorNoStack.value;
this.path = errorNoStack.path;
this.errors = errorNoStack.errors;
this.inner = errorNoStack.inner;

if (!disableStack && Error.captureStackTrace)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, ValidationError);
}
}

static [Symbol.hasInstance](inst: any) {
return (
ValidationErrorNoStack[Symbol.hasInstance](inst) ||
super[Symbol.hasInstance](inst)
);
}

[Symbol.toStringTag] = 'Error';
}
66 changes: 0 additions & 66 deletions src/ValidationErrorNoStack.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import TupleSchema, { create as tupleCreate } from './tuple';
import Reference, { create as refCreate } from './Reference';
import Lazy, { create as lazyCreate } from './Lazy';
import ValidationError from './ValidationError';
import ValidationErrorNoStack from './ValidationErrorNoStack';
import reach, { getIn } from './util/reach';
import isSchema from './util/isSchema';
import printValue from './util/printValue';
Expand Down Expand Up @@ -109,7 +108,6 @@ export {
setLocale,
defaultLocale,
ValidationError,
ValidationErrorNoStack,
};

export {
Expand Down
7 changes: 7 additions & 0 deletions test/ValidationError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,11 @@ describe('ValidationError', () => {
expect(str).toContain('0');
});
});

it('should disable stacks', () => {
const disabled = new ValidationError('error', 1, 'field', 'type', true);

expect(disabled.constructor.name).toEqual('ValidationErrorNoStack');
expect(disabled).toBeInstanceOf(ValidationError);
});
});

0 comments on commit 320ac86

Please sign in to comment.