package com.backendsys.modules.common.utils;

import com.backendsys.exception.CustException;
import com.backendsys.modules.cms.article.entity.ArticleCategory;
import com.backendsys.modules.cms.article.entity.ArticleCategoryI18n;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;

import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public class ValidationUtil {

    private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

    public static <T> void validateObject(T object, Class<?>... groups) {
        Set<ConstraintViolation<T>> violations = validator.validate(object, groups);
        if (!violations.isEmpty()) {
            Optional<ConstraintViolation<T>> violation = violations.stream().findFirst();
            String errorMessage = violation.get().getMessage();
            String propertyName = violation.get().getPropertyPath().toString();
            throw new CustException(errorMessage, ResultEnum.PARAMETER_EXCEPTION.getCode(), "Field: " + propertyName);
        }
    }

}
