📜 ⬆️ ⬇️

Introduction to the Bean Validation API

Not so long ago in Java there was no standard describing the method of data validation. Each wound out as best he could, his crafts were written (and written) and some features of common services like Spring or Hibernate were also used. The biggest problem was that validation could be implemented separately from the subject model and be redundanto scattered across the front and backend. Now, using the JSR 303: Bean Validation standard (in practice, it is Hibernate's standardized validator), it becomes possible to follow the principle of “Don't Repeat Yourself”: declare data restrictions directly in the subject model and validate data anywhere, even on the server, desktop application.



Usage example


package test.validator;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

public class User {

@NotNull(message= " " )
String firstname;

@NotNull(message= " " )
@Size(min = 3, message= " " )
String lastname;

@NotNull(message= " " )
@Pattern(regexp = "^(?:[a-zA-Z0-9_'^&/+-])+(?:\\.(?:[a-zA-Z0-9_'^&/+-])+)" +
"*@(?:(?:\\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\\.)" +
"{3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\]?)|(?:[a-zA-Z0-9-]+\\.)" +
"+(?:[a-zA-Z]){2,}\\.?)$" ,
message = " " )
String email;

@Override
public String toString() {
return String .format( "firstname: [%s], lastname: [%s], email: [%s]" ,
firstname, lastname, email);
}

public static void validate(Object object , Validator validator) {
Set<ConstraintViolation<Object>> constraintViolations = validator
.validate( object );

System. out .println( object );
System. out .println( String .format( "- : %d" ,
constraintViolations.size()));

for (ConstraintViolation<Object> cv : constraintViolations)
System. out .println( String .format(
", ! property: [%s], value: [%s], message: [%s]" ,
cv.getPropertyPath(), cv.getInvalidValue(), cv.getMessage()));
}

public static void main( String [] args) {
ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
Validator validator = vf.getValidator();

User user = new User();
validate(user, validator);

user.firstname = "" ;
validate(user, validator);

user.lastname = "" ;
validate(user, validator);

user.lastname = "" ;
validate(user, validator);

user.email = " @example.com" ;
validate(user, validator);

user.email = "vasya.poupkine@example.com" ;
validate(user, validator);

}

}

* This source code was highlighted with Source Code Highlighter .

')
Thus, data validation is carried out in three basic steps:

At the moment there are only a small number of annotations in a standard that has not yet been released. But there is not a small probability that such annotations as Email , @CreditCard, Password, etc. will be available at least through the standards implementation providers.

This concludes our introduction to JSR 303. And with such details as extending / writing your own limitations, internalizing messages, factory configurations and other things, you can also get acquainted with the standard itself.

PS To run the code, the example used Hibernate 4.0.0 Beta2 implementation , pleasant experiments!

Source: https://habr.com/ru/post/68318/


All Articles