All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.itroadlabs.apicross.beanvalidation.MinPropertiesValidator Maven / Gradle / Ivy

The newest version!
package io.github.itroadlabs.apicross.beanvalidation;

import org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorContext;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

public class MinPropertiesValidator implements ConstraintValidator {
    private int minProperties;

    @Override
    public void initialize(MinProperties constraintAnnotation) {
        this.minProperties = constraintAnnotation.value();
    }

    @Override
    public boolean isValid(HasSpecifiedProperties value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }

        Set specifiedProperties = new HashSet<>(value.$specifiedProperties());
        if (specifiedProperties.size() < minProperties) {
            HibernateConstraintValidatorContext hibernateConstraintValidatorContext = context.unwrap(HibernateConstraintValidatorContext.class);
            hibernateConstraintValidatorContext.addMessageParameter("minProperties", minProperties);
            hibernateConstraintValidatorContext.addMessageParameter("specifiedProperties", specifiedProperties);
            return false;
        } else {
            return true;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy