¶Ô±ÈÐÂÎļþ |
| | |
| | | package org.dromara.common.core.validate.enumd;
|
| | |
|
| | | import jakarta.validation.ConstraintValidator;
|
| | | import jakarta.validation.ConstraintValidatorContext;
|
| | | import org.dromara.common.core.utils.StringUtils;
|
| | | import org.dromara.common.core.utils.reflect.ReflectUtils;
|
| | |
|
| | | /**
|
| | | * èªå®ä¹æä¸¾æ ¡éªæ³¨è§£å®ç°
|
| | | *
|
| | | * @author ç§è¾æªå¯
|
| | | * @date 2024-12-09
|
| | | */
|
| | | public class EnumPatternValidator implements ConstraintValidator<EnumPattern, String> {
|
| | |
|
| | | private EnumPattern annotation;;
|
| | |
|
| | | @Override
|
| | | public void initialize(EnumPattern annotation) {
|
| | | ConstraintValidator.super.initialize(annotation);
|
| | | this.annotation = annotation;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
|
| | | if (StringUtils.isNotBlank(value)) {
|
| | | String fieldName = annotation.fieldName();
|
| | | for (Object e : annotation.type().getEnumConstants()) {
|
| | | if (value.equals(ReflectUtils.invokeGetter(e, fieldName))) {
|
| | | return true;
|
| | | }
|
| | | }
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | }
|