You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 6, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+24-9Lines changed: 24 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ The default validation pipe is great, but error it returns is just an array of e
10
10
{
11
11
"statusCode": 400,
12
12
"error": "Bad Request",
13
-
"message": ["email must be an email", "phone cannot be empty"]
13
+
"message": ["email must be an email", "phone should not be empty"]
14
14
}
15
15
```
16
16
@@ -20,10 +20,25 @@ This package changes the `message` to be an object with field names as keys:
20
20
{
21
21
"statusCode": 400,
22
22
"error": "Bad Request",
23
-
"message": {
24
-
"email": ["email must be an email"],
25
-
"phone": ["phone cannot be empty"]
26
-
}
23
+
"message": [
24
+
{
25
+
"field": ["email"],
26
+
"errors": ["email must be an email"]
27
+
},
28
+
{
29
+
"field": ["phone"],
30
+
"errors": ["phone should not be empty"]
31
+
}
32
+
]
33
+
}
34
+
```
35
+
36
+
It also works for [nested values](https://github.com/typestack/class-validator#validating-nested-objects):
37
+
38
+
```json
39
+
{
40
+
"field": ["nestedObject", "name"],
41
+
"errors": ["name should not be empty"]
27
42
}
28
43
```
29
44
@@ -43,10 +58,10 @@ On NPM:
43
58
npm install @exonest/better-validation-pipe
44
59
```
45
60
46
-
## Motivation
61
+
## Usage
47
62
48
-
This behavior is achievable by passing a custom `exceptionFactory` to the original pipe, but I found myself writing the same exception factory for each one of my projects, so I made this small package.
63
+
Just use it as you would normally use [Nest's built-in validation pipe](https://docs.nestjs.com/techniques/validation#using-the-built-in-validationpipe). You can also pass options to it, just like you would with the built-in one.
49
64
50
-
## Inspiration
65
+
## Motivation
51
66
52
-
This is the same way [Laravel](https://laravel.com/docs/master/validation) returns errors; as an object that contains arrays of errors (strings).
67
+
This behavior is achievable by passing a custom `exceptionFactory` to the original pipe, but I found myself writing the same exception factory for each one of my projects, so I made this package to do the job.
0 commit comments