Search in sources :

Example 1 with ConstraintViolationException

use of jakarta.validation.ConstraintViolationException 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 ConstraintViolationException

use of jakarta.validation.ConstraintViolationException in project hibernate-orm by hibernate.

the class HibernateTraversableResolverTest method testEmbedded.

@Test
public void testEmbedded() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Screen screen = new Screen();
    PowerSupply ps = new PowerSupply();
    screen.setPowerSupply(ps);
    Button button = new Button();
    button.setName(null);
    button.setSize(3);
    screen.setStopButton(button);
    try {
        s.persist(screen);
        s.flush();
        fail("@NotNull on embedded property is not evaluated");
    } catch (ConstraintViolationException e) {
        assertEquals(1, e.getConstraintViolations().size());
        ConstraintViolation<?> cv = e.getConstraintViolations().iterator().next();
        assertEquals(Screen.class, cv.getRootBeanClass());
        // toString works since hibernate validator's Path implementation works accordingly. Should do a Path comparison though
        assertEquals("stopButton.name", cv.getPropertyPath().toString());
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 3 with ConstraintViolationException

use of jakarta.validation.ConstraintViolationException in project hibernate-orm by hibernate.

the class HibernateTraversableResolverTest method testNonLazyAssocFieldWithConstraintsFailureExpected.

@Test
public void testNonLazyAssocFieldWithConstraintsFailureExpected() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Screen screen = new Screen();
    screen.setPowerSupply(null);
    try {
        s.persist(screen);
        s.flush();
        fail("@NotNull on a non lazy association is not evaluated");
    } catch (ConstraintViolationException e) {
        assertEquals(1, e.getConstraintViolations().size());
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with ConstraintViolationException

use of jakarta.validation.ConstraintViolationException in project hibernate-orm by hibernate.

the class HibernateTraversableResolverTest method testEmbeddedCollection.

@Test
public void testEmbeddedCollection() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Screen screen = new Screen();
    PowerSupply ps = new PowerSupply();
    screen.setPowerSupply(ps);
    DisplayConnector conn = new DisplayConnector();
    conn.setNumber(0);
    screen.getConnectors().add(conn);
    try {
        s.persist(screen);
        s.flush();
        fail("Collection of embedded objects should be validated");
    } catch (ConstraintViolationException e) {
        assertEquals(1, e.getConstraintViolations().size());
        final ConstraintViolation constraintViolation = e.getConstraintViolations().iterator().next();
        assertEquals(Screen.class, constraintViolation.getRootBeanClass());
        // toString works since hibernate validator's Path implementation works accordingly. Should do a Path comparison though
        assertEquals("connectors[].number", constraintViolation.getPropertyPath().toString());
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 5 with ConstraintViolationException

use of jakarta.validation.ConstraintViolationException in project hibernate-orm by hibernate.

the class HibernateTraversableResolverTest method testToOneAssocNotValidated.

@Test
public void testToOneAssocNotValidated() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Screen screen = new Screen();
    PowerSupply ps = new PowerSupply();
    ps.setPosition("1");
    ps.setPower(new BigDecimal(350));
    screen.setPowerSupply(ps);
    try {
        s.persist(screen);
        s.flush();
        fail("Associated objects should not be validated");
    } catch (ConstraintViolationException e) {
        assertEquals(1, e.getConstraintViolations().size());
        final ConstraintViolation constraintViolation = e.getConstraintViolations().iterator().next();
        assertEquals(PowerSupply.class, constraintViolation.getRootBeanClass());
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException) BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

ConstraintViolationException (jakarta.validation.ConstraintViolationException)114 Test (org.testng.annotations.Test)55 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)35 ConstraintViolation (jakarta.validation.ConstraintViolation)32 Validator (jakarta.validation.Validator)29 TestForIssue (org.hibernate.validator.testutil.TestForIssue)26 HibernateValidator (org.hibernate.validator.HibernateValidator)19 Size (jakarta.validation.constraints.Size)16 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)15 Test (org.junit.Test)14 Session (org.hibernate.Session)10 Transaction (org.hibernate.Transaction)10 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)10 CustomerRepositoryImpl (org.hibernate.validator.test.internal.engine.methodvalidation.service.CustomerRepositoryImpl)10 Customer (org.hibernate.validator.test.internal.engine.methodvalidation.model.Customer)9 FacesMessage (javax.faces.application.FacesMessage)8 NotNull (jakarta.validation.constraints.NotNull)7 BigDecimal (java.math.BigDecimal)7 Set (java.util.Set)7 EntityManager (jakarta.persistence.EntityManager)6