I am having difficulty with validating a nested object. Running nestJs using class-validator. The top level fields (first_name, last_name etc) validate OK. The Profile object is validated OK at the top level, ie if I submit as an array I get back the correct error that it should be an object.
The contents of Profile however are not being validated. I have followed suggestions on the docs but maybe I am just missing something.
Does anyone know how to validate nested object fields?
export enum GenderType {
Male,
Female,
}
export class Profile {
@IsEnum(GenderType) gender: string;
}
export class CreateClientDto {
@Length(1) first_name: string;
@Length(1) last_name: string;
@IsEmail() email: string;
@IsObject()
@ValidateNested({each: true})
@Type(() => Profile)
profile: Profile;
}
When I send this payload I expect it to fail because gender is not in the enum or a string. But it is not failing
{
"first_name":"A",
"last_name":"B",
"profile":{
"gender":1
}
}