In my Spring project I have a POJO class with a property for a CMYK color.
I want this property to be represented by a JSON array with exactly 4 floating-point numbers.
Each number must be in the range between 0.0
and 1.0
.
Currently I'm struggling with the validation of this property.
I have researched already and found that the @DecimalMin
and @DecimalMax
annotations cannot be used on Float
or float
(see the answers to this question).
Therefore I already abandoned List<Float>
and use List<BigDecimal>
instead.
Here is my stripped down POJO class:
public class Settings {
@NotNull
@Size(min = 4, max = 4)
@DecimalMin("0")
@DecimalMax("1")
private List<BigDecimal> cmykColor;
public List<BigDecimal> getCmykColor() {
return cmykColor;
}
public void setCmykColor(List<BigDecimal> cmykColor) {
this.cmykColor = cmykColor;
}
}
and here the JUnit test:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SettingsTest extends Assert {
@Autowired
private ObjectMapper objectMapper;
@Autowired
private Validator validator; // from org.springframework.validation
private Errors validate(String json) throws IOException {
Settings settings = objectMapper.readValue(json, Settings.class);
Errors errors = new BeanPropertyBindingResult(settings, "settings");
validator.validate(settings, errors);
return errors;
}
@Test
public void testValid() throws IOException {
String json = "{ \"cmykColor\": [0.5, 1.0, 0.3, 0.0] }";
Errors errors = validate(json);
assertEquals(0, errors.getErrorCount());
}
@Test
public void testTooBig() throws IOException {
String json = "{ \"cmykColor\": [0.5, 1.01, 0.3, 0.0] }";
Errors errors = validate(json);
assertEquals(1, errors.getErrorCount());
}
@Test
public void testTooMany() throws IOException {
String json = "{ \"cmykColor\": [0.5, 1.0, 0.3, 0.0, 0.0] }";
Errors errors = validate(json);
assertEquals(1, errors.getErrorCount());
assertNotNull(errors.getFieldError("cmykColor"));
}
}
When I comment out the @DecimalMin("0")
and @DecimalMax("1")
annotations,
all test-cases succeed, except of course the testTooBig
test-case,
which would rely on the @DecimalMax
constraint.
But when I add the @DecimalMin("0")
and @DecimalMax("1")
annotations,
I get the following exception on all test-cases, saying that there is no validator
for DecimalMax
to validate List<BigDecimal>
.
javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.DecimalMax' validating type 'java.util.List<java.math.BigDecimal>'. Check configuration for 'cmykColor'
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getExceptionForNullValidator(ConstraintTree.java:108)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getInitializedConstraintValidator(ConstraintTree.java:140)
at org.hibernate.validator.internal.engine.constraintvalidation.SimpleConstraintTree.validateConstraints(SimpleConstraintTree.java:55)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:73)
at org.hibernate.validator.internal.metadata.core.MetaConstraint.doValidateConstraint(MetaConstraint.java:127)
at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:120)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:533)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForSingleDefaultGroupElement(ValidatorImpl.java:496)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:465)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:430)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:380)
at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:169)
at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:104)
at com.oce.spring.domain.SettingsTest.validate(SettingsTest.java:29)
at com.oce.spring.domain.SettingsTest.testTooMany(SettingsTest.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
So, how am I supposed to use @DecimalMin("0")
and @DecimalMax("1")
on a List<BigDecimal>
correctly?
We implemented container element constraints in Hibernate Validator 6.0.x.
So upgrade to Hibernate Validator 6.0.x (6.0.9.Final is the latest) and Bean Validation 2.0.1 and do as follows:
Be careful, the groupId of Hibernate Validator 6.0 is org.hibernate.validator.