如何为列表创建 ConstraintValidator

发布于 2024-12-11 20:03:32 字数 1771 浏览 0 评论 0原文

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

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 个或多个非法值,则只会有一个(第一个)约束违规。我希望它有多个。我该怎么做呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

金橙橙 2024-12-18 20:03:32

您当前的代码有两个问题:

在您的 CoBoundedStringListConstraintsisValid 方法中,您应该像这样迭代给定列表的所有元素(设置一个 allValid< /code> 标志适当):

@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() 返回一个设置!)。当您始终放入相同的消息(应该是 [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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文