首页 > 其他分享 >自定义校验器 - 多字段联合校验

自定义校验器 - 多字段联合校验

时间:2022-09-24 09:55:26浏览次数:54  
标签:domain designer 自定义 runachina 校验 sc import com 多字段

@MinMax
        private Boolean getMinMaxValidate() {
            if (maxWidth == null || minWidth == null) {
                return false;
            }
            return maxWidth > minWidth;
        }

定义一个私有的get方法,既可以用返回值作为自定义校验器的值,又可以避免get方法在json序列化时被作为属性值传到前端

 

package com.runachina.sc.designer.domain.validator.annotations;

import com.runachina.sc.designer.domain.validator.CodeExistValidator;
import com.runachina.sc.designer.domain.validator.MinMaxValidator;
import com.runachina.sc.designer.domain.validator.vo.DataTypeEnum;
import com.runachina.sc.designer.domain.validator.vo.ValidateScopeEnum;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
 * 数值大小比较
 */
@Target({ ElementType.METHOD })
@Retention(RUNTIME)
@Constraint(validatedBy = MinMaxValidator.class)
public @interface MinMax {

    String message() default "最大值不能小于最小值";

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

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

}

 

package com.runachina.sc.designer.domain.validator;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.runachina.cloud.starter.base.exception.RestResultException;
import com.runachina.cloud.starter.base.rest.ResultStatus;
import com.runachina.sc.designer.domain.entity.LayoutEntity;
import com.runachina.sc.designer.domain.validator.annotations.CodeExist;
import com.runachina.sc.designer.domain.validator.annotations.MinMax;
import com.runachina.sc.designer.domain.validator.utils.ValidateDataStore;

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

/**
 * lcx
 * 校验icon的引用是否存在 - icon存在性校验
 */
public class MinMaxValidator implements ConstraintValidator<MinMax, Boolean> {

    @Override
    public void initialize(MinMax minMax) {
        ConstraintValidator.super.initialize(minMax);
    }

    @Override
    public boolean isValid(Boolean value, ConstraintValidatorContext constraintValidatorContext) {
        if (value == null) {
            return true;
        }
        return value;
    }

//    public static void main(String[] args) {
//        LayoutEntity layoutEntity = new LayoutEntity();
//        layoutEntity.setResolution(new LayoutEntity.Resolution());
//
//        ObjectMapper mapper = new ObjectMapper();
//        try {
//            System.out.println(mapper.writeValueAsString(layoutEntity.getResolution()));
//        } catch (JsonProcessingException e) {
//            throw new RestResultException(ResultStatus.ILLEGAL_ARGUMENT, "对象转json字符串异常");
//        }
//
//    }

}

 

标签:domain,designer,自定义,runachina,校验,sc,import,com,多字段
From: https://www.cnblogs.com/LcxSummer/p/16724986.html

相关文章