0

In Hibernate Validator, we can validate an object either by @Valid, or by directly calling up a Validator instance

Validator validator = Validation.buildDefaultValidatorFactory().getValidator()

and do a validator.validate(myObject) to validate it. If I already separate the constraints on the object into different groups, I can do a validator.validate(myObject, myGroup.class) to validate only the constraints in my group.

Is there any way to use a Validator instance to validate only one constraint, without using groups?

Background

I am using Hibernate Validator to validate various objects at a Spring Controller. It looks like Collection<Object> parameters in controller methods do not do cascaded validation, because Spring AbstractMessageConverterMethodArgumentResolver does validation by using a Validator instance. Since the annotation Collection<@Valid Object> is not working, I am trying to see if there is any way I can validate each object in the collection.

Thank you!

Pig
  • 109
  • 1
  • 2
  • Correct as it should. You should annotate the `Collection` field with `@Valid` to cascade validation (and that has nothing to do with Spring but with how javax. validation works)> – M. Deinum Dec 12 '16 at 19:00
  • @M.Deinum, this works for a usual POJO/usual method, but I have been unable to make it work for a method AT SPRING CONTROLLER. Are you certain that the same thing still works per se? – Pig Dec 12 '16 at 19:01
  • To be precise, I first tried `@Valid Collection` - no cascade validation occured. I next tried `Collection<@Valid Object>` or `@Valid Collection<@Valid Object>` - java complains that annotation @Valid cannot appear there. – Pig Dec 12 '16 at 19:03
  • Then you have the wrong `@Valid` or wrong `javax.validation` version and yes I'm sure it works as we use the same in multiple applications. Because `@Valid Collection` should work as stated we use it in different sections. The same applies to embedded objects (like `Person` and `Address` you would have to annotate the `address` field with `@Valid`). Make sure you have annotated the method argument with `@Valid` as well and that that is actually using a JSR-303 validator and not a default Spring Validator (it should if you haven't modified anything in a `@InitBinder` method). – M. Deinum Dec 12 '16 at 19:09
  • See the answer to this question http://stackoverflow.com/questions/4106772/jsr-303-how-to-validate-a-collection-of-annotated-objects which also has a reference to the specs stating how it should work. – M. Deinum Dec 12 '16 at 19:14
  • @M.Deinum Thanks! I'll look into the javax.validation version then. Just to reconfirm - you are suggesting that the expected behaviour should still occur even at Spring Controller. ( Cascade Validation of method param works for me; non-cascade validation of method param at Spring Controller works for me; just not cascade validation of method param at Spring controller). – Pig Dec 12 '16 at 19:15
  • @M.Deinum And thanks for the link - I actually read that in the past and it was helpful. But like I said, my question is very specific about behaviour at Spring controller method - not for a general bean or a general method, which has been working for me so far. – Pig Dec 12 '16 at 19:16
  • Yes it works for a spring controller as that just delegates to the JSR-303 validator... For which that is default behavior... As mentioned it has nothing to do wit the fact you are using a controller or regular method. – M. Deinum Dec 12 '16 at 19:17
  • @M.Deinum For further confirmation, are you also suggesting that `@Valid Collection<@MyAnnotation Object>` should also work as expected? Again it doesn't work for me specifically for Spring Controller methods. – Pig Dec 12 '16 at 19:17
  • Again for the third time yes... – M. Deinum Dec 12 '16 at 19:18

0 Answers0