I am trying to perform validation using Spring validation. I am wondering what is the best way to perform validation, which depends mainly on the user's actions, in the future I have three different approaches, but I donβt know which one is better.
Suppose we have the following Foo class for validation and many different validation rules depending on the action performed by the user.
public class Foo { private String name; private int age; private String description; private int evaluation;
What is the best way to perform these checks (for example: at the time of creation, only the name and age are required during the validity of the evaluation, I only need the evaluation that needs to be checked, etc.)
solution 1: one validator class for validation rule
public class CreateFooValidator implements Validator {
solution 2: one validator class with several methods
public class FooValidator implements Validator { @Override public boolean supports(Class<?> clazz) { return Foo.class.equals(clazz); }
solution 3: Additional property action in class Foo, one validator
public class Foo { private String name; private int age; private String description; private int evaluation; private String actionOnFoo;
What is the best solution among 3 or another solution, if any? Thanks!
aslan source share