Skip to content

Commit 9c49bd6

Browse files
cduffChris Duff
andauthored
fix: forbidUnknownValues should default true when validatorOptions undefined (#2196)
* fix: forbidUnknownValues should default true when validatorOptions undefined. * docs: correct ValidatorOptions.forbidUnknownValues comment. * docs: add another example to ValidatorOptions.forbidUnknownValues comment for further clarity. * test: improve validator-options.spec.ts based on PR feedback. * test: added further test to validator-options.spec.ts. * test: revert changes to validator-options.spec.ts unrelated to PR. --------- Co-authored-by: Chris Duff <chris@praxhub.com>
1 parent adffae3 commit 9c49bd6

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

src/validation/ValidationExecutor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class ValidationExecutor {
6767
);
6868
const groupedMetadatas = this.metadataStorage.groupByPropertyName(targetMetadatas);
6969

70-
if (this.validatorOptions && forbidUnknownValues && !targetMetadatas.length) {
70+
if (forbidUnknownValues && !targetMetadatas.length) {
7171
const validationError = new ValidationError();
7272

7373
if (

src/validation/ValidatorOptions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ export interface ValidatorOptions {
7575
* Fails validation for objects unknown to class-validator. Defaults to true.
7676
*
7777
* For instance, since a plain empty object has no annotations used for validation:
78-
* - `validate({})` // passes
78+
* - `validate({})` // fails.
7979
* - `validate({}, { forbidUnknownValues: true })` // fails.
80+
* - `validate({}, { forbidUnknownValues: false })` // passes.
8081
* - `validate(new SomeAnnotatedEmptyClass(), { forbidUnknownValues: true })` // passes.
8182
*/
8283
forbidUnknownValues?: boolean;

test/functional/validator-options.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,43 @@ describe('validator options', () => {
4444
expect(errors.length).toEqual(0);
4545
});
4646
});
47+
48+
it(`should return error on unknown objects if no options object argument is passed`, () => {
49+
const anonymousObject = { badKey: 'This should not pass.' };
50+
51+
return validator.validate(anonymousObject, undefined).then(errors => {
52+
expect(errors.length).toEqual(1);
53+
expect(errors[0].target).toEqual(anonymousObject);
54+
expect(errors[0].property).toEqual(undefined);
55+
expect(errors[0].value).toEqual(undefined);
56+
expect(errors[0].children).toBeInstanceOf(Array);
57+
expect(errors[0].constraints).toEqual({ unknownValue: 'an unknown value was passed to the validate function' });
58+
});
59+
});
60+
61+
it(`should return error on unknown objects if empty options object argument passed`, () => {
62+
const anonymousObject = { badKey: 'This should not pass.' };
63+
64+
return validator.validate(anonymousObject, {}).then(errors => {
65+
expect(errors.length).toEqual(1);
66+
expect(errors[0].target).toEqual(anonymousObject);
67+
expect(errors[0].property).toEqual(undefined);
68+
expect(errors[0].value).toEqual(undefined);
69+
expect(errors[0].children).toBeInstanceOf(Array);
70+
expect(errors[0].constraints).toEqual({ unknownValue: 'an unknown value was passed to the validate function' });
71+
});
72+
});
73+
74+
it(`should return error on unknown objects if options object argument passed with forbidUnknownValues undefined`, () => {
75+
const anonymousObject = { badKey: 'This should not pass.' };
76+
77+
return validator.validate(anonymousObject, { forbidUnknownValues: undefined }).then(errors => {
78+
expect(errors.length).toEqual(1);
79+
expect(errors[0].target).toEqual(anonymousObject);
80+
expect(errors[0].property).toEqual(undefined);
81+
expect(errors[0].value).toEqual(undefined);
82+
expect(errors[0].children).toBeInstanceOf(Array);
83+
expect(errors[0].constraints).toEqual({ unknownValue: 'an unknown value was passed to the validate function' });
84+
});
85+
});
4786
});

0 commit comments

Comments
 (0)