1
1
import * as zod from 'zod' ;
2
+ import { ZodError } from 'zod' ;
2
3
3
4
import {
4
5
fromZodError ,
@@ -13,21 +14,23 @@ describe('fromZodError()', () => {
13
14
try {
14
15
emailSchema . parse ( 'foobar' ) ;
15
16
} catch ( err ) {
16
- const validationError = fromZodError ( err ) ;
17
- expect ( validationError ) . toBeInstanceOf ( ValidationError ) ;
18
- expect ( validationError . message ) . toMatchInlineSnapshot (
19
- `"Validation error: Invalid email"`
20
- ) ;
21
- expect ( validationError . details ) . toMatchInlineSnapshot ( `
22
- Array [
23
- Object {
17
+ if ( err instanceof ZodError ) {
18
+ const validationError = fromZodError ( err ) ;
19
+ expect ( validationError ) . toBeInstanceOf ( ValidationError ) ;
20
+ expect ( validationError . message ) . toMatchInlineSnapshot (
21
+ `"Validation error: Invalid email"`
22
+ ) ;
23
+ expect ( validationError . details ) . toMatchInlineSnapshot ( `
24
+ [
25
+ {
24
26
"code": "invalid_string",
25
27
"message": "Invalid email",
26
- "path": Array [],
28
+ "path": [],
27
29
"validation": "email",
28
30
},
29
31
]
30
32
` ) ;
33
+ }
31
34
}
32
35
} ) ;
33
36
@@ -43,35 +46,39 @@ describe('fromZodError()', () => {
43
46
name : 'a' ,
44
47
} ) ;
45
48
} catch ( err ) {
46
- const validationError = fromZodError ( err ) ;
47
- expect ( validationError ) . toBeInstanceOf ( ValidationError ) ;
48
- expect ( validationError . message ) . toMatchInlineSnapshot (
49
- `"Validation error: Number must be greater than 0 at \\"id\\"; String must contain at least 2 character(s) at \\"name\\""`
50
- ) ;
51
- expect ( validationError . details ) . toMatchInlineSnapshot ( `
52
- Array [
53
- Object {
49
+ if ( err instanceof ZodError ) {
50
+ const validationError = fromZodError ( err ) ;
51
+ expect ( validationError ) . toBeInstanceOf ( ValidationError ) ;
52
+ expect ( validationError . message ) . toMatchInlineSnapshot (
53
+ `"Validation error: Number must be greater than 0 at "id"; String must contain at least 2 character(s) at "name""`
54
+ ) ;
55
+ expect ( validationError . details ) . toMatchInlineSnapshot ( `
56
+ [
57
+ {
54
58
"code": "too_small",
59
+ "exact": false,
55
60
"inclusive": false,
56
61
"message": "Number must be greater than 0",
57
62
"minimum": 0,
58
- "path": Array [
63
+ "path": [
59
64
"id",
60
65
],
61
66
"type": "number",
62
67
},
63
- Object {
68
+ {
64
69
"code": "too_small",
70
+ "exact": false,
65
71
"inclusive": true,
66
72
"message": "String must contain at least 2 character(s)",
67
73
"minimum": 2,
68
- "path": Array [
74
+ "path": [
69
75
"name",
70
76
],
71
77
"type": "string",
72
78
},
73
79
]
74
80
` ) ;
81
+ }
75
82
}
76
83
} ) ;
77
84
@@ -81,42 +88,44 @@ describe('fromZodError()', () => {
81
88
try {
82
89
objSchema . parse ( [ 1 , 'a' , true , 1.23 ] ) ;
83
90
} catch ( err ) {
84
- const validationError = fromZodError ( err ) ;
85
- expect ( validationError ) . toBeInstanceOf ( ValidationError ) ;
86
- expect ( validationError . message ) . toMatchInlineSnapshot (
87
- `"Validation error: Expected number, received string at \\"[1]\\"; Expected number, received boolean at \\"[2]\\"; Expected integer, received float at \\"[3]\\""`
88
- ) ;
89
- expect ( validationError . details ) . toMatchInlineSnapshot ( `
90
- Array [
91
- Object {
91
+ if ( err instanceof ZodError ) {
92
+ const validationError = fromZodError ( err ) ;
93
+ expect ( validationError ) . toBeInstanceOf ( ValidationError ) ;
94
+ expect ( validationError . message ) . toMatchInlineSnapshot (
95
+ `"Validation error: Expected number, received string at "[1]"; Expected number, received boolean at "[2]"; Expected integer, received float at "[3]""`
96
+ ) ;
97
+ expect ( validationError . details ) . toMatchInlineSnapshot ( `
98
+ [
99
+ {
92
100
"code": "invalid_type",
93
101
"expected": "number",
94
102
"message": "Expected number, received string",
95
- "path": Array [
103
+ "path": [
96
104
1,
97
105
],
98
106
"received": "string",
99
107
},
100
- Object {
108
+ {
101
109
"code": "invalid_type",
102
110
"expected": "number",
103
111
"message": "Expected number, received boolean",
104
- "path": Array [
112
+ "path": [
105
113
2,
106
114
],
107
115
"received": "boolean",
108
116
},
109
- Object {
117
+ {
110
118
"code": "invalid_type",
111
119
"expected": "integer",
112
120
"message": "Expected integer, received float",
113
- "path": Array [
121
+ "path": [
114
122
3,
115
123
],
116
124
"received": "float",
117
125
},
118
126
]
119
127
` ) ;
128
+ }
120
129
}
121
130
} ) ;
122
131
@@ -138,46 +147,50 @@ describe('fromZodError()', () => {
138
147
} ,
139
148
} ) ;
140
149
} catch ( err ) {
141
- const validationError = fromZodError ( err ) ;
142
- expect ( validationError ) . toBeInstanceOf ( ValidationError ) ;
143
- expect ( validationError . message ) . toMatchInlineSnapshot (
144
- `"Validation error: Number must be greater than 0 at \\"id\\"; Expected number, received string at \\"arr[1]\\"; String must contain at least 2 character(s) at \\"nestedObj.name\\""`
145
- ) ;
146
- expect ( validationError . details ) . toMatchInlineSnapshot ( `
147
- Array [
148
- Object {
150
+ if ( err instanceof ZodError ) {
151
+ const validationError = fromZodError ( err ) ;
152
+ expect ( validationError ) . toBeInstanceOf ( ValidationError ) ;
153
+ expect ( validationError . message ) . toMatchInlineSnapshot (
154
+ `"Validation error: Number must be greater than 0 at "id"; Expected number, received string at "arr[1]"; String must contain at least 2 character(s) at "nestedObj.name""`
155
+ ) ;
156
+ expect ( validationError . details ) . toMatchInlineSnapshot ( `
157
+ [
158
+ {
149
159
"code": "too_small",
160
+ "exact": false,
150
161
"inclusive": false,
151
162
"message": "Number must be greater than 0",
152
163
"minimum": 0,
153
- "path": Array [
164
+ "path": [
154
165
"id",
155
166
],
156
167
"type": "number",
157
168
},
158
- Object {
169
+ {
159
170
"code": "invalid_type",
160
171
"expected": "number",
161
172
"message": "Expected number, received string",
162
- "path": Array [
173
+ "path": [
163
174
"arr",
164
175
1,
165
176
],
166
177
"received": "string",
167
178
},
168
- Object {
179
+ {
169
180
"code": "too_small",
181
+ "exact": false,
170
182
"inclusive": true,
171
183
"message": "String must contain at least 2 character(s)",
172
184
"minimum": 2,
173
- "path": Array [
185
+ "path": [
174
186
"nestedObj",
175
187
"name",
176
188
],
177
189
"type": "string",
178
190
},
179
191
]
180
192
` ) ;
193
+ }
181
194
}
182
195
} ) ;
183
196
} ) ;
0 commit comments