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

    @Override
    public void initialize(ZipV constraintAnnotation) {
        pattern = "^[1-9]\\d{5}$";
        compile = Pattern.compile(pattern);
    }

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