0


Do u have any ideas how to validate one object in this case?
I have class A which is built of two another classes B and C:

public class A{
private B b;
private C c;
}

In that case I would like to validate all fields from object B, which looks like this:

public class B{
private String name;
private Long number;
}

This kind of object A is being passed into my Controller as request object, so that I'd like to validate it before it's sent.
The point is that I'd like to validate name and number objects when they are both passed but if any of them is null then I'd just like to validate the one which is not null.
Do you guys have any ideas how to do that? I made @Valid on A object as well as on B field but it shows no ConstraintValidatonException.


Problem solved - Validation doesn't work on Restcontroller method but after passing it on Service, everthing works well. I'm going to investigate it in a free time.

  • I believe it is only achievable by object level validation. So you would probably end up with `@Constraint(validatedBy = YourBValidator.class)` annotation on `class B`. – yegodm Mar 15 '18 at 14:41

1 Answers1

0

I would leave a comment but I'm unable to so here is my answer:

Why dont you create a validate method in class B which you can call in class A.

HoldTight
  • 205
  • 1
  • 2
  • 10
  • Just because I would need to add like 15 other properties to Class B, so that I'd need to write huge validate method. I hope, there is a way in Hibernate Validation to do that. – Maciej Papurzyński Mar 15 '18 at 13:10