@@ -14,6 +14,10 @@ A rule based validator developed for easy use with the Spring Boot framework.
14
14
- [ ` array ` rules] ( #array-rules )
15
15
- [ Custom rules] ( #custom-rules )
16
16
17
+
18
+ ---
19
+
20
+
17
21
## Adding dependency
18
22
To use the validator add the following dependency to your ` pom.xml ` .
19
23
``` xml
@@ -24,6 +28,10 @@ To use the validator add the following dependency to your `pom.xml`.
24
28
</dependency >
25
29
```
26
30
31
+
32
+ ---
33
+
34
+
27
35
## Usage
28
36
29
37
Easiest way to use the validator in a Spring Boot project is to setup a ` ValidatorConfig ` class. There you can
@@ -55,13 +63,21 @@ the validate method. That way you get the validated and updated input DTO for fu
55
63
dto = validator. validate(dto);
56
64
```
57
65
66
+
67
+ ---
68
+
69
+
58
70
## Internationalization
59
71
Unfortunately it is not possible to change the outputted language to something different than english.
60
72
If you need to provide other languages as well, you can make use of the error type. When
61
73
you return the result of the ` getErrors() ` method of the ErrorBag that you got from the thrown
62
74
` ValidationException ` you have the error type available and can show messages based on this
63
75
unique key.
64
76
77
+
78
+ ---
79
+
80
+
65
81
## Builders and Methods
66
82
As said, to generate a validator you should make use of the static helper methods.
67
83
The following types are supported at the moment:
@@ -71,7 +87,8 @@ The following types are supported at the moment:
71
87
- array
72
88
73
89
In the following sections you learn which methods and rules are available by default and how
74
- to use them.
90
+ to use them. Each ` Builder ` has a ` build() ` method which returns a ` Validateable ` , if you need
91
+ to have one of those.
75
92
76
93
### ` string ` rules
77
94
@@ -125,7 +142,8 @@ string(field)
125
142
126
143
- ##### ` defaultValue(String) `
127
144
* Sets the fields value to the given string* , if the fields value is null or empty.
128
-
145
+ Rejects if the given parameter is not assignable to the fields value.
146
+
129
147
- ##### ` custom(Rule) `
130
148
Registers a custom defined rule.
131
149
@@ -139,6 +157,106 @@ The following snippet returns an instance of the `NumberRuleBuilder` class.
139
157
number(field)
140
158
```
141
159
160
+ #### Available rules
161
+
162
+ - ##### ` required() `
163
+ Marks the field as * required* meaning it cannot be null.
164
+
165
+ - ##### ` optional() `
166
+ Marks the field as * optional* . All rules behind this rule can fail.
167
+
168
+ - ##### ` length(int) `
169
+ Checks if the * fields value is the same* as the provided parameter ` length ` .
170
+
171
+ - ##### ` size(int, int) `
172
+ Checks if the * value is between* the first and second parameter integers.
173
+
174
+ - ##### ` min(int) `
175
+ Checks if the fields value is * greater or equal* to the given parameter.
176
+
177
+ - ##### ` max(int) `
178
+ Checks if the fields value of the string is * smaller or equal* to the given parameter.
179
+
180
+ - ##### ` defaultValue(int) `
181
+ * Sets the fields value to the given number* , if the fields value is null or 0.
182
+ Rejects if the given parameter is not assignable to the fields value.
183
+
184
+ - ##### ` custom(Rule) `
185
+ Registers a custom defined rule.
186
+
187
+ ---
188
+
189
+ ### ` object ` rules
190
+
191
+ #### Usage
192
+ The following snippet returns an instance of the ` ObjectRuleBuilder ` class.
193
+ ``` java
194
+ object(field)
195
+ ```
196
+
197
+ #### Available rules
198
+
199
+ - ##### ` optional() `
200
+ Marks the field as * optional* . All rules behind this rule can fail.
201
+
202
+ - ##### ` child(Builder) `
203
+ Adds a check for * one child property* using a ` Builder ` .
204
+
205
+ - ##### ` child(Validatable) `
206
+ Adds a check for * one child property* using a ` Validatable ` .
207
+
208
+ - ##### ` fields(Builder[]) `
209
+ Adds a check for * one or more child properties* using the given ` Builder ` .
210
+
211
+ ---
212
+
213
+ ### ` array ` rules
214
+
215
+ #### Usage
216
+ The following snippet returns an instance of the ` ArrayRuleBuilder ` class.
217
+ ``` java
218
+ array(field)
219
+ ```
220
+
221
+ #### Available rules
222
+
223
+ - ##### ` required() `
224
+ Marks the field as * required* meaning it cannot be null or empty.
225
+
226
+ - ##### ` optional() `
227
+ Marks the field as * optional* . All rules behind this rule can fail.
228
+
229
+ - ##### ` length(int) `
230
+ Checks if the * length of the array is the same* as the provided parameter ` length ` .
231
+
232
+ - ##### ` size(int, int) `
233
+ Checks if the * length of the array is between* the first and second parameter integers.
234
+
235
+ - ##### ` min(int) `
236
+ Checks if the length of the array is * greater or equal* to the given parameter.
237
+
238
+ - ##### ` max(int) `
239
+ Checks if the length of the array is * smaller or equal* to the given parameter.
240
+
241
+ - ##### ` objects(Builder[]) `
242
+ * Used for object arrays only!* Gets an array of ` Builder ` instances, which are validated for every object in the array.
243
+
244
+ - ##### ` custom(Rule) `
245
+ Registers a custom defined rule.
246
+
247
+ #### Additional methods
248
+
249
+ Arrays are handled differently as they can have elements or objects as children. To handle
250
+ an array of elements you have to use the ` elements ` method which returns an ` ArrayElementRuleBuilder ` .
251
+
252
+ - ##### ` elements() `
253
+ Returns an ` ArrayElementRuleBuilder ` which has access to nearly every rule from above as
254
+ the type of the arrays elements is unknown.
255
+
256
+
257
+ ---
258
+
259
+
142
260
## Custom rules
143
261
You can easily extend the functionality of the validator by defining custom rules. If you need a specific Regex and don't
144
262
want to use the ` PatternRule ` over and over again you can create your own rule.
0 commit comments