gpt4 book ai didi

java - JSR-303 Bean 验证 - 将多个注释自定义约束到一个 validator

转载 作者:行者123 更新时间:2023-12-01 05:22:38 25 4
gpt4 key购买 nike

编写自定义约束时,是否可以通过一个 validator 实现来验证多个注释。例如,我有几个注释,它们规定了不同的 @size 注释,但我希望它们都指向同一个 validator 类,该 validator 类进行一些全局检查,即所有注释都必须与某个正则表达式匹配。据我所知,该实现采用一种注释类型。

一个注释

@Target( { METHOD, FIELD, ANNOTATION_TYPE, TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {UCNValidator.class})
@Documented
@Size(min = 9, max = 9, message = "{exactlength}")
public @interface UCN {

String message() default "{invalidFormat}";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

String fieldName() default "ucn";

}

validator

public class UCNValidator implements ConstraintValidator<UCN, String>
{

private String pattern = "[a-zA-Z].*";
private String fieldName;

@Override
public void initialize( UCN constraintAnnotation )
{
this.fieldName = constraintAnnotation.fieldName();
}

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

if ( value != null )
{
if ( !value.matches(pattern) )
{
//do some stuff
return false;
}

}

return true;

}

最佳答案

在验证对象的属性之一时,似乎没有办法从对象访问其他值。我使用的解决方案是将注释放在类上,然后 validator 将获取整个对象进行验证,并且您可以仅访问执行验证所需的信息。

这是我写的一个用于比较对象的两个不同属性的代码:

@Target(TYPE)
@Retention(RUNTIME)
@Constraint(validatedBy = LessThanValidator.class)
@Documented
public @interface LessThan {

String message() default "{com.bullethq.constraints.LessThan}";

String bigValueProperty();

String littleValueProperty();

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};
}

那么 validator 类是:

public class LessThanValidator implements ConstraintValidator<LessThan, Object> {

private LessThan constraint;

public void initialize(LessThan constraintAnnotation) {
constraint = constraintAnnotation;
}

public boolean isValid(Object object, ConstraintValidatorContext cvc) {
Object bigValue = getValue(object, constraint.bigValueProperty());
Object littleValue = getValue(object, constraint.littleValueProperty());

// If one of the values is null, then we do not perform validation.
if (bigValue == null || littleValue == null) {
return true;
}

if (bigValue instanceof Comparable && littleValue instanceof Comparable) {
boolean valid = ((Comparable<Object>) bigValue).compareTo(littleValue) > 0;
if (!valid) {
// If the values are not valid, then build a custom violations which has the correct path in it.
cvc.buildConstraintViolationWithTemplate(cvc.getDefaultConstraintMessageTemplate())
.addNode(constraint.littleValueProperty())
.addConstraintViolation().disableDefaultConstraintViolation();
}
return valid;
}
throw new IllegalArgumentException("Properties " + constraint.bigValueProperty() + " and " + constraint.littleValueProperty() + " both need to be comparable in " + object.getClass());
}
}

getValue() 方法只是一个使用反射从对象获取值的静态方法。

关于java - JSR-303 Bean 验证 - 将多个注释自定义约束到一个 validator ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10138328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com