Skip to content

Commit

Permalink
feat: 增加 DictValid3 适应 SpringBoot 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
houkunlin committed Aug 19, 2024
1 parent 5cc7f08 commit 417dc3f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ dependencies {
api 'com.google.guava:guava:33.0.0-jre'
api 'com.github.ben-manes.caffeine:caffeine'
api 'org.javassist:javassist:3.28.0-GA'
compileOnly 'javax.validation:validation-api:2.0.1.Final'
compileOnly 'jakarta.validation:jakarta.validation-api:3.0.2'
compileOnly 'io.swagger:swagger-annotations:1.6.6'
testImplementation 'io.swagger:swagger-annotations:1.6.6'
compileOnly 'io.swagger.core.v3:swagger-annotations:2.2.2'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.houkunlin.system.dict.starter.json;


import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
Expand All @@ -9,7 +8,8 @@
import java.lang.annotation.Target;

/**
* 字典校验
* 字典校验,如果找不到字典值文本,则校验失败。
* SpringBoot 2.x 的校验用法,主要是 javax.validation 包名问题,SpringBoot 3.x 改包名了
*
* @author HouKunLin
*/
Expand All @@ -28,6 +28,7 @@
* 当此注解在系统字典枚举上时,该字段表示字典类型代码。
*
* @return 数据字典代码
* @see DictText#value()
*/
String value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.houkunlin.system.dict.starter.json;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 字典校验,如果找不到字典值文本,则校验失败。
* SpringBoot 3.x 的校验用法。主要是 javax.validation 包名问题,SpringBoot 3.x 改包名了
*
* @author HouKunLin
*/
@Constraint(validatedBy = {DictValid3ConstraintValidator.class})
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DictValid3 {
String message() default "字典参数错误";

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

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

/**
* 数据字典的代码。
* 当此注解在系统字典枚举上时,该字段表示字典类型代码。
*
* @return 数据字典代码
* @see DictText#value()
*/
String value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.houkunlin.system.dict.starter.json;

import com.houkunlin.system.dict.starter.DictUtil;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

/**
* 校验字典信息是否在字典列表中。校验字典信息的填写是否符合需求
*
* @author HouKunLin
*/
public class DictValid3ConstraintValidator implements ConstraintValidator<DictValid3, Object> {
private String dictType;

@Override
public boolean isValid(final Object value, final ConstraintValidatorContext context) {
if (dictType == null || value == null) {
return false;
}
return DictUtil.getDictText(dictType, value.toString()) != null;
}

@Override
public void initialize(final DictValid3 constraintAnnotation) {
this.dictType = constraintAnnotation.value();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public boolean isValid(final Object value, final ConstraintValidatorContext cont
if (dictType == null || value == null) {
return false;
}
return DictUtil.getDictText(dictType, String.valueOf(value)) != null;
return DictUtil.getDictText(dictType, value.toString()) != null;
}

@Override
Expand Down

0 comments on commit 417dc3f

Please sign in to comment.