3
3
import assert from "node:assert" ;
4
4
import { test } from "node:test" ;
5
5
import { z } from "zod" ;
6
- import { createSafeActionClient , flattenBindArgsValidationErrors , formatBindArgsValidationErrors } from ".." ;
6
+ import { createSafeActionClient , DEFAULT_SERVER_ERROR_MESSAGE } from ".." ;
7
+ import { ActionBindArgsValidationError } from "../validation-errors" ;
7
8
8
9
// Default client tests.
9
10
10
11
const dac = createSafeActionClient ( ) ;
11
12
12
- test ( "action with invalid bind args input gives back an object with correct `bindArgsValidationErrors` (default formatted shape)" , async ( ) => {
13
- const bindArgsSchemas : [ age : z . ZodNumber , userId : z . ZodString , product : z . ZodObject < { id : z . ZodString } > ] = [
14
- z . number ( ) . positive ( ) ,
15
- z . string ( ) . uuid ( ) ,
16
- z . object ( {
17
- id : z . string ( ) . uuid ( ) ,
18
- } ) ,
19
- ] ;
20
-
21
- const action = dac . bindArgsSchemas ( bindArgsSchemas ) . action ( async ( ) => {
22
- return {
23
- ok : true ,
24
- } ;
13
+ test ( "action with invalid bind args input and valid main input gives back a server error" , async ( ) => {
14
+ const schema = z . object ( {
15
+ username : z . string ( ) . min ( 3 ) ,
25
16
} ) ;
26
17
27
- const actualResult = await action ( - 123 , crypto . randomUUID ( ) , { id : "invalid_uuid" } ) ;
28
-
29
- const expectedResult = {
30
- bindArgsValidationErrors : [
31
- {
32
- _errors : [ "Number must be greater than 0" ] ,
33
- } ,
34
- { } ,
35
- {
36
- id : {
37
- _errors : [ "Invalid uuid" ] ,
38
- } ,
39
- } ,
40
- ] ,
41
- } ;
42
-
43
- assert . deepStrictEqual ( actualResult , expectedResult ) ;
44
- } ) ;
45
-
46
- test ( "action with invalid bind args input gives back an object with correct `bindArgsValidationErrors` (default formatted shape overridden by custom flattened shape)" , async ( ) => {
47
18
const bindArgsSchemas : [ age : z . ZodNumber , userId : z . ZodString , product : z . ZodObject < { id : z . ZodString } > ] = [
48
19
z . number ( ) . positive ( ) ,
49
20
z . string ( ) . uuid ( ) ,
@@ -53,129 +24,40 @@ test("action with invalid bind args input gives back an object with correct `bin
53
24
] ;
54
25
55
26
const action = dac
56
- . bindArgsSchemas ( bindArgsSchemas , {
57
- handleBindArgsValidationErrorsShape : async ( ve ) => flattenBindArgsValidationErrors ( ve ) ,
58
- } )
27
+ . schema ( schema )
28
+ . bindArgsSchemas ( bindArgsSchemas )
59
29
. action ( async ( ) => {
60
30
return {
61
31
ok : true ,
62
32
} ;
63
33
} ) ;
64
34
65
- const actualResult = await action ( - 123 , crypto . randomUUID ( ) , { id : "invalid_uuid" } ) ;
35
+ const actualResult = await action ( - 123 , crypto . randomUUID ( ) , { id : "invalid_uuid" } , { username : "johndoe" } ) ;
66
36
67
37
const expectedResult = {
68
- bindArgsValidationErrors : [
69
- {
70
- formErrors : [ "Number must be greater than 0" ] ,
71
- fieldErrors : { } ,
72
- } ,
73
- {
74
- formErrors : [ ] ,
75
- fieldErrors : { } ,
76
- } ,
77
- {
78
- formErrors : [ ] ,
79
- fieldErrors : {
80
- id : [ "Invalid uuid" ] ,
81
- } ,
82
- } ,
83
- ] ,
38
+ serverError : DEFAULT_SERVER_ERROR_MESSAGE ,
84
39
} ;
85
40
86
41
assert . deepStrictEqual ( actualResult , expectedResult ) ;
87
42
} ) ;
88
43
89
- // Formatted shape tests (same as default).
90
-
91
- const foac = createSafeActionClient ( {
92
- defaultValidationErrorsShape : "formatted" ,
93
- } ) ;
94
-
95
- test ( "action with invalid bind args input gives back an object with correct `bindArgsValidationErrors` (set formatted shape)" , async ( ) => {
96
- const bindArgsSchemas : [ age : z . ZodNumber , userId : z . ZodString , product : z . ZodObject < { id : z . ZodString } > ] = [
97
- z . number ( ) . positive ( ) ,
98
- z . string ( ) . uuid ( ) ,
99
- z . object ( {
100
- id : z . string ( ) . uuid ( ) ,
101
- } ) ,
102
- ] ;
103
-
104
- const action = foac . bindArgsSchemas ( bindArgsSchemas ) . action ( async ( ) => {
105
- return {
106
- ok : true ,
107
- } ;
108
- } ) ;
109
-
110
- const actualResult = await action ( - 123 , crypto . randomUUID ( ) , { id : "invalid_uuid" } ) ;
111
-
112
- const expectedResult = {
113
- bindArgsValidationErrors : [
114
- {
115
- _errors : [ "Number must be greater than 0" ] ,
116
- } ,
117
- { } ,
118
- {
119
- id : {
120
- _errors : [ "Invalid uuid" ] ,
121
- } ,
122
- } ,
123
- ] ,
124
- } ;
125
-
126
- assert . deepStrictEqual ( actualResult , expectedResult ) ;
127
- } ) ;
44
+ // Unmasked server error client.
128
45
129
- test ( "action with invalid bind args input gives back an object with correct `bindArgsValidationErrors` (set formatted shape overridden by custom flattened shape)" , async ( ) => {
130
- const bindArgsSchemas : [ age : z . ZodNumber , userId : z . ZodString , product : z . ZodObject < { id : z . ZodString } > ] = [
131
- z . number ( ) . positive ( ) ,
132
- z . string ( ) . uuid ( ) ,
133
- z . object ( {
134
- id : z . string ( ) . uuid ( ) ,
135
- } ) ,
136
- ] ;
137
-
138
- const action = foac
139
- . bindArgsSchemas ( bindArgsSchemas , {
140
- handleBindArgsValidationErrorsShape : async ( ve ) => flattenBindArgsValidationErrors ( ve ) ,
141
- } )
142
- . action ( async ( ) => {
46
+ const uac = createSafeActionClient ( {
47
+ handleServerError ( error ) {
48
+ if ( error instanceof ActionBindArgsValidationError ) {
143
49
return {
144
- ok : true ,
50
+ bindArgsValidationErrors : error . validationErrors ,
145
51
} ;
146
- } ) ;
52
+ }
147
53
148
- const actualResult = await action ( - 123 , crypto . randomUUID ( ) , { id : "invalid_uuid" } ) ;
149
-
150
- const expectedResult = {
151
- bindArgsValidationErrors : [
152
- {
153
- formErrors : [ "Number must be greater than 0" ] ,
154
- fieldErrors : { } ,
155
- } ,
156
- {
157
- formErrors : [ ] ,
158
- fieldErrors : { } ,
159
- } ,
160
- {
161
- formErrors : [ ] ,
162
- fieldErrors : {
163
- id : [ "Invalid uuid" ] ,
164
- } ,
165
- } ,
166
- ] ,
167
- } ;
168
-
169
- assert . deepStrictEqual ( actualResult , expectedResult ) ;
170
- } ) ;
171
-
172
- // Flattened shape tests.
173
-
174
- const flac = createSafeActionClient ( {
175
- defaultValidationErrorsShape : "flattened" ,
54
+ return {
55
+ message : error . message ,
56
+ } ;
57
+ } ,
176
58
} ) ;
177
59
178
- test ( "action with invalid bind args input gives back an object with correct `bindArgsValidationErrors` (set flattened shape) " , async ( ) => {
60
+ test ( "action with invalid bind args input gives back a server error object with correct `bindArgsValidationErrors` property " , async ( ) => {
179
61
const bindArgsSchemas : [ age : z . ZodNumber , userId : z . ZodString , product : z . ZodObject < { id : z . ZodString } > ] = [
180
62
z . number ( ) . positive ( ) ,
181
63
z . string ( ) . uuid ( ) ,
@@ -184,7 +66,7 @@ test("action with invalid bind args input gives back an object with correct `bin
184
66
} ) ,
185
67
] ;
186
68
187
- const action = flac . bindArgsSchemas ( bindArgsSchemas ) . action ( async ( ) => {
69
+ const action = uac . bindArgsSchemas ( bindArgsSchemas ) . action ( async ( ) => {
188
70
return {
189
71
ok : true ,
190
72
} ;
@@ -193,60 +75,19 @@ test("action with invalid bind args input gives back an object with correct `bin
193
75
const actualResult = await action ( - 123 , crypto . randomUUID ( ) , { id : "invalid_uuid" } ) ;
194
76
195
77
const expectedResult = {
196
- bindArgsValidationErrors : [
197
- {
198
- formErrors : [ "Number must be greater than 0" ] ,
199
- fieldErrors : { } ,
200
- } ,
201
- {
202
- formErrors : [ ] ,
203
- fieldErrors : { } ,
204
- } ,
205
- {
206
- formErrors : [ ] ,
207
- fieldErrors : {
208
- id : [ "Invalid uuid" ] ,
78
+ serverError : {
79
+ bindArgsValidationErrors : [
80
+ {
81
+ _errors : [ "Number must be greater than 0" ] ,
209
82
} ,
210
- } ,
211
- ] ,
212
- } ;
213
-
214
- assert . deepStrictEqual ( actualResult , expectedResult ) ;
215
- } ) ;
216
-
217
- test ( "action with invalid bind args input gives back an object with correct `bindArgsValidationErrors` (set flattened shape overridden by custom formatted shape)" , async ( ) => {
218
- const bindArgsSchemas : [ age : z . ZodNumber , userId : z . ZodString , product : z . ZodObject < { id : z . ZodString } > ] = [
219
- z . number ( ) . positive ( ) ,
220
- z . string ( ) . uuid ( ) ,
221
- z . object ( {
222
- id : z . string ( ) . uuid ( ) ,
223
- } ) ,
224
- ] ;
225
-
226
- const action = flac
227
- . bindArgsSchemas ( bindArgsSchemas , {
228
- handleBindArgsValidationErrorsShape : async ( ve ) => formatBindArgsValidationErrors ( ve ) ,
229
- } )
230
- . action ( async ( ) => {
231
- return {
232
- ok : true ,
233
- } ;
234
- } ) ;
235
-
236
- const actualResult = await action ( - 123 , crypto . randomUUID ( ) , { id : "invalid_uuid" } ) ;
237
-
238
- const expectedResult = {
239
- bindArgsValidationErrors : [
240
- {
241
- _errors : [ "Number must be greater than 0" ] ,
242
- } ,
243
- { } ,
244
- {
245
- id : {
246
- _errors : [ "Invalid uuid" ] ,
83
+ { } ,
84
+ {
85
+ id : {
86
+ _errors : [ "Invalid uuid" ] ,
87
+ } ,
247
88
} ,
248
- } ,
249
- ] ,
89
+ ] ,
90
+ } ,
250
91
} ;
251
92
252
93
assert . deepStrictEqual ( actualResult , expectedResult ) ;
0 commit comments