Search in sources :

Example 1 with ValidationExtension

use of dev.morphia.validation.ValidationExtension in project morphia by mongodb.

the class TestMorphiaValidation method testValidation.

/**
 * Test method for {@link ValidationExtension#prePersist(Object, Document, Mapper)}.
 */
public void testValidation() {
    final Data data = new Data();
    data.email = "not an email";
    getDs().getMapper().addInterceptor(new ValidationExtension());
    try {
        getDs().save(data);
        Assert.fail("Should have failed validation");
    } catch (ValidationException exception) {
        assertTrue(exception.getMessage().contains("Data.email:not a well-formed email address ('not an email')"));
    }
    data.email = "foo@bar.com";
    getDs().save(data);
}
Also used : ValidationException(jakarta.validation.ValidationException) ValidationExtension(dev.morphia.validation.ValidationExtension)

Example 2 with ValidationExtension

use of dev.morphia.validation.ValidationExtension in project morphia by MorphiaOrg.

the class TestMorphiaValidation method testValidation.

/**
 * Test method for {@link ValidationExtension#prePersist(Object, Document, Mapper)}.
 */
public void testValidation() {
    final Data data = new Data();
    data.email = "not an email";
    getDs().getMapper().addInterceptor(new ValidationExtension());
    try {
        getDs().save(data);
        Assert.fail("Should have failed validation");
    } catch (ValidationException exception) {
        assertTrue(exception.getMessage().contains("Data.email:not a well-formed email address ('not an email')"));
    }
    data.email = "foo@bar.com";
    getDs().save(data);
}
Also used : ValidationException(jakarta.validation.ValidationException) ValidationExtension(dev.morphia.validation.ValidationExtension)

Aggregations

ValidationExtension (dev.morphia.validation.ValidationExtension)2 ValidationException (jakarta.validation.ValidationException)2