making / yavi

Yet Another Validation for Java (A lambda based type safe validation framework)
https://yavi.ik.am
Apache License 2.0
743 stars 61 forks source link

Feature request: Type Level Annotations for ConstraintTarget #379

Open jochenwierum opened 5 months ago

jochenwierum commented 5 months ago

Most important: Thanks for this great library!

As already mentioned in #290, it would be very helpful to have an Annotation like ConstraintTarget on the type level. In my Opinion, the first snipped reduces the amount of boilerplate code, especially if you have (much) more than two properties:

@ConstraintTargets
class Person {
  private String firstname;
  private String firstname;

  public Person(String firstname, String lastname) {
    this.firstname = firstname;
    this.lastname = lastname;
  }

  String getFirstname() { return firstname; }
  String getLastname() { return lastname; }
}
class Person {
  private String firstname;
  private String firstname;

  public Person(String firstname, String lastname) {
    this.firstname = firstname;
    this.lastname = lastname;
  }

  @ConstraintTarget
  String getFirstname() { return firstname; }

  @ConstraintTarget
  String getLastname() { return lastname; }
}

I think we can assume that for a Variable T x there is either a getter T getX() or T x() (or T isX() if x is a boolean). More complex cases could be still written in the classic style by annotating each getter.

Also, this would be really great to work in combination with lombok, so we only would need to write:

@ConstraintTargets
@Value
class Person {
  String firstname;
  String firstname;
}

However, this might be a different problem, I guess.