Skip to content

Commit

Permalink
Revert "fix: performance improvement (#2043) (#2044)"
Browse files Browse the repository at this point in the history
This reverts commit ee1b731.
  • Loading branch information
jquense committed Jul 28, 2023
1 parent ee1b731 commit 26989d3
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 51 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ const parsedUser = await userSchema.validate(
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->


- [Schema basics](#schema-basics)
- [Parsing: Transforms](#parsing-transforms)
- [Validation: Tests](#validation-tests)
Expand Down
14 changes: 5 additions & 9 deletions src/ValidationError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ let strReg = /\$\{\s*(\w+)\s*\}/g;

type Params = Record<string, unknown>;

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

this.name = 'ValidationError';
this.value = value;
this.path = field;
Expand All @@ -54,8 +52,7 @@ export default class ValidationError implements Error {
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);
this.inner = this.inner.concat(err.inner.length ? err.inner : err);
} else {
this.errors.push(err);
}
Expand All @@ -66,7 +63,6 @@ export default class ValidationError implements Error {
? `${this.errors.length} errors occurred`
: this.errors[0];

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

test(args!, panicOnce, function finishTestRun(err) {
if (err) {
Array.isArray(err)
? nestedErrors.push(...err)
: nestedErrors.push(err);
nestedErrors = nestedErrors.concat(err);
}
if (--count <= 0) {
nextOnce(nestedErrors);
Expand Down Expand Up @@ -559,8 +553,6 @@ export default abstract class Schema<
options?: ValidateOptions<TContext>,
): Promise<this['__outputType']> {
let schema = this.resolve({ ...options, value });
let disableStackTrace =
options?.disableStackTrace ?? schema.spec.disableStackTrace;

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

schema._validate(
value,
Expand All @@ -604,14 +585,7 @@ export default abstract class Schema<
throw error;
},
(errors, validated) => {
if (errors.length)
throw new ValidationError(
errors!,
value,
undefined,
undefined,
disableStackTrace,
);
if (errors.length) throw new ValidationError(errors!, value);
result = validated;
},
);
Expand Down
4 changes: 0 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ 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: 1 addition & 8 deletions src/util/createValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export type CreateErrorOptions = {
message?: Message<any>;
params?: ExtraParams;
type?: string;
disableStackTrace?: boolean;
};

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

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

0 comments on commit 26989d3

Please sign in to comment.