The hibernate validator documentation for @Negative, @NegativeOrZero, @Positive, and @PositiveOrZero states that hibernate validator supports validating any subtype of CharSequence:
additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the character sequence is evaluated)
However, when attempting to validate a String field with any of those constraints, hibernate validator throws the following exception:
javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.Negative' validating type 'java.lang.String'. Check configuration for 'string'
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getExceptionForNullValidator(ConstraintTree.java:116)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getInitializedConstraintValidator(ConstraintTree.java:162)
at org.hibernate.validator.internal.engine.constraintvalidation.SimpleConstraintTree.validateConstraints(SimpleConstraintTree.java:54)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:75)
at org.hibernate.validator.internal.metadata.core.MetaConstraint.doValidateConstraint(MetaConstraint.java:130)
at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:123)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:555)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForSingleDefaultGroupElement(ValidatorImpl.java:518)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:488)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:450)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:400)
at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:172)
at NegativeConstraintTest.test(NegativeConstraintTest.java:25)
Here is a unit test that demonstrates the problem:
import org.junit.jupiter.api.Test;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.constraints.Negative;
public class NegativeConstraintTest {
public static class Target {
private final String string;
public Target(String string) {
this.string = string;
}
@Negative
public String getString() {
return string;
}
}
@Test
public void test() {
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
validator.validate(new Target("1.0"));
}
}
Please either update the documentation, or add support for CharSequences to these constraints. |