Skip to content

Commit

Permalink
checkSchema: don't silently fail when using not/withMessage
Browse files Browse the repository at this point in the history
Fixes #664
  • Loading branch information
gustavohenke committed Apr 13, 2023
1 parent c858901 commit 5d5da09
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/middlewares/schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,28 @@ describe('on each field', () => {
foo: {
errorMessage: 'bla',
isInt: true,
// @ts-expect-error
isBla: true,
escape: true,
} as any, // as any because of JS consumers doing the wrong thing
},
})[0];

expect(chainToContext(chain).stack).toHaveLength(2);
});

it('does not add validators called not and withMessage', () => {
const chain = checkSchema({
foo: {
// @ts-expect-error
withMessage: 'bla',
// @ts-expect-error
not: true,
},
})[0];

expect(chainToContext(chain).stack).toHaveLength(0);
});

it('adds with options', async () => {
const schema = checkSchema({
foo: {
Expand Down
4 changes: 3 additions & 1 deletion src/middlewares/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type CustomValidatorSchemaOptions = BaseValidatorSchemaOptions & {
export type ExtensionValidatorSchemaOptions = true | BaseValidatorSchemaOptions;

export type ValidatorsSchema = {
[K in keyof Validators<any>]?: ValidatorSchemaOptions<K>;
[K in Exclude<keyof Validators<any>, 'not' | 'withMessage'>]?: ValidatorSchemaOptions<K>;
};

type SanitizerSchemaOptions<K extends keyof Sanitizers<any>> =
Expand Down Expand Up @@ -165,6 +165,8 @@ export function createCheckSchema<C extends ValidationChainLike>(
entry: [string, any],
): entry is [keyof Validators<any>, ValidatorSchemaOptions<any>] {
return (
// #664 - explicitly exclude properties which should be set per validator
!['not', 'withMessage'].includes(entry[0]) &&
(entry[0] in ValidatorsImpl.prototype || (extraValidators as string[]).includes(entry[0])) &&
entry[1]
);
Expand Down

0 comments on commit 5d5da09

Please sign in to comment.