2

I have a bean object and want to validate its fields using oval validation framework. e.g

public class Demo{

    @Range(min=1)
    private long id;

    @NotNull
    private long[] values;
}

As above, I have a field whose type is a array of long. I want to ensure that the array is not null and each element in it must be greater than zero. I don't know how to validate to ensure that its elements are greater than zero. Anyone can tell me ? Thanks.

kino lucky
  • 1,365
  • 4
  • 15
  • 24

2 Answers2

4

You can use the appliesTo attribute:

@NotNull(appliesTo = {ConstraintTarget.CONTAINER, ConstraintTarget.VALUES})
private long[] values;
Georg
  • 56
  • 1
1
/* ConstraintTarget.CONTAINER(i.e List) and 
 * ConstraintTarget.VALUES( i.e List elements)
 * it means List shouldn't be null & Its elements shouldn't be null 
 */
@NotNull(appliesTo= {ConstraintTarget.CONTAINER,ConstraintTarget.VALUES})
/*
 * List size should be greater than or equal to 1
 */
@Size(min=1)
/*
 * ConstraintTarget.VALUES means List elements shouldn't be empty
 */
@NotEmpty(appliesTo = {ConstraintTarget.VALUES})
private List<String> roles;
Ziaullhaq Savanur
  • 1,848
  • 2
  • 17
  • 20