0

I have 2 Domain classes like ParentClass and ChildClass. (ParentClass contains a list of Child class objects in Array list.)

I created separate Validator classes for ParentClass and ChildClass to do the custom validations.

Example validator class for the Parent class:

    public class ParentClassValidator implements 
ConstraintValidator<ValidatedByParentClassInterface, ParentClass> {

and then override the isValid() method of ConstraintValidator,

    @Override
    public boolean isValid(final ParentClass parentClass, 
                     final ConstraintValidatorContext constraintValidatorContext) {

Example validator class for the Child class:

public class ChildClassValidator implements 
    ConstraintValidator<ValidatedByChildClassInterface, ChildClass> {

and then override the isValid() method of ConstraintValidator,

    @Override
            public boolean isValid(final ChildClass childClass, 
               final ConstraintValidatorContext constraintValidatorContext) { 
   //No option to pass Parent class object here?

One of the fields in my Child class need to be validated based on the value of the field in Parent class. But the isValid() method of ConstraintValidator(https://docs.oracle.com/javaee/7/api/javax/validation/ConstraintValidator.html) has only 2 arguments so how do I pass the ParentClass object here in my ChildClassValidator?

or Should I use a for loop in my ParentClassValidator to validate this scenario?

firstpostcommenter
  • 2,328
  • 4
  • 30
  • 59

1 Answers1

0

Looks like using a for loop in my ParentClassValidator to validate this scenario is the only possible solution

So, instead of having 2 validators i.e 1 for parent class and 1 for chid class, it is better to put all the custom validations(including child class validations) in the parent class validator

firstpostcommenter
  • 2,328
  • 4
  • 30
  • 59