#1757 - Add support for javax.validation.constraints.NotBlank.
The presence of @NonBlank on a representation model's property now causes it to be considered required. It will also cause a default pattern exposed that allows clients to validate input values. Fixes #1757.
This commit is contained in:
committed by
Oliver Drotbohm
parent
c501fa20f7
commit
1f6f1e24d4
@@ -31,6 +31,7 @@ import javax.validation.constraints.DecimalMin;
|
||||
import javax.validation.constraints.Email;
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Pattern;
|
||||
|
||||
@@ -68,6 +69,8 @@ import com.fasterxml.jackson.annotation.JsonUnwrapped;
|
||||
*/
|
||||
public class PropertyUtils {
|
||||
|
||||
static final String NOT_BLANK_REGEX = "^\\s*(\\S+\\s*)+$";
|
||||
|
||||
private static final Map<ResolvableType, ResolvableType> DOMAIN_TYPE_CACHE = new ConcurrentReferenceHashMap<>();
|
||||
private static final Map<ResolvableType, InputPayloadMetadata> METADATA_CACHE = new ConcurrentReferenceHashMap<>();
|
||||
private static final Set<String> FIELDS_TO_IGNORE = new HashSet<>(Arrays.asList("class", "links"));
|
||||
@@ -601,7 +604,10 @@ public class PropertyUtils {
|
||||
*/
|
||||
@Override
|
||||
public boolean isRequired() {
|
||||
return super.isRequired() || property.getAnnotation(NotNull.class).isPresent();
|
||||
|
||||
return super.isRequired() //
|
||||
|| property.getAnnotation(NotNull.class).isPresent() //
|
||||
|| property.getAnnotation(NotBlank.class).isPresent();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -610,7 +616,14 @@ public class PropertyUtils {
|
||||
*/
|
||||
@Override
|
||||
public Optional<String> getPattern() {
|
||||
return getAnnotationAttribute(Pattern.class, "regexp", String.class);
|
||||
|
||||
Optional<String> attribute = getAnnotationAttribute(Pattern.class, "regexp", String.class);
|
||||
|
||||
if (!attribute.isPresent() && property.getAnnotation(NotBlank.class).isPresent()) {
|
||||
attribute = Optional.of(NOT_BLANK_REGEX);
|
||||
}
|
||||
|
||||
return attribute;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -31,6 +31,7 @@ import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.validation.constraints.Email;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Pattern;
|
||||
|
||||
@@ -158,10 +159,20 @@ class PropertyUtilsTest {
|
||||
assertThat(it.isRequired()).isTrue();
|
||||
});
|
||||
|
||||
assertThat(getProperty(metadata, "nonBlank")).hasValueSatisfying(it -> {
|
||||
assertThat(it.isRequired()).isTrue();
|
||||
assertThat(it.getPattern()).hasValue(PropertyUtils.NOT_BLANK_REGEX);
|
||||
});
|
||||
|
||||
assertThat(getProperty(metadata, "pattern")).hasValueSatisfying(it -> {
|
||||
assertThat(it.getPattern()).hasValue("\\w");
|
||||
});
|
||||
|
||||
assertThat(getProperty(metadata, "nonBlankPattern")).hasValueSatisfying(it -> {
|
||||
assertThat(it.isRequired()).isTrue();
|
||||
assertThat(it.getPattern()).hasValue("\\w");
|
||||
});
|
||||
|
||||
assertThat(getProperty(metadata, "annotated")).hasValueSatisfying(it -> {
|
||||
assertThat(it.getPattern()).hasValue("regex");
|
||||
});
|
||||
@@ -250,7 +261,9 @@ class PropertyUtilsTest {
|
||||
static class Jsr303SamplePayload {
|
||||
|
||||
@NotNull String nonNull;
|
||||
@NotBlank String nonBlank;
|
||||
@Pattern(regexp = "\\w") String pattern;
|
||||
@NotBlank @Pattern(regexp = "\\w") String nonBlankPattern;
|
||||
TypeAnnotated annotated;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user