Home > Net >  How to create a custom annotated validation that has a param in spring boot?
How to create a custom annotated validation that has a param in spring boot?

Time:10-26

Is it possible to pass a value when using a custom annotated validation? The logic is different depending on the param value. In the example below, the chill room may require the key-value pairs to include "snack" : "" with max length 10, min length 1 similar to the @Size(min = 1, max = 10). I'm implementing the ConstraintValidator and set up the interface.

i.e.

@ConcertValidation(dressingRoom = "chill")
private List<Map<String, String>> json;

CodePudding user response:

Inside the initialize method of the ConstraintValidator implementation, you can capture the value from annotation. For example:

class ConcertValidator implements ConstraintValidator<ConcertValidation, List<Map<String, String>>> {

    String dressingRoom;

    @Override
    public void initialize(ConcertValidation constraintAnnotation) {
        dressingRoom = constraintAnnotation.dressingRoom();
    }

    @Override
    public boolean isValid(List<Map<String, String>> value, ConstraintValidatorContext context) {
        boolean isValid = false;
        //TODO implement your logic depending on dressingRoom value
        return isValid;
    }
}

CodePudding user response:

The Validator class implemented from ConcertValidation interface, Validator class override the isValid method and implements the expected validation logic ("snack" : "", with max length 10, min length 1).

For example:

public @interface ConcertValidation {

    String message() default "Json value must contain snack value and the length between 1 to 10, current value is : ";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    private int minSnack;
    private int maxSnack;

    private List<Map<String, String>> dressingRoom;

    class Validator implements ConstraintValidator<ConcertValidation, String> {
        private int minSnack = 1;
        private int maxSnack = 10;
        private List<Map<String, String>> dressingRoom;

        @Override
        public void initialize(ConcertValidation requiredIfChecked) {
            this.dressingRoom = requiredIfChecked.dressingRoom;
            this.minSnack = requiredIfChecked.minSnack;
            this.maxSnack = requiredIfChecked.maxSnack;
        }

        @Override
        public boolean isValid(List<Map<String, String>> value, ConstraintValidatorContext context) {

            String snackValue = countries.stream().filter(m -> m.containsValue(country)).map(m -> m.get("snack")).findFirst().orElse("");
            Boolean valid = snackValue.length()<=10 && snackValue.length()>1;

            if (!Boolean.TRUE.equals(valid)) {
                context.disableDefaultConstraintViolation();
                context.buildConstraintViolationWithTemplate(message.concat(snackValue))
                        .addPropertyNode(this.dressingRoom).addConstraintViolation();
            }
            return valid;
        }
    }
}

mor info - https://bigstack.dev/c/28/Spring-Boot--Create-Custom-Annotation-to-validation-for-Request-Parameter!

  • Related