0
# Base Validation Utilities
1
2
Core utilities for creating configured validators and formatting constraint violations. These classes provide the foundation for all validation operations in Dropwizard applications, integrating Hibernate Validator with Dropwizard-specific configurations and utilities.
3
4
## Capabilities
5
6
### Validator Factory
7
8
Creates configured Validator instances with Dropwizard-specific value extractors and configurations.
9
10
```java { .api }
11
public class BaseValidator {
12
/**
13
* Creates a new Validator based on newConfiguration().
14
*
15
* @return the new Validator instance
16
*/
17
public static Validator newValidator();
18
19
/**
20
* Creates a new HibernateValidatorConfiguration with the base custom unwrappers registered.
21
*
22
* @return the configured HibernateValidatorConfiguration
23
*/
24
public static HibernateValidatorConfiguration newConfiguration();
25
}
26
```
27
28
**Usage Example:**
29
30
```java
31
import io.dropwizard.validation.BaseValidator;
32
import javax.validation.Validator;
33
import javax.validation.ConstraintViolation;
34
import java.util.Set;
35
36
// Create a validator with Dropwizard's default configuration
37
Validator validator = BaseValidator.newValidator();
38
39
// Validate an object
40
MyConfig config = new MyConfig();
41
Set<ConstraintViolation<MyConfig>> violations = validator.validate(config);
42
43
// Or create a custom configuration
44
HibernateValidatorConfiguration configuration = BaseValidator.newConfiguration();
45
// Add custom configurations...
46
Validator customValidator = configuration.buildValidatorFactory().getValidator();
47
```
48
49
### Constraint Violation Formatting
50
51
Utilities for formatting constraint violations into human-readable messages, with special handling for ValidationMethod annotations.
52
53
```java { .api }
54
public class ConstraintViolations {
55
/**
56
* Computes a string representation from a ConstraintViolation.
57
*
58
* @param v the constraint violation
59
* @return the formatted message
60
* @param <T> the generic type of the constraint violation
61
*/
62
public static <T> String format(ConstraintViolation<T> v);
63
64
/**
65
* Computes a set of formatted messages from the given typed set of ConstraintViolations.
66
*
67
* @param violations the constraint violations to format
68
* @return a new set containing the formatted messages
69
* @param <T> the generic type of the constraint violations
70
*/
71
public static <T> Collection<String> format(Set<ConstraintViolation<T>> violations);
72
73
/**
74
* Computes a set of formatted messages from the given untyped set of ConstraintViolations.
75
*
76
* @param violations the constraint violations to format
77
* @return a new set containing the formatted messages
78
*/
79
public static Collection<String> formatUntyped(Set<ConstraintViolation<?>> violations);
80
81
/**
82
* Copies a set of ConstraintViolations.
83
*
84
* @param violations the source set
85
* @return a new HashSet containing the violations from the source set
86
* @param <T> the generic type of the constraint violations
87
*/
88
public static <T> Set<ConstraintViolation<?>> copyOf(Set<ConstraintViolation<T>> violations);
89
}
90
```
91
92
**Usage Example:**
93
94
```java
95
import io.dropwizard.validation.ConstraintViolations;
96
import javax.validation.ConstraintViolation;
97
import java.util.Set;
98
import java.util.Collection;
99
100
// Format individual violation
101
ConstraintViolation<MyConfig> violation = // ... obtained from validation
102
String formatted = ConstraintViolations.format(violation);
103
// Returns: "propertyName violation message" or just "violation message" for ValidationMethod
104
105
// Format multiple violations
106
Set<ConstraintViolation<MyConfig>> violations = validator.validate(config);
107
Collection<String> messages = ConstraintViolations.format(violations);
108
// Returns a sorted collection of formatted violation messages
109
110
// Format untyped violations (e.g., from mixed validation)
111
Set<ConstraintViolation<?>> untypedViolations = // ... mixed violations
112
Collection<String> untypedMessages = ConstraintViolations.formatUntyped(untypedViolations);
113
114
// Copy violations for processing
115
Set<ConstraintViolation<?>> copy = ConstraintViolations.copyOf(violations);
116
```
117
118
### Message Interpolation Utilities
119
120
Utilities for escaping special characters in validation message parameters, ensuring proper message formatting.
121
122
```java { .api }
123
public final class InterpolationHelper {
124
/**
125
* A constant representing the '{' character.
126
*/
127
public static final char BEGIN_TERM = '{';
128
129
/**
130
* A constant representing the '}' character.
131
*/
132
public static final char END_TERM = '}';
133
134
/**
135
* A constant representing the '$' character.
136
*/
137
public static final char EL_DESIGNATOR = '$';
138
139
/**
140
* A constant representing the '\' character.
141
*/
142
public static final char ESCAPE_CHARACTER = '\\';
143
144
/**
145
* Escapes a string with the ESCAPE_MESSAGE_PARAMETER_PATTERN.
146
*
147
* @param messageParameter the string to escape
148
* @return the escaped string or null, if the input was null
149
*/
150
@Nullable
151
public static String escapeMessageParameter(@Nullable String messageParameter);
152
}
153
```
154
155
**Usage Example:**
156
157
```java
158
import io.dropwizard.validation.InterpolationHelper;
159
import org.checkerframework.checker.nullness.qual.Nullable;
160
161
// Escape special characters in message parameters
162
String userInput = "Value with {special} $characters";
163
String escaped = InterpolationHelper.escapeMessageParameter(userInput);
164
// Returns: "Value with \\{special\\} \\$characters"
165
166
// Use in custom validation message parameters
167
Map<String, Object> messageParams = new HashMap<>();
168
messageParams.put("userValue", InterpolationHelper.escapeMessageParameter(userInput));
169
```
170
171
## Integration Notes
172
173
- `BaseValidator.newValidator()` automatically includes Guava Optional value extractors
174
- `ConstraintViolations.format()` provides special handling for `@ValidationMethod` annotations
175
- All utilities are thread-safe and can be used in concurrent environments
176
- These utilities integrate seamlessly with Dropwizard's Jersey resource validation