0
# Validation
1
2
Bean Validation API for validating JavaBeans using constraint annotations and validation groups.
3
4
## Core Interfaces
5
6
```java { .api }
7
public interface Validator {
8
<T> Set<ConstraintViolation<T>> validate(T object, Class<?>... groups);
9
<T> Set<ConstraintViolation<T>> validateProperty(T object, String propertyName, Class<?>... groups);
10
<T> Set<ConstraintViolation<T>> validateValue(Class<T> beanType, String propertyName, Object value, Class<?>... groups);
11
BeanDescriptor getConstraintsForClass(Class<?> clazz);
12
<T> T unwrap(Class<T> type);
13
ExecutableValidator forExecutables();
14
}
15
16
public interface ValidatorFactory extends AutoCloseable {
17
Validator getValidator();
18
ValidatorContext usingContext();
19
MessageInterpolator getMessageInterpolator();
20
TraversableResolver getTraversableResolver();
21
ConstraintValidatorFactory getConstraintValidatorFactory();
22
ParameterNameProvider getParameterNameProvider();
23
ClockProvider getClockProvider();
24
<T> T unwrap(Class<T> type);
25
void close();
26
}
27
```
28
29
## Constraint Annotations
30
31
```java { .api }
32
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
33
@Retention(RetentionPolicy.RUNTIME)
34
@Constraint(validatedBy = {})
35
public @interface NotNull {
36
String message() default "{javax.validation.constraints.NotNull.message}";
37
Class<?>[] groups() default {};
38
Class<? extends Payload>[] payload() default {};
39
}
40
41
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
42
@Retention(RetentionPolicy.RUNTIME)
43
@Constraint(validatedBy = {})
44
public @interface NotEmpty {
45
String message() default "{javax.validation.constraints.NotEmpty.message}";
46
Class<?>[] groups() default {};
47
Class<? extends Payload>[] payload() default {};
48
}
49
50
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
51
@Retention(RetentionPolicy.RUNTIME)
52
@Constraint(validatedBy = {})
53
public @interface Size {
54
String message() default "{javax.validation.constraints.Size.message}";
55
Class<?>[] groups() default {};
56
Class<? extends Payload>[] payload() default {};
57
int min() default 0;
58
int max() default Integer.MAX_VALUE;
59
}
60
61
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
62
@Retention(RetentionPolicy.RUNTIME)
63
@Constraint(validatedBy = {})
64
public @interface Email {
65
String message() default "{javax.validation.constraints.Email.message}";
66
Class<?>[] groups() default {};
67
Class<? extends Payload>[] payload() default {};
68
String regexp() default ".*";
69
Pattern.Flag[] flags() default {};
70
}
71
```
72
73
## Usage Example
74
75
```java
76
public class User {
77
@NotNull
78
@Size(min = 2, max = 50)
79
private String name;
80
81
@NotNull
82
83
private String email;
84
85
@Min(18)
86
@Max(120)
87
private Integer age;
88
89
// getters and setters
90
}
91
92
@Stateless
93
public class UserService {
94
95
@Inject
96
private Validator validator;
97
98
public void createUser(@Valid User user) {
99
Set<ConstraintViolation<User>> violations = validator.validate(user);
100
if (!violations.isEmpty()) {
101
throw new ValidationException("User validation failed");
102
}
103
// Save user
104
}
105
}
106
```