Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate validation - only validate if object is not null

I want to validate the myClass variable. The trouble is that myClass can be either null or a reference to a myClass object. If it is a reference I want it to validate the object, if it is null obviously no validation should occur.

The following works but myClass cannot be null.

public class ParentClass {
    @Valid
    @NotNull
    private MyClass myClass;
}


public MyClass {

    @Pattern(regexp="\\d.{10}")
    private String myField;

}

If I remove the @NotNull annotation from ParentClass it no longer performs validation even if I keep the @Valid annotation.

public class ParentClass {
    @Valid
    private MyClass myClass;
}


public MyClass {

    @Pattern(regexp="\\d.{10}") //this validation never takes place
    private String myField;

}

Is is possible to validate an optionally null field using jpa/hibernate annotations?

like image 839
jax Avatar asked Jul 10 '13 12:07

jax


People also ask

How do you validate NOT null?

@NotEmpty validates that the property is not null or empty; can be applied to String, Collection, Map or Array values. @NotBlank can be applied only to text values and validates that the property is not null or whitespace.

What is difference between @NotEmpty and @NotBlank?

@NotEmpty: a constrained CharSequence, Collection, Map, or Array is valid as long as it's not null, and its size/length is greater than zero. @NotBlank: a constrained String is valid as long as it's not null, and the trimmed length is greater than zero.

Is Hibernate Validator deprecated?

Generally, as something is marked as deprecated in a javadoc, the javadoc explains also what is the alternative to. Note that since Hibernate 5, Hibernate deprecated several of its specific validation constraint classes in favor to standard JSR 380-bean validation classes.

What is the difference between javax validation and Hibernate Validator?

The Javax bean validation API provides the following most frequently used annotations. The Hibernate validator provides the following commonly used annotations for validation. In case of product or project development we must use both the annotations for bean validation.


1 Answers

When you say that this validation never takes place are you referring to situations where myField is null or populated (or either)?

As I am also using the Validator on one of my projects, I ran a little test and when myField is null, it is not validated. When it is not null, it is. I am using the following versions:

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.1.0.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.0.1.Final</version>
</dependency>
<dependency>
    <groupId>javax.el</groupId>
    <artifactId>javax.el-api</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>javax.el</artifactId>
    <version>2.2.4</version>
</dependency>

Here is the test code:

public class MyClass {

    public MyClass() {
    }

    public MyClass(String myField) {
        this.myField = myField;
    }

    @Pattern(regexp="\\d+\\D+")
    private String myField; 
}

public class ParentClass {

    @Valid
    private MyClass myClass;

    public ParentClass() {
    }

    public ParentClass(MyClass myClass) {
        this.myClass = myClass;
    }
}

public class ValidatorTest {

    javax.validation.Validator v;

    @Before
    public void setup() {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        v = factory.getValidator();     
    }

    @Test
    public void testValidations() {
        Assert.assertEquals(0, v.validate(new ParentClass()).size()); // Null reference
        Assert.assertEquals(0, v.validate(new ParentClass(new MyClass())).size()); // Valid reference, null field
        Assert.assertEquals(1, v.validate(new ParentClass(new MyClass("I fail"))).size());
        Assert.assertEquals(0, v.validate(new ParentClass(new MyClass("1 pass"))).size());
    }
}

I hope this helps. It seems (to me) the behaviour demonstrated is correct.

like image 147
Brice Avatar answered Oct 20 '22 01:10

Brice