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;

/**
 * 自定义EMail地址校验注解实现
 * 
 * @author ruoyi
 */
public class EmailValidator implements ConstraintValidator<EmailV, String>
{
    String pattern;
    Pattern compile;

    @Override
    public void initialize(EmailV constraintAnnotation) {
        pattern = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
        compile = Pattern.compile(pattern);
    }

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