package com.ruoyi.ext.api.validate;

import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * 自定义字母和数字校验注解实现
 * 
 * @author ruoyi
 */
public class NumberOrEnglishValidator implements ConstraintValidator<NumberOrEnglishV, String>
{
    String pattern;
    Pattern compile;

    @Override
    public void initialize(NumberOrEnglishV constraintAnnotation) {
        pattern = "^([A-Za-z]|[0-9])+$";
        compile = Pattern.compile(pattern);
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isBlank(value)){
            return true;
        }
        return compile.matcher(value).matches();
    }
}