|
I don't see how the existing constraints can be changed. The Bean Validation specification only recommends that null values should be considered and of course only where it makes sense. The intention of @NotBlank and @NotEmpty is to consider null not valid (also historically these constraints were taken over from Hibernate Validator 3 and pre Bean Validation). The javadocs are also clear on this:
NotBlank
:
Validate that the annotated string is not
Unknown macro: {@code null}
or empty. The difference to
Unknown macro: {@code NotEmpty}
is that trailing whitespaces are getting ignored.
NotEmpty
:
Asserts that the annotated string, collection, map or array is not
Unknown macro: {@code null}
or empty.
That all said, there would be two options:
-
Create new constraints which consider null as valid. TBH, I am not sure how to name these constraints
-
Add a flag to the existing constraints determining the behavior regarding null values. Per default the existing behavior would apply.
Also, on second thought, there is also the Bean Validation constraint @Size which could be used if null is supposed to be treated as valid.
Overall I am not sure whether anything should be done here.
|