diff --git a/README.md b/README.md index 46a40469..86c1df76 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,6 @@ const parsedUser = await userSchema.validate( - - [Schema basics](#schema-basics) - [Parsing: Transforms](#parsing-transforms) - [Validation: Tests](#validation-tests) diff --git a/src/ValidationError.ts b/src/ValidationError.ts index 89047b90..f02879ff 100644 --- a/src/ValidationError.ts +++ b/src/ValidationError.ts @@ -5,10 +5,7 @@ let strReg = /\$\{\s*(\w+)\s*\}/g; type Params = Record; -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; @@ -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; @@ -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); } @@ -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); } } diff --git a/src/schema.ts b/src/schema.ts index b43973d0..b8cf53d2 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -43,7 +43,6 @@ export type SchemaSpec = { strip?: boolean; strict?: boolean; recursive?: boolean; - disableStackTrace?: boolean; label?: string | undefined; meta?: SchemaMetadata; }; @@ -192,7 +191,6 @@ export default abstract class Schema< strict: false, abortEarly: true, recursive: true, - disableStackTrace: false, nullable: false, optional: true, coerce: true, @@ -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, }; } @@ -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); @@ -559,8 +553,6 @@ export default abstract class Schema< options?: ValidateOptions, ): Promise { let schema = this.resolve({ ...options, value }); - let disableStackTrace = - options?.disableStackTrace ?? schema.spec.disableStackTrace; return new Promise((resolve, reject) => schema._validate( @@ -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']); }, ), @@ -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, @@ -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; }, ); diff --git a/src/types.ts b/src/types.ts index 4a04d59a..c63fee56 100644 --- a/src/types.ts +++ b/src/types.ts @@ -61,10 +61,6 @@ export interface ValidateOptions { * 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()) */ diff --git a/src/util/createValidation.ts b/src/util/createValidation.ts index 036fbfdb..4d946bab 100644 --- a/src/util/createValidation.ts +++ b/src/util/createValidation.ts @@ -22,7 +22,6 @@ export type CreateErrorOptions = { message?: Message; params?: ExtraParams; type?: string; - disableStackTrace?: boolean; }; export type TestContext = { @@ -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(item: T | Reference) { return Ref.isRef(item) ? item.getValue(value, parent, context) : item; @@ -111,7 +105,6 @@ export default function createValidation(config: { value, nextParams.path, overrides.type || name, - overrides.disableStackTrace ?? disableStackTrace, ); error.params = nextParams; return error;