Hello team, I've observed an inconsistency in the default validation behavior that affects the validation of nullable fields.
As documented, most constraints in the `jakarta.validation.constraints` package (with the exception of `@Null`) consider `null` values as valid. This is generally useful as it allows combining constraints with `@NotNull` only when required. However, `@NotEmpty` and `@NotBlank` are exceptions to this rule - they explicitly fail on `null` values. This creates difficulty when validating nullable fields. For example, for an optional string field that should be non-blank when present, there's no clean, declarative way to express this constraint. The current workaround requires creating custom constraints, which adds boilerplate for what seems like a common use case. Therefore, I'd like to propose introducing a new annotation, for example: - `@NotBlankOrNull`, or - `@NotBlankIfPresent` This annotation would validate that a string is not blank only when it's present (non-null), treating `null` as valid. This would align with other constraints and provide a consistent way to validate optional, non-blank strings. Thank you for your time and consideration. For reference, I have also opened an issue <https://github.com/jakartaee/validation/issues/316> in the jakarta-validation repository to discuss this, but haven't received feedback there yet. Best regards, Ivan Malutin _______________________________________________ hibernate-dev mailing list -- [email protected] To unsubscribe send an email to [email protected] Privacy Statement: https://www.redhat.com/en/about/privacy-policy List Archives:
