package com.changhu.common.validator;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.changhu.common.validator.annotation.IdCard;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

/**
 * @author 20252
 * @createTime 2024/8/21 下午3:41
 * @desc IdCardValidator...
 */
public class IdCardValidator implements ConstraintValidator<IdCard, String> {

    private boolean required;

    @Override
    public void initialize(IdCard constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (required) {
            return Validator.isCitizenId(value);
        } else {
            if (StrUtil.isBlank(value)) {
                return true;
            } else {
                return Validator.isCitizenId(value);
            }
        }
    }
}
