Close

JavaBean Validation - @Valid Examples

JavaBean Validation JAVA EE 

import javax.validation.*;
import javax.validation.constraints.Digits;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import java.text.ParseException;
import java.util.Comparator;
import java.util.Date;
import java.util.Set;

public class ValidAnnotationExample {

private static class DriverLicense {
@NotNull
@Valid
private Driver driver;
@Digits(integer = 7, fraction = 0)
private int number;

public DriverLicense(Driver driver, int number) {
this.driver = driver;
this.number = number;
}
}

private static class Driver {
@NotNull
private String fullName;
@Min(100)
private int height;
@Past
@NotNull
private Date dateOfBirth;

public Driver(String fullName, int height, Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
this.fullName = fullName;
this.height = height;
}
}

public static void main(String[] args) throws ParseException {
Driver driver = new Driver("Joseph Waters", 60,
new Date(System.currentTimeMillis() + 100000));
DriverLicense dl = new DriverLicense(driver, 3454343);

Validator validator = createValidator();
Set<ConstraintViolation<DriverLicense>> violations = validator.validate(dl);
if (violations.size() == 0) {
System.out.println("No violations.");
} else {
System.out.printf("%s violations:%n", violations.size());
violations.stream().sorted(Comparator.comparing(o -> o.getPropertyPath().toString()))
.forEach(ValidAnnotationExample::printError);
}
}

private static void printError(ConstraintViolation<?> violation) {
System.out.println(violation.getPropertyPath()
+ " " + violation.getMessage());
}

public static Validator createValidator() {
Configuration<?> config = Validation.byDefaultProvider().configure();
ValidatorFactory factory = config.buildValidatorFactory();
Validator validator = factory.getValidator();
factory.close();
return validator;
}
}

Output

2 violations:
driver.dateOfBirth must be a past date
driver.height must be greater than or equal to 100
Original Post




Using @Valid on a collection;

import javax.validation.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

public class ValidAnnotationExample {

private static class Department {
@NotNull
@Valid
private List<Employee> employees;
@NotNull
private String name;

public Department(String name, List<Employee> employees) {
this.employees = employees;
this.name = name;
}
}

private static class Employee {
@NotNull
private String name;
@Pattern(regexp = "\\d{3}-\\d{3}-\\d{4}")
private String phone;

public Employee(String name, String phone) {
this.name = name;
this.phone = phone;
}
}

public static void main(String[] args) throws ParseException {
Employee e1 = new Employee(null, "333333");
Employee e2 = new Employee("Jake", "abc");

Department dept = new Department("Admin", Arrays.asList(e1, e2));

Validator validator = createValidator();
Set<ConstraintViolation<Department>> violations = validator.validate(dept);
if (violations.size() == 0) {
System.out.println("No violations.");
} else {
System.out.printf("%s violations:%n", violations.size());
violations.stream().sorted(Comparator.comparing(o -> o.getPropertyPath().toString()))
.forEach(ValidAnnotationExample::printError);
}
}

private static void printError(ConstraintViolation<?> violation) {
System.out.println(violation.getPropertyPath()
+ " " + violation.getMessage());
}

public static Validator createValidator() {
Configuration<?> config = Validation.byDefaultProvider().configure();
ValidatorFactory factory = config.buildValidatorFactory();
Validator validator = factory.getValidator();
factory.close();
return validator;
}
}

Output

3 violations:
employees[0].name must not be null
employees[0].phone must match "\d{3}-\d{3}-\d{4}"
employees[1].phone must match "\d{3}-\d{3}-\d{4}"
Original Post




import javax.validation.Valid;
import javax.validation.constraints.NotNull;

public interface Employee {

String getName();

@Valid
@NotNull
PhoneNumber getPhoneNumber();

@NotNull
String getDept();
}
Original Post




See Also