I have TypeScript NestJS project.
I need to validate incoming DTO to my API. It can be described as "creating of project" where we have type of building (House, Flat, Garden) and depending on that type we need to define:
Example of house type:
{
type: HOUSE,
floors: [
{
name: "1st floor",
rooms: [
{
name: "bedroom"
}
]
}
]
}
Example of flat type:
{
type: FLAT,
rooms: [
{
name: "bedroom"
}
]
}
I've done this in past with help of AJV
, but now as we migrated to NestJS, we started using class-validator
.
My question is, if I can make those advanced conditionals (eg. when type is FLAT, then expect ROOMS only, but not FLOORS) in class-validator
?
With class-validator
you have the options of Conditional Validation and of Group Validation, or you could always create a custom pipe and use AJV as you are used to. For the conditional validation you could create validations based on type
and then let class-validator take care of the rest
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With