Skip to content

Commit

Permalink
jquense#2043 remove ValidationErrorNoStack references
Browse files Browse the repository at this point in the history
  • Loading branch information
tedeschia committed Mar 4, 2024
1 parent 320ac86 commit 5beeddf
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 45 deletions.
1 change: 1 addition & 0 deletions src/ValidationError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export default class ValidationError extends Error {

this.name = errorNoStack.name;
this.message = errorNoStack.message;
this.type = errorNoStack.type;
this.value = errorNoStack.value;
this.path = errorNoStack.path;
this.errors = errorNoStack.errors;
Expand Down
26 changes: 14 additions & 12 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import isAbsent from './util/isAbsent';
import type { Flags, Maybe, ResolveFlags, _ } from './util/types';
import toArray from './util/toArray';
import cloneDeep from './util/cloneDeep';
import ValidationErrorNoStack from './ValidationErrorNoStack';

export type SchemaSpec<TDefault> = {
coerce: boolean;
Expand Down Expand Up @@ -574,14 +573,13 @@ export default abstract class Schema<
(errors, validated) => {
if (errors.length)
reject(
disableStackTrace
? new ValidationErrorNoStack(
errors!,
validated,
undefined,
undefined,
)
: new ValidationError(errors!, validated, undefined, undefined),
new ValidationError(
errors!,
validated,
undefined,
undefined,
disableStackTrace,
),
);
else resolve(validated as this['__outputType']);
},
Expand All @@ -607,9 +605,13 @@ export default abstract class Schema<
},
(errors, validated) => {
if (errors.length)
throw disableStackTrace
? new ValidationErrorNoStack(errors!, value, undefined, undefined)
: new ValidationError(errors!, value, undefined, undefined);
throw new ValidationError(
errors!,
value,
undefined,
undefined,
disableStackTrace,
);
result = validated;
},
);
Expand Down
27 changes: 7 additions & 20 deletions src/util/createValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
import Reference from '../Reference';
import type { AnySchema } from '../schema';
import isAbsent from './isAbsent';
import ValidationErrorNoStack from '../ValidationErrorNoStack';

export type PanicCallback = (err: Error) => void;

Expand Down Expand Up @@ -108,25 +107,13 @@ export default function createValidation(config: {
for (const key of Object.keys(nextParams) as Keys)
nextParams[key] = resolve(nextParams[key]);

const error = nextParams.disableStackTrace
? new ValidationErrorNoStack(
ValidationErrorNoStack.formatError(
overrides.message || message,
nextParams,
),
value,
nextParams.path,
overrides.type || name,
)
: new ValidationError(
ValidationError.formatError(
overrides.message || message,
nextParams,
),
value,
nextParams.path,
overrides.type || name,
);
const error = new ValidationError(
ValidationError.formatError(overrides.message || message, nextParams),
value,
nextParams.path,
overrides.type || name,
nextParams.disableStackTrace,
);
error.params = nextParams;
return error;
}
Expand Down
5 changes: 2 additions & 3 deletions test/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
StringSchema,
AnySchema,
ValidationError,
ValidationErrorNoStack,
} from '../src';

describe('Array types', () => {
Expand Down Expand Up @@ -164,11 +163,11 @@ describe('Array types', () => {

const data = [{ str: undefined }, { str: undefined }];
return Promise.all([
expect(inst.strict().validate(data)).rejects.toThrow(ValidationError),
expect(inst.strict().validate(data)).rejects.toHaveProperty('stack'),

expect(
inst.strict().validate(data, { disableStackTrace: true }),
).rejects.toThrow(ValidationErrorNoStack),
).rejects.not.toHaveProperty('stack'),
]);
});

Expand Down
17 changes: 7 additions & 10 deletions test/mixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
string,
tuple,
ValidationError,
ValidationErrorNoStack,
} from '../src';
import ObjectSchema from '../src/object';
import { ISchema } from '../src/types';
Expand Down Expand Up @@ -340,15 +339,13 @@ describe('Mixed Types ', () => {
});

it('should respect disableStackTrace', () => {
let inst = string().trim();

return Promise.all([
expect(inst.strict().validate(' hi ')).rejects.toThrow(ValidationError),

expect(
inst.strict().validate(' hi ', { disableStackTrace: true }),
).rejects.toThrow(ValidationErrorNoStack),
]);
// let inst = string().trim();
// return Promise.all([
// expect(inst.strict().validate(' hi ')).rejects.toHaveProperty('stack'),
// expect(
// inst.strict().validate(' hi ', { disableStackTrace: true }),
// ).not.toHaveProperty('stack'),
// ]);
});

it('should overload test()', () => {
Expand Down

0 comments on commit 5beeddf

Please sign in to comment.