Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set check order for JSR303 bean validation

I use the JSR303 Bean Validation to check the form input.

@NotBlank
@Size(min = 4, max = 30)
private String name;

@NotBlank
@Size(max = 100)
@Email
private String mail;

when then name = '' and email = '',the @NotBlank, @Size at name, @NotBlank, @Size, @Email at mail will be checked.

I want set the check order, when the previous order is invalid, the next is not checked, for example.

@NotBlank(order = 1)
@Size(min = 4, max = 30, order = 2)
private String name;

(above is not support by JSR303)

is there a way to implement it for using JSR303? (i think the custom annotation will done, but i don't like to add a custom annotation for every property)

And I also consider the validation group and group sequence, but i think there are some difference for my need.

@NotBlank(groups = First.class)
@Size(min = 4, max = 30, groups = Second.class)
private String name;

@NotBlank(groups = First.class)
@Size(max = 100, groups = Second.class)
@Email(groups = Third.class)
private String mail;

When the name is blank and the mail is "abc". Only the @NotBlank at name, and @NotBlank at mail will be checked by JSR303, Because the @NotBlank at name is invalid, so the Second and Third group is not be checked.

But my need is @NotBlank at name and @NotBlank, @Size, @Email at mail should be checked. Because the @NotBlank at name is invalid, so the @Size at name is not be checked, and the @NotBlank,@Size at mail is valid, so the @Email at mail is be checked.

Perhaps, the below pseudocode will be clear to describe my requirement.

// retrieves all fields and check one by one.
for (Field field : fields) {
  List annotations = getJsr303Annotations(field);
  // retrieves all annotations on field and check one by one.
  for (Annotation annotation : annotations) {
    CheckResult result = check(annotation, field);
    if (result.isValid()) {
       continue;
    } else {
       errors.add(result);
       break;
    }
  }
}

Is there a good way to implement my requirement by JSR303?

like image 680
eckelcn Avatar asked Sep 30 '10 03:09

eckelcn


People also ask

What is jsr303 validation?

JSR-303 bean validation is an specification whose objective is to standardize the validation of Java beans through annotations. The objective of the JSR-303 standard is to use annotations directly in a Java bean class.

What does @NotNull annotation mean in bean property?

@NotNull The @NotNull annotation is, actually, an explicit contract declaring that: A method should not return null. Variables (fields, local variables, and parameters) cannot hold a null value.

What is the use of @validated annotation?

@Validated annotation is a class-level annotation that we can use to tell Spring to validate parameters that are passed into a method of the annotated class. @Valid annotation on method parameters and fields to tell Spring that we want a method parameter or field to be validated.


1 Answers

Yes Bean Validation supports this feature. It is called validation groups and group sequences. Groups are simple marker interfaces, for example you could create the two interfaces First and Second and change your code to something like this:

@NotBlank(groups = First.class)
@Size(min = 4, max = 30, groups = Second.class)
private String name;

Then you can define a group sequence:

@GroupSequence({ First.class, Second.class})
interface All {
}

Get hold of the Validator and call validator.validate(myObject, All.class)

It's all in the documentation.

like image 118
Hardy Avatar answered Dec 19 '22 15:12

Hardy