Good day.
I'm trying to deal with unknown values on a model, but it seems not working. What i'm doing wrong?
yarn list v1.12.3
鈹斺攢 [email protected]
```
import { IsString, validate } from 'class-validator';
class Post {
@IsString()
message: string;
}
let x = new Post();
x.message = 's';
x['hoho'] = 123;
validate(x, { forbidUnknownValues: true }).then(console.log);
Output is empty array:
[]
```
+1
Same behavior, using v0.9.1. Haven't tried with other versions.
I also tried with the following ValidatorOptions, still the same, doesn't throw any validation errors.
const validatorOptions: ValidatorOptions = {
skipMissingProperties: false,
whitelist: false,
forbidNonWhitelisted: true,
groups: [],
dismissDefaultMessages: false,
validationError: {
target: true,
value: true
},
forbidUnknownValues: true
};
I think what you want would be {whitelist: true, forbidNonWhitelisted: true}. This will throw an error, if the object has a property that is not part of the validation class.
It doesn't seem to work with whitelist:true either (using NestJS).
export class UpdateDto {
@MinLength(3)
@MaxLength(255)
readonly company: string;
@IsOptional()
@IsUUID('4')
readonly roleId: string;
}
//...
console.log(object);
const errors = await validate(object, { whitelist: true, forbidUnknownValues: true });
// console.log:
// UpdateDto {
// id: 'ed83f01a-9162-408b-9a6d-e0fd51f26284',
// roleId: '72ea4c50-f0c6-48d4-b3b6-318cc43448c3',
// company: 'Test',
// badkey: 1 }
Note "id" and "badKey" is not defined in UpdateDto, errors are empty and the whole object is passed down to the controller with no stripping or finding errors.
this config worked for me:
{
whitelist: true, // i supose this creates a white list with properties
forbidNonWhitelisted: true, // i supose this restrict by white list criteria
forbidUnknownValues: true, // i dont know why exists
}
should exists an option like:
supressUnknownValues: true
To remove any not allowed field/value, and not throw error.
Having the same issue anyone share to shed some light on this ? :)
@nigelthinksprint dunno, but same here too.
Hi all,
Option name forbidUnknownValues is little bit misleading. Actually this option is about objects passed into validation function not about values/properties in object. Check out tests:
https://github.com/typestack/class-validator/blob/94e3ead764b44b85da74b813e528869a7dc97770/test/functional/validator-options.spec.ts#L42-L63
forbidUnknownValues will create validation error in case you pass instance of class not registered in class-validator metadata storage.
So you have to use {whitelist: true, forbidNonWhitelisted: true} if you want validation error for unknown properties in object.
We should update docs in this case and describe forbidUnknownValues a little bit more.
@vlapo thanks for the reply. My primary issue here is that the last time I checked, whitelist:true is not stripping off non-white-listed properties. In other words, it seems to not actually do _anything_. I have to also add forbidNonWhitelisted: true to make it throw an exception to sanitize my data. Really, I'd prefer it to just silently strip the properties off.
@rmainseas Did not test it on my environment but we have test with whitelist: true and all tests pass.
https://github.com/typestack/class-validator/blob/94e3ead764b44b85da74b813e528869a7dc97770/test/functional/whitelist-validation.spec.ts#L26-L47
@vlapo Hi. Any chance you guys update the naming (or add a bit detailed description) for forbidUnknownValues option as you were saying above?
this config worked for me:
{ whitelist: true, // i supose this creates a white list with properties forbidNonWhitelisted: true, // i supose this restrict by white list criteria forbidUnknownValues: true, // i dont know why exists }
This worked for me :)
Most helpful comment
I think what you want would be
{whitelist: true, forbidNonWhitelisted: true}. This will throw an error, if the object has a property that is not part of the validation class.