parent
f21b632ddc
commit
49e4f92707
@ -0,0 +1,18 @@
|
||||
package ${basePackage}.frame.validation;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Target({ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Constraint(validatedBy = DictValidator.class)
|
||||
public @interface Dict {
|
||||
String message() default "字典项错误";
|
||||
|
||||
String name() default "";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package ${basePackage}.frame.validation;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class DictValidator implements ConstraintValidator<Dict, String> {
|
||||
|
||||
private String name;
|
||||
|
||||
@Override
|
||||
public void initialize(Dict constraint) {
|
||||
name = constraint.name();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(String o, ConstraintValidatorContext constraintValidatorContext) {
|
||||
if (null == o) {
|
||||
return true;
|
||||
} else if (name == null) {
|
||||
constraintValidatorContext.disableDefaultConstraintViolation();
|
||||
constraintValidatorContext.buildConstraintViolationWithTemplate("字典名称为空").addConstraintViolation();
|
||||
return false;
|
||||
} else {
|
||||
// name 字典名称
|
||||
HashSet<String> codeSet = new HashSet<>();
|
||||
if (codeSet.contains(o)) {
|
||||
return true;
|
||||
} else {
|
||||
constraintValidatorContext.disableDefaultConstraintViolation();
|
||||
constraintValidatorContext.buildConstraintViolationWithTemplate("非法的字典[" + name + "]值").addConstraintViolation();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
package ${basePackage}.module.${moduleName}.enums;
|
||||
|
||||
|
||||
/**
|
||||
* ${Type} - ${FieldComment}
|
||||
*
|
||||
* @author ${author?default("")}
|
||||
* @version 0.0.1
|
||||
* @since ${date?string("yyyy-MM-dd")}
|
||||
*/
|
||||
public enum ${Type} {
|
||||
|
||||
}
|
Loading…
Reference in new issue