0

I hope you are very well.

Right now I am developing a custom annotation to validates the arguments in a method, of this way:

Interface:

@Target({METHOD})
@Retention(RUNTIME)
@Constraint(validatedBy = ValidarImpl.class)
public @interface Validar {
    public abstract String message() default "ERROR VALIDATION";
    public abstract Class<?>[] groups() default {};
    public abstract Class<? extends Payload>[] payload() default {};
}

Class:

public class ValidarImpl implements ConstraintValidator<Validar, Object> {

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {

        return false;
    }

}

Implementation of the annotation:

@Validar 
public void method1 (@Positive int value, @Valid Car car){}

I am trying to obtain the name of the parameters and their value from ConstraintValidatorContext class with the intention validate the arguments of method1 method, however I haven't been able to do it yet

I would like to do something like this: How can I validate two or more fields in combination? with methods.

Thanks for your help.

J. Abel
  • 890
  • 3
  • 17
  • 38

1 Answers1

1

You can refer to https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/?v=6.1#section-cross-parameter-constraints to get more details.

You can change your code as below:

@SupportedValidationTarget(ValidationTarget.PARAMETERS)
public class ValidarImpl implements
ConstraintValidator < Validar, Object[] > {

    @Override
    public boolean isValid(Object[] value, ConstraintValidatorContext context) {
        if (value.length != 2) {
            throw new IllegalArgumentException("Illegal method signature");
        }

        if (value[0] == null || value[1] == null) {
            return true;
        }

    }
}
Ganesh chaitanya
  • 638
  • 6
  • 18