Search in sources :

Example 96 with ConstraintViolation

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

the class ConstructorConstraintMappingTest method testMultipleParameterConstraintsAtDifferentParameters.

@Test
public void testMultipleParameterConstraintsAtDifferentParameters() throws Exception {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).constructor(String.class, String.class).parameter(0).constraint(new SizeDef().min(1).max(10)).parameter(1).constraint(new SizeDef().min(1).max(10));
    config.addMapping(mapping);
    Constructor<GreetingService> constructor = GreetingService.class.getConstructor(String.class, String.class);
    Object[] parameterValues = new Object[] { "", "" };
    ExecutableValidator executableValidator = getConfiguredExecutableValidator();
    Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorParameters(constructor, parameterValues);
    assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("message", 0)), violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("anotherMessage", 1)));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ExecutableValidator(jakarta.validation.executable.ExecutableValidator) Size(jakarta.validation.constraints.Size) ConstraintViolation(jakarta.validation.ConstraintViolation) SizeDef(org.hibernate.validator.cfg.defs.SizeDef) Test(org.testng.annotations.Test)

Example 97 with ConstraintViolation

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

the class ConstructorConstraintMappingTest method testProgrammaticAndAnnotationParameterConstraintsAddUp.

@Test
public void testProgrammaticAndAnnotationParameterConstraintsAddUp() throws Exception {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).constructor(CharSequence.class).parameter(0).constraint(new SizeDef().min(2).max(10));
    config.addMapping(mapping);
    Constructor<GreetingService> constructor = GreetingService.class.getConstructor(CharSequence.class);
    Object[] parameterValues = new Object[] { "" };
    ExecutableValidator executableValidator = getConfiguredExecutableValidator();
    Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorParameters(constructor, parameterValues);
    assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 1 and 10").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("message", 0)), violationOf(Size.class).withMessage("size must be between 2 and 10").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("message", 0)));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ExecutableValidator(jakarta.validation.executable.ExecutableValidator) Size(jakarta.validation.constraints.Size) ConstraintViolation(jakarta.validation.ConstraintViolation) SizeDef(org.hibernate.validator.cfg.defs.SizeDef) Test(org.testng.annotations.Test)

Example 98 with ConstraintViolation

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

the class ConstructorConstraintMappingTest method shouldDetermineConstraintTargetForReturnValueConstraint.

@Test
public void shouldDetermineConstraintTargetForReturnValueConstraint() throws Exception {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).constructor(String.class, String.class).returnValue().constraint(new GenericConstraintDef<>(GenericAndCrossParameterConstraint.class));
    config.addMapping(mapping);
    Constructor<GreetingService> constructor = GreetingService.class.getConstructor(String.class, String.class);
    ExecutableValidator executableValidator = getConfiguredExecutableValidator();
    Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorReturnValue(constructor, new GreetingService("", ""));
    assertThat(violations).containsOnlyViolations(violationOf(GenericAndCrossParameterConstraint.class).withMessage("default message").withPropertyPath(pathWith().constructor(GreetingService.class).returnValue()));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ExecutableValidator(jakarta.validation.executable.ExecutableValidator) ConstraintViolation(jakarta.validation.ConstraintViolation) Test(org.testng.annotations.Test)

Example 99 with ConstraintViolation

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

the class ConstructorConstraintMappingTest method testParameterConstraint.

@Test
public void testParameterConstraint() throws Exception {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).constructor(User.class).parameter(0).constraint(new NotNullDef());
    config.addMapping(mapping);
    Constructor<GreetingService> constructor = GreetingService.class.getConstructor(User.class);
    Object[] parameterValues = new Object[] { null };
    ExecutableValidator executableValidator = getConfiguredExecutableValidator();
    Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorParameters(constructor, parameterValues);
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null").withPropertyPath(pathWith().constructor(GreetingService.class).parameter("user", 0)));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ExecutableValidator(jakarta.validation.executable.ExecutableValidator) ConstraintViolation(jakarta.validation.ConstraintViolation) NotNullDef(org.hibernate.validator.cfg.defs.NotNullDef) NotNull(jakarta.validation.constraints.NotNull) Test(org.testng.annotations.Test)

Example 100 with ConstraintViolation

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

the class ConstructorConstraintMappingTest method crossParameterConstraint.

@Test
public void crossParameterConstraint() throws Exception {
    ConstraintMapping mapping = config.createConstraintMapping();
    mapping.type(GreetingService.class).constructor(String.class, String.class).crossParameter().constraint(new GenericConstraintDef<>(GenericAndCrossParameterConstraint.class));
    config.addMapping(mapping);
    Constructor<GreetingService> constructor = GreetingService.class.getConstructor(String.class, String.class);
    Object[] parameterValues = new Object[] { "", "" };
    ExecutableValidator executableValidator = getConfiguredExecutableValidator();
    Set<ConstraintViolation<GreetingService>> violations = executableValidator.validateConstructorParameters(constructor, parameterValues);
    assertThat(violations).containsOnlyViolations(violationOf(GenericAndCrossParameterConstraint.class).withMessage("default message").withPropertyPath(pathWith().constructor(GreetingService.class).crossParameter()));
}
Also used : ConstraintMapping(org.hibernate.validator.cfg.ConstraintMapping) ExecutableValidator(jakarta.validation.executable.ExecutableValidator) ConstraintViolation(jakarta.validation.ConstraintViolation) Test(org.testng.annotations.Test)

Aggregations

ConstraintViolation (jakarta.validation.ConstraintViolation)397 Test (org.testng.annotations.Test)309 Validator (jakarta.validation.Validator)258 TestForIssue (org.hibernate.validator.testutil.TestForIssue)134 HibernateValidator (org.hibernate.validator.HibernateValidator)123 ValidatorUtil.getValidator (org.hibernate.validator.testutils.ValidatorUtil.getValidator)70 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)60 ConstraintValidator (jakarta.validation.ConstraintValidator)57 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)54 Method (java.lang.reflect.Method)46 ValidatorFactory (jakarta.validation.ValidatorFactory)41 Test (org.junit.Test)39 ConstraintViolationException (jakarta.validation.ConstraintViolationException)35 Path (jakarta.validation.Path)35 NotNull (jakarta.validation.constraints.NotNull)32 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)27 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)19 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)18 Size (jakarta.validation.constraints.Size)17 PropertyNode (org.hibernate.validator.path.PropertyNode)14