cn.ipokerface.common.validation.validator.PatternConstraintValidator Maven / Gradle / Ivy
Show all versions of common-beans Show documentation
package cn.ipokerface.common.validation.validator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by PokerFace
* Create Date 2019-11-25.
* Email: [email protected]
* Version 1.0.0
*
* Description:
*/
public class PatternConstraintValidator implements ConstraintValidator {
/**
* Validate string by some pattern
* Pattern.compile()
* pattern.match()
*
* @param value parameter value
* @param constraintAnnotation parameter annotation
* @return
*/
public boolean validate(String value, cn.ipokerface.common.validation.constraint.Pattern constraintAnnotation) {
if (value == null || "".equals(value)) return true;
String patternValue = constraintAnnotation.value();
if (patternValue == null || "".equals(patternValue)) return true;
Pattern pattern = java.util.regex.Pattern.compile(patternValue);
Matcher matcher = pattern.matcher(value);
if (matcher.matches()) return true;
return false;
}
}