【问题标题】:how to create a ConstraintValidator for List如何为 List 创建一个 ConstraintValidator
【发布时间】:2011-10-23 08:51:06
【问题描述】:

我有一个简单的验证器来验证字符串值是否是预定义列表的一部分:

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());
}

}

例如它将验证:

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

我想为字符串列表创建一个验证器来验证类似这样的内容:

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

我试过了:

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;
}

}

问题是,如果列表包含 2 个或更多非法值,则只会出现一个(第一个)约束违规。我希望它有不止一个。我该怎么做?

【问题讨论】:

标签: java hibernate list bean-validation


【解决方案1】:

您当前的代码存在 2 个问题:

在您的CoBoundedStringListConstraintsisValid 方法中,您应该像这样遍历给定列表的所有元素(设置适当的allValid 标志):

@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;
}

第二个是针对违反约束的equals 的实现(javax.validation.Validator.validate() 返回一个集合!)。当您始终输入相同的消息 (should be one of [a, b]) 时,该集合仍将仅包含 1 个元素。作为一种解决方案,您可以将当前值添加到消息中(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;
}

【讨论】:

  • 谢谢!有用! @Valid 假设做什么 CoBoundedStringListConstraints 做什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-03
  • 1970-01-01
  • 2022-08-19
  • 2010-12-06
  • 2012-06-03
  • 2011-05-26
  • 1970-01-01
相关资源
最近更新 更多