package com.storlead.framework.common.validation;

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

/**
 * @program: sp-sales-platform
 * @description:
 * @author: chenkq
 * @create: 2024-12-11 11:04
 */
public class NeContainsValidator implements ConstraintValidator<NeContains, String> {

    private String forbiddenString;

    @Override
    public void initialize(NeContains constraintAnnotation) {
        // 获取注解中定义的不能包含的字符串
        this.forbiddenString = constraintAnnotation.value();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        // 空值直接返回 true，不参与校验
        if (value == null) {
            return false;
        }
        // 判断字符串是否包含指定的字符串
        return value.indexOf(forbiddenString) > 0;
    }
}
