Questions around validation

128 views Asked by At

I think this might possibly be answered elsewhere, but was getting lost in the context of other posts. So, decided to restate here. If anyone knows of this being already answered elsewhere, please point me in that direction. So, my questions.

  1. I see JSR-303, JSR-349 and Hibernate Validator. What is the difference?
  2. It seems to me that Hibernate Validator is a reference implementation of the JSR-303 spec, is that true?
  3. Then what is 349?
  4. If Hibernate Validator is a reference impl of the spec, are there other impl's of the spec?
  5. Now some specifics of what I am looking for. I have done some reading about JSR-303 and I follow generally how the annotations work. My question is can I dynamically change the constraint values? Take the following for example:

    public class Foo {
        @Min(value = 18, message = "Age must be greater than or equal to 18")
        @Max(value = 150, message = "Age must be less than or equal to 150")
        int fooAge;
    }
    

So, is there a way using the spec to feed the age constraints at runtime. So, lets say for some reason in one case I need the min to be 20 and max to be 100. In another case I need 10 and 50, etc. etc.

Thanks in advance for helping to clear this up for me.

2

There are 2 answers

3
JB Nizet On BEST ANSWER
  1. 303 is the JSR for Bean Validation 1.0. 349 is the JSR for Bean Validation 1.1.

  2. The home page of the project says: "Hibernate Validator 5.x is the reference implementation Bean Validation 1.1!"

  3. The JSR defines the specification. Hibernate validator is the reference implementation of this specification.

  4. It seems not: http://beanvalidation.org/1.1/certified/

  5. Yes, you can do that by assigning the constraints to validation groups, and activate one group or another depending on the use-case. See http://beanvalidation.org/1.0/spec/#validationapi-validatorapi-groups

0
wsl On

As far as question is concerned:

If Hibernate Validator is a reference impl of the spec, are there other impl's of the spec?

There is also apache implementation of JSR-303 -> http://bval.apache.org/. Unfortunately the latest release version Apache BVal (0.5) doesn't support Bean Validation 1.1. It should be introduced in version: Apache BVal (1.1.0). Right now there is an alpa-snapshot release:

<dependency>
   <groupId>org.apache.bval</groupId>
   <artifactId>bval-jsr</artifactId>
   <version>1.1.0-alpha-SNAPSHOT</version>
</dependency>

More information on: http://cxf.apache.org/docs/validationfeature.html