IAmYourFaja
IAmYourFaja

Reputation: 56912

Do Javax and Hibernate Validations imply NotNull?

The list of supported Javax Validations are here.

The list of supported Hibernate Validations (extending Javax) are here.

Do each of these annotations "extend" or imply @NotNull?

For instance, if I annotate the following entity:

public class Widget {
    @Email
    private String email;

    @URL
    private String website;

    // etc...
}

Does @Email and @URL also enforce @NotNull? Or are they simply applied/enforced if those properties are defined for a particular Widget instance?

Which annotations extend/imply @NotNull, and which ones don't? Are there any other implied relationships with these annotations?

Upvotes: 1

Views: 608

Answers (1)

Bohuslav Burghardt
Bohuslav Burghardt

Reputation: 34776

No. If you look at the source code of EmailValidator from hibernate validator for example, it contains this code at the beginning of isValid() method:

if ( value == null || value.length() == 0 ) {
    return true;
}

Same for URLValidator and so on. Generally all validators for the corresponding annotations consider the value valid when you try to validate null value, so a good rule of thumb would be to always perform the not-null validations separately.

Edit: here is a quote from JSR-303 specification related to this issue:

While not mandatory, it is considered a good practice to split the core constraint validation from the not null constraint validation (for example, an @Email constraint will return true on a null object, i.e. will not take care of the @NotNull validation)

Upvotes: 3

Related Questions