io.github.itroadlabs.apicross.beanvalidation.MinPropertiesValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of apicross-toolbox Show documentation
Show all versions of apicross-toolbox Show documentation
APICROSS utility classes library
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