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
397 views
in Technique[技术] by (71.8m points)

java - how to create a ConstraintValidator for List

I have a simple validator to validate that a String value is part of a predefined list:

public class CoBoundedStringConstraints implements ConstraintValidator<CoBoundedString, String>
{

private List<String> m_boundedTo;

@Override
public void initialize(CoBoundedString annotation)
{
    m_boundedTo = FunctorUtils.transform(annotation.value(), new ToLowerCase());
}

@Override
public boolean isValid(String value, ConstraintValidatorContext context)
{
    if (value == null )
    {
        return true; 
    }

    context.disableDefaultConstraintViolation();
    context.buildConstraintViolationWithTemplate("should be one of " + m_boundedTo).addConstraintViolation();
    return m_boundedTo.contains(value.toLowerCase());
}

}

For example it will Validate:

@CoBoundedString({"a","b" })
public String operations;

I want to create a validator For a list of Strings to validate something like this:

@CoBoundedString({"a","b" })
public List<String> operations = new ArrayList<String>();

I tried this:

public class CoBoundedStringListConstraints implements ConstraintValidator<CoBoundedString, List<String>>
{

private CoBoundedString m_annotation;

@Override
public void initialize(CoBoundedString annotation)
{
    m_annotation = annotation;
}

@Override
public boolean isValid(List<String> value, ConstraintValidatorContext context)
{
    if (value == null )
    {
        return true; 
    }

    CoBoundedStringConstraints constraints = new CoBoundedStringConstraints();
    constraints.initialize(m_annotation);
    for (String string : value)
    {
        if (!constraints.isValid(string, context))
        {
            return false;
        }
    }
    return true;
}

}

The problem is, if list contains 2 or more illeagal values, there will be only one (the first one) constraint violation. I want it to have more than one. How should I do that?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

There are 2 problems with your current code:

In your CoBoundedStringListConstraints's isValid method you should iterate over all elements of the given list like this (set a allValid flag appropriate):

@Override
public boolean isValid(List<String> value,
        ConstraintValidatorContext context) {
    if (value == null) {
        return true;
    }

    boolean allValid = true;
    CoBoundedStringConstraints constraints = new CoBoundedStringConstraints();
    constraints.initialize(m_annotation);
    for (String string : value) {
        if (!constraints.isValid(string, context)) {
            allValid = false;
        }
    }
    return allValid;
}

The second is the implementation of equals for the constraint violation (javax.validation.Validator.validate() returns a set!). When you are always putting in the same message (should be one of [a, b]), the set will still contain only 1 element. As a solution you could prepend the current value to the message (class CoBoundedStringConstraints):

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {

    if (value == null) {
        return true;
    }

    if (!m_boundedTo.contains(value)) {
        context.disableDefaultConstraintViolation();
        context.buildConstraintViolationWithTemplate(
                value + " should be one of " + m_boundedTo)
                .addConstraintViolation();
        return false;
    }
    return true;
}

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

...