简体   繁体   中英

Class Validators are not working when the type is a Record <k,v> - NestJS

We have a DTO property where its type is Record<string, Animal> and Animal is another DTO with class validators like @IsDefined(), @IsNotEmpty(). However these validations are not working as expected and it's probably because it has the Record type.

I already tried adding @ValidatedNested({ each: true }) but still didn't work. How could we make the validation work for the Animal dto in Record<string, Animal> ?

Hi You can try building partial class validator, https://trilon.io/blog/introducing-mapped-types-for-nestjs here is some documentation. for eg:

 export class Record extends PartialType( OmitType(Animal, ['legs'] as const), ) {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM