jakartaee / validation-spec

Jakarta Validation spec document
http://beanvalidation.org
Apache License 2.0
20 stars 22 forks source link

Add optional "groups" attribute to @Valid #269

Open FabriSr opened 1 year ago

FabriSr commented 1 year ago

At the moment, when using @Valid annotation, there is no way to specify which groups must be validated. Inspired by the Springs @Validated annotation, my suggestion is to add an optional attribute to @Valid annotation, "groups", where we can define which groups should be validated.

blutorange commented 2 months ago

You mean that it lets us define which will be validated? E.g.

class User {
  @Valid(group = Basic.class)
  private Account account;
}
class Account {
  @NotEmpty(group = Basic.class)
  private String name;

  @NotEmpty(group = Advanced.class)
  private String details;
}

It would then validate the account field using the Basic group? That also sounds useful, but as mentioned in in #268, it would also be great if we could specify the groups that must be active for @Valid to be applied at all.

class User {
  @NotEmpty
  private String name;
  @Valid(group = MustValidateAccount.class)
  private Account account;
}
class Account {
  @NotEmpty
  private String name;

  @NotEmpty
  private String details;
}

This would only validate the account field when the user instance is validated with the MustValidateAccount group. For the first use case, I would prefer the name targetGroup instead of group.