package com.ff.validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.List;

/**
 * 列表大小验证器
 *
 * @author shi
 * @date 2025/02/12
 */
public class ListSizeValidator implements ConstraintValidator<ListSizeCheck, List<?>> {

    private int maxSize;

    @Override
    public void initialize(ListSizeCheck constraintAnnotation) {
        // 从注解中获取最大值
        this.maxSize = constraintAnnotation.maxSize();
    }

    @Override
    public boolean isValid(List<?> value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }
        // 判断列表是否为空且大小不超过 maxSize
        return !value.isEmpty() && value.size() <= maxSize;
    }
}