fix: v0.14.2 validate seems cause error
Description
My project has new error when I deploy it:
Error: An instance of QueryPages has failed the validation:
- property undefined has failed the following constraints: unknownValue
It only happens in v0.14.2.
I just ran into this issue and found this old issue ticket: https://github.com/typestack/class-validator/issues/1873
It's due to a regression/breaking change caused by the default behavior changing in 0.14.2:
https://github.com/typestack/class-validator/releases/tag/v0.14.2
fix: forbidUnknownValues should default true when validatorOptions undefined by @cduff in https://github.com/typestack/class-validator/pull/2196
If you have any classes without any Expose or validator annotations you'll get this error for those classes.
You'll want to update your validate functions to include the forbidUnknownValues: false option.
const validationErrors: ValidationError[] = await validate(object, {
forbidUnknownValues: false, // allow classes without annotations by default
});
I get this also on a class which does not have any properties without validator annotations in 0.14.2 (0.14.1 works OK):
export class OrderDataDto {
@IsEmail() email!: string;
@IsOptional() @IsString() phoneNumber?: string;
@IsString() @IsNotEmpty() companyName!: string;
@IsObject() @ValidateNested() address!: RequiredAddress;
@IsOptional() @IsString() businessNumber?: string;
@IsOptional() @IsString() vatNumber?: string;
@IsOptional() @IsString() invoiceNote?: string;
}
export class RequiredAddress {
@IsString() @IsNotEmpty() street!: string;
@IsString() @IsNotEmpty() city!: string;
@IsString() @IsNotEmpty() postalCode!: string;
@IsString() @IsNotEmpty() country!: string;
}
const orderDataInput = {
email: '[email protected]',
phoneNumber: undefined,
companyName: 'Profile Company',
address: {
street: 'Profile Street',
city: 'Profile City',
postalCode: '12345',
country: 'Czech Republic'
},
businessNumber: '123',
vatNumber: 'CZ123',
invoiceNote: '',
};
const orderData = Object.assign(new OrderDataDto(), orderDataInput);
await validateOrReject(orderData);
An instance of OrderDataDto has failed the validation:
- property address.undefined has failed the following constraints: unknownValue
@SmallhillCZ your code is failing because the address is not an instance of RequiredAddress. The library otherwise does not know which type to validate against.
E.g this should work:
const address = Object.assign(new RequiredAddress(), {
street: 'Profile Street',
city: 'Profile City',
postalCode: '12345',
country: 'Czech Republic',
});
const orderDataInput = {
email: '[email protected]',
phoneNumber: undefined,
companyName: 'Profile Company',
address,
businessNumber: '123',
vatNumber: 'CZ123',
invoiceNote: '',
};
const orderData = Object.assign(new OrderDataDto(), orderDataInput);
await validateOrReject(orderData);
@SmallhillCZ your code is failing because the address is not an instance of RequiredAddress.
Oh okay, sorry, I am used to the NestJS usage in query params and there they probably convert it to instances automatically.
Thanks!
they put a breaking change in a patch version. we shall lock the version to 0.14.1 to prevent update
@Diluka is correct. A bugfix shipped in 0.14.2 caused a change of behaviour that is breaking many people's environments. It should have been released as 0.15.0. My apologies.
@braaar is there any plan to release 0.14.3 to revert the breaking change?
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.