JSR 303 Overriding User Constraints

I want to put a lot of standard constraints (for example, a non-null alphanumeric string between 3 and 240 characters long) in the fields (in this case String), and you want to know if there is a way to override some of these constraints in the model code. Also, will it be an override or just a double check for an overridden annotation?

it should be something like this

@AlphanumericString @Size(min=100, max=150) //override standart values from AlphanumericString annotation 

thanks for answers

ok, answer. there is @OverridesParameter that helps reassign nested annotation parameter

 @Numerical @Size //arbitrary parameter values @ConstraintValidator(FrenchZipcodeValidator.class) @Documented @Target({ANNOTATION_TYPE, METHOD, FIELD}) @Retention(RUNTIME) public @interface FrenchZipCode { String message() default "Wrong zipcode"; String[] groups() default {}; @OverridesParameters( { @OverridesParameter(constraint=Size.class, parameter="min") @OverridesParameter(constraint=Size.class, parameter="max") } ) int size() default 5; @OverridesParameter(constraint=Size.class, parameter="message") String sizeMessage() default "{error.zipcode.size}"; @OverridesParameter(constraint=Numerical.class, parameter="message") String numericalMessage() default "{error.zipcode.numerical}"; } 

a source

+7
source share
1 answer

This is a good question. JSR 303 Bean The validation specification describes the verification procedure in section 3.5.

For this group, the verification procedure that applies to this Bean instance is applied for verification; it is expected to fulfill the following validation restriction in an arbitrary order:

  • for all reachable fields, perform all field-level checks (including those expressed in superclasses) corresponding to the target group, if this check constraint has not already been processed during this check procedure for a given navigation path (see section 3.5.1) as part of a previous group match.

...

The object verification procedure is described as such. For each declaration of restriction:

  • define, for the declaration of constraint, the appropriate ConstraintValidator to use (see section 3.5.3)
  • perform the isValid operation (from the implementation of the compliance check) on the relevant data (see section 2.4).
  • if isValid returns true, go to the next restriction,
  • If isValid returns false, the Bean Validation provider populates the ConstraintViolation object according to the rules defined in section 2.4 and adds these objects to the list of constraint violations.

In your case, you are dealing with checking a simple String field, where the target group is Default . You have two validation restrictions (@AlphanumericString and @Size), which, according to the documentation, will be checked / processed separately in a specific order.

So, to answer your question. No, when using @Size extray no override will be applied to your @AlphanumericString . To achieve what I think you are trying to do, you can create a set of constraints where you redefine attributes from compositions of such annotations:

 @Pattern(regexp="[a-zA-Z]*") @Size @Constraint(validatedBy = AlphanumericStringValidator.class) @Documented @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) @Retention(RUNTIME) public @interface AlphanumericString { // ... @OverridesAttribute(constraint=Size.class, name="min") int min() default 3 @OverridesAttribute(constraint=Size.class, name="max") int max() default 230; // ... } 

and use it like this:

 @AlphanumericString(min = 100, max = 150) 
+5
source

All Articles