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

com.github.ldeitos.validation.impl.ValidatorImpl Maven / Gradle / Ivy

package com.github.ldeitos.validation.impl;

import static com.github.ldeitos.exception.ViolationException.throwNew;
import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static org.apache.commons.collections.CollectionUtils.isNotEmpty;

import java.security.InvalidParameterException;
import java.util.HashSet;
import java.util.Set;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.ValidatorFactory;
import jakarta.validation.executable.ExecutableValidator;
import jakarta.validation.metadata.BeanDescriptor;

import com.github.ldeitos.exception.ViolationException;
import com.github.ldeitos.qualifier.ExtendedValidator;
import com.github.ldeitos.validation.Message;
import com.github.ldeitos.validation.Validator;

/**
 * Extended validator concrete implementation.
 *
 * @author Leandro Deitos
 *
 */
@ExtendedValidator
public class ValidatorImpl implements Validator {

	private ValidatorFactory factory = Validation.buildDefaultValidatorFactory();

	private jakarta.validation.Validator delegate = factory.getValidator();

	@Override
	public  Set> validate(T object, Class... groups) {
		return delegate.validate(object, groups);
	}

	@Override
	public  Set> validateProperty(T object, String propertyName, Class... groups) {
		return delegate.validateProperty(object, propertyName, groups);
	}

	@Override
	public  Set> validateValue(Class beanType, String propertyName,
	    Object value, Class... groups) {
		return delegate.validateValue(beanType, propertyName, value, groups);
	}

	@Override
	public BeanDescriptor getConstraintsForClass(Class clazz) {
		return delegate.getConstraintsForClass(clazz);
	}

	@Override
	public  T unwrap(Class type) {
		if (type.isAssignableFrom(Validator.class)) {
			return type.cast(this);
		}

		throw new InvalidParameterException(format("Impossible to get %s instance.", type.getName()));
	}

	@Override
	public ExecutableValidator forExecutables() {
		return delegate.forExecutables();
	}

	@Override
	public  Set validateBean(T object, Class... groups) {
		Set> constraints = validate(object, groups);
		return new HashSet(constraints.stream().map(MessageImpl::new).collect(toList()));
	}

	@Override
	public  Set validatePropertyBean(T object, String propertyName, Class... groups) {
		Set> constraints = validateProperty(object, propertyName, groups);
		return new HashSet(constraints.stream().map(MessageImpl::new).collect(toList()));
	}

	@Override
	public  Set validateValueBean(Class beanType, String propertyName, Object value,
	    Class... groups) {
		Set> constraints = validateValue(beanType, propertyName, value, groups);
		return new HashSet(constraints.stream().map(MessageImpl::new).collect(toList()));
	}

	@Override
	public  void validateAndThrow(T object, Class... groups) throws ViolationException {
		Set violations = validateBean(object, groups);

		if (isNotEmpty(violations)) {
			throwNew(format("Validated object [%s] has violations.", object), violations);
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy