java 如何为 List 创建 ConstraintValidator
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7865209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to create a ConstraintValidator for List
提问by oshai
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?
问题是,如果 list 包含 2 个或更多非法值,则只有一个(第一个)违反约束。我希望它有不止一个。我该怎么做?
采纳答案by jeha
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):
在你CoBoundedStringListConstraints
的isValid
方法中,你应该像这样迭代给定列表的所有元素(设置一个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;
}
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
):
第二个是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;
}