0

If I am using the javax.validation.Validator to validate an object annotated with constraints, will it always evaluate the field level constraints before the object?

For example if I have:

@DummyClassValidation
public static class DummyClassToValidate {

    private Integer myNum;

    @Min(value = 50)
    @Max(value = 100)
    public Integer getMyNum() {
        return myNum;
    }

    public void setMyNum(Integer myNum) {
        this.myNum = myNum;
    }
}

And I validate it, is it guaranteed that the @DummyClassValidation will be evaluated only after @Min and @Max? I know I can do so with Groupings but I rather not if I don't have to (i.e. Field validations are implicitly grouped to validate before object validations).

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Aaron Silverman
  • 22,070
  • 21
  • 83
  • 103

1 Answers1

2

Per the specification, section 3.5:

For a given group to validate, the validation routine applied on a given bean instance is expected to execute the following constraint validations in no particular order:

  • for all reachable fields, execute all field level validations (including the ones expressed on superclasses) matching the targeted group unless the given validation constraint has already been processed during this validation routine for a given navigation path (see Section 3.5.1) as part of a previous group match.

  • for all reachable getters, execute all getter level validations (including the ones expressed on interfaces and superclasses) matching the targeted group unless the given validation constraint has already been processed during this validation routine for a given navigation path (see Section 3.5.1) as part of a previous group match.

  • execute all class level validations (including the ones expressed on interfaces and superclasses) matching the targeted group unless the given validation constraint has already been processed during this validation routine for a given navigation path (see Section 3.5.1) as part of a previous group match.

  • for all reachable and cascadable associations, execute all cascading validations (see Section 3.5.1) including the ones expressed on interfaces and superclasses (see Section 3.4.5)

In short, you can't rely on that unless you want to find an implementation that guarantees it and stick with that.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • Maybe interesting to note, that some implementations "disregard" this specification: [JSF 2 doesn't execute class-level validations](http://stackoverflow.com/q/9212777/1803692) – Vogel612 Jan 05 '15 at 13:47