package com.ruoyi.ext.api.validate;

import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 自定义本地枚举字典校验注解实现
 * 采用  @Pattern(regexp = "^((0)|(1)|(2))$", message = "性别 取值范围为:男,女,未知") 注释也可以
 * @author ruoyi
 */
public class EnumValidator implements ConstraintValidator<EnumV, String>
{
    private String[] dicts;


    @Override
    public void initialize(EnumV constraintAnnotation) {
        dicts = constraintAnnotation.limitValue();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext)
    {
        if (StringUtils.isBlank(value)){
            return true;
        }
        for (String longValue : dicts) {
            if (value.equals(longValue)) {
                return true;
            }
        }
        return false;
    }


}