Skip to content

HV-2193: Create a new constraint @NullOrNotBlank similar to @NotBlank for use with nullable fields and Optional<> - #1906

Open
koentsje wants to merge 7 commits into
hibernate:mainfrom
koentsje:HV-2193
Open

HV-2193: Create a new constraint @NullOrNotBlank similar to @NotBlank for use with nullable fields and Optional<>#1906
koentsje wants to merge 7 commits into
hibernate:mainfrom
koentsje:HV-2193

Conversation

@koentsje

Copy link
Copy Markdown
Member

Add a new Hibernate Validator specific constraint @NullOrNotBlank that validates a character sequence is either null or contains at least one non-whitespace character.

See: https://hibernate.atlassian.net/browse/HV-2193

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on licensing, please check here.


…for use with nullable fields and Optional

 - Add the initial implementation of constraint NullOrNotBlank

Signed-off-by: Koen Aers <koen.aers@gmail.com>
 - Add NullOrNotBlankValidator for CharSequence validation
 - Add NullOrNotBlankValidatorTest covering null, not blank, empty and blank cases

Signed-off-by: Koen Aers <koen.aers@gmail.com>
 - Register NullOrNotBlank in BuiltinConstraint and ConstraintHelper
 - Add validation message to ValidationMessages.properties
 - Update MessagePropertiesTest and PredefinedScopeAllConstraintsTest
 - Add NullOrNotBlankConstrainedTest covering direct field and Optional usage

Signed-off-by: Koen Aers <koen.aers@gmail.com>
 - Add NullOrNotBlankDef class

Signed-off-by: Koen Aers <koen.aers@gmail.com>
 - Add @testForissue(jiraKey = "HV-2193") to NullOrNotBlankValidatorTest and NullOrNotBlankConstrainedTest
 - Add testProgrammaticDefinition test for NullOrNotBlankDef in NullOrNotBlankValidatorTest

Signed-off-by: Koen Aers <koen.aers@gmail.com>
 - Add NULL_OR_NOT_BLANK constant to TypeNames
 - Register NullOrNotBlank for CharSequence in AP ConstraintHelper
 - Add ModelWithNullOrNotBlankConstraints test model
 - Add nullOrNotBlankConstraints test to ConstraintValidationProcessorIT

Signed-off-by: Koen Aers <koen.aers@gmail.com>
 - Add @NullOrNotBlank entry to the reference guide in chapter 2

Signed-off-by: Koen Aers <koen.aers@gmail.com>
@koentsje
koentsje requested a review from marko-bekhta March 13, 2026 12:29

@marko-bekhta marko-bekhta left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @koentsje 👋🏻 🙂

thanks! looks good, let's update that since tag and merge ?

/**
* Constraint definition for {@link NullOrNotBlank}.
* @author Koen Aers
* @since 9.1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @since 9.1
* @since 9.2

as 9.1 is already out, we'll add it to the 9.2 🙂

* (extracted as {@code null}) should be valid, but a present value must not be blank.
*
* @author Koen Aers
* @since 9.1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @since 9.1
* @since 9.2

and here 🙂

@ivamly

ivamly commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Hi! Thanks for this useful addition. I raised this topic back in November on the mailing list and opened an issue in the Jakarta Validation project.

Just wanted to check: are there any plans to introduce @NullOrNotEmpty (or similar variants)? If so, I’d be happy to help with the implementation.

@ivamly

ivamly commented May 6, 2026

Copy link
Copy Markdown
Contributor

@marko-bekhta ping.

@sonarqubecloud

sonarqubecloud Bot commented May 9, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants