I'm trying to find a nice way to validate a body using DTO (using the brilliant class-validator
and class-transformer
libraries). It works really well, even for nested structures but in my case I'd like to have the body property based on some conditions.
Example that will probably help to understand:
Let's imagine my body should always have selectedCategory
.
Based on that field, the content could either be from category 1, which contains prop1
OR from category 2, which contains prop2
.
I do not want to allow a null for both of them, I really want to have to either have prop1
defined or prop2
based on the selectedCategory
.
I think that I could use a pipe, but then how can I specify the correct DTO to use?
I've built a "base" class with all the common properties and few other classes that inherit from it.
I could instantiate the pipe manually based on the property selectedCategory
, that'd be ideal but I have no clue what to pass as a second argument of the pipe (metadata).
Thanks for your help.