package com.cf.imes.module.system.validation.dict;

import org.apache.commons.lang3.StringUtils;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

/**
 * @author Gqr
 * @since 2024/9/20 15:27
 */
public class CssClassValidator implements ConstraintValidator<CssClassValid, String> {
    private static final String REGEX = "^#([0-9a-fA-F]{3}){1,2}$";

    @Override
    public void initialize(CssClassValid constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

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