Git Product home page Git Product logo

Comments (1)

awa-xima avatar awa-xima commented on June 23, 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)

from validation-spec.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.