-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($Validation): support enum value validation
- Loading branch information
1 parent
5086351
commit 0951f97
Showing
10 changed files
with
171 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...-center/src/main/java/com/jmsoftware/maf/authcenter/user/entity/GetUserStatusPayload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.jmsoftware.maf.authcenter.user.entity; | ||
|
||
import com.jmsoftware.maf.common.domain.authcenter.user.UserStatus; | ||
import com.jmsoftware.maf.springcloudstarter.annotation.ValidEnumValue; | ||
import lombok.Data; | ||
|
||
/** | ||
* <h1>GetUserStatusPayload</h1> | ||
* <p> | ||
* Change description here. | ||
* | ||
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com | ||
* @date 6/6/21 4:31 PM | ||
**/ | ||
@Data | ||
public class GetUserStatusPayload { | ||
@ValidEnumValue(targetEnum = UserStatus.class) | ||
private Byte status; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...tarter/src/main/java/com/jmsoftware/maf/springcloudstarter/annotation/ValidEnumValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.jmsoftware.maf.springcloudstarter.annotation; | ||
|
||
import com.jmsoftware.maf.springcloudstarter.validation.EnumValueValidator; | ||
|
||
import javax.validation.Constraint; | ||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
/** | ||
* <h1>ValidEnumValue</h1> | ||
* <p> | ||
* Change description here. | ||
* | ||
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com | ||
* @date 5/29/21 12:31 PM | ||
**/ | ||
@Documented | ||
@Retention(RUNTIME) | ||
@Target({FIELD, PARAMETER}) | ||
@Constraint(validatedBy = EnumValueValidator.class) | ||
public @interface ValidEnumValue { | ||
String message() default "Invalid enumeration value"; | ||
|
||
Class<?> targetEnum() default Class.class; | ||
|
||
boolean ignoreNull() default false; | ||
|
||
Class[] groups() default {}; | ||
|
||
Class[] payload() default {}; | ||
} |
77 changes: 77 additions & 0 deletions
77
...er/src/main/java/com/jmsoftware/maf/springcloudstarter/validation/EnumValueValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.jmsoftware.maf.springcloudstarter.validation; | ||
|
||
import cn.hutool.core.util.ObjectUtil; | ||
import com.jmsoftware.maf.springcloudstarter.annotation.ValidEnumValue; | ||
import lombok.extern.slf4j.Slf4j; | ||
import lombok.val; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.Objects; | ||
|
||
/** | ||
* <h1>EnumValueValidator</h1> | ||
* <p> | ||
* Change description here. | ||
* | ||
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com | ||
* @date 5/29/21 12:34 PM | ||
**/ | ||
@Slf4j | ||
public class EnumValueValidator implements ConstraintValidator<ValidEnumValue, Number> { | ||
private final static String METHOD_NAME = "getValue"; | ||
private ValidEnumValue validEnumValue; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void initialize(ValidEnumValue constraintAnnotation) { | ||
ConstraintValidator.super.initialize(constraintAnnotation); | ||
validEnumValue = constraintAnnotation; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public boolean isValid(Number value, ConstraintValidatorContext context) { | ||
val enumClass = validEnumValue.targetEnum(); | ||
val ignoreNull = validEnumValue.ignoreNull(); | ||
if (!enumClass.isEnum()) { | ||
log.warn("The given target enum class is not enum! {}", enumClass.getName()); | ||
return false; | ||
} | ||
if (ignoreNull && ObjectUtil.isNull(value)) { | ||
return true; | ||
} | ||
val enumConstantArray = enumClass.getEnumConstants(); | ||
Method method; | ||
try { | ||
method = enumClass.getMethod(METHOD_NAME); | ||
} catch (NoSuchMethodException | SecurityException e) { | ||
log.warn("Did not find the method {} in the class {}", METHOD_NAME, enumClass.getName()); | ||
return false; | ||
} | ||
var validResult = false; | ||
try { | ||
for (var obj : enumConstantArray) { | ||
Object valueDeclaredInEnum = method.invoke(obj); | ||
if (!(valueDeclaredInEnum instanceof Number)) { | ||
log.error("The value declared in enum is not an instance of Number!"); | ||
throw new IllegalArgumentException("The value declared in enum is not an instance of Number!"); | ||
} | ||
if (Objects.deepEquals(value, valueDeclaredInEnum)) { | ||
validResult = true; | ||
break; | ||
} | ||
} | ||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { | ||
log.error("Exception occurred when invoking method! Exception message: {}", e.getMessage()); | ||
return false; | ||
} | ||
return validResult; | ||
} | ||
} |