Skip to content

Commit 424c080

Browse files
committed
Updated README
1 parent b22c7e0 commit 424c080

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

README.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Java Validator
22
A rule based validator developed for easy use with the Spring Boot framework.
33

4-
- *Extendable*: You can always add custom rules
4+
- *Extendable* ✨: You can always add custom rules
5+
- **
56

67
## Table of contents
78
- [Adding dependency](#adding-dependency)
@@ -12,6 +13,7 @@ A rule based validator developed for easy use with the Spring Boot framework.
1213
- [`number` rules](#number-rules)
1314
- [`object` rules](#object-rules)
1415
- [`array` rules](#array-rules)
16+
- [Use with Spring Boot](#use-with-spring-boot)
1517
- [Custom rules](#custom-rules)
1618

1719

@@ -69,6 +71,7 @@ dto = validator.validate(dto);
6971

7072
## Internationalization
7173
Unfortunately it is not possible to change the outputted language to something different than english.
74+
7275
If you need to provide other languages as well, you can make use of the error type. When
7376
you return the result of the `getErrors()` method of the ErrorBag that you got from the thrown
7477
`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
257260
---
258261

259262

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+
260312
## Custom rules
261313
You can easily extend the functionality of the validator by defining custom rules. If you need a specific Regex and don't
262314
want to use the `PatternRule` over and over again you can create your own rule.
263315

264316
To do this, implement the `Rule` interface and add an instance of your rule to the validator like shown below:
265317

266318
```java
267-
import dev.ditsche.validator.rule.RuleResult;public class MyRule implements Rule {
319+
public class MyRule implements Rule {
268320
@Override
269321
public RuleResult test(Object value) {
270322

0 commit comments

Comments
 (0)