Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

java - When implementing a constraint validator is it possible to re-use one of the base validator implementations

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...