class-validator icon indicating copy to clipboard operation
class-validator copied to clipboard

fix: v0.14.2 validate seems cause error

Open fancyoung opened this issue 7 months ago • 5 comments

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.

fancyoung avatar May 09 '25 17:05 fancyoung

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
    });

dereekb avatar May 09 '25 21:05 dereekb

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 avatar May 15 '25 12:05 SmallhillCZ

@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);

alex-springer-sr avatar May 20 '25 10:05 alex-springer-sr

@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!

SmallhillCZ avatar May 20 '25 10:05 SmallhillCZ

they put a breaking change in a patch version. we shall lock the version to 0.14.1 to prevent update

Diluka avatar Jun 07 '25 01:06 Diluka

@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 avatar Jun 30 '25 10:06 braaar

@braaar is there any plan to release 0.14.3 to revert the breaking change?

stwiname avatar Jul 10 '25 04:07 stwiname

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.

github-actions[bot] avatar Aug 10 '25 00:08 github-actions[bot]