package com.backendsys.entity.validator;

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

import java.util.Arrays;
import java.util.List;

/**
 * 自定义注解 (逻辑实现)
 */
public class RangeStringArrayValidator implements ConstraintValidator<RangeStringArray, String> {
    private List<String> value;

    @Override
    public void initialize(RangeStringArray constrainAnnotation) {
        value = Arrays.asList(constrainAnnotation.value());
        ConstraintValidator.super.initialize(constrainAnnotation);
    }

    @Override
    public boolean isValid(String validValue, ConstraintValidatorContext constraintValidatorContext) {
        if (validValue == null || validValue.isEmpty()) { return true; }
        if (value != null) {
            if (!value.contains(validValue)) {
                return false;
            }
        }
        return true;
    }
}
