@IsDefined({groups: ["createCollections","updateCollections"]})
@IsIn(AllCollections.allLanguages)
subtitleLanguage: string=null;
if I call validation on group 'createCollections' it doesnt call 'IsIn'. So I have to add it to the group. But now its not called if I pass no group to the validator. Is this desired functionality? B/c I would be nice if it called the no group validation even when a group is specified. Like, in all groups I need IsIn to be called, however I sometimes need it to be defined.
So, it seems once I use groups I always have to use groups, b/c all my annotations will have to be part of a group. I am currently getting around this by creating an all groups List.
There's an always: true option for the decorators which will run the validation for that field, regardless if it has a group or not.
The always: true option has the "problem" that it ignores validateIf
Want to chime in supporting that this is currently confusing. I've written some code using this that wasn't using groups/always and now I need to use groups but I have to trace the object hierarchy adding groups/always in a bunch of places and make sure it works all over.
Would be much better if non-grouped decorators always validated by default or at least had a default group. Then it'd be much easier to add groups later when you need to use them.
In addition, if you don't specify a group then all groups are validated. I'd much rather not specifying a group not validate any grouped properties. The idea being that I can be sure that validation code written elsewhere in my app doesn't need to be updated because I added a new groups down the line.
Im currently working with NestJs. Nest implements a class which automatically validates your input called ValidationPipe.
My requests have 2 types of fields, compulsory fields AND fields specific to a 'mode'. I want it so that ValidationPipe validates the compulsary fields first. Then i will have custom code in my request handler which discovers the request mode. I would then want to run validation again on the fields that correspond to the mode.
I tried to implement this by decorating compulsory fields with validators with no validation groups. For the fields related to a mode, I used validators with groups.
The problem Im having is that the first validation phase says my mode fields are invalid. Class-validator will validate every field regardless of its associated groups, if you execute the validator without a group.
I second the notion there should be a default group which anything without a group is added to. Then in ValidationPipe, validate should be called and "Default" passed as the validation group. This would validate all the compulsary fields. Fpr those who prefer the default behaviour, calling validate with no group should validate everything compulsory/ non-compulsary... like it does now
Most helpful comment
Want to chime in supporting that this is currently confusing. I've written some code using this that wasn't using groups/always and now I need to use groups but I have to trace the object hierarchy adding groups/always in a bunch of places and make sure it works all over.
Would be much better if non-grouped decorators always validated by default or at least had a default group. Then it'd be much easier to add groups later when you need to use them.
In addition, if you don't specify a group then all groups are validated. I'd much rather not specifying a group not validate any grouped properties. The idea being that I can be sure that validation code written elsewhere in my app doesn't need to be updated because I added a new groups down the line.