jim jones
jim jones

Reputation: 31

How to validate @NoNull first then @AssertTrue after

I am trying to validate a simple DTO:

@Data
public class Dto {

    @NotNull
    private Integer anInt;

    @AssertTrue
    public boolean isIntCustomValid() {
        return anInt == 123 || anInt == 999;
    }

}

In my Controller, with @Valid. When anInt is null, isIntCustomValid is still validated and I get this error because anInt is null:

HV000090: Unable to access

If I remove isIntCustomValid, then @NotNull works correctly. I want to validate these separately.

I tried using @GroupSequence and @Groups, which works, but I have to create empty marker interfaces which is does not seem like an elegant solution.

Upvotes: 3

Views: 602

Answers (1)

Upendra Rai
Upendra Rai

Reputation: 1

If you got HV000090, then should be handle exceptions like this: [Link][1]

@AssertTrue
public boolean isIntCustomValid() { 
    if( (Objects.nonNull(anInt) ) {
           return anInt == 123 || anInt == 999; 
    } 
    return true;
}


 [enter image description here][1]


  [1]: https://i.sstatic.net/u8Jpr.jpg

Upvotes: 0

Related Questions