Validate with object Annotations within a method

Asked

Viewed 347 times

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.

1 answer

1

IMHO, what you’re doing is reinventing the wheel. In an object-oriented language, assuming you are using some persistence model, it seems to me more correct to create a Functio class and its specializations, example:

public class Funcionario implements Serializable {
    private Integer id;
    private String nome;
    // Validações referentes aos campos de funcionários
}

And Surfer class, for example:

public class Surfista extends Funcionario implements Serializable { // Relacionamento um-para-um
    private Double tamanhoDaOnda;
    // Validações referentes aos campos de surfistas
}

Depending on the project, it is worth taking a look at the Hibernate Validator (http://hibernate.org/validator/) and the JSF 2.2 validation scheme (http://incepttechnologies.blogspot.com.br/p/validation-in-jsf.html).

  • 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().

  • Hibernate Validator is precisely the implementation I’m using.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.