JSR-303 Bean Validation annotate multiple fields

前端 未结 2 1469
无人及你
无人及你 2021-01-07 15:15

I\'ve just started testing JSR-303 Bean validation and was wondering if something was possible. We have a default regular expression in our app for all String fields. If I

相关标签:
2条回答
  • 2021-01-07 16:05

    I didnt use but java supporting scripting in server side using grovvy ,javascript.. .You can check @ScriptAssert(lang = "javascript", script =_this.startDate.before(_this.endDate) scripting annotation which is hibernate annotation.

    0 讨论(0)
  • 2021-01-07 16:06

    To validate more than one field at once, use an annotation on type-Level and write a custom Validator that checks all String fields using your REGEXP.

    Edit: Provide example. This is quite ugly, because it uses Reflection and violates security, but maybe it gives you a general idea. If you dont use "object" but a concrete class or interface, you could possibly have success with regular getters.

    The Class under Test (and the Runner)

       import javax.validation.Validation;
    import javax.validation.Validator;
    
    import validation.AllStringsRegex;
    
    @AllStringsRegex(value="l")
    public class UnderValidation {
        String a;
        String b;
    
       public static void main(String... args) {
           UnderValidation object = new UnderValidation();
           object.a = "hello";
           object.b = "world";
    
           Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
           System.out.println(validator.validate(object));
       }
    }
    

    My Annotation:

    @Target( { TYPE, ANNOTATION_TYPE })
    @Retention(RUNTIME)
    @Constraint(validatedBy = AllStringsRegexValidator.class)
    @Documented
    public @interface AllStringsRegex {
        String message() default "String not regex";
        String value() default "";
        Class<?>[] groups() default {};
        Class<? extends Payload>[] payload() default {};
    }
    

    My Validator

    public class AllStringsRegexValidator implements ConstraintValidator<AllStringsRegex, Object> {
        private Pattern pattern = null;
    
        @Override
        public void initialize(AllStringsRegex annotation) {
            pattern = Pattern.compile(annotation.value());
        }
    
        @Override
        public boolean isValid(Object object, ConstraintValidatorContext ctx) {
            for (Field f : object.getClass().getDeclaredFields()) {
                if (f.getType().equals(String.class)) {
                    try {
                        f.setAccessible(true);
                        String value = (String)f.get(object);
                        if (!pattern.matcher(value).find()) {
                            return false;
                        }
                    }
                    catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            return true;
        }
    }
    
    0 讨论(0)
提交回复
热议问题