Search in sources :

Example 16 with ValidationException

use of jakarta.validation.ValidationException in project hibernate-validator by hibernate.

the class GetAnnotationsParameterTest method testGetMessageParameter.

@Test
public void testGetMessageParameter() {
    NotNull testAnnotation = new NotNull() {

        @Override
        public String message() {
            return "test";
        }

        @Override
        public Class<?>[] groups() {
            return new Class<?>[] { Default.class };
        }

        @Override
        public Class<? extends Payload>[] payload() {
            @SuppressWarnings("unchecked") Class<? extends Payload>[] classes = new Class[] {};
            return classes;
        }

        @Override
        public Class<? extends Annotation> annotationType() {
            return this.getClass();
        }
    };
    String message = GetAnnotationAttribute.action(testAnnotation, "message", String.class).run();
    assertEquals("test", message, "Wrong message");
    Class<?>[] group = GetAnnotationAttribute.action(testAnnotation, "groups", Class[].class).run();
    assertEquals(group[0], Default.class, "Wrong group");
    try {
        GetAnnotationAttribute.action(testAnnotation, "message", Integer.class).run();
        fail();
    } catch (ValidationException e) {
        assertThat(e.getMessage()).startsWith("HV000082").as("Wrong exception message");
    }
    try {
        GetAnnotationAttribute.action(testAnnotation, "foo", Integer.class).run();
        fail();
    } catch (ValidationException e) {
        assertThat(e.getMessage()).startsWith("HV000083").as("Wrong exception message");
    }
}
Also used : ValidationException(jakarta.validation.ValidationException) Payload(jakarta.validation.Payload) NotNull(jakarta.validation.constraints.NotNull) Default(jakarta.validation.groups.Default) Test(org.testng.annotations.Test)

Example 17 with ValidationException

use of jakarta.validation.ValidationException in project hibernate-validator by hibernate.

the class ValidationBootstrapParameters method setValueExtractors.

@SuppressWarnings("unchecked")
private void setValueExtractors(Set<String> valueExtractorFqcns, ClassLoader externalClassLoader) {
    for (String valueExtractorFqcn : valueExtractorFqcns) {
        ValueExtractor<?> valueExtractor;
        try {
            Class<? extends ValueExtractor<?>> clazz = (Class<? extends ValueExtractor<?>>) run(LoadClass.action(valueExtractorFqcn, externalClassLoader));
            valueExtractor = run(NewInstance.action(clazz, "value extractor class"));
        } catch (ValidationException e) {
            throw LOG.getUnableToInstantiateValueExtractorClassException(valueExtractorFqcn, e);
        }
        ValueExtractorDescriptor descriptor = new ValueExtractorDescriptor(valueExtractor);
        ValueExtractorDescriptor previous = valueExtractorDescriptors.put(descriptor.getKey(), descriptor);
        if (previous != null) {
            throw LOG.getValueExtractorForTypeAndTypeUseAlreadyPresentException(valueExtractor, previous.getValueExtractor());
        }
        LOG.addingValueExtractor((Class<? extends ValueExtractor<?>>) valueExtractor.getClass());
    }
}
Also used : ValidationException(jakarta.validation.ValidationException) ValueExtractorDescriptor(org.hibernate.validator.internal.engine.valueextraction.ValueExtractorDescriptor) LoadClass(org.hibernate.validator.internal.util.privilegedactions.LoadClass) ValueExtractor(jakarta.validation.valueextraction.ValueExtractor)

Example 18 with ValidationException

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

the class GeneralValidatorImpl method isMethodValidatable.

@Override
public boolean isMethodValidatable(Method m) {
    if (!isExecutableValidationEnabled) {
        return false;
    }
    ExecutableType[] types = null;
    List<ExecutableType[]> typesList = getExecutableTypesOnMethodInHierarchy(m);
    if (typesList.size() > 1) {
        throw new ValidationException(Messages.MESSAGES.validateOnExceptionOnMultipleMethod());
    }
    if (typesList.size() == 1) {
        types = typesList.get(0);
    } else {
        ValidateOnExecution voe = m.getDeclaringClass().getAnnotation(ValidateOnExecution.class);
        if (voe == null) {
            types = defaultValidatedExecutableTypes;
        } else {
            if (voe.type().length > 0) {
                types = voe.type();
            } else {
                types = defaultValidatedExecutableTypes;
            }
        }
    }
    boolean isGetterMethod = isGetter(m);
    for (int i = 0; i < types.length; i++) {
        switch(types[i]) {
            case IMPLICIT:
            case ALL:
                return true;
            case NONE:
                continue;
            case NON_GETTER_METHODS:
                if (!isGetterMethod) {
                    return true;
                }
                continue;
            case GETTER_METHODS:
                if (isGetterMethod) {
                    return true;
                }
                continue;
            default:
                continue;
        }
    }
    return false;
}
Also used : ExecutableType(jakarta.validation.executable.ExecutableType) ValidateOnExecution(jakarta.validation.executable.ValidateOnExecution) ValidationException(jakarta.validation.ValidationException) Constraint(jakarta.validation.Constraint)

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