GroupSequence and ordered evaluation in JSR 303

In our application, we have the following case:

  • Limitations must be evaluated in a specific order. (cheap and expensive)
  • Limitations should not be evaluated after a violation in the field.
  • All fields must be checked.

For the first two, sequencesequence fits very well. However, for my third requirement, I could not find a way to solve.

public class AccountBean { @CheepValidation @ExpensiveValidation @VeryExpensiveValidation private String name; @CheepValidation @ExpensiveValidation @VeryExpensiveValidation private String surname } 

For instance,

Let's say that the VeryExpensiveValidationconstraint name field is violated, and the ExpensiveValidation constraint is violated for the last name field.

In this case, I should display:

Field name: only error message VeryExpensiveValidation For field last name: error message ExpensiveValidation

Please note that for the field last name we did not evaluate the VeryExpensiveValidation restriction.

Is there a way to implement it using JSR 303?

thanks

+4
source share
2 answers

You can use groups and @GroupSequence, but it's a little cumbersome.

 public class AccountBean { @CheapValidation(groups=Name1.class) @ExpensiveValidation(groups=Name2.class) @VeryExpensiveValidation(groups=Name3.class) String name; @CheapValidation(groups=Surname1.class) @ExpensiveValidation(groups=Surname2.class) @VeryExpensiveValidation(groups=Surname3.class) String surname; public interface Name1 {} public interface Name2 {} public interface Name3 {} @GroupSequence({Name1.class, Name2.class, Name3.class}) public interface Name {} public interface Surname1 {} public interface Surname2 {} public interface Surname3 {} @GroupSequence({Surname1.class, Surname2.class, Surname3.class}) public interface Surname {} } 

Then check:

 validator.validate(myAccountBean, AccountBean.Name.class, AccountBean.Surname.class) 

The key is to have two completely independent group sequences.

Unfortunately, it seems that you should explicitly list groups for all fields that you want to check. I was unable to get it to work with the "default" @GroupSequence. Can anyone improve this?

+3
source

I performed an ordered validation using GroupSequence, but generally speaking, the implementation of the validation validation of GroupSequence beans is not transparent.

Meaning, until the first group is fully verified, you cannot activate the verification of the second group.

eg.

I have 3 tested fields with custom validators. The idea is quite simple: each field should be checked using a set of validators from top to bottom independently (decreasing power).

  @StringPropertyNotNullOrEmptyConstraint(message = "Group name is required", groups = {ValidationStep1.class}) private final StringProperty groupName; @StringPropertyNotNullOrEmptyConstraint(message = "Group password is required", groups = {ValidationStep1.class}) @StringPropertyMatchConstraint(message = "The given password phrases do not match", dependentProperties = {"groupPasswordMatch"}, groups = {ValidationStep2.class}) private final StringProperty groupPassword; @StringPropertyNotNullOrEmptyConstraint(message = "Group password match is required", groups = {ValidationStep1.class}) @StringPropertyMatchConstraint(message = "The given passwords phrases do not match", dependentProperties = {"groupPassword"}, groups = {ValidationStep2.class}) private final StringProperty groupPasswordMatch; public interface ValidationStep1 { } public interface ValidationStep2 { } @GroupSequence({GroupDialogModel.class, ValidationStep1.class, ValidationStep2.class}) public interface GroupDialogModelValidationSequence { } ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); Validator validator = validatorFactory.getValidator(); Set<ConstraintViolation<GroupDialogModel>> constraintViolations = validator.validate(this, GroupDialogModelValidationSequence.class); 

A caveat to this approach is that each field must pass through ValidationStep1 first, and only after each verification of step 1 is successful will it go to step 2. For example, even if the password fields are not empty, but contain different values, check they succeed if the group name field does not contain any value. And only after I enter some value in the group name, the group ValidationStep1 will be successful, and then display the result of the ValidationStep2 check (passwords do not match).

Creating each group for each field in each sequence is IMHO bad practice, but it seems like there is no other choice.

Any other solution is of great importance.

0
source

Source: https://habr.com/ru/post/1343468/


All Articles