prisma-generator-express 1.12.0 → 1.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +87 -17
- package/dist/generator.js +2 -12
- package/dist/generator.js.map +1 -1
- package/dist/helpers/generateAggregate.js +13 -13
- package/dist/helpers/generateAggregate.js.map +1 -1
- package/dist/helpers/generateCount.js +12 -13
- package/dist/helpers/generateCount.js.map +1 -1
- package/dist/helpers/generateCreate.js +13 -15
- package/dist/helpers/generateCreate.js.map +1 -1
- package/dist/helpers/generateCreateMany.js +13 -15
- package/dist/helpers/generateCreateMany.js.map +1 -1
- package/dist/helpers/generateDelete.js +12 -15
- package/dist/helpers/generateDelete.js.map +1 -1
- package/dist/helpers/generateDeleteMany.js +13 -14
- package/dist/helpers/generateDeleteMany.js.map +1 -1
- package/dist/helpers/generateFindFirst.js +10 -15
- package/dist/helpers/generateFindFirst.js.map +1 -1
- package/dist/helpers/generateFindMany.js +10 -15
- package/dist/helpers/generateFindMany.js.map +1 -1
- package/dist/helpers/generateFindUnique.js +10 -15
- package/dist/helpers/generateFindUnique.js.map +1 -1
- package/dist/helpers/generateGroupBy.js +12 -13
- package/dist/helpers/generateGroupBy.js.map +1 -1
- package/dist/helpers/generateRouteFile.js +68 -35
- package/dist/helpers/generateRouteFile.js.map +1 -1
- package/dist/helpers/generateUpdate.js +12 -15
- package/dist/helpers/generateUpdate.js.map +1 -1
- package/dist/helpers/generateUpdateMany.js +12 -15
- package/dist/helpers/generateUpdateMany.js.map +1 -1
- package/dist/helpers/generateUpsert.js +12 -15
- package/dist/helpers/generateUpsert.js.map +1 -1
- package/dist/utils/copyFiles.js +26 -0
- package/dist/utils/copyFiles.js.map +1 -0
- package/package.json +4 -1
- package/src/copy/createOutputValidatorMiddleware.ts +44 -0
- package/src/copy/createValidatorMiddleware.ts +55 -0
- package/src/copy/encodeQueryParams.spec.ts +303 -0
- package/src/copy/encodeQueryParams.ts +44 -0
- package/src/copy/misc.spec.ts +62 -0
- package/src/copy/misc.ts +25 -0
- package/src/copy/parseQueryParams.spec.ts +187 -0
- package/src/copy/parseQueryParams.ts +42 -0
- package/src/copy/routeConfig.ts +34 -0
- package/src/copy/transformZod.spec.ts +714 -0
- package/src/copy/transformZod.ts +140 -0
- package/src/generator.ts +3 -13
- package/src/helpers/generateAggregate.ts +13 -13
- package/src/helpers/generateCount.ts +12 -13
- package/src/helpers/generateCreate.ts +14 -15
- package/src/helpers/generateCreateMany.ts +13 -15
- package/src/helpers/generateDelete.ts +13 -15
- package/src/helpers/generateDeleteMany.ts +14 -14
- package/src/helpers/generateFindFirst.ts +10 -15
- package/src/helpers/generateFindMany.ts +10 -15
- package/src/helpers/generateFindUnique.ts +10 -15
- package/src/helpers/generateGroupBy.ts +12 -13
- package/src/helpers/generateRouteFile.ts +68 -35
- package/src/helpers/generateUpdate.ts +13 -15
- package/src/helpers/generateUpdateMany.ts +13 -15
- package/src/helpers/generateUpsert.ts +13 -15
- package/src/utils/copyFiles.ts +27 -0
- package/dist/helpers/generateQsParser.js +0 -56
- package/dist/helpers/generateQsParser.js.map +0 -1
- package/dist/helpers/generateRouteConfigType.js +0 -34
- package/dist/helpers/generateRouteConfigType.js.map +0 -1
- package/src/helpers/generateQsParser.ts +0 -51
- package/src/helpers/generateRouteConfigType.ts +0 -29
|
@@ -0,0 +1,714 @@
|
|
|
1
|
+
import { z, ZodError } from 'zod'
|
|
2
|
+
import { allow, flattenObject, forbid } from './transformZod'
|
|
3
|
+
|
|
4
|
+
describe('Cryptocurrency Schema Validation', () => {
|
|
5
|
+
const cryptoSchema = z.object({
|
|
6
|
+
transactions: z
|
|
7
|
+
.array(
|
|
8
|
+
z.object({
|
|
9
|
+
amount: z.number(),
|
|
10
|
+
currency: z.string(),
|
|
11
|
+
date: z.string().optional(),
|
|
12
|
+
details: z
|
|
13
|
+
.object({
|
|
14
|
+
fee: z.number().optional(),
|
|
15
|
+
type: z.string(),
|
|
16
|
+
})
|
|
17
|
+
.optional(),
|
|
18
|
+
}),
|
|
19
|
+
)
|
|
20
|
+
.optional(),
|
|
21
|
+
wallet: z.object({
|
|
22
|
+
id: z.string(),
|
|
23
|
+
owner: z.object({
|
|
24
|
+
name: z.string(),
|
|
25
|
+
age: z.number().optional(),
|
|
26
|
+
}),
|
|
27
|
+
}),
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
const allowedFields = [
|
|
31
|
+
'wallet.id',
|
|
32
|
+
'wallet.owner.name',
|
|
33
|
+
'transactions[].amount',
|
|
34
|
+
'transactions[].currency',
|
|
35
|
+
]
|
|
36
|
+
const forbiddenFields = ['wallet.owner.age', 'transactions.details.fee']
|
|
37
|
+
|
|
38
|
+
describe('Allow Function', () => {
|
|
39
|
+
it('should validate data with only allowed fields', () => {
|
|
40
|
+
const inputData = {
|
|
41
|
+
transactions: [
|
|
42
|
+
{
|
|
43
|
+
amount: 100,
|
|
44
|
+
currency: 'BTC',
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
wallet: {
|
|
48
|
+
id: 'wallet123',
|
|
49
|
+
owner: {
|
|
50
|
+
name: 'Alice',
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
const result = allow(cryptoSchema, allowedFields).safeParse(inputData)
|
|
57
|
+
|
|
58
|
+
expect(result.success).toBe(true)
|
|
59
|
+
} catch (error) {
|
|
60
|
+
throw error
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('should return an error for data with disallowed fields', () => {
|
|
65
|
+
const inputData = {
|
|
66
|
+
transactions: [
|
|
67
|
+
{
|
|
68
|
+
amount: 100,
|
|
69
|
+
currency: 'BTC',
|
|
70
|
+
details: {
|
|
71
|
+
fee: 0.01,
|
|
72
|
+
type: 'transfer',
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
wallet: {
|
|
77
|
+
id: 'wallet123',
|
|
78
|
+
owner: {
|
|
79
|
+
name: 'Alice',
|
|
80
|
+
age: 30,
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
const result = allow(cryptoSchema, allowedFields).safeParse(inputData)
|
|
87
|
+
|
|
88
|
+
expect(result.success).toBe(false)
|
|
89
|
+
} catch (error) {
|
|
90
|
+
expect(error).toBeInstanceOf(ZodError)
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
describe('Forbid Function', () => {
|
|
96
|
+
it('should successfully parse when forbidden fields are absent', () => {
|
|
97
|
+
const inputData = {
|
|
98
|
+
transactions: [
|
|
99
|
+
{
|
|
100
|
+
amount: 200,
|
|
101
|
+
currency: 'ETH',
|
|
102
|
+
},
|
|
103
|
+
],
|
|
104
|
+
wallet: {
|
|
105
|
+
id: 'wallet456',
|
|
106
|
+
owner: {
|
|
107
|
+
name: 'Bob',
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
try {
|
|
113
|
+
const result = forbid(cryptoSchema, forbiddenFields).safeParse(
|
|
114
|
+
inputData,
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
expect(result.success).toBe(true)
|
|
118
|
+
} catch (error) {
|
|
119
|
+
throw error
|
|
120
|
+
}
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it('should return an error when forbidden fields are present', () => {
|
|
124
|
+
const inputData = {
|
|
125
|
+
transactions: [
|
|
126
|
+
{
|
|
127
|
+
amount: 200,
|
|
128
|
+
currency: 'ETH',
|
|
129
|
+
details: {
|
|
130
|
+
fee: 0.02,
|
|
131
|
+
type: 'exchange',
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
],
|
|
135
|
+
wallet: {
|
|
136
|
+
id: 'wallet456',
|
|
137
|
+
owner: {
|
|
138
|
+
name: 'Bob',
|
|
139
|
+
age: 40,
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
try {
|
|
145
|
+
const result = forbid(cryptoSchema, forbiddenFields).safeParse(
|
|
146
|
+
inputData,
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
expect(result.success).toBe(false)
|
|
150
|
+
} catch (error) {
|
|
151
|
+
expect(error).toBeInstanceOf(ZodError)
|
|
152
|
+
}
|
|
153
|
+
})
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
it('should handle deeply nested structures correctly', () => {
|
|
157
|
+
const inputData = {
|
|
158
|
+
transactions: [
|
|
159
|
+
{
|
|
160
|
+
amount: 300,
|
|
161
|
+
currency: 'XRP',
|
|
162
|
+
details: {
|
|
163
|
+
fee: 0.03,
|
|
164
|
+
type: 'purchase',
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
],
|
|
168
|
+
wallet: {
|
|
169
|
+
id: 'wallet789',
|
|
170
|
+
owner: {
|
|
171
|
+
name: 'Charlie',
|
|
172
|
+
age: 50,
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
try {
|
|
178
|
+
const result = forbid(cryptoSchema, forbiddenFields).safeParse(inputData)
|
|
179
|
+
expect(result.success).toBe(false)
|
|
180
|
+
} catch (error) {
|
|
181
|
+
expect(error).toBeInstanceOf(ZodError)
|
|
182
|
+
}
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
it('should not error for absent optional fields', () => {
|
|
186
|
+
const inputData = {
|
|
187
|
+
transactions: [],
|
|
188
|
+
wallet: {
|
|
189
|
+
id: 'wallet012',
|
|
190
|
+
owner: {
|
|
191
|
+
name: 'Dave',
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
try {
|
|
197
|
+
const result = allow(cryptoSchema, allowedFields).safeParse(inputData)
|
|
198
|
+
expect(result.success).toBe(true)
|
|
199
|
+
} catch (error) {
|
|
200
|
+
throw error
|
|
201
|
+
}
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
it('should flag all fields when all are forbidden', () => {
|
|
205
|
+
const inputData = {
|
|
206
|
+
transactions: [
|
|
207
|
+
{
|
|
208
|
+
amount: 400,
|
|
209
|
+
currency: 'LTC',
|
|
210
|
+
details: {
|
|
211
|
+
fee: 0.04,
|
|
212
|
+
type: 'withdrawal',
|
|
213
|
+
},
|
|
214
|
+
},
|
|
215
|
+
],
|
|
216
|
+
wallet: {
|
|
217
|
+
id: 'wallet345',
|
|
218
|
+
owner: {
|
|
219
|
+
name: 'Eve',
|
|
220
|
+
age: 60,
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
}
|
|
224
|
+
const allForbidden = [
|
|
225
|
+
'wallet.id',
|
|
226
|
+
'wallet.owner.name',
|
|
227
|
+
'wallet.owner.age',
|
|
228
|
+
'transactions[].amount',
|
|
229
|
+
'transactions[].currency',
|
|
230
|
+
'transactions[].details.fee',
|
|
231
|
+
]
|
|
232
|
+
|
|
233
|
+
try {
|
|
234
|
+
const result = forbid(cryptoSchema, allForbidden).safeParse(inputData)
|
|
235
|
+
expect(result.success).toBe(false)
|
|
236
|
+
} catch (error) {
|
|
237
|
+
expect(error).toBeInstanceOf(ZodError)
|
|
238
|
+
}
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
it('should handle combination of allowed and forbidden correctly', () => {
|
|
242
|
+
const inputData = {
|
|
243
|
+
transactions: [
|
|
244
|
+
{
|
|
245
|
+
amount: 500,
|
|
246
|
+
currency: 'ADA',
|
|
247
|
+
details: {
|
|
248
|
+
fee: 0.05,
|
|
249
|
+
type: 'deposit',
|
|
250
|
+
},
|
|
251
|
+
},
|
|
252
|
+
],
|
|
253
|
+
wallet: {
|
|
254
|
+
id: 'wallet678',
|
|
255
|
+
owner: {
|
|
256
|
+
name: 'Frank',
|
|
257
|
+
age: 70,
|
|
258
|
+
},
|
|
259
|
+
},
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
try {
|
|
263
|
+
const resultAllow = allow(cryptoSchema, [
|
|
264
|
+
'wallet.owner.name',
|
|
265
|
+
'transactions.currency',
|
|
266
|
+
]).safeParse(inputData)
|
|
267
|
+
expect(resultAllow.success).toBe(false)
|
|
268
|
+
} catch (error) {
|
|
269
|
+
expect(error).toBeInstanceOf(ZodError)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
try {
|
|
273
|
+
const resultForbid = forbid(cryptoSchema, forbiddenFields).safeParse(
|
|
274
|
+
inputData,
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
expect(resultForbid.success).toBe(false)
|
|
278
|
+
} catch (error) {
|
|
279
|
+
expect(error).toBeInstanceOf(ZodError)
|
|
280
|
+
}
|
|
281
|
+
})
|
|
282
|
+
|
|
283
|
+
describe('Additional Cryptocurrency Schema Validation', () => {
|
|
284
|
+
it('should return an error for invalid input types', () => {
|
|
285
|
+
const inputData = {
|
|
286
|
+
transactions: [
|
|
287
|
+
{
|
|
288
|
+
amount: '100', // Invalid type: should be a number
|
|
289
|
+
currency: 'BTC',
|
|
290
|
+
},
|
|
291
|
+
],
|
|
292
|
+
wallet: {
|
|
293
|
+
id: 123, // Invalid type: should be a string
|
|
294
|
+
owner: {
|
|
295
|
+
name: 'Alice',
|
|
296
|
+
},
|
|
297
|
+
},
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
try {
|
|
301
|
+
const result = allow(cryptoSchema, allowedFields).safeParse(inputData)
|
|
302
|
+
expect(result.success).toBe(false)
|
|
303
|
+
} catch (error) {
|
|
304
|
+
expect(error).toBeInstanceOf(ZodError)
|
|
305
|
+
}
|
|
306
|
+
})
|
|
307
|
+
|
|
308
|
+
it('should handle deeply nested arrays correctly', () => {
|
|
309
|
+
const inputData = {
|
|
310
|
+
transactions: [
|
|
311
|
+
{
|
|
312
|
+
amount: 300,
|
|
313
|
+
currency: 'XRP',
|
|
314
|
+
details: {
|
|
315
|
+
fee: 0.03,
|
|
316
|
+
type: 'purchase',
|
|
317
|
+
},
|
|
318
|
+
},
|
|
319
|
+
],
|
|
320
|
+
wallet: {
|
|
321
|
+
id: 'wallet789',
|
|
322
|
+
owner: {
|
|
323
|
+
name: 'Charlie',
|
|
324
|
+
age: 50,
|
|
325
|
+
},
|
|
326
|
+
},
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
try {
|
|
330
|
+
const result = forbid(cryptoSchema, forbiddenFields).safeParse(
|
|
331
|
+
inputData,
|
|
332
|
+
)
|
|
333
|
+
|
|
334
|
+
expect(result.success).toBe(false)
|
|
335
|
+
} catch (error) {
|
|
336
|
+
expect(error).toBeInstanceOf(ZodError)
|
|
337
|
+
}
|
|
338
|
+
})
|
|
339
|
+
|
|
340
|
+
it('should correctly handle mixed allowed and forbidden paths', () => {
|
|
341
|
+
const inputData = {
|
|
342
|
+
transactions: [
|
|
343
|
+
{
|
|
344
|
+
amount: 200,
|
|
345
|
+
currency: 'ETH',
|
|
346
|
+
details: {
|
|
347
|
+
fee: 0.02,
|
|
348
|
+
type: 'exchange',
|
|
349
|
+
},
|
|
350
|
+
},
|
|
351
|
+
],
|
|
352
|
+
wallet: {
|
|
353
|
+
id: 'wallet456',
|
|
354
|
+
owner: {
|
|
355
|
+
name: 'Bob',
|
|
356
|
+
age: 40,
|
|
357
|
+
},
|
|
358
|
+
},
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
try {
|
|
362
|
+
const resultAllow = allow(cryptoSchema, [
|
|
363
|
+
'wallet.owner.name',
|
|
364
|
+
]).safeParse(inputData)
|
|
365
|
+
|
|
366
|
+
expect(resultAllow.success).toBe(false)
|
|
367
|
+
} catch (error) {
|
|
368
|
+
expect(error).toBeInstanceOf(ZodError)
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
try {
|
|
372
|
+
const resultForbid = forbid(cryptoSchema, [
|
|
373
|
+
'wallet.owner.age',
|
|
374
|
+
]).safeParse(inputData)
|
|
375
|
+
|
|
376
|
+
expect(resultForbid.success).toBe(false)
|
|
377
|
+
} catch (error) {
|
|
378
|
+
expect(error).toBeInstanceOf(ZodError)
|
|
379
|
+
}
|
|
380
|
+
})
|
|
381
|
+
|
|
382
|
+
it('should handle empty input objects gracefully', () => {
|
|
383
|
+
const optionalCryptoSchema = z.object({
|
|
384
|
+
transactions: z
|
|
385
|
+
.array(
|
|
386
|
+
z.object({
|
|
387
|
+
amount: z.number().optional(),
|
|
388
|
+
currency: z.string().optional(),
|
|
389
|
+
date: z.string().optional(),
|
|
390
|
+
details: z
|
|
391
|
+
.object({
|
|
392
|
+
fee: z.number().optional(),
|
|
393
|
+
type: z.string().optional(),
|
|
394
|
+
})
|
|
395
|
+
.optional(),
|
|
396
|
+
}),
|
|
397
|
+
)
|
|
398
|
+
.optional(),
|
|
399
|
+
wallet: z
|
|
400
|
+
.object({
|
|
401
|
+
id: z.string().optional(),
|
|
402
|
+
owner: z
|
|
403
|
+
.object({
|
|
404
|
+
name: z.string().optional(),
|
|
405
|
+
age: z.number().optional(),
|
|
406
|
+
})
|
|
407
|
+
.optional(),
|
|
408
|
+
})
|
|
409
|
+
.optional(),
|
|
410
|
+
})
|
|
411
|
+
|
|
412
|
+
const inputData = {}
|
|
413
|
+
|
|
414
|
+
try {
|
|
415
|
+
const result = allow(optionalCryptoSchema, allowedFields).safeParse(
|
|
416
|
+
inputData,
|
|
417
|
+
)
|
|
418
|
+
expect(result.success).toBe(true)
|
|
419
|
+
} catch (error) {
|
|
420
|
+
console.error(
|
|
421
|
+
'Error in empty input objects test:',
|
|
422
|
+
JSON.stringify(error, null, 2),
|
|
423
|
+
)
|
|
424
|
+
throw error
|
|
425
|
+
}
|
|
426
|
+
})
|
|
427
|
+
|
|
428
|
+
it('should handle edge cases correctly', () => {
|
|
429
|
+
const inputData = {
|
|
430
|
+
transactions: [
|
|
431
|
+
{
|
|
432
|
+
amount: null,
|
|
433
|
+
currency: undefined,
|
|
434
|
+
},
|
|
435
|
+
],
|
|
436
|
+
wallet: {
|
|
437
|
+
id: 'walletEdge',
|
|
438
|
+
owner: {
|
|
439
|
+
name: null,
|
|
440
|
+
age: undefined,
|
|
441
|
+
},
|
|
442
|
+
},
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
try {
|
|
446
|
+
const result = forbid(cryptoSchema, forbiddenFields).safeParse(
|
|
447
|
+
inputData,
|
|
448
|
+
)
|
|
449
|
+
expect(result.success).toBe(false)
|
|
450
|
+
} catch (error) {
|
|
451
|
+
console.error(
|
|
452
|
+
'Error in edge cases test:',
|
|
453
|
+
JSON.stringify(error, null, 2),
|
|
454
|
+
)
|
|
455
|
+
expect(error).toBeInstanceOf(ZodError)
|
|
456
|
+
}
|
|
457
|
+
})
|
|
458
|
+
})
|
|
459
|
+
|
|
460
|
+
it('should skip keys with undefined values in optional fields', () => {
|
|
461
|
+
const inputData = {
|
|
462
|
+
wallet: {
|
|
463
|
+
owner: {
|
|
464
|
+
age: undefined, // Optional key with undefined value
|
|
465
|
+
},
|
|
466
|
+
},
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
const schema = z.object({
|
|
470
|
+
wallet: z.object({
|
|
471
|
+
owner: z.object({
|
|
472
|
+
age: z.number().optional(),
|
|
473
|
+
}),
|
|
474
|
+
}),
|
|
475
|
+
})
|
|
476
|
+
|
|
477
|
+
const result = flattenObject(inputData, '', schema)
|
|
478
|
+
expect(result).toEqual({})
|
|
479
|
+
})
|
|
480
|
+
|
|
481
|
+
it('should handle deeply nested objects correctly', () => {
|
|
482
|
+
const inputData = {
|
|
483
|
+
wallet: {
|
|
484
|
+
owner: {
|
|
485
|
+
details: {
|
|
486
|
+
address: {
|
|
487
|
+
city: 'New York',
|
|
488
|
+
},
|
|
489
|
+
},
|
|
490
|
+
},
|
|
491
|
+
},
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
const schema = z.object({
|
|
495
|
+
wallet: z.object({
|
|
496
|
+
owner: z.object({
|
|
497
|
+
details: z.object({
|
|
498
|
+
address: z.object({
|
|
499
|
+
city: z.string(),
|
|
500
|
+
}),
|
|
501
|
+
}),
|
|
502
|
+
}),
|
|
503
|
+
}),
|
|
504
|
+
})
|
|
505
|
+
|
|
506
|
+
const result = flattenObject(inputData, '', schema)
|
|
507
|
+
expect(result).toEqual({
|
|
508
|
+
'wallet.owner.details.address.city': 'New York',
|
|
509
|
+
})
|
|
510
|
+
})
|
|
511
|
+
|
|
512
|
+
it('should add key-value pairs to the result object', () => {
|
|
513
|
+
const inputData = {
|
|
514
|
+
wallet: {
|
|
515
|
+
id: 'wallet123',
|
|
516
|
+
},
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
const schema = z.object({
|
|
520
|
+
wallet: z.object({
|
|
521
|
+
id: z.string(),
|
|
522
|
+
}),
|
|
523
|
+
})
|
|
524
|
+
|
|
525
|
+
const result = flattenObject(inputData, '', schema)
|
|
526
|
+
expect(result).toEqual({
|
|
527
|
+
'wallet.id': 'wallet123',
|
|
528
|
+
})
|
|
529
|
+
})
|
|
530
|
+
|
|
531
|
+
it('should handle ZodUnion with ZodLazy correctly', () => {
|
|
532
|
+
const lazySchema = z.lazy(() => z.string())
|
|
533
|
+
const unionSchema = z.union([lazySchema, z.number()])
|
|
534
|
+
|
|
535
|
+
const inputData = {
|
|
536
|
+
wallet: {
|
|
537
|
+
details: 'lazy value',
|
|
538
|
+
},
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
const schema = z.object({
|
|
542
|
+
wallet: z.object({
|
|
543
|
+
details: unionSchema,
|
|
544
|
+
}),
|
|
545
|
+
})
|
|
546
|
+
|
|
547
|
+
try {
|
|
548
|
+
const result = flattenObject(inputData, '', schema)
|
|
549
|
+
expect(result).toEqual({
|
|
550
|
+
'wallet.details': 'lazy value',
|
|
551
|
+
})
|
|
552
|
+
} catch (error) {
|
|
553
|
+
expect(error).toBeInstanceOf(ZodError)
|
|
554
|
+
}
|
|
555
|
+
})
|
|
556
|
+
|
|
557
|
+
it('deep nesting', () => {
|
|
558
|
+
const taskSchema = z.object({
|
|
559
|
+
select: z
|
|
560
|
+
.object({
|
|
561
|
+
id: z.boolean().optional(),
|
|
562
|
+
project_id: z.boolean().optional(),
|
|
563
|
+
list_id: z.boolean().optional(),
|
|
564
|
+
user_assignments: z
|
|
565
|
+
.object({
|
|
566
|
+
select: z.object({
|
|
567
|
+
user: z.boolean().optional(),
|
|
568
|
+
}),
|
|
569
|
+
})
|
|
570
|
+
.optional(),
|
|
571
|
+
tags_mappings: z
|
|
572
|
+
.object({
|
|
573
|
+
select: z.object({
|
|
574
|
+
tag: z.boolean().optional(),
|
|
575
|
+
}),
|
|
576
|
+
})
|
|
577
|
+
.optional(),
|
|
578
|
+
attachments: z
|
|
579
|
+
.object({
|
|
580
|
+
select: z.object({
|
|
581
|
+
attachment: z.boolean().optional(),
|
|
582
|
+
}),
|
|
583
|
+
where: z
|
|
584
|
+
.object({
|
|
585
|
+
is_image: z.boolean().optional(),
|
|
586
|
+
})
|
|
587
|
+
.optional(),
|
|
588
|
+
take: z.number().optional(),
|
|
589
|
+
orderBy: z
|
|
590
|
+
.object({
|
|
591
|
+
created_at: z.enum(['asc', 'desc']).optional(),
|
|
592
|
+
})
|
|
593
|
+
.optional(),
|
|
594
|
+
})
|
|
595
|
+
.optional(),
|
|
596
|
+
rendered_description: z.boolean().optional(),
|
|
597
|
+
description: z.boolean().optional(),
|
|
598
|
+
created_at: z.boolean().optional(),
|
|
599
|
+
start_date: z.boolean().optional(),
|
|
600
|
+
reactions: z.boolean().optional(),
|
|
601
|
+
intervals: z.boolean().optional(),
|
|
602
|
+
column_id: z.boolean().optional(),
|
|
603
|
+
priority: z.boolean().optional(),
|
|
604
|
+
due_date: z.boolean().optional(),
|
|
605
|
+
column: z.boolean().optional(),
|
|
606
|
+
title: z.boolean().optional(),
|
|
607
|
+
order: z.boolean().optional(),
|
|
608
|
+
color: z.boolean().optional(),
|
|
609
|
+
})
|
|
610
|
+
.optional(),
|
|
611
|
+
where: z
|
|
612
|
+
.object({
|
|
613
|
+
id: z.string().optional(),
|
|
614
|
+
AND: z
|
|
615
|
+
.array(
|
|
616
|
+
z.object({
|
|
617
|
+
OR: z
|
|
618
|
+
.array(
|
|
619
|
+
z.object({
|
|
620
|
+
to_delete: z.boolean().nullable().optional(),
|
|
621
|
+
}),
|
|
622
|
+
)
|
|
623
|
+
.optional(),
|
|
624
|
+
}),
|
|
625
|
+
)
|
|
626
|
+
.optional(),
|
|
627
|
+
})
|
|
628
|
+
.optional(),
|
|
629
|
+
})
|
|
630
|
+
|
|
631
|
+
const allowedFields = [
|
|
632
|
+
'select.id',
|
|
633
|
+
'select.project_id',
|
|
634
|
+
'select.list_id',
|
|
635
|
+
'select.user_assignments.select.user',
|
|
636
|
+
'select.tags_mappings.select.tag',
|
|
637
|
+
'select.attachments.select.attachment',
|
|
638
|
+
'select.attachments.where.is_image',
|
|
639
|
+
'select.attachments.take',
|
|
640
|
+
'select.attachments.orderBy.created_at',
|
|
641
|
+
'select.rendered_description',
|
|
642
|
+
'select.description',
|
|
643
|
+
'select.created_at',
|
|
644
|
+
'select.start_date',
|
|
645
|
+
'select.reactions',
|
|
646
|
+
'select.intervals',
|
|
647
|
+
'select.column_id',
|
|
648
|
+
'select.priority',
|
|
649
|
+
'select.due_date',
|
|
650
|
+
'select.column',
|
|
651
|
+
'select.order',
|
|
652
|
+
'select.color',
|
|
653
|
+
'where.id',
|
|
654
|
+
'where.AND[].OR[].to_delete',
|
|
655
|
+
]
|
|
656
|
+
const inputData = {
|
|
657
|
+
select: {
|
|
658
|
+
id: true,
|
|
659
|
+
project_id: true,
|
|
660
|
+
list_id: true,
|
|
661
|
+
user_assignments: {
|
|
662
|
+
select: {
|
|
663
|
+
user: true,
|
|
664
|
+
},
|
|
665
|
+
},
|
|
666
|
+
tags_mappings: {
|
|
667
|
+
select: {
|
|
668
|
+
tag: true,
|
|
669
|
+
},
|
|
670
|
+
},
|
|
671
|
+
attachments: {
|
|
672
|
+
select: {
|
|
673
|
+
attachment: true,
|
|
674
|
+
},
|
|
675
|
+
where: {
|
|
676
|
+
is_image: true,
|
|
677
|
+
},
|
|
678
|
+
take: 100,
|
|
679
|
+
orderBy: {
|
|
680
|
+
created_at: 'desc',
|
|
681
|
+
},
|
|
682
|
+
},
|
|
683
|
+
rendered_description: true,
|
|
684
|
+
description: true,
|
|
685
|
+
created_at: true,
|
|
686
|
+
start_date: true,
|
|
687
|
+
reactions: true,
|
|
688
|
+
intervals: true,
|
|
689
|
+
column_id: true,
|
|
690
|
+
priority: true,
|
|
691
|
+
due_date: true,
|
|
692
|
+
column: true,
|
|
693
|
+
title: true,
|
|
694
|
+
order: true,
|
|
695
|
+
color: true,
|
|
696
|
+
},
|
|
697
|
+
where: {
|
|
698
|
+
id: 'task_id',
|
|
699
|
+
AND: [
|
|
700
|
+
{
|
|
701
|
+
OR: [{ to_delete: false }, { to_delete: null }],
|
|
702
|
+
},
|
|
703
|
+
],
|
|
704
|
+
},
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
try {
|
|
708
|
+
const result = allow(taskSchema, allowedFields).safeParse(inputData)
|
|
709
|
+
expect(result.success).toBe(false)
|
|
710
|
+
} catch (error) {
|
|
711
|
+
expect(error).toBeInstanceOf(ZodError)
|
|
712
|
+
}
|
|
713
|
+
})
|
|
714
|
+
})
|