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 FzjgValidator implements ConstraintValidator<FzjgV, String>
{
    String pattern;
    Pattern compile;

    @Override
    public void initialize(FzjgV constraintAnnotation) {
        pattern = "[无]{1}$|^[藏川鄂甘赣桂贵黑沪吉冀津晋京辽鲁蒙闽宁青琼陕苏皖湘新渝豫粤云浙][A-Z0-9]{1}$";
        compile = Pattern.compile(pattern);
    }

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