Search in sources :

Example 6 with Validator

use of jakarta.validation.Validator in project gocd-groovy-dsl-config-plugin by gocd-contrib.

the class TestBase method validate.

protected static void validate(Object object, Consumer<Set<ConstraintViolation<Object>>> errorHandler) {
    ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
    Validator validator = validatorFactory.getValidator();
    Set<ConstraintViolation<Object>> violations = validator.validate(object);
    if (!violations.isEmpty()) {
        errorHandler.accept(violations);
    }
}
Also used : ValidatorFactory(jakarta.validation.ValidatorFactory) ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator)

Example 7 with Validator

use of jakarta.validation.Validator in project resteasy by resteasy.

the class GeneralValidatorImpl method getValidator.

protected Validator getValidator(HttpRequest request) {
    Validator v = Validator.class.cast(request.getAttribute(Validator.class.getName()));
    if (v == null) {
        Locale locale = getLocale(request);
        if (locale == null) {
            v = validatorFactory.getValidator();
        } else {
            MessageInterpolator interpolator = new LocaleSpecificMessageInterpolator(validatorFactory.getMessageInterpolator(), locale);
            v = validatorFactory.usingContext().messageInterpolator(interpolator).getValidator();
        }
        request.setAttribute(Validator.class.getName(), v);
    }
    return v;
}
Also used : Locale(java.util.Locale) Validator(jakarta.validation.Validator) MessageInterpolator(jakarta.validation.MessageInterpolator)

Example 8 with Validator

use of jakarta.validation.Validator in project resteasy by resteasy.

the class GeneralValidatorImpl method validateAllParameters.

@Override
public void validateAllParameters(HttpRequest request, Object object, Method method, Object[] parameterValues, Class<?>... groups) {
    if (method.getParameterCount() == 0) {
        checkViolations(request);
        return;
    }
    Validator validator = getValidator(request);
    SimpleViolationsContainer violationsContainer = getViolationsContainer(request, object);
    Set<ConstraintViolation<Object>> cvs = null;
    try {
        cvs = validator.forExecutables().validateParameters(object, method, parameterValues, groups);
    } catch (Exception e) {
        violationsContainer.setException(e);
        throw toValidationException(e, violationsContainer);
    }
    violationsContainer.addViolations(cvs);
    if ((violationsContainer.isFieldsValidated() || !GetRestful.isRootResource(object.getClass()) || hasApplicationScope(object)) && violationsContainer.size() > 0) {
        throw new ResteasyViolationExceptionImpl(violationsContainer, request.getHttpHeaders().getAcceptableMediaTypes());
    }
}
Also used : ResteasyConstraintViolation(org.jboss.resteasy.api.validation.ResteasyConstraintViolation) ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator) ConstraintDeclarationException(jakarta.validation.ConstraintDeclarationException) ValidationException(jakarta.validation.ValidationException) PrivilegedActionException(java.security.PrivilegedActionException) ResteasyViolationException(org.jboss.resteasy.api.validation.ResteasyViolationException) ConstraintViolationException(jakarta.validation.ConstraintViolationException) ConstraintDefinitionException(jakarta.validation.ConstraintDefinitionException) GroupDefinitionException(jakarta.validation.GroupDefinitionException)

Example 9 with Validator

use of jakarta.validation.Validator in project resteasy by resteasy.

the class GeneralValidatorImpl method validate.

@Override
public void validate(HttpRequest request, Object object, Class<?>... groups) {
    Validator validator = getValidator(request);
    Set<ConstraintViolation<Object>> cvs = null;
    SimpleViolationsContainer violationsContainer = getViolationsContainer(request, object);
    if (alreadyFoundClassOrPropertyConstraint(violationsContainer)) {
        return;
    }
    try {
        cvs = validator.validate(object, groups);
    } catch (Exception e) {
        violationsContainer.setException(e);
        violationsContainer.setFieldsValidated(true);
        throw toValidationException(e, violationsContainer);
    }
    violationsContainer.addViolations(cvs);
    violationsContainer.setFieldsValidated(true);
}
Also used : ResteasyConstraintViolation(org.jboss.resteasy.api.validation.ResteasyConstraintViolation) ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator) ConstraintDeclarationException(jakarta.validation.ConstraintDeclarationException) ValidationException(jakarta.validation.ValidationException) PrivilegedActionException(java.security.PrivilegedActionException) ResteasyViolationException(org.jboss.resteasy.api.validation.ResteasyViolationException) ConstraintViolationException(jakarta.validation.ConstraintViolationException) ConstraintDefinitionException(jakarta.validation.ConstraintDefinitionException) GroupDefinitionException(jakarta.validation.GroupDefinitionException)

Example 10 with Validator

use of jakarta.validation.Validator in project resteasy by resteasy.

the class GeneralValidatorImpl method validateReturnValue.

@Override
public void validateReturnValue(HttpRequest request, Object object, Method method, Object returnValue, Class<?>... groups) {
    Validator validator = getValidator(request);
    SimpleViolationsContainer violationsContainer = getViolationsContainer(request, object);
    Set<ConstraintViolation<Object>> cvs = null;
    try {
        cvs = validator.forExecutables().validateReturnValue(object, method, returnValue, groups);
    } catch (Exception e) {
        violationsContainer.setException(e);
        throw toValidationException(e, violationsContainer);
    }
    violationsContainer.addViolations(cvs);
    if (violationsContainer.size() > 0) {
        throw new ResteasyViolationExceptionImpl(violationsContainer, request.getHttpHeaders().getAcceptableMediaTypes());
    }
}
Also used : ResteasyConstraintViolation(org.jboss.resteasy.api.validation.ResteasyConstraintViolation) ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator) ConstraintDeclarationException(jakarta.validation.ConstraintDeclarationException) ValidationException(jakarta.validation.ValidationException) PrivilegedActionException(java.security.PrivilegedActionException) ResteasyViolationException(org.jboss.resteasy.api.validation.ResteasyViolationException) ConstraintViolationException(jakarta.validation.ConstraintViolationException) ConstraintDefinitionException(jakarta.validation.ConstraintDefinitionException) GroupDefinitionException(jakarta.validation.GroupDefinitionException)

Aggregations

Validator (jakarta.validation.Validator)413 Test (org.testng.annotations.Test)340 ConstraintViolation (jakarta.validation.ConstraintViolation)258 HibernateValidator (org.hibernate.validator.HibernateValidator)215 TestForIssue (org.hibernate.validator.testutil.TestForIssue)181 ConstraintValidator (jakarta.validation.ConstraintValidator)114 ValidatorUtil.getValidator (org.hibernate.validator.testutils.ValidatorUtil.getValidator)88 ValidatorFactory (jakarta.validation.ValidatorFactory)79 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)71 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)43 Test (org.junit.Test)38 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)33 ConstraintViolationException (jakarta.validation.ConstraintViolationException)32 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)28 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)27 NotNull (jakarta.validation.constraints.NotNull)26 Path (jakarta.validation.Path)19 Size (jakarta.validation.constraints.Size)18 CountValidationCallsValidator (org.hibernate.validator.testutil.CountValidationCallsValidator)17 List (java.util.List)13