I've replaced the Validator for the javax.validation.constraints.Size
annotation:
<constraint-mappings
xmlns="http://xmlns.jcp.org/xml/ns/validation/mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/validation/mapping
http://xmlns.jcp.org/xml/ns/validation/mapping/validation-mapping-2.0.xsd"
version="2.0">
<constraint-definition annotation="javax.validation.constraints.Size">
<validated-by include-existing-validators="true">
<value>com.org.beanvalidation.validator.CustomSizeValidator</value>
</validated-by>
</constraint-definition>
</constraint-mappings>
Is it possible make a call to the out-of-the-box implementation of the Size
validator, something like sizeValidator.isValid(value, cxt)
to re-use the base implementation?
Inside CustomSizeValidator
, I cant seem to import SizeValidator
.. Cannot resolve symbol 'SizeValidator'
Is there some other way of accessing the out-of-the-box implementation of the Size
validator?
My goal is to add some logic on top of the Size
base validation.
public class CustomSizeValidator implements
ConstraintValidator<Size, String> {
Size size;
@Override
public void initialize(Size size) {
this.size = size;
}
@Override
public boolean isValid(String value,
ConstraintValidatorContext cxt) {
// SizeValidator sizeValidator = new SizeValidator();
// boolean isValid = sizeValidator.isValid(value, cxt);
// Having to reimplement logic of original `Size` annotation:
if (value.length() > this.size.min() && value.length() < this.size.max() && isValid) {
return true;
} else {
return false;
}
}
}
question from:
https://stackoverflow.com/questions/66049391/when-implementing-a-constraint-validator-is-it-possible-to-re-use-one-of-the-bas 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…