@miroir-framework/jzod-ts 0.5.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 +3 -0
- package/babel.config.cjs +13 -0
- package/dist/bundle.d.ts +419 -0
- package/dist/bundle.js +303 -0
- package/dist/src/JzodToTs.d.ts +17 -0
- package/dist/src/JzodTsInterface.d.ts +397 -0
- package/dist/src/facade.d.ts +8 -0
- package/dist/src/index.d.ts +3 -0
- package/dist/src/tools.d.ts +4 -0
- package/jest.config.js +63 -0
- package/package.json +38 -0
- package/rollup.config.js +36 -0
- package/src/JzodToTs.ts +104 -0
- package/src/JzodTsInterface.ts +450 -0
- package/src/index.ts +60 -0
- package/src/tools.ts +25 -0
- package/tests/jzod-ts.test.ts +108 -0
- package/tests/resources/tsTypeGeneration-testJzodSchema1 - reference.ts +5 -0
- package/tests/resources/tsTypeGeneration-testJzodSchema2 - reference.ts +11 -0
- package/tests/resources/tsTypeGeneration-testJzodSchema3 - reference.ts +19 -0
- package/tests/resources/tsTypeGeneration-testJzodSchema4 - reference.ts +250 -0
- package/tsconfig.json +26 -0
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
import { ZodType, z } from "zod";
|
|
2
|
+
|
|
3
|
+
const jzodRootSchema = z.object({
|
|
4
|
+
optional: z.boolean().optional(),
|
|
5
|
+
}).strict();
|
|
6
|
+
type JzodRoot = z.infer<typeof jzodRootSchema>;
|
|
7
|
+
|
|
8
|
+
// ##############################################################################################################
|
|
9
|
+
export const jzodEnumAttributeTypesSchema = z.enum([
|
|
10
|
+
"any",
|
|
11
|
+
"bigint",
|
|
12
|
+
"boolean",
|
|
13
|
+
"date",
|
|
14
|
+
"never",
|
|
15
|
+
"null",
|
|
16
|
+
"number",
|
|
17
|
+
"string",
|
|
18
|
+
"uuid",
|
|
19
|
+
"undefined",
|
|
20
|
+
"unknown",
|
|
21
|
+
"void",
|
|
22
|
+
])
|
|
23
|
+
|
|
24
|
+
export type JzodEnumTypes = z.infer<typeof jzodEnumAttributeTypesSchema>;
|
|
25
|
+
|
|
26
|
+
// ##############################################################################################################
|
|
27
|
+
export const jzodEnumElementTypesSchema = z.enum([
|
|
28
|
+
"array",
|
|
29
|
+
"enum",
|
|
30
|
+
"function",
|
|
31
|
+
"lazy",
|
|
32
|
+
"literal",
|
|
33
|
+
"intersection",
|
|
34
|
+
"map",
|
|
35
|
+
"object",
|
|
36
|
+
"promise",
|
|
37
|
+
"record",
|
|
38
|
+
"schemaReference",
|
|
39
|
+
"set",
|
|
40
|
+
"simpleType",
|
|
41
|
+
"tuple",
|
|
42
|
+
"union",
|
|
43
|
+
])
|
|
44
|
+
|
|
45
|
+
export type JzodEnumElementTypes = z.infer<typeof jzodEnumElementTypesSchema>;
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
// ##############################################################################################################
|
|
49
|
+
export interface JzodArray extends JzodRoot {
|
|
50
|
+
optional?: boolean,
|
|
51
|
+
nullable?: boolean,
|
|
52
|
+
extra?: {[k:string]:any},
|
|
53
|
+
type: 'array',
|
|
54
|
+
definition: JzodElement
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export const jzodArraySchema: z.ZodType<JzodArray> = z.object({ // issue with JsonSchema conversion when using extend from ZodRootSchema, although the 2 are functionnaly equivalent
|
|
58
|
+
optional: z.boolean().optional(),
|
|
59
|
+
nullable: z.boolean().optional(),
|
|
60
|
+
extra: z.record(z.string(),z.any()).optional(),
|
|
61
|
+
type: z.literal('array'),
|
|
62
|
+
definition: z.lazy(()=>jzodElementSchema)
|
|
63
|
+
}).strict();
|
|
64
|
+
|
|
65
|
+
// ##############################################################################################################
|
|
66
|
+
export const jzodAttributeDateValidationsSchema = z
|
|
67
|
+
.object({
|
|
68
|
+
extra: z.record(z.string(), z.any()).optional(),
|
|
69
|
+
type: z.enum([
|
|
70
|
+
"min",
|
|
71
|
+
"max",
|
|
72
|
+
]),
|
|
73
|
+
parameter: z.any(),
|
|
74
|
+
})
|
|
75
|
+
.strict();
|
|
76
|
+
|
|
77
|
+
export type JzodAttributeDateValidations = z.infer<typeof jzodAttributeDateValidationsSchema>;
|
|
78
|
+
|
|
79
|
+
// ##############################################################################################################
|
|
80
|
+
export const jzodAttributeNumberValidationsSchema = z
|
|
81
|
+
.object({
|
|
82
|
+
extra: z.record(z.string(), z.any()).optional(),
|
|
83
|
+
type: z.enum([
|
|
84
|
+
"gt",
|
|
85
|
+
"gte",
|
|
86
|
+
"lt",
|
|
87
|
+
"lte",
|
|
88
|
+
"int",
|
|
89
|
+
"positive",
|
|
90
|
+
"nonpositive",
|
|
91
|
+
"negative",
|
|
92
|
+
"nonnegative",
|
|
93
|
+
"multipleOf",
|
|
94
|
+
"finite",
|
|
95
|
+
"safe",
|
|
96
|
+
]),
|
|
97
|
+
parameter: z.any(),
|
|
98
|
+
})
|
|
99
|
+
.strict();
|
|
100
|
+
|
|
101
|
+
export type JzodAttributeNumberValidations = z.infer<typeof jzodAttributeNumberValidationsSchema>;
|
|
102
|
+
|
|
103
|
+
// ##############################################################################################################
|
|
104
|
+
export const jzodAttributeStringValidationsSchema = z
|
|
105
|
+
.object({
|
|
106
|
+
extra: z.record(z.string(), z.any()).optional(),
|
|
107
|
+
type: z.enum([
|
|
108
|
+
"max",
|
|
109
|
+
"min",
|
|
110
|
+
"length",
|
|
111
|
+
"email",
|
|
112
|
+
"url",
|
|
113
|
+
"emoji",
|
|
114
|
+
"uuid",
|
|
115
|
+
"cuid",
|
|
116
|
+
"cuid2",
|
|
117
|
+
"ulid",
|
|
118
|
+
"regex",
|
|
119
|
+
"includes",
|
|
120
|
+
"startsWith",
|
|
121
|
+
"endsWith",
|
|
122
|
+
"datetime",
|
|
123
|
+
"ip",
|
|
124
|
+
]),
|
|
125
|
+
parameter: z.any(),
|
|
126
|
+
})
|
|
127
|
+
.strict();
|
|
128
|
+
|
|
129
|
+
export type JzodAttributeStringValidations = z.infer<typeof jzodAttributeStringValidationsSchema>;
|
|
130
|
+
|
|
131
|
+
// ##############################################################################################################
|
|
132
|
+
export const jzodAttributeDateWithValidationsSchema = z.object({
|
|
133
|
+
optional: z.boolean().optional(),
|
|
134
|
+
nullable: z.boolean().optional(),
|
|
135
|
+
extra: z.record(z.string(),z.any()).optional(),
|
|
136
|
+
coerce: z.boolean().optional(),
|
|
137
|
+
type: z.literal(jzodEnumElementTypesSchema.enum.simpleType),
|
|
138
|
+
definition: z.literal(jzodEnumAttributeTypesSchema.enum.date),
|
|
139
|
+
validations: z.array(jzodAttributeDateValidationsSchema),
|
|
140
|
+
}).strict();
|
|
141
|
+
|
|
142
|
+
export type JzodAttributeDateWithValidations = z.infer<typeof jzodAttributeDateWithValidationsSchema>;
|
|
143
|
+
|
|
144
|
+
// ##############################################################################################################
|
|
145
|
+
export const jzodAttributeNumberWithValidationsSchema = z.object({
|
|
146
|
+
optional: z.boolean().optional(),
|
|
147
|
+
nullable: z.boolean().optional(),
|
|
148
|
+
extra: z.record(z.string(),z.any()).optional(),
|
|
149
|
+
coerce: z.boolean().optional(),
|
|
150
|
+
type: z.literal(jzodEnumElementTypesSchema.enum.simpleType),
|
|
151
|
+
definition: z.literal(jzodEnumAttributeTypesSchema.enum.number),
|
|
152
|
+
validations: z.array(jzodAttributeNumberValidationsSchema),
|
|
153
|
+
}).strict();
|
|
154
|
+
|
|
155
|
+
export type JzodAttributeNumberWithValidations = z.infer<typeof jzodAttributeNumberWithValidationsSchema>;
|
|
156
|
+
|
|
157
|
+
// ##############################################################################################################
|
|
158
|
+
export const jzodAttributeStringWithValidationsSchema = z.object({
|
|
159
|
+
optional: z.boolean().optional(),
|
|
160
|
+
nullable: z.boolean().optional(),
|
|
161
|
+
extra: z.record(z.string(),z.any()).optional(),
|
|
162
|
+
coerce: z.boolean().optional(),
|
|
163
|
+
type: z.literal(jzodEnumElementTypesSchema.enum.simpleType),
|
|
164
|
+
definition: z.literal(jzodEnumAttributeTypesSchema.enum.string),
|
|
165
|
+
validations: z.array(jzodAttributeStringValidationsSchema),
|
|
166
|
+
}).strict();
|
|
167
|
+
|
|
168
|
+
export type JzodAttributeStringWithValidations = z.infer<typeof jzodAttributeStringWithValidationsSchema>;
|
|
169
|
+
|
|
170
|
+
// ##############################################################################################################
|
|
171
|
+
export const jzodAttributeSchema = z.object({
|
|
172
|
+
optional: z.boolean().optional(),
|
|
173
|
+
nullable: z.boolean().optional(),
|
|
174
|
+
extra: z.record(z.string(),z.any()).optional(),
|
|
175
|
+
coerce: z.boolean().optional(),
|
|
176
|
+
type: z.literal(jzodEnumElementTypesSchema.enum.simpleType),
|
|
177
|
+
definition: z.lazy(()=>jzodEnumAttributeTypesSchema),
|
|
178
|
+
}).strict();
|
|
179
|
+
|
|
180
|
+
export type JzodAttribute = z.infer<typeof jzodAttributeSchema>;
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
// ##############################################################################################################
|
|
184
|
+
export type JzodElement =
|
|
185
|
+
| JzodArray
|
|
186
|
+
| JzodAttribute
|
|
187
|
+
| JzodAttributeDateWithValidations
|
|
188
|
+
| JzodAttributeNumberWithValidations
|
|
189
|
+
| JzodAttributeStringWithValidations
|
|
190
|
+
| JzodEnum
|
|
191
|
+
| JzodFunction
|
|
192
|
+
| JzodLazy
|
|
193
|
+
| JzodLiteral
|
|
194
|
+
| JzodIntersection
|
|
195
|
+
| JzodMap
|
|
196
|
+
| JzodRecord
|
|
197
|
+
| JzodObject
|
|
198
|
+
| JzodPromise
|
|
199
|
+
| JzodReference
|
|
200
|
+
| JzodSet
|
|
201
|
+
| JzodTuple
|
|
202
|
+
| JzodUnion
|
|
203
|
+
;
|
|
204
|
+
|
|
205
|
+
export const jzodElementSchema: z.ZodType<JzodElement> = z.union([
|
|
206
|
+
z.lazy(()=>jzodArraySchema),
|
|
207
|
+
z.lazy(()=>jzodAttributeSchema),
|
|
208
|
+
z.lazy(()=>jzodAttributeDateWithValidationsSchema),
|
|
209
|
+
z.lazy(()=>jzodAttributeNumberWithValidationsSchema),
|
|
210
|
+
z.lazy(()=>jzodAttributeStringWithValidationsSchema),
|
|
211
|
+
z.lazy(()=>jzodEnumSchema),
|
|
212
|
+
z.lazy(()=>jzodFunctionSchema),
|
|
213
|
+
z.lazy(()=>jzodLazySchema),
|
|
214
|
+
z.lazy(()=>jzodLiteralSchema),
|
|
215
|
+
z.lazy(()=>jzodIntersectionSchema),
|
|
216
|
+
z.lazy(()=>jzodMapSchema),
|
|
217
|
+
z.lazy(()=>jzodObjectSchema),
|
|
218
|
+
z.lazy(()=>jzodPromiseSchema),
|
|
219
|
+
z.lazy(()=>jzodRecordSchema),
|
|
220
|
+
z.lazy(()=>jzodReferenceSchema),
|
|
221
|
+
z.lazy(()=>jzodSetSchema),
|
|
222
|
+
z.lazy(()=>jzodTupleSchema),
|
|
223
|
+
z.lazy(()=>jzodUnionSchema),
|
|
224
|
+
])
|
|
225
|
+
|
|
226
|
+
// // ##############################################################################################################
|
|
227
|
+
// export const jzodElementSetSchema = z.record(z.string(),jzodElementSchema);
|
|
228
|
+
// export type JzodElementSet = z.infer<typeof jzodElementSetSchema>;
|
|
229
|
+
|
|
230
|
+
// ##############################################################################################################
|
|
231
|
+
export const jzodEnumSchema = z.object({
|
|
232
|
+
optional: z.boolean().optional(),
|
|
233
|
+
nullable: z.boolean().optional(),
|
|
234
|
+
extra: z.record(z.string(),z.any()).optional(),
|
|
235
|
+
type: z.literal(jzodEnumElementTypesSchema.enum.enum),
|
|
236
|
+
definition: z.array(z.string()),
|
|
237
|
+
}).strict();
|
|
238
|
+
|
|
239
|
+
export type JzodEnum = z.infer<typeof jzodEnumSchema>;
|
|
240
|
+
|
|
241
|
+
// ##############################################################################################################
|
|
242
|
+
export interface JzodFunction {
|
|
243
|
+
// optional?: boolean,
|
|
244
|
+
// nullable?: boolean,
|
|
245
|
+
extra?: {[k:string]:any},
|
|
246
|
+
type: 'function',
|
|
247
|
+
definition: {args: JzodElement[], returns?: JzodElement},
|
|
248
|
+
}
|
|
249
|
+
export const jzodFunctionSchema: ZodType<JzodFunction> = z.object({
|
|
250
|
+
type: z.literal(jzodEnumElementTypesSchema.enum.function),
|
|
251
|
+
extra: z.record(z.string(),z.any()).optional(),
|
|
252
|
+
// anyway, arg and returns types are not use upon validation to check the function's interface. Suffices for it to be a function, it is then valid.
|
|
253
|
+
definition: z.object({
|
|
254
|
+
args:z.array(jzodElementSchema),
|
|
255
|
+
returns: jzodElementSchema.optional(),
|
|
256
|
+
})
|
|
257
|
+
}).strict();
|
|
258
|
+
|
|
259
|
+
// ##############################################################################################################
|
|
260
|
+
export const jzodLazySchema = z.object({
|
|
261
|
+
type: z.literal(jzodEnumElementTypesSchema.enum.lazy),
|
|
262
|
+
extra: z.record(z.string(),z.any()).optional(),
|
|
263
|
+
definition: jzodFunctionSchema,
|
|
264
|
+
}).strict();
|
|
265
|
+
|
|
266
|
+
export type JzodLazy = z.infer<typeof jzodLazySchema>;
|
|
267
|
+
|
|
268
|
+
// ##############################################################################################################
|
|
269
|
+
export const jzodLiteralSchema = z.object({
|
|
270
|
+
optional: z.boolean().optional(),
|
|
271
|
+
nullable: z.boolean().optional(),
|
|
272
|
+
extra: z.record(z.string(),z.any()).optional(),
|
|
273
|
+
type: z.literal(jzodEnumElementTypesSchema.enum.literal),
|
|
274
|
+
definition: z.string(),
|
|
275
|
+
}).strict();
|
|
276
|
+
|
|
277
|
+
export type JzodLiteral = z.infer<typeof jzodLiteralSchema>;
|
|
278
|
+
|
|
279
|
+
// ##############################################################################################################
|
|
280
|
+
export interface JzodIntersection {
|
|
281
|
+
optional?: boolean,
|
|
282
|
+
nullable?: boolean,
|
|
283
|
+
extra?: {[k:string]:any},
|
|
284
|
+
type: 'intersection',
|
|
285
|
+
definition: {left: JzodElement, right: JzodElement},
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export const jzodIntersectionSchema: z.ZodType<JzodIntersection> = z.object({
|
|
289
|
+
optional: z.boolean().optional(),
|
|
290
|
+
nullable: z.boolean().optional(),
|
|
291
|
+
extra: z.record(z.string(),z.any()).optional(),
|
|
292
|
+
type: z.literal(jzodEnumElementTypesSchema.enum.intersection),
|
|
293
|
+
definition: z.lazy(()=>z.object({left: jzodElementSchema, right: jzodElementSchema}))
|
|
294
|
+
}).strict();
|
|
295
|
+
|
|
296
|
+
// ##############################################################################################################
|
|
297
|
+
export interface JzodMap extends JzodRoot {
|
|
298
|
+
optional?: boolean,
|
|
299
|
+
nullable?: boolean,
|
|
300
|
+
extra?: {[k:string]:any},
|
|
301
|
+
type: 'map',
|
|
302
|
+
definition: [JzodElement,JzodElement]
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
export const jzodMapSchema: z.ZodType<JzodMap> = z.object({ // issue with JsonSchema conversion when using extend from ZodRootSchema, although the 2 are functionnaly equivalent
|
|
306
|
+
optional: z.boolean().optional(),
|
|
307
|
+
nullable: z.boolean().optional(),
|
|
308
|
+
extra: z.record(z.string(),z.any()).optional(),
|
|
309
|
+
type: z.literal('map'),
|
|
310
|
+
definition: z.lazy(()=>z.tuple([jzodElementSchema, jzodElementSchema]))
|
|
311
|
+
}).strict();
|
|
312
|
+
|
|
313
|
+
// ##############################################################################################################
|
|
314
|
+
export interface JzodObject extends JzodRoot {
|
|
315
|
+
optional?: boolean,
|
|
316
|
+
nullable?: boolean,
|
|
317
|
+
extend?: JzodReference | JzodObject,
|
|
318
|
+
extra?: {[k:string]:any},
|
|
319
|
+
type: "object",
|
|
320
|
+
nonStrict?: boolean,
|
|
321
|
+
// context?: {[attributeName:string]: JzodElement},
|
|
322
|
+
definition: {[attributeName:string]: JzodElement}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export const jzodObjectSchema: z.ZodType<JzodObject> = z.object({
|
|
326
|
+
optional: z.boolean().optional(),
|
|
327
|
+
nullable: z.boolean().optional(),
|
|
328
|
+
extend: z.lazy(()=>z.union([jzodReferenceSchema,jzodObjectSchema])).optional(),
|
|
329
|
+
extra: z.record(z.string(),z.any()).optional(),
|
|
330
|
+
type: z.literal(jzodEnumElementTypesSchema.enum.object),
|
|
331
|
+
nonStrict: z.boolean().optional(),
|
|
332
|
+
definition: z.lazy(()=>z.record(z.string(),jzodElementSchema)),
|
|
333
|
+
}).strict();
|
|
334
|
+
|
|
335
|
+
// ##############################################################################################################
|
|
336
|
+
export interface JzodPromise extends JzodRoot {
|
|
337
|
+
// optional?: boolean,
|
|
338
|
+
// nullable?: boolean,
|
|
339
|
+
extra?: {[k:string]:any},
|
|
340
|
+
type: 'promise',
|
|
341
|
+
definition: JzodElement
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
export const jzodPromiseSchema: z.ZodType<JzodPromise> = z.object({ // issue with JsonSchema conversion when using extend from ZodRootSchema, although the 2 are functionnaly equivalent
|
|
345
|
+
// optional: z.boolean().optional(),
|
|
346
|
+
// nullable: z.boolean().optional(),
|
|
347
|
+
extra: z.record(z.string(),z.any()).optional(),
|
|
348
|
+
type: z.literal('promise'),
|
|
349
|
+
definition: z.lazy(()=>jzodElementSchema)
|
|
350
|
+
}).strict();
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
// ##############################################################################################################
|
|
354
|
+
export interface JzodRecord {
|
|
355
|
+
optional?: boolean,
|
|
356
|
+
nullable?: boolean,
|
|
357
|
+
extra?: {[k:string]:any},
|
|
358
|
+
type: 'record',
|
|
359
|
+
definition: JzodElement,
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
export const jzodRecordSchema: z.ZodType<JzodRecord> = z.object({
|
|
363
|
+
optional: z.boolean().optional(),
|
|
364
|
+
nullable: z.boolean().optional(),
|
|
365
|
+
extra: z.record(z.string(),z.any()).optional(),
|
|
366
|
+
type: z.literal(jzodEnumElementTypesSchema.enum.record),
|
|
367
|
+
definition: z.lazy(()=>jzodElementSchema)
|
|
368
|
+
}).strict();
|
|
369
|
+
|
|
370
|
+
// ##############################################################################################################
|
|
371
|
+
export interface JzodReference {
|
|
372
|
+
optional?: boolean,
|
|
373
|
+
nullable?: boolean,
|
|
374
|
+
extra?: {[k:string]:any},
|
|
375
|
+
context?: {[attributeName:string]: JzodElement},
|
|
376
|
+
type: 'schemaReference',
|
|
377
|
+
definition: {
|
|
378
|
+
eager?: boolean,
|
|
379
|
+
relativePath?: string,
|
|
380
|
+
absolutePath?: string,
|
|
381
|
+
},
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
export const jzodReferenceSchema: ZodType<JzodReference> = z.object({ // inheritance from ZodRootSchema leads to a different JsonSchema thus invalidates tests, although it is semantically equivalent
|
|
385
|
+
optional: z.boolean().optional(),
|
|
386
|
+
nullable: z.boolean().optional(),
|
|
387
|
+
extra: z.record(z.string(),z.any()).optional(),
|
|
388
|
+
type: z.literal(jzodEnumElementTypesSchema.enum.schemaReference),
|
|
389
|
+
context: z.lazy(()=>z.record(z.string(),jzodElementSchema)).optional(),
|
|
390
|
+
definition: z.object({
|
|
391
|
+
eager: z.boolean().optional(),
|
|
392
|
+
relativePath: z.string().optional(),
|
|
393
|
+
absolutePath: z.string().optional(),
|
|
394
|
+
})
|
|
395
|
+
}).strict()
|
|
396
|
+
|
|
397
|
+
// export type JzodReference = z.infer<typeof jzodReferenceSchema>;
|
|
398
|
+
|
|
399
|
+
// ##############################################################################################################
|
|
400
|
+
export interface JzodSet extends JzodRoot {
|
|
401
|
+
optional?: boolean,
|
|
402
|
+
nullable?: boolean,
|
|
403
|
+
extra?: {[k:string]:any},
|
|
404
|
+
type: 'set',
|
|
405
|
+
definition: JzodElement
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
export const jzodSetSchema: z.ZodType<JzodSet> = z.object({ // issue with JsonSchema conversion when using extend from ZodRootSchema, although the 2 are functionnaly equivalent
|
|
409
|
+
optional: z.boolean().optional(),
|
|
410
|
+
nullable: z.boolean().optional(),
|
|
411
|
+
extra: z.record(z.string(),z.any()).optional(),
|
|
412
|
+
type: z.literal('set'),
|
|
413
|
+
definition: z.lazy(()=>jzodElementSchema)
|
|
414
|
+
}).strict();
|
|
415
|
+
|
|
416
|
+
// ##############################################################################################################
|
|
417
|
+
export interface JzodTuple {
|
|
418
|
+
optional?: boolean,
|
|
419
|
+
nullable?: boolean,
|
|
420
|
+
extra?: {[k:string]:any},
|
|
421
|
+
type: 'tuple',
|
|
422
|
+
definition: JzodElement[],
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
export const jzodTupleSchema: z.ZodType<JzodTuple> = z.object({
|
|
426
|
+
optional: z.boolean().optional(),
|
|
427
|
+
nullable: z.boolean().optional(),
|
|
428
|
+
extra: z.record(z.string(),z.any()).optional(),
|
|
429
|
+
type: z.literal(jzodEnumElementTypesSchema.enum.tuple),
|
|
430
|
+
definition: z.array(jzodElementSchema),
|
|
431
|
+
}).strict();
|
|
432
|
+
|
|
433
|
+
// ##############################################################################################################
|
|
434
|
+
export interface JzodUnion {
|
|
435
|
+
optional?: boolean,
|
|
436
|
+
nullable?: boolean,
|
|
437
|
+
extra?: {[k:string]:any},
|
|
438
|
+
type: "union",
|
|
439
|
+
discriminator?: string,
|
|
440
|
+
definition: JzodElement[],
|
|
441
|
+
}
|
|
442
|
+
export const jzodUnionSchema: z.ZodType<JzodUnion> = z.object({
|
|
443
|
+
optional: z.boolean().optional(),
|
|
444
|
+
nullable: z.boolean().optional(),
|
|
445
|
+
extra: z.record(z.string(),z.any()).optional(),
|
|
446
|
+
type: z.literal(jzodEnumElementTypesSchema.enum.union),
|
|
447
|
+
discriminator: z.string().optional(),
|
|
448
|
+
definition: z.lazy(()=>z.array(jzodElementSchema))
|
|
449
|
+
}).strict();
|
|
450
|
+
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export {
|
|
2
|
+
jzodToTsCode,
|
|
3
|
+
jzodToTsTypeAliasesAndZodText,
|
|
4
|
+
TsTypeAliasesAndZodText as JzodTypeAliasesAndZodText,
|
|
5
|
+
TsTypeAliases,
|
|
6
|
+
} from "./JzodToTs";
|
|
7
|
+
export { printTsTypeAlias, printTsTypeAliases } from "./tools";
|
|
8
|
+
|
|
9
|
+
export {
|
|
10
|
+
// SCHEMAS (const)
|
|
11
|
+
jzodArraySchema,
|
|
12
|
+
jzodAttributeSchema,
|
|
13
|
+
jzodAttributeDateValidationsSchema,
|
|
14
|
+
jzodAttributeDateWithValidationsSchema,
|
|
15
|
+
jzodAttributeNumberValidationsSchema,
|
|
16
|
+
jzodAttributeNumberWithValidationsSchema,
|
|
17
|
+
jzodAttributeStringValidationsSchema,
|
|
18
|
+
jzodAttributeStringWithValidationsSchema,
|
|
19
|
+
jzodElementSchema,
|
|
20
|
+
jzodEnumAttributeTypesSchema,
|
|
21
|
+
jzodEnumElementTypesSchema,
|
|
22
|
+
jzodEnumSchema,
|
|
23
|
+
jzodFunctionSchema,
|
|
24
|
+
jzodIntersectionSchema,
|
|
25
|
+
jzodLazySchema,
|
|
26
|
+
jzodLiteralSchema,
|
|
27
|
+
jzodMapSchema,
|
|
28
|
+
jzodObjectSchema,
|
|
29
|
+
jzodPromiseSchema,
|
|
30
|
+
jzodRecordSchema,
|
|
31
|
+
jzodReferenceSchema,
|
|
32
|
+
jzodSetSchema,
|
|
33
|
+
jzodTupleSchema,
|
|
34
|
+
jzodUnionSchema,
|
|
35
|
+
// TYPES
|
|
36
|
+
JzodArray,
|
|
37
|
+
JzodAttribute,
|
|
38
|
+
JzodAttributeDateValidations,
|
|
39
|
+
JzodAttributeDateWithValidations,
|
|
40
|
+
JzodAttributeNumberValidations,
|
|
41
|
+
JzodAttributeNumberWithValidations,
|
|
42
|
+
JzodAttributeStringValidations,
|
|
43
|
+
JzodAttributeStringWithValidations,
|
|
44
|
+
JzodElement,
|
|
45
|
+
JzodEnum,
|
|
46
|
+
JzodEnumTypes,
|
|
47
|
+
JzodFunction,
|
|
48
|
+
JzodEnumElementTypes,
|
|
49
|
+
JzodIntersection,
|
|
50
|
+
JzodLazy,
|
|
51
|
+
JzodLiteral,
|
|
52
|
+
JzodMap,
|
|
53
|
+
JzodObject,
|
|
54
|
+
JzodPromise,
|
|
55
|
+
JzodRecord,
|
|
56
|
+
JzodReference,
|
|
57
|
+
JzodSet,
|
|
58
|
+
JzodUnion,
|
|
59
|
+
JzodTuple,
|
|
60
|
+
} from "./JzodTsInterface";
|
package/src/tools.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { printNode } from "zod-to-ts";
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
|
|
4
|
+
import { TsTypeAliases } from "./JzodToTs";
|
|
5
|
+
|
|
6
|
+
// ################################################################################################
|
|
7
|
+
export function printTsTypeAlias(
|
|
8
|
+
typeAlias: ts.TypeAliasDeclaration,
|
|
9
|
+
exportPrefix: boolean = true,
|
|
10
|
+
): string {
|
|
11
|
+
return (exportPrefix?"export ":"")+printNode(typeAlias)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// ################################################################################################
|
|
15
|
+
export function printTsTypeAliases(
|
|
16
|
+
typeAliases: TsTypeAliases,
|
|
17
|
+
exportPrefix: boolean = true,
|
|
18
|
+
): string {
|
|
19
|
+
const result = Object.entries(typeAliases).reduce((acc, curr) => {
|
|
20
|
+
// console.log("printTypeAliases ", JSON.stringify(curr));
|
|
21
|
+
return `${acc}
|
|
22
|
+
${printTsTypeAlias(curr[1],exportPrefix)}`;
|
|
23
|
+
}, "");
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
|
|
4
|
+
import { jzodBootstrapElementSchema } from "@miroir-framework/jzod";
|
|
5
|
+
import { jzodToTsCode } from "../src/JzodToTs";
|
|
6
|
+
import { JzodElement, jzodElementSchema } from "../src/JzodTsInterface";
|
|
7
|
+
|
|
8
|
+
const refsPath = "./tests/resources"
|
|
9
|
+
const tmpPath = "./tests/tmp";
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
const testJzodToTs = (
|
|
13
|
+
// testDirectory: string,
|
|
14
|
+
referenceFileName: string,
|
|
15
|
+
testFileName: string,
|
|
16
|
+
testJzodSchema:JzodElement,
|
|
17
|
+
exportPrefix: boolean,
|
|
18
|
+
typeName: string
|
|
19
|
+
) => {
|
|
20
|
+
console.log("testJzodToTs:", typeName);
|
|
21
|
+
|
|
22
|
+
const testResultSchemaFilePath = path.join(tmpPath,testFileName);
|
|
23
|
+
const expectedSchemaFilePath = path.join(refsPath,referenceFileName);
|
|
24
|
+
|
|
25
|
+
const result = jzodToTsCode(testJzodSchema,exportPrefix,typeName)
|
|
26
|
+
fs.writeFileSync(testResultSchemaFilePath,result);
|
|
27
|
+
|
|
28
|
+
const resultContents = result.replace(/(\r\n|\n|\r)/gm, "");
|
|
29
|
+
// console.log("ts Type generation resultContents", resultContents);
|
|
30
|
+
|
|
31
|
+
const expectedFileContents = fs.readFileSync(expectedSchemaFilePath).toString().replace(/(\r\n|\n|\r)/gm, "")
|
|
32
|
+
expect(resultContents).toEqual(expectedFileContents);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
describe(
|
|
37
|
+
'Jzod-Ts',
|
|
38
|
+
() => {
|
|
39
|
+
// ############################################################################################
|
|
40
|
+
it(
|
|
41
|
+
"Jzod to TS Type",
|
|
42
|
+
async() => {
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
// ########################################################################################
|
|
46
|
+
const testJzodSchema1: JzodElement = { type: "simpleType", definition: "string" };
|
|
47
|
+
|
|
48
|
+
testJzodToTs(
|
|
49
|
+
"tsTypeGeneration-testJzodSchema1 - reference.ts",
|
|
50
|
+
"tsTypeGeneration-testJzodSchema1.ts",
|
|
51
|
+
testJzodSchema1,
|
|
52
|
+
true,
|
|
53
|
+
"testJzodSchema1"
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
// ########################################################################################
|
|
57
|
+
const testJzodSchema2: JzodElement = {
|
|
58
|
+
type: "schemaReference",
|
|
59
|
+
context: {
|
|
60
|
+
a: { type: "simpleType", definition: "string" },
|
|
61
|
+
b: {
|
|
62
|
+
type: "object",
|
|
63
|
+
definition: {
|
|
64
|
+
test: { type: "schemaReference", definition: { relativePath: "a" } }
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
definition: { relativePath: "b" },
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
testJzodToTs(
|
|
72
|
+
"tsTypeGeneration-testJzodSchema2 - reference.ts",
|
|
73
|
+
"tsTypeGeneration-testJzodSchema2.ts",
|
|
74
|
+
testJzodSchema2,
|
|
75
|
+
true,
|
|
76
|
+
"testJzodSchema2"
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
// ########################################################################################
|
|
80
|
+
const testJzodSchema4:JzodElement =
|
|
81
|
+
{
|
|
82
|
+
type: "schemaReference",
|
|
83
|
+
context: {
|
|
84
|
+
...jzodBootstrapElementSchema.context,
|
|
85
|
+
a: {
|
|
86
|
+
type: "array",
|
|
87
|
+
definition: { type: "schemaReference", definition: {relativePath: "jzodArraySchema"} }
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
definition: {
|
|
91
|
+
relativePath: "a"
|
|
92
|
+
},
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
testJzodToTs(
|
|
96
|
+
"tsTypeGeneration-testJzodSchema4 - reference.ts",
|
|
97
|
+
"tsTypeGeneration-testJzodSchema4.ts",
|
|
98
|
+
testJzodSchema4,
|
|
99
|
+
true,
|
|
100
|
+
"testJzodSchema4"
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ZodType, ZodTypeAny, z } from "zod";
|
|
2
|
+
|
|
3
|
+
export type a = string;
|
|
4
|
+
export type b = {
|
|
5
|
+
test?: a;
|
|
6
|
+
};
|
|
7
|
+
export type testJzodSchema2 = b;
|
|
8
|
+
|
|
9
|
+
export const a=z.string();
|
|
10
|
+
export const b=z.object({test:z.lazy(() =>a),}).strict();
|
|
11
|
+
export const testJzodSchema2 = z.lazy(() =>b);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ZodType, ZodTypeAny, z } from "zod";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
type jzodLiteralSchema = {
|
|
5
|
+
optional?: boolean | undefined;
|
|
6
|
+
extra?: {
|
|
7
|
+
[x: string]: any;
|
|
8
|
+
} | undefined;
|
|
9
|
+
type: "literal";
|
|
10
|
+
definition: string;
|
|
11
|
+
};
|
|
12
|
+
type jzodElementSchema = jzodLiteralSchema;
|
|
13
|
+
type testJzodSchema3 = {
|
|
14
|
+
b: jzodElementSchema[];
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const jzodLiteralSchema=z.object({optional:z.boolean().optional(),extra:z.record(z.string(),z.any()).optional(),type:z.literal("literal"),definition:z.string(),}).strict();
|
|
18
|
+
export const jzodElementSchema=z.union([z.lazy(() =>jzodLiteralSchema),]);
|
|
19
|
+
export const testJzodSchema3 = z.object({b:z.array(z.lazy(() =>jzodElementSchema)),}).strict();
|