package cc.zeelan.framework.utils.valid.custom;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
/**
* 自定義ID驗證規則
* @author witts
* @project core-utils
* @package cc.zeelan.framework.valid.custom
* @version 1.0
* @message 林花謝了春紅,太匆匆。無奈朝來寒雨,晚來風
*/
@Documented
@Constraint(validatedBy = {_IDCustomValid.class })
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface IDCustomValid {
String message() default "ID為32位有效ID";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
package cc.zeelan.framework.utils.valid.custom;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
/**
* ID注解規則設置
* @author witts
* @project seller-core-utils
* @package cc.zeelan.framework.utils.valid.custom
* @version 1.0
* @message 林花謝了春紅,太匆匆。無奈朝來寒雨,晚來風
*/
public class _IDCustomValid implements ConstraintValidator<IDCustomValid,String>{
@Override
public void initialize(IDCustomValid constraintAnnotation) {}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if(null == value) {
return false;
}
if(value.trim().length()>=32) {
return true;
}
return false;
}
}