jakartaee / validation-spec

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

Add "provider" attribute to @GroupSequence inspired by the Hibernate's @GroupSequenceProvider #268

Open FabriSr opened 1 year ago

FabriSr commented 1 year ago

At the moment there is no mechanism to define the group sequence validation at runtime. Hibernate Validator has its own mechanism, through @GroupSequenceProvider annotation and DefaultGroupSequenceProvider interface.

Inspired by the Hibernate's solution, my suggestion is to add the attribute "provider" to the @GroupSequence annotation which expects a reference to a class that implements the interface "GroupSequenceProvider".

More info: Hibernate Validator docs.

awa-xima commented 2 months ago

One common use case GroupSequenceProvider solves for us is conditional validation: We have many beans where some fields should only be validated when some other fields have a value.

There is for example this library that provides conditional constraint annotations such as NotEmptyWhen. It's quite concise, but it is also not-standard, requires a complicated, messy implementations, needs an additional dummy annotation for each normal annotation (NotEmptyWhen for NotEmpty etc.), which is also does not play well with custom constraint annotations.

I find GroupSequenceProvider to be a nice way to handle conditional validation in a mostly declarative manner, that does not require much boilerplate.

This would also combine well with groups for the @Valid annotation #269

For reference, an example of how GroupSequenceProvider could be used:

@GroupSequenceProvider(ValidationGroup.class)
class MyBean {
  private String resolutionType;

  @NotEmpty(groups = ValidationGroup.WhenRelativeLink)
  private String baseUrl;

  class ValidationGroup extends BaseGroupSequenceProvider<MyBean> {
    public void addGroups(GroupBuilder<MyModel> builder) {
      builder.when(model -> "relative".equals(model.resolutionType)).add(WhenRelativeLink.class);
    }

    interface WhenRelativeLink {}
  }
}

(where BaseGroupSequenceProvider is a simple super class to reduce the boilerplate a bit more and make it more declarative)