依赖于其他字段的 Spring 自定义验证器

Xmus Jackson Flaxon Waxon

我们正在为控制器端点中使用的请求对象使用 spring 自定义验证器。我们实现它的方式与下面链接中的实现方式相同:

https://www.baeldung.com/spring-mvc-custom-validator

我们面临的问题是,如果特定字段也依赖于其他输入字段,它就无法工作。例如,我们将以下代码作为控制器端点的请求对象:

public class FundTransferRequest {

     private String accountTo;
     private String accountFrom;
     private String amount;
     
     @CustomValidator
     private String reason;

     private Metadata metadata;

}

public class Metadata {
   private String channel; //e.g. mobile, web, etc.
}

基本上@CustomValidator 是我们的自定义验证器类,我们想要的逻辑是,如果从元数据提供的通道是“WEB”。不需要请求的“原因”字段。否则,它将是必需的。

有没有办法做到这一点?我做了额外的研究,看不到任何处理这种情况的方法。

德卡德

显然,如果您需要访问自定义验证器中的多个字段,则必须使用类级别的注释。

你提到的同一篇文章有​​一个例子:https ://www.baeldung.com/spring-mvc-custom-validator#custom-class-level-validation

在您的情况下,它可能看起来像这样:

@Constraint(validatedBy = CustomValidator.class)
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomValidation {

    String message() default "Reason required";
    String checkedField() default "metadata.channel";
    String checkedValue() default "WEB";
    String requiredField() default "reason";

    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}
package com.example.demo;

import org.springframework.beans.BeanWrapperImpl;
import org.springframework.stereotype.Component;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/*
If the supplied channel from Metadata is "WEB". The field "reason" of the request won't be required.
Else, it will be required.
 */
@Component
public class CustomValidator implements ConstraintValidator<CustomValidation, Object> {
    private String checkedField;
    private String checkedValue;
    private String requiredField;

    @Override
    public void initialize(CustomValidation constraintAnnotation) {
        this.checkedField = constraintAnnotation.checkedField();
        this.checkedValue = constraintAnnotation.checkedValue();
        this.requiredField = constraintAnnotation.requiredField();
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        Object checkedFieldValue = new BeanWrapperImpl(value)
                .getPropertyValue(checkedField);
        Object requiredFieldValue = new BeanWrapperImpl(value)
                .getPropertyValue(requiredField);

        return checkedFieldValue != null && checkedFieldValue.equals(checkedValue) || requiredFieldValue != null;
    }
}

用法将是:

@CustomValidation
public class FundTransferRequest {
...

或指定参数:

@CustomValidation(checkedField = "metadata.channel", 
        checkedValue = "WEB", 
        requiredField = "reason", 
        message = "Reason required")
public class FundTransferRequest {
...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

工厂男孩:定义依赖于其他字段的字段

HTML 输入验证(Angular)依赖于其他字段

验证依赖于其他输入的输入

Hyperopt:定义依赖于其他参数的参数

如何创建依赖于Spring Bean的自定义Spring PropertySource

Spring MVC自定义验证器

我可以有依赖于值的自定义Sequelize验证器吗?

依赖于其他表单控件的 Angular formControl 验证器

如何使用@Configuration排除Spring配置文件以依赖于其他项目

创建依赖于选项的自定义记录器和依赖于记录器的选项验证器时的循环依赖项异常

使用自定义验证器进行Spring Bean验证

Spring MVC验证器注释+自定义验证

使微调器依赖于其他微调器上选择的项目

MatLab:如何定义依赖于其他属性的(非常量)属性?

流星/铁路由器:如何等待依赖于其他数据的数据

Spring \ Spring Boot:是否需要自定义验证器?

依赖于另一个表单控件的Angular 2自定义验证器

在自定义验证器Spring Hibernate中注入@PersistenceContext

Spring自定义验证器注入NullPointerException

Spring JPA自定义验证器Bean插入

Spring不进入自定义验证器

Django Rest Framework 自定义只读字段依赖于相关模型

从旧模块中删除依赖于自定义字段的Django迁移

Scapy - 添加类型依赖于其他字段的字段

木偶:使自定义功能依赖于资源

Spring-boot-两个相关字段的自定义验证

具有多个字段的Spring自定义批注验证

自定义字段唯一性验证,用于实现公共接口Hibernate + Spring的类

获取和设置依赖于其他字段的方法