1
1
# Java Validator
2
2
A rule based validator developed for easy use with the Spring Boot framework.
3
3
4
- - * Extendable* : You can always add custom rules
4
+ - * Extendable* ✨: You can always add custom rules
5
+ - **
5
6
6
7
## Table of contents
7
8
- [ Adding dependency] ( #adding-dependency )
@@ -12,6 +13,7 @@ A rule based validator developed for easy use with the Spring Boot framework.
12
13
- [ ` number ` rules] ( #number-rules )
13
14
- [ ` object ` rules] ( #object-rules )
14
15
- [ ` array ` rules] ( #array-rules )
16
+ - [ Use with Spring Boot] ( #use-with-spring-boot )
15
17
- [ Custom rules] ( #custom-rules )
16
18
17
19
@@ -69,6 +71,7 @@ dto = validator.validate(dto);
69
71
70
72
## Internationalization
71
73
Unfortunately it is not possible to change the outputted language to something different than english.
74
+
72
75
If you need to provide other languages as well, you can make use of the error type. When
73
76
you return the result of the ` getErrors() ` method of the ErrorBag that you got from the thrown
74
77
` ValidationException ` you have the error type available and can show messages based on this
@@ -257,14 +260,63 @@ an array of elements you have to use the `elements` method which returns an `Arr
257
260
---
258
261
259
262
263
+ ## Use with Spring Boot
264
+ To make use of the functionalities of Spring Boot and the structure of the errors returned by
265
+ the ` ErrorBag ` instance of the thrown ` ValidationException ` you'd need to define a custom exception
266
+ handler. Spring Boot makes it easy to create one.
267
+
268
+ The following advice generates a well-structured json response that the frontend can use
269
+ to display the errors in the UI. It returns with the ` 422 Unprocessable Entity ` status code
270
+ of http which indicates a validation problem.
271
+
272
+ ``` java
273
+ @ControllerAdvice
274
+ public class ValidationAdvice {
275
+
276
+ @ExceptionHandler (ValidationException . class)
277
+ @ResponseBody
278
+ public ResponseEntity<?> handleValidationErrors (ValidationException ex ) {
279
+ return ResponseEntity . status(HttpStatus . UNPROCESSABLE_ENTITY ). body(
280
+ ex. getErrors()
281
+ );
282
+ }
283
+
284
+ }
285
+ ```
286
+
287
+ The resulting json would look like this:
288
+
289
+ ``` json
290
+ [
291
+ {
292
+ "field" : " email" ,
293
+ "errors" : [
294
+ {
295
+ "message" : " The field \" email\" is required" ,
296
+ "errorType" : " validation.error.format.required"
297
+ },
298
+ {
299
+ "message" : " The field \" email\" needs to be a valid email address" ,
300
+ "errorType" : " validation.error.format.email"
301
+ }
302
+ ]
303
+ },
304
+ ...
305
+ ]
306
+ ```
307
+
308
+
309
+ ---
310
+
311
+
260
312
## Custom rules
261
313
You can easily extend the functionality of the validator by defining custom rules. If you need a specific Regex and don't
262
314
want to use the ` PatternRule ` over and over again you can create your own rule.
263
315
264
316
To do this, implement the ` Rule ` interface and add an instance of your rule to the validator like shown below:
265
317
266
318
``` java
267
- import dev.ditsche.validator.rule.RuleResult ; public class MyRule implements Rule {
319
+ public class MyRule implements Rule {
268
320
@Override
269
321
public RuleResult test (Object value ) {
270
322
0 commit comments