如何为List创建一个ConstraintValidator [英] how to create a ConstraintValidator for List
问题描述
我有一个简单的验证器来验证String值是否为预定义列表的一部分:
public class CoBoundedStringConstraints implements ConstraintValidator< ; CoBoundedString,String>
{
私人列表< String> m_boundedTo;
@Override
public void initialize(CoBoundedString annotation)
{
m_boundedTo = FunctorUtils.transform(annotation.value(),new ToLowerCase());
$ b @Override
public boolean isValid(String value,ConstraintValidatorContext context)
{
if(value == null)
{
返回true;
}
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(应该是+ m_boundedTo之一).addConstraintViolation();
返回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;
$ b @Override
public boolean isValid(List< String> value,ConstraintValidatorContext context)
{
if(value == null)
{
返回true;
}
CoBoundedStringConstraints约束= new CoBoundedStringConstraints();
constraints.initialize(m_annotation);
for(String string:value)
{
if(!constraints.isValid(string,context))
{
return false;
}
}
返回true;
}
}
问题是,如果列表包含2个或更多的非法值,将只有一个(第一个)违反约束。我希望它有不止一个。我应该怎么做?
您的当前代码有两个问题:
在你的 CoBoundedStringListConstraints
的 isValid
方法中,你应该迭代给定列表的所有元素,像这样(设置 allValid
标志适当):
@Override
public boolean isValid(List< String> value,
ConstraintValidatorContext context){
if(value == null){
return true;
}
布尔值allValid = true;
CoBoundedStringConstraints约束=新的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个元素。作为一种解决方案,您可以在消息前添加当前值(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 +应该是+ m_boundedTo)
.addConstraintViolation()中的一个。
返回false;
}
返回true;
}
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?
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;
}
这篇关于如何为List创建一个ConstraintValidator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!