-
-
Notifications
You must be signed in to change notification settings - Fork 595
HV-2193: Create a new constraint @NullOrNotBlank similar to @NotBlank for use with nullable fields and Optional<> #1906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
439c77f
HV-2193: Create a new constraint @NullOrNotBlank similar to @NotBlank…
koentsje 53c0b7f
HV-2193: Add constraint validator and tests for @NullOrNotBlank
koentsje 83bff7e
HV-2193: Register @NullOrNotBlank constraint and add integration tests
koentsje 3c99b5d
HV-2193: Add programmatic constraint definition for @NullOrNotBlank
koentsje bfd2cea
HV-2193: Add @TestForIssue annotation to @NullOrNotBlank test classes
koentsje f7b08cf
HV-2193: Add annotation processor support for @NullOrNotBlank
koentsje ef38976
HV-2193: Add documentation for @NullOrNotBlank
koentsje File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...rc/test/java/org/hibernate/validator/ap/testmodel/ModelWithNullOrNotBlankConstraints.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright Red Hat Inc. and Hibernate Authors | ||
| */ | ||
| package org.hibernate.validator.ap.testmodel; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| import org.hibernate.validator.constraints.NullOrNotBlank; | ||
|
|
||
| public class ModelWithNullOrNotBlankConstraints { | ||
|
|
||
| @NullOrNotBlank | ||
| public Collection<String> collection; | ||
|
|
||
| @NullOrNotBlank | ||
| public List<String> list; | ||
|
|
||
| @NullOrNotBlank | ||
| public Set<String> set; | ||
|
|
||
| @NullOrNotBlank | ||
| public String string; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
engine/src/main/java/org/hibernate/validator/cfg/defs/NullOrNotBlankDef.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright Red Hat Inc. and Hibernate Authors | ||
| */ | ||
|
|
||
| package org.hibernate.validator.cfg.defs; | ||
|
|
||
| import org.hibernate.validator.cfg.ConstraintDef; | ||
| import org.hibernate.validator.constraints.NullOrNotBlank; | ||
|
|
||
| /** | ||
| * Constraint definition for {@link NullOrNotBlank}. | ||
| * @author Koen Aers | ||
| * @since 9.1 | ||
| */ | ||
| public class NullOrNotBlankDef extends ConstraintDef<NullOrNotBlankDef, NullOrNotBlank> { | ||
|
|
||
| public NullOrNotBlankDef() { | ||
| super( NullOrNotBlank.class ); | ||
| } | ||
| } | ||
57 changes: 57 additions & 0 deletions
57
engine/src/main/java/org/hibernate/validator/constraints/NullOrNotBlank.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,57 @@ | ||||||
| /* | ||||||
| * SPDX-License-Identifier: Apache-2.0 | ||||||
| * Copyright Red Hat Inc. and Hibernate Authors | ||||||
| */ | ||||||
| package org.hibernate.validator.constraints; | ||||||
|
|
||||||
| import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||||||
| import static java.lang.annotation.ElementType.CONSTRUCTOR; | ||||||
| import static java.lang.annotation.ElementType.FIELD; | ||||||
| import static java.lang.annotation.ElementType.METHOD; | ||||||
| import static java.lang.annotation.ElementType.PARAMETER; | ||||||
| import static java.lang.annotation.ElementType.TYPE_USE; | ||||||
| import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||||||
|
|
||||||
| import java.lang.annotation.Documented; | ||||||
| import java.lang.annotation.Repeatable; | ||||||
| import java.lang.annotation.Retention; | ||||||
| import java.lang.annotation.Target; | ||||||
|
|
||||||
| import jakarta.validation.Constraint; | ||||||
| import jakarta.validation.Payload; | ||||||
|
|
||||||
| import org.hibernate.validator.constraints.NullOrNotBlank.List; | ||||||
|
|
||||||
| /** | ||||||
| * The annotated element must be {@code null} or not blank. A character sequence is considered | ||||||
| * not blank when it contains at least one non-whitespace character. | ||||||
| * <p> | ||||||
| * This is particularly useful with {@link java.util.Optional} where an empty {@code Optional} | ||||||
| * (extracted as {@code null}) should be valid, but a present value must not be blank. | ||||||
| * | ||||||
| * @author Koen Aers | ||||||
| * @since 9.1 | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
and here 🙂 |
||||||
| */ | ||||||
| @Documented | ||||||
| @Constraint(validatedBy = { }) | ||||||
| @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) | ||||||
| @Retention(RUNTIME) | ||||||
| @Repeatable(List.class) | ||||||
| public @interface NullOrNotBlank { | ||||||
|
|
||||||
| String message() default "{org.hibernate.validator.constraints.NullOrNotBlank.message}"; | ||||||
|
|
||||||
| Class<?>[] groups() default { }; | ||||||
|
|
||||||
| Class<? extends Payload>[] payload() default { }; | ||||||
|
|
||||||
| /** | ||||||
| * Defines several {@code @NullOrNotBlank} annotations on the same element. | ||||||
| */ | ||||||
| @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) | ||||||
| @Retention(RUNTIME) | ||||||
| @Documented | ||||||
| public @interface List { | ||||||
| NullOrNotBlank[] value(); | ||||||
| } | ||||||
| } | ||||||
30 changes: 30 additions & 0 deletions
30
...ava/org/hibernate/validator/internal/constraintvalidators/hv/NullOrNotBlankValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright Red Hat Inc. and Hibernate Authors | ||
| */ | ||
| package org.hibernate.validator.internal.constraintvalidators.hv; | ||
|
|
||
| import jakarta.validation.ConstraintValidator; | ||
| import jakarta.validation.ConstraintValidatorContext; | ||
|
|
||
| import org.hibernate.validator.constraints.NullOrNotBlank; | ||
|
|
||
| /** | ||
| * Check that the character sequence is either {@code null} or not blank. | ||
| * | ||
| * @author Koen Aers | ||
| */ | ||
| public class NullOrNotBlankValidator implements ConstraintValidator<NullOrNotBlank, CharSequence> { | ||
|
|
||
| @Override | ||
| public boolean isValid( | ||
| CharSequence value, | ||
| ConstraintValidatorContext constraintValidatorContext) { | ||
| if ( value == null ) { | ||
| return true; | ||
| } | ||
| else { | ||
| return !value.toString().trim().isEmpty(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...rg/hibernate/validator/test/constraints/annotations/hv/NullOrNotBlankConstrainedTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright Red Hat Inc. and Hibernate Authors | ||
| */ | ||
| package org.hibernate.validator.test.constraints.annotations.hv; | ||
|
|
||
| import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertNoViolations; | ||
| import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertThat; | ||
| import static org.hibernate.validator.testutil.ConstraintViolationAssert.violationOf; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.Set; | ||
|
|
||
| import jakarta.validation.ConstraintViolation; | ||
|
|
||
| import org.hibernate.validator.constraints.NullOrNotBlank; | ||
| import org.hibernate.validator.test.constraints.annotations.AbstractConstrainedTest; | ||
| import org.hibernate.validator.testutil.TestForIssue; | ||
|
|
||
| import org.testng.annotations.Test; | ||
|
|
||
| /** | ||
| * @author Koen Aers | ||
| */ | ||
| @TestForIssue(jiraKey = "HV-2193") | ||
| public class NullOrNotBlankConstrainedTest extends AbstractConstrainedTest { | ||
|
|
||
| @Test | ||
| public void nullIsValid() { | ||
| Set<ConstraintViolation<Foo>> violations = | ||
| validator.validate( new Foo( null ) ); | ||
| assertNoViolations( violations ); | ||
| } | ||
|
|
||
| @Test | ||
| public void notBlankIsValid() { | ||
| Set<ConstraintViolation<Foo>> violations = | ||
| validator.validate( new Foo( "foobar" ) ); | ||
| assertNoViolations( violations ); | ||
| } | ||
|
|
||
| @Test | ||
| public void blankIsInvalid() { | ||
| Set<ConstraintViolation<Foo>> violations = | ||
| validator.validate( new Foo( " " ) ); | ||
| assertThat( violations ).containsOnlyViolations( | ||
| violationOf( NullOrNotBlank.class ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| public void emptyIsInvalid() { | ||
| Set<ConstraintViolation<Foo>> violations = | ||
| validator.validate( new Foo( "" ) ); | ||
| assertThat( violations ).containsOnlyViolations( | ||
| violationOf( NullOrNotBlank.class ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| public void optionalWithValueIsValid() { | ||
| Set<ConstraintViolation<Bar>> violations = | ||
| validator.validate( new Bar( Optional.of( "foobar" ) ) ); | ||
| assertNoViolations( violations ); | ||
| } | ||
|
|
||
| @Test | ||
| public void emptyOptionalIsValid() { | ||
| Set<ConstraintViolation<Bar>> violations = | ||
| validator.validate( new Bar( Optional.empty() ) ); | ||
| assertNoViolations( violations ); | ||
| } | ||
|
|
||
| @Test | ||
| public void optionalWithBlankValueIsInvalid() { | ||
| Set<ConstraintViolation<Bar>> violations = | ||
| validator.validate( new Bar( Optional.of( " " ) ) ); | ||
| assertThat( violations ).containsOnlyViolations( | ||
| violationOf( NullOrNotBlank.class ) | ||
| ); | ||
| } | ||
|
|
||
| private static class Foo { | ||
|
|
||
| @NullOrNotBlank | ||
| private final String string; | ||
|
|
||
| public Foo(String string) { | ||
| this.string = string; | ||
| } | ||
| } | ||
|
|
||
| private static class Bar { | ||
|
|
||
| private final Optional<@NullOrNotBlank String> string; | ||
|
Check warning on line 95 in engine/src/test/java/org/hibernate/validator/test/constraints/annotations/hv/NullOrNotBlankConstrainedTest.java
|
||
|
|
||
| public Bar(Optional<String> string) { | ||
| this.string = string; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as 9.1 is already out, we'll add it to the 9.2 🙂