Search in sources :

Example 1 with ExecutableValidator

use of jakarta.validation.executable.ExecutableValidator in project spring-framework by spring-projects.

the class MethodValidationInterceptor method invoke.

@Override
@Nullable
public Object invoke(MethodInvocation invocation) throws Throwable {
    // Avoid Validator invocation on FactoryBean.getObjectType/isSingleton
    if (isFactoryBeanMetadataMethod(invocation.getMethod())) {
        return invocation.proceed();
    }
    Class<?>[] groups = determineValidationGroups(invocation);
    // Standard Bean Validation 1.1 API
    ExecutableValidator execVal = this.validator.forExecutables();
    Method methodToValidate = invocation.getMethod();
    Set<ConstraintViolation<Object>> result;
    Object target = invocation.getThis();
    Assert.state(target != null, "Target must not be null");
    try {
        result = execVal.validateParameters(target, methodToValidate, invocation.getArguments(), groups);
    } catch (IllegalArgumentException ex) {
        // Probably a generic type mismatch between interface and impl as reported in SPR-12237 / HV-1011
        // Let's try to find the bridged method on the implementation class...
        methodToValidate = BridgeMethodResolver.findBridgedMethod(ClassUtils.getMostSpecificMethod(invocation.getMethod(), target.getClass()));
        result = execVal.validateParameters(target, methodToValidate, invocation.getArguments(), groups);
    }
    if (!result.isEmpty()) {
        throw new ConstraintViolationException(result);
    }
    Object returnValue = invocation.proceed();
    result = execVal.validateReturnValue(target, methodToValidate, returnValue, groups);
    if (!result.isEmpty()) {
        throw new ConstraintViolationException(result);
    }
    return returnValue;
}
Also used : ExecutableValidator(jakarta.validation.executable.ExecutableValidator) ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Method(java.lang.reflect.Method) Nullable(org.springframework.lang.Nullable)

Example 2 with ExecutableValidator

use of jakarta.validation.executable.ExecutableValidator in project hibernate-validator by hibernate.

the class ConstraintValidatorContextTest method testAddParameterNodeUsingCustomParameterNameProvider.

@Test
@TestForIssue(jiraKey = "HV-709")
public void testAddParameterNodeUsingCustomParameterNameProvider() throws Exception {
    ExecutableValidator executableValidator = ValidatorUtil.getConfiguration().parameterNameProvider(new PrefixableParameterNameProvider("param")).buildValidatorFactory().getValidator().forExecutables();
    Set<ConstraintViolation<User>> constraintViolations = executableValidator.validateParameters(new User(), User.class.getMethod("setAddresses", Map.class), new java.lang.Object[] { new HashMap<>() });
    assertThat(constraintViolations).containsOnlyPaths(pathWith().method("setAddresses").parameter("param0", 0), pathWith().method("setAddresses").parameter("param0", 0).bean(), pathWith().method("setAddresses").parameter("param0", 0).property("myNode1", true, null, 23).bean());
}
Also used : ExecutableValidator(jakarta.validation.executable.ExecutableValidator) PrefixableParameterNameProvider(org.hibernate.validator.testutil.PrefixableParameterNameProvider) ConstraintViolation(jakarta.validation.ConstraintViolation) HashMap(java.util.HashMap) Map(java.util.Map) CollectionHelper.newHashMap(org.hibernate.validator.internal.util.CollectionHelper.newHashMap) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 3 with ExecutableValidator

use of jakarta.validation.executable.ExecutableValidator in project hibernate-validator by hibernate.

the class TypeVariableMethodParameterResolutionTest method testTypeVariableMethodParameterResolution.

@Test
public void testTypeVariableMethodParameterResolution() throws NoSuchMethodException, SecurityException {
    ExecutableValidator validator = Validation.buildDefaultValidatorFactory().getValidator().forExecutables();
    Set<ConstraintViolation<Bean>> violations = validator.validateParameters(new Bean(), Bean.class.getMethod("method", List.class), new Object[] { new ArrayList<>() });
    assertThat(violations).containsOnlyViolations(violationOf(Size.class));
}
Also used : ExecutableValidator(jakarta.validation.executable.ExecutableValidator) Size(jakarta.validation.constraints.Size) ConstraintViolation(jakarta.validation.ConstraintViolation) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 4 with ExecutableValidator

use of jakarta.validation.executable.ExecutableValidator in project hibernate-validator by hibernate.

the class ConstraintViolationSerializationTest method testSuccessfulSerializationWithMethodReturnValue.

@Test
@TestForIssue(jiraKey = "HV-1485")
public void testSuccessfulSerializationWithMethodReturnValue() throws Exception {
    ExecutableValidator validator = ValidatorUtil.getValidator().forExecutables();
    SerializableClass testInstance = new SerializableClass("s");
    Set<ConstraintViolation<SerializableClass>> constraintViolations = validator.validateReturnValue(testInstance, SerializableClass.class.getMethod("fooReturnValue"), "s");
    byte[] bytes = serialize(constraintViolations);
    Set<ConstraintViolation<?>> deserializedViolations = deserialize(bytes);
    assertThat(deserializedViolations).containsOnlyViolations(violationOf(Size.class).withPropertyPath(pathWith().method("fooReturnValue").returnValue()));
    checkSerializedViolation(deserializedViolations.iterator().next(), testInstance, null, "s");
}
Also used : ExecutableValidator(jakarta.validation.executable.ExecutableValidator) ConstraintViolation(jakarta.validation.ConstraintViolation) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 5 with ExecutableValidator

use of jakarta.validation.executable.ExecutableValidator in project hibernate-validator by hibernate.

the class ConstraintViolationSerializationTest method testSuccessfulSerializationWithMethodParameters.

@Test
@TestForIssue(jiraKey = "HV-1485")
public void testSuccessfulSerializationWithMethodParameters() throws Exception {
    ExecutableValidator validator = ValidatorUtil.getValidator().forExecutables();
    SerializableClass testInstance = new SerializableClass("s");
    Set<ConstraintViolation<SerializableClass>> constraintViolations = validator.validateParameters(testInstance, SerializableClass.class.getMethod("fooParameter", String.class), new Object[] { "s" });
    byte[] bytes = serialize(constraintViolations);
    Set<ConstraintViolation<?>> deserializedViolations = deserialize(bytes);
    assertThat(deserializedViolations).containsOnlyViolations(violationOf(Size.class).withPropertyPath(pathWith().method("fooParameter").parameter("foo", 0)));
    checkSerializedViolation(deserializedViolations.iterator().next(), testInstance, new Object[] { "s" }, null);
}
Also used : ExecutableValidator(jakarta.validation.executable.ExecutableValidator) ConstraintViolation(jakarta.validation.ConstraintViolation) Test(org.testng.annotations.Test) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Aggregations

ExecutableValidator (jakarta.validation.executable.ExecutableValidator)35 ConstraintViolation (jakarta.validation.ConstraintViolation)34 Test (org.testng.annotations.Test)30 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)13 Method (java.lang.reflect.Method)11 Path (jakarta.validation.Path)8 NotNull (jakarta.validation.constraints.NotNull)8 TestForIssue (org.hibernate.validator.testutil.TestForIssue)6 ConstraintViolationException (jakarta.validation.ConstraintViolationException)5 Size (jakarta.validation.constraints.Size)5 GenericConstraintDef (org.hibernate.validator.cfg.GenericConstraintDef)3 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)3 Nullable (cn.taketoday.lang.Nullable)2 HashMap (java.util.HashMap)2 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)2 PrefixableParameterNameProvider (org.hibernate.validator.testutil.PrefixableParameterNameProvider)2 AroundConstruct (jakarta.interceptor.AroundConstruct)1 AroundInvoke (jakarta.interceptor.AroundInvoke)1 ConstraintValidator (jakarta.validation.ConstraintValidator)1 ConstraintValidatorFactory (jakarta.validation.ConstraintValidatorFactory)1