pl.allegro.tech.hermes.management.api.validator.ApiPreconditions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hermes-management Show documentation
Show all versions of hermes-management Show documentation
Fast and reliable message broker built on top of Kafka.
package pl.allegro.tech.hermes.management.api.validator;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.groups.Default;
import java.util.Set;
import org.springframework.stereotype.Component;
import pl.allegro.tech.hermes.api.constraints.AdminPermitted;
@Component
public class ApiPreconditions {
private final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
public void checkConstraints(T object, boolean skipAdminPermitted) {
Class>[] groups = groupsToValidate(skipAdminPermitted);
Set> violations = validator.validate(object, groups);
if (!violations.isEmpty()) {
throw new ConstraintViolationException(violations);
}
}
private Class>[] groupsToValidate(boolean skipAdminPermitted) {
if (skipAdminPermitted) {
return new Class[] {Default.class};
} else {
return new Class[] {Default.class, AdminPermitted.class};
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy