Search in sources :

Example 1 with ValidationException

use of jakarta.validation.ValidationException in project minijax by minijax.

the class MinijaxConstraintDescriptor method build.

@SuppressWarnings("unchecked")
public static <T extends Annotation> MinijaxConstraintDescriptor<T> build(final AnnotatedType annotatedType, final T annotation) {
    final Constraint constraint = annotation.annotationType().getAnnotation(Constraint.class);
    if (constraint == null) {
        return null;
    }
    final Class<?> valueClass = ReflectionUtils.getRawType(annotatedType);
    final Class<?> annotationClass = annotation.annotationType();
    if (constraint.validatedBy().length > 0) {
        return buildDeclaredValidator(annotation, constraint.validatedBy()[0]);
    } else if (annotationClass == AssertFalse.class) {
        return (MinijaxConstraintDescriptor<T>) buildAssertFalseValidator((AssertFalse) annotation, valueClass);
    } else if (annotationClass == AssertTrue.class) {
        return (MinijaxConstraintDescriptor<T>) buildAssertTrueValidator((AssertTrue) annotation, valueClass);
    } else if (annotationClass == Max.class) {
        return (MinijaxConstraintDescriptor<T>) buildMaxValidator((Max) annotation, valueClass);
    } else if (annotationClass == Min.class) {
        return (MinijaxConstraintDescriptor<T>) buildMinValidator((Min) annotation, valueClass);
    } else if (annotationClass == NotBlank.class) {
        return (MinijaxConstraintDescriptor<T>) buildNotBlankValidator((NotBlank) annotation, valueClass);
    } else if (annotationClass == NotEmpty.class) {
        return (MinijaxConstraintDescriptor<T>) buildNotEmptyValidator((NotEmpty) annotation, valueClass);
    } else if (annotationClass == NotNull.class) {
        return (MinijaxConstraintDescriptor<T>) buildNotNullValidator((NotNull) annotation);
    } else if (annotationClass == Pattern.class) {
        return (MinijaxConstraintDescriptor<T>) buildPatternValidator((Pattern) annotation, valueClass);
    } else if (annotationClass == Size.class) {
        return (MinijaxConstraintDescriptor<T>) buildSizeValidator((Size) annotation, valueClass);
    } else {
        throw new ValidationException("Unrecognized constraint annotation: " + annotation);
    }
}
Also used : Pattern(jakarta.validation.constraints.Pattern) ValidationException(jakarta.validation.ValidationException) Constraint(jakarta.validation.Constraint) Max(jakarta.validation.constraints.Max) Size(jakarta.validation.constraints.Size) AssertTrue(jakarta.validation.constraints.AssertTrue) NotNull(jakarta.validation.constraints.NotNull) Min(jakarta.validation.constraints.Min) NotBlank(jakarta.validation.constraints.NotBlank) AssertFalse(jakarta.validation.constraints.AssertFalse) NotEmpty(jakarta.validation.constraints.NotEmpty)

Example 2 with ValidationException

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

the class BranchHelper method createContext.

public static BranchContext createContext(Attributes<?> attrs, MergeCandidate merge) {
    final String ref = merge.ref();
    final String branch = prettifyRef(gitShortRef(ref), attrs);
    final BranchContext bc = new BranchContext(ref, branch, createMaterial(attrs, merge));
    bc.setProvider(attrs.asConnectionConfig());
    bc.setIdentifier(requireNonNullElse(merge.identifier(), ""));
    bc.setTitle(requireNonNullElse(merge.title(), ""));
    bc.setAuthor(requireNonNullElse(merge.author(), ""));
    bc.setReferenceUrl(requireNonNullElse(merge.showUrl(), ""));
    bc.setLabels(new ArrayList<>(requireNonNullElse(merge.labels(), Collections.emptyList())));
    validate(bc, (errors) -> {
        throw new ValidationException("Branch context binding is missing data! " + "Check the provider's API response. Error(s):\n" + errors.stream().map(ConstraintViolation::getMessage).collect(Collectors.joining("\n", "  ", "")));
    });
    return bc;
}
Also used : ValidationException(jakarta.validation.ValidationException) BranchContext(cd.go.contrib.plugins.configrepo.groovy.dsl.BranchContext)

Example 3 with ValidationException

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

the class ResteasyViolationExceptionMapper method toResponse.

public Response toResponse(ValidationException exception) {
    if (exception instanceof ConstraintDefinitionException) {
        return buildResponse(unwrapException(exception), MediaType.TEXT_PLAIN, Status.INTERNAL_SERVER_ERROR);
    }
    if (exception instanceof ConstraintDeclarationException) {
        return buildResponse(unwrapException(exception), MediaType.TEXT_PLAIN, Status.INTERNAL_SERVER_ERROR);
    }
    if (exception instanceof GroupDefinitionException) {
        return buildResponse(unwrapException(exception), MediaType.TEXT_PLAIN, Status.INTERNAL_SERVER_ERROR);
    }
    if (exception instanceof ResteasyViolationException) {
        ResteasyViolationException resteasyViolationException = ResteasyViolationException.class.cast(exception);
        Exception e = resteasyViolationException.getException();
        if (e != null) {
            return buildResponse(unwrapException(e), MediaType.TEXT_PLAIN, Status.INTERNAL_SERVER_ERROR);
        } else if (resteasyViolationException.getReturnValueViolations().size() == 0) {
            return buildViolationReportResponse(resteasyViolationException, Status.BAD_REQUEST);
        } else {
            return buildViolationReportResponse(resteasyViolationException, Status.INTERNAL_SERVER_ERROR);
        }
    }
    return buildResponse(unwrapException(exception), MediaType.TEXT_PLAIN, Status.INTERNAL_SERVER_ERROR);
}
Also used : GroupDefinitionException(jakarta.validation.GroupDefinitionException) ConstraintDeclarationException(jakarta.validation.ConstraintDeclarationException) ResteasyViolationException(org.jboss.resteasy.api.validation.ResteasyViolationException) ResteasyViolationException(org.jboss.resteasy.api.validation.ResteasyViolationException) ConstraintDeclarationException(jakarta.validation.ConstraintDeclarationException) ConstraintDefinitionException(jakarta.validation.ConstraintDefinitionException) ValidationException(jakarta.validation.ValidationException) GroupDefinitionException(jakarta.validation.GroupDefinitionException) ConstraintDefinitionException(jakarta.validation.ConstraintDefinitionException)

Example 4 with ValidationException

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

the class AbstractValidatorContextResolver method getContext.

public GeneralValidatorCDI getContext(Class<?> type) {
    try {
        BootstrapConfiguration bootstrapConfiguration = getConfig();
        boolean isExecutableValidationEnabled = bootstrapConfiguration.isExecutableValidationEnabled();
        Set<ExecutableType> defaultValidatedExecutableTypes = bootstrapConfiguration.getDefaultValidatedExecutableTypes();
        return new GeneralValidatorImpl(getValidatorFactory(), isExecutableValidationEnabled, defaultValidatedExecutableTypes);
    } catch (Exception e) {
        throw new ValidationException(Messages.MESSAGES.unableToLoadValidationSupport(), e);
    }
}
Also used : ExecutableType(jakarta.validation.executable.ExecutableType) ValidationException(jakarta.validation.ValidationException) BootstrapConfiguration(jakarta.validation.BootstrapConfiguration) NamingException(javax.naming.NamingException) ValidationException(jakarta.validation.ValidationException)

Example 5 with ValidationException

use of jakarta.validation.ValidationException in project mojarra by eclipse-ee4j.

the class BeanValidation method getValidatorFactory.

public static ValidatorFactory getValidatorFactory(FacesContext context) {
    ValidatorFactory validatorFactory = null;
    Object cachedObject = context.getExternalContext().getApplicationMap().get(VALIDATOR_FACTORY_KEY);
    if (cachedObject instanceof ValidatorFactory) {
        validatorFactory = (ValidatorFactory) cachedObject;
    } else {
        try {
            validatorFactory = buildDefaultValidatorFactory();
        } catch (ValidationException e) {
            throw new FacesException("Could not build a default Bean Validator factory", e);
        }
        context.getExternalContext().getApplicationMap().put(VALIDATOR_FACTORY_KEY, validatorFactory);
    }
    return validatorFactory;
}
Also used : ValidationException(jakarta.validation.ValidationException) Validation.buildDefaultValidatorFactory(jakarta.validation.Validation.buildDefaultValidatorFactory) ValidatorFactory(jakarta.validation.ValidatorFactory) FacesException(jakarta.faces.FacesException)

Aggregations

ValidationException (jakarta.validation.ValidationException)18 ValidatorFactory (jakarta.validation.ValidatorFactory)6 Test (org.testng.annotations.Test)5 Validator (jakarta.validation.Validator)3 NotNull (jakarta.validation.constraints.NotNull)3 ValidationExtension (dev.morphia.validation.ValidationExtension)2 FacesException (jakarta.faces.FacesException)2 Constraint (jakarta.validation.Constraint)2 TraversableResolver (jakarta.validation.TraversableResolver)2 ExecutableType (jakarta.validation.executable.ExecutableType)2 HibernateValidator (org.hibernate.validator.HibernateValidator)2 LoadClass (org.hibernate.validator.internal.util.privilegedactions.LoadClass)2 BranchContext (cd.go.contrib.plugins.configrepo.groovy.dsl.BranchContext)1 BootstrapConfiguration (jakarta.validation.BootstrapConfiguration)1 ConstraintDeclarationException (jakarta.validation.ConstraintDeclarationException)1 ConstraintDefinitionException (jakarta.validation.ConstraintDefinitionException)1 GroupDefinitionException (jakarta.validation.GroupDefinitionException)1 Payload (jakarta.validation.Payload)1 Validation.buildDefaultValidatorFactory (jakarta.validation.Validation.buildDefaultValidatorFactory)1 AssertFalse (jakarta.validation.constraints.AssertFalse)1