@miroir-framework/jzod-ts 0.5.3 → 0.6.1

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.
@@ -1,450 +0,0 @@
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 jzodEnumAttributeTypes = 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 jzodEnumAttributeTypes>;
25
-
26
- // ##############################################################################################################
27
- export const jzodEnumElementTypes = 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 jzodEnumElementTypes>;
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 jzodArray: 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(()=>jzodElement)
63
- }).strict();
64
-
65
- // ##############################################################################################################
66
- export const jzodAttributeDateValidations = 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 jzodAttributeDateValidations>;
78
-
79
- // ##############################################################################################################
80
- export const jzodAttributeNumberValidations = 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 jzodAttributeNumberValidations>;
102
-
103
- // ##############################################################################################################
104
- export const jzodAttributeStringValidations = 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 jzodAttributeStringValidations>;
130
-
131
- // ##############################################################################################################
132
- export const jzodAttributeDateWithValidations = 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(jzodEnumElementTypes.enum.simpleType),
138
- definition: z.literal(jzodEnumAttributeTypes.enum.date),
139
- validations: z.array(jzodAttributeDateValidations),
140
- }).strict();
141
-
142
- export type JzodAttributeDateWithValidations = z.infer<typeof jzodAttributeDateWithValidations>;
143
-
144
- // ##############################################################################################################
145
- export const jzodAttributeNumberWithValidations = 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(jzodEnumElementTypes.enum.simpleType),
151
- definition: z.literal(jzodEnumAttributeTypes.enum.number),
152
- validations: z.array(jzodAttributeNumberValidations),
153
- }).strict();
154
-
155
- export type JzodAttributeNumberWithValidations = z.infer<typeof jzodAttributeNumberWithValidations>;
156
-
157
- // ##############################################################################################################
158
- export const jzodAttributeStringWithValidations = 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(jzodEnumElementTypes.enum.simpleType),
164
- definition: z.literal(jzodEnumAttributeTypes.enum.string),
165
- validations: z.array(jzodAttributeStringValidations),
166
- }).strict();
167
-
168
- export type JzodAttributeStringWithValidations = z.infer<typeof jzodAttributeStringWithValidations>;
169
-
170
- // ##############################################################################################################
171
- export const jzodAttribute = 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(jzodEnumElementTypes.enum.simpleType),
177
- definition: z.lazy(()=>jzodEnumAttributeTypes),
178
- }).strict();
179
-
180
- export type JzodAttribute = z.infer<typeof jzodAttribute>;
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 jzodElement: z.ZodType<JzodElement> = z.union([
206
- z.lazy(()=>jzodArray),
207
- z.lazy(()=>jzodAttribute),
208
- z.lazy(()=>jzodAttributeDateWithValidations),
209
- z.lazy(()=>jzodAttributeNumberWithValidations),
210
- z.lazy(()=>jzodAttributeStringWithValidations),
211
- z.lazy(()=>jzodEnum),
212
- z.lazy(()=>jzodFunction),
213
- z.lazy(()=>jzodLazy),
214
- z.lazy(()=>jzodLiteral),
215
- z.lazy(()=>jzodIntersection),
216
- z.lazy(()=>jzodMapSchema),
217
- z.lazy(()=>jzodObject),
218
- z.lazy(()=>jzodPromise),
219
- z.lazy(()=>jzodRecord),
220
- z.lazy(()=>jzodReference),
221
- z.lazy(()=>jzodSet),
222
- z.lazy(()=>jzodTuple),
223
- z.lazy(()=>jzodUnion),
224
- ])
225
-
226
- // // ##############################################################################################################
227
- // export const jzodElementSetSchema = z.record(z.string(),jzodElement);
228
- // export type JzodElementSet = z.infer<typeof jzodElementSetSchema>;
229
-
230
- // ##############################################################################################################
231
- export const jzodEnum = 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(jzodEnumElementTypes.enum.enum),
236
- definition: z.array(z.string()),
237
- }).strict();
238
-
239
- export type JzodEnum = z.infer<typeof jzodEnum>;
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 jzodFunction: ZodType<JzodFunction> = z.object({
250
- type: z.literal(jzodEnumElementTypes.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(jzodElement),
255
- returns: jzodElement.optional(),
256
- })
257
- }).strict();
258
-
259
- // ##############################################################################################################
260
- export const jzodLazy = z.object({
261
- type: z.literal(jzodEnumElementTypes.enum.lazy),
262
- extra: z.record(z.string(),z.any()).optional(),
263
- definition: jzodFunction,
264
- }).strict();
265
-
266
- export type JzodLazy = z.infer<typeof jzodLazy>;
267
-
268
- // ##############################################################################################################
269
- export const jzodLiteral = 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(jzodEnumElementTypes.enum.literal),
274
- definition: z.string(),
275
- }).strict();
276
-
277
- export type JzodLiteral = z.infer<typeof jzodLiteral>;
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 jzodIntersection: 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(jzodEnumElementTypes.enum.intersection),
293
- definition: z.lazy(()=>z.object({left: jzodElement, right: jzodElement}))
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([jzodElement, jzodElement]))
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 jzodObject: z.ZodType<JzodObject> = z.object({
326
- optional: z.boolean().optional(),
327
- nullable: z.boolean().optional(),
328
- extend: z.lazy(()=>z.union([jzodReference,jzodObject])).optional(),
329
- extra: z.record(z.string(),z.any()).optional(),
330
- type: z.literal(jzodEnumElementTypes.enum.object),
331
- nonStrict: z.boolean().optional(),
332
- definition: z.lazy(()=>z.record(z.string(),jzodElement)),
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 jzodPromise: 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(()=>jzodElement)
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 jzodRecord: 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(jzodEnumElementTypes.enum.record),
367
- definition: z.lazy(()=>jzodElement)
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 jzodReference: 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(jzodEnumElementTypes.enum.schemaReference),
389
- context: z.lazy(()=>z.record(z.string(),jzodElement)).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 jzodReference>;
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 jzodSet: 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(()=>jzodElement)
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 jzodTuple: 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(jzodEnumElementTypes.enum.tuple),
430
- definition: z.array(jzodElement),
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 jzodUnion: 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(jzodEnumElementTypes.enum.union),
447
- discriminator: z.string().optional(),
448
- definition: z.lazy(()=>z.array(jzodElement))
449
- }).strict();
450
-