class-validator icon indicating copy to clipboard operation
class-validator copied to clipboard

always: false doesn't take any effect

Open tareksalem opened this issue 1 year ago • 1 comments

Description

I can see in the documentation there is an option called always: true which is used in combination with groups to force the field to be validated in all cases, however, I think this defeats the purpose of groups, because if I want to set groups so I don't want the validation to be applied on all cases. This is not the point, the point here is: if I define always: false the validation still be applied however I expect it should not be applied!

Minimal code-snippet showcasing the problem

export class UserModel {
  @IsDefined({ always: true })
  name: string;

  @IsEmail()
  email: string;

  @IsNumber({}, { groups: ["numberGroup"] })
  @Min(10, { always: false, groups: ["numberGroup"] })
  @IsString({ always: false, groups: ["ssGroup"] })
  ss: string | number;

  age: number;

  constructor({ name, email }: { name: string; email: string }) {
    this.name = name;
    this.email = email;
    this.age = 20;
    this.ss = 5;
  }
}

This is the validation function

const user = new UserModel({ name: "test", email: "[email protected]" });

validate(user, {
  skipMissingProperties: true,
}).then((errors) => {
  console.log(errors);
});

Expected behavior

I expect when calling the validate function without specifying any group name then it should validate the object by ignoring the validations with groups or I should have an option to specify it called for example ignore: true | false

Actual behavior

It raises a validation error from the validation constraints that are specified with groups however it shouldn't

tareksalem avatar Dec 09 '23 10:12 tareksalem