Marek Urbanowicz
Marek Urbanowicz

Reputation: 13634

Spring - How to validate min/max value of elements in byte[] in DTO/POJO?

I was trying to find it but I can't. I have my DTO where I am validating the data sent by user.

    import javax.validation.constraints.Max;
    import javax.validation.constraints.Min;

    @Min(value = 0)
    @Max( value = 6)
    private byte[] days;

It is throwing an error:

No validator could be found for constraint 'javax.validation.constraints.Min' validating type 'short[]'. Check configuration for 'days'.

What is wrong with that?

Upvotes: 0

Views: 3583

Answers (1)

Gondy
Gondy

Reputation: 5295

You are using wrong contraints, Min and Max validates actual value, but you have an array. For validation of array length, use

@Size(min=0, max=6)
private byte[] days;

http://docs.oracle.com/javaee/6/api/javax/validation/constraints/Size.html

If you want to check if EVERY element of array has value between 0 - 6, you probably have to create your own validator

Upvotes: 1

Related Questions