2
I have a Javabean with several attributes that need to be validated. It has an attribute of type Enum, and according to the value of that attribute, the object needs some specific validations, and there are validations that are independent of the value of Enum.
I found an answer in another very interesting question (link: Validation of business object avoiding/reducing use of if’s and Else’s ), where Enum itself returns a validator. And I was thinking that this would be the best option.
So basically there’s a Javabean, which has an Enum attribute. Enum has a getValidator method that returns a validator object that implements a Validator interface.
But in the end, the validations would happen in the . validate method of the Validator implementation:
public class CeletistaValidator implements FuncionarioValidator {
@Override
public void validate(Funcionario f) throws Exception {
System.out.println(f.getNome() + " -> " + getClass().getName());
}}
But from what I could understand, in this case I will have to make the validations within this method validate() and using methods. I would like to make the validations using the Annotations.
I thought of creating another Javabean for each type of employee, and in this new Javabean put the Annotations, but so would be with many classes and confused and seems not a good option.
Then by exemplifying otherwise my problem. Suppose I have a Functionary class, and there is the attribute Typofuncionario, which is an Enum with the values celetista, permanent board, surfer. If you are a surfer, there will be more specific validations for contact attributes such as email and phone, if you are a celetist there are specific name and document validations, but they all follow standard address validations. I would like to find a way to implement validations with Annotations in this case, or if the solution I thought is acceptable.
I thought about doing it using inheritance, but I’m avoiding the problems it brings. I found the solution presented in the link I posted interesting and would like to use it, but I just didn’t understand how to do at the time of validation to be able to use Annotations, since it occurs in the method . validate().
– dev1234599
Hibernate Validator is precisely the implementation I’m using.
– dev1234599