Search code examples
javavalidationannotations

Is there a Java Validation annotation for value equality


Is there an annotation for checking that two variables of an object have the same value?

Somethin like this:

public class User{

    private String id;

    @Size(min = 3, max = 18)
    private String username;

    @Size(min = 5, max = 32)
    private String password;

    @Equals("password")
    private String password2;

}    

Solution

  • Generally speaking (as in: as general as in your question), the answer is: no.

    And there can't be one - as it is not clear what "checking" would mean in that context. When should it happen; what should happen, and so on.

    The only "general checking" I am aware of would be the checker-framework

    But when you ask the same question for specific frameworks, that deal with data; then sure, such annotations can be created and used. In our project, we do have "property annotations" for a specific class of objects; and a whole lot of "framework" code around that to provide specific semantics when you are dealing with these kinds of objects.

    In other words: depending on your "use case", there might be existing framworks (like Apache Spring) that provide some kind of annotation-based checking.