Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
FabriSr opened this issue May 4, 2023 · 1 comment

Comments

@FabriSr
Copy link

FabriSr commented May 4, 2023

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
Copy link

awa-xima commented Jun 18, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants