@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/dist/bundle.js ADDED
@@ -0,0 +1,303 @@
1
+ import { printNode, withGetType, zodToTs, createTypeAlias } from 'zod-to-ts';
2
+ import { jzodElementSchemaToZodSchemaAndDescription } from '@miroir-framework/jzod';
3
+ import { z } from 'zod';
4
+
5
+ function printTsTypeAlias(typeAlias, exportPrefix = true) {
6
+ return (exportPrefix ? "export " : "") + printNode(typeAlias);
7
+ }
8
+ function printTsTypeAliases(typeAliases, exportPrefix = true) {
9
+ const result = Object.entries(typeAliases).reduce((acc, curr) => {
10
+ return `${acc}
11
+ ${printTsTypeAlias(curr[1], exportPrefix)}`;
12
+ }, "");
13
+ return result;
14
+ }
15
+
16
+ function jzodToTsTypeAliasesAndZodText(element, typeName) {
17
+ var _a, _b;
18
+ const elementZodSchemaAndDescription = jzodElementSchemaToZodSchemaAndDescription(element, () => ({}), () => ({}), (innerReference, relativeReference) => withGetType(innerReference, (ts) => {
19
+ return ts.factory.createTypeReferenceNode(ts.factory.createIdentifier(relativeReference ? relativeReference : "RELATIVEPATH_NOT_DEFINED"));
20
+ }));
21
+ const contextTsTypesString = Object.fromEntries(Object.entries((_a = elementZodSchemaAndDescription.contextZodSchema) !== null && _a !== void 0 ? _a : {}).map((curr) => {
22
+ const tsNode = zodToTs(curr[1], typeName).node;
23
+ const typeAlias = createTypeAlias(tsNode, curr[0]);
24
+ return [curr[0], typeAlias];
25
+ }));
26
+ const tsTypeStringNode = zodToTs(elementZodSchemaAndDescription.zodSchema, typeName).node;
27
+ const tsTypeStringTypeAlias = createTypeAlias(tsTypeStringNode, typeName !== null && typeName !== void 0 ? typeName : "");
28
+ return {
29
+ contextTsTypeAliases: contextTsTypesString,
30
+ contextZodText: (_b = elementZodSchemaAndDescription.contextZodText) !== null && _b !== void 0 ? _b : {},
31
+ mainZodText: elementZodSchemaAndDescription.zodText,
32
+ mainTsTypeAlias: tsTypeStringTypeAlias,
33
+ };
34
+ }
35
+ function jzodToTsCode(jzodElement, exportPrefix = true, typeName) {
36
+ const schemaName = typeName ? typeName.replace(/^(.)(.*)$/, (a, b, c) => b.toLowerCase() + c) : "";
37
+ const header = `import { ZodType, ZodTypeAny, z } from "zod";`;
38
+ const typeAliasesAndZodText = jzodToTsTypeAliasesAndZodText(jzodElement, typeName);
39
+ const bodyJsCode = `export const ${schemaName} = ${typeAliasesAndZodText.mainZodText};`;
40
+ const contextTsTypesString = printTsTypeAliases(typeAliasesAndZodText.contextTsTypeAliases, exportPrefix);
41
+ const contextJsCode = typeAliasesAndZodText.contextZodText
42
+ ? Object.entries(typeAliasesAndZodText.contextZodText).reduce((acc, curr) => {
43
+ return `${acc}
44
+ export const ${curr[0]}=${curr[1]};`;
45
+ }, "")
46
+ : "";
47
+ const tsTypesString = (exportPrefix ? "export " : "") + printNode(typeAliasesAndZodText.mainTsTypeAlias);
48
+ return `${header}
49
+ ${contextTsTypesString}
50
+ ${tsTypesString}
51
+ ${contextJsCode}
52
+ ${bodyJsCode}
53
+ `;
54
+ }
55
+
56
+ z.object({
57
+ optional: z.boolean().optional(),
58
+ }).strict();
59
+ const jzodEnumAttributeTypesSchema = z.enum([
60
+ "any",
61
+ "bigint",
62
+ "boolean",
63
+ "date",
64
+ "never",
65
+ "null",
66
+ "number",
67
+ "string",
68
+ "uuid",
69
+ "undefined",
70
+ "unknown",
71
+ "void",
72
+ ]);
73
+ const jzodEnumElementTypesSchema = z.enum([
74
+ "array",
75
+ "enum",
76
+ "function",
77
+ "lazy",
78
+ "literal",
79
+ "intersection",
80
+ "map",
81
+ "object",
82
+ "promise",
83
+ "record",
84
+ "schemaReference",
85
+ "set",
86
+ "simpleType",
87
+ "tuple",
88
+ "union",
89
+ ]);
90
+ const jzodArraySchema = z.object({
91
+ optional: z.boolean().optional(),
92
+ nullable: z.boolean().optional(),
93
+ extra: z.record(z.string(), z.any()).optional(),
94
+ type: z.literal('array'),
95
+ definition: z.lazy(() => jzodElementSchema)
96
+ }).strict();
97
+ const jzodAttributeDateValidationsSchema = z
98
+ .object({
99
+ extra: z.record(z.string(), z.any()).optional(),
100
+ type: z.enum([
101
+ "min",
102
+ "max",
103
+ ]),
104
+ parameter: z.any(),
105
+ })
106
+ .strict();
107
+ const jzodAttributeNumberValidationsSchema = z
108
+ .object({
109
+ extra: z.record(z.string(), z.any()).optional(),
110
+ type: z.enum([
111
+ "gt",
112
+ "gte",
113
+ "lt",
114
+ "lte",
115
+ "int",
116
+ "positive",
117
+ "nonpositive",
118
+ "negative",
119
+ "nonnegative",
120
+ "multipleOf",
121
+ "finite",
122
+ "safe",
123
+ ]),
124
+ parameter: z.any(),
125
+ })
126
+ .strict();
127
+ const jzodAttributeStringValidationsSchema = z
128
+ .object({
129
+ extra: z.record(z.string(), z.any()).optional(),
130
+ type: z.enum([
131
+ "max",
132
+ "min",
133
+ "length",
134
+ "email",
135
+ "url",
136
+ "emoji",
137
+ "uuid",
138
+ "cuid",
139
+ "cuid2",
140
+ "ulid",
141
+ "regex",
142
+ "includes",
143
+ "startsWith",
144
+ "endsWith",
145
+ "datetime",
146
+ "ip",
147
+ ]),
148
+ parameter: z.any(),
149
+ })
150
+ .strict();
151
+ const jzodAttributeDateWithValidationsSchema = z.object({
152
+ optional: z.boolean().optional(),
153
+ nullable: z.boolean().optional(),
154
+ extra: z.record(z.string(), z.any()).optional(),
155
+ coerce: z.boolean().optional(),
156
+ type: z.literal(jzodEnumElementTypesSchema.enum.simpleType),
157
+ definition: z.literal(jzodEnumAttributeTypesSchema.enum.date),
158
+ validations: z.array(jzodAttributeDateValidationsSchema),
159
+ }).strict();
160
+ const jzodAttributeNumberWithValidationsSchema = z.object({
161
+ optional: z.boolean().optional(),
162
+ nullable: z.boolean().optional(),
163
+ extra: z.record(z.string(), z.any()).optional(),
164
+ coerce: z.boolean().optional(),
165
+ type: z.literal(jzodEnumElementTypesSchema.enum.simpleType),
166
+ definition: z.literal(jzodEnumAttributeTypesSchema.enum.number),
167
+ validations: z.array(jzodAttributeNumberValidationsSchema),
168
+ }).strict();
169
+ const jzodAttributeStringWithValidationsSchema = z.object({
170
+ optional: z.boolean().optional(),
171
+ nullable: z.boolean().optional(),
172
+ extra: z.record(z.string(), z.any()).optional(),
173
+ coerce: z.boolean().optional(),
174
+ type: z.literal(jzodEnumElementTypesSchema.enum.simpleType),
175
+ definition: z.literal(jzodEnumAttributeTypesSchema.enum.string),
176
+ validations: z.array(jzodAttributeStringValidationsSchema),
177
+ }).strict();
178
+ const jzodAttributeSchema = z.object({
179
+ optional: z.boolean().optional(),
180
+ nullable: z.boolean().optional(),
181
+ extra: z.record(z.string(), z.any()).optional(),
182
+ coerce: z.boolean().optional(),
183
+ type: z.literal(jzodEnumElementTypesSchema.enum.simpleType),
184
+ definition: z.lazy(() => jzodEnumAttributeTypesSchema),
185
+ }).strict();
186
+ const jzodElementSchema = z.union([
187
+ z.lazy(() => jzodArraySchema),
188
+ z.lazy(() => jzodAttributeSchema),
189
+ z.lazy(() => jzodAttributeDateWithValidationsSchema),
190
+ z.lazy(() => jzodAttributeNumberWithValidationsSchema),
191
+ z.lazy(() => jzodAttributeStringWithValidationsSchema),
192
+ z.lazy(() => jzodEnumSchema),
193
+ z.lazy(() => jzodFunctionSchema),
194
+ z.lazy(() => jzodLazySchema),
195
+ z.lazy(() => jzodLiteralSchema),
196
+ z.lazy(() => jzodIntersectionSchema),
197
+ z.lazy(() => jzodMapSchema),
198
+ z.lazy(() => jzodObjectSchema),
199
+ z.lazy(() => jzodPromiseSchema),
200
+ z.lazy(() => jzodRecordSchema),
201
+ z.lazy(() => jzodReferenceSchema),
202
+ z.lazy(() => jzodSetSchema),
203
+ z.lazy(() => jzodTupleSchema),
204
+ z.lazy(() => jzodUnionSchema),
205
+ ]);
206
+ const jzodEnumSchema = z.object({
207
+ optional: z.boolean().optional(),
208
+ nullable: z.boolean().optional(),
209
+ extra: z.record(z.string(), z.any()).optional(),
210
+ type: z.literal(jzodEnumElementTypesSchema.enum.enum),
211
+ definition: z.array(z.string()),
212
+ }).strict();
213
+ const jzodFunctionSchema = z.object({
214
+ type: z.literal(jzodEnumElementTypesSchema.enum.function),
215
+ extra: z.record(z.string(), z.any()).optional(),
216
+ definition: z.object({
217
+ args: z.array(jzodElementSchema),
218
+ returns: jzodElementSchema.optional(),
219
+ })
220
+ }).strict();
221
+ const jzodLazySchema = z.object({
222
+ type: z.literal(jzodEnumElementTypesSchema.enum.lazy),
223
+ extra: z.record(z.string(), z.any()).optional(),
224
+ definition: jzodFunctionSchema,
225
+ }).strict();
226
+ const jzodLiteralSchema = z.object({
227
+ optional: z.boolean().optional(),
228
+ nullable: z.boolean().optional(),
229
+ extra: z.record(z.string(), z.any()).optional(),
230
+ type: z.literal(jzodEnumElementTypesSchema.enum.literal),
231
+ definition: z.string(),
232
+ }).strict();
233
+ const jzodIntersectionSchema = z.object({
234
+ optional: z.boolean().optional(),
235
+ nullable: z.boolean().optional(),
236
+ extra: z.record(z.string(), z.any()).optional(),
237
+ type: z.literal(jzodEnumElementTypesSchema.enum.intersection),
238
+ definition: z.lazy(() => z.object({ left: jzodElementSchema, right: jzodElementSchema }))
239
+ }).strict();
240
+ const jzodMapSchema = z.object({
241
+ optional: z.boolean().optional(),
242
+ nullable: z.boolean().optional(),
243
+ extra: z.record(z.string(), z.any()).optional(),
244
+ type: z.literal('map'),
245
+ definition: z.lazy(() => z.tuple([jzodElementSchema, jzodElementSchema]))
246
+ }).strict();
247
+ const jzodObjectSchema = z.object({
248
+ optional: z.boolean().optional(),
249
+ nullable: z.boolean().optional(),
250
+ extend: z.lazy(() => z.union([jzodReferenceSchema, jzodObjectSchema])).optional(),
251
+ extra: z.record(z.string(), z.any()).optional(),
252
+ type: z.literal(jzodEnumElementTypesSchema.enum.object),
253
+ nonStrict: z.boolean().optional(),
254
+ definition: z.lazy(() => z.record(z.string(), jzodElementSchema)),
255
+ }).strict();
256
+ const jzodPromiseSchema = z.object({
257
+ extra: z.record(z.string(), z.any()).optional(),
258
+ type: z.literal('promise'),
259
+ definition: z.lazy(() => jzodElementSchema)
260
+ }).strict();
261
+ const jzodRecordSchema = z.object({
262
+ optional: z.boolean().optional(),
263
+ nullable: z.boolean().optional(),
264
+ extra: z.record(z.string(), z.any()).optional(),
265
+ type: z.literal(jzodEnumElementTypesSchema.enum.record),
266
+ definition: z.lazy(() => jzodElementSchema)
267
+ }).strict();
268
+ const jzodReferenceSchema = z.object({
269
+ optional: z.boolean().optional(),
270
+ nullable: z.boolean().optional(),
271
+ extra: z.record(z.string(), z.any()).optional(),
272
+ type: z.literal(jzodEnumElementTypesSchema.enum.schemaReference),
273
+ context: z.lazy(() => z.record(z.string(), jzodElementSchema)).optional(),
274
+ definition: z.object({
275
+ eager: z.boolean().optional(),
276
+ relativePath: z.string().optional(),
277
+ absolutePath: z.string().optional(),
278
+ })
279
+ }).strict();
280
+ const jzodSetSchema = z.object({
281
+ optional: z.boolean().optional(),
282
+ nullable: z.boolean().optional(),
283
+ extra: z.record(z.string(), z.any()).optional(),
284
+ type: z.literal('set'),
285
+ definition: z.lazy(() => jzodElementSchema)
286
+ }).strict();
287
+ const jzodTupleSchema = z.object({
288
+ optional: z.boolean().optional(),
289
+ nullable: z.boolean().optional(),
290
+ extra: z.record(z.string(), z.any()).optional(),
291
+ type: z.literal(jzodEnumElementTypesSchema.enum.tuple),
292
+ definition: z.array(jzodElementSchema),
293
+ }).strict();
294
+ const jzodUnionSchema = z.object({
295
+ optional: z.boolean().optional(),
296
+ nullable: z.boolean().optional(),
297
+ extra: z.record(z.string(), z.any()).optional(),
298
+ type: z.literal(jzodEnumElementTypesSchema.enum.union),
299
+ discriminator: z.string().optional(),
300
+ definition: z.lazy(() => z.array(jzodElementSchema))
301
+ }).strict();
302
+
303
+ export { jzodArraySchema, jzodAttributeDateValidationsSchema, jzodAttributeDateWithValidationsSchema, jzodAttributeNumberValidationsSchema, jzodAttributeNumberWithValidationsSchema, jzodAttributeSchema, jzodAttributeStringValidationsSchema, jzodAttributeStringWithValidationsSchema, jzodElementSchema, jzodEnumAttributeTypesSchema, jzodEnumElementTypesSchema, jzodEnumSchema, jzodFunctionSchema, jzodIntersectionSchema, jzodLazySchema, jzodLiteralSchema, jzodMapSchema, jzodObjectSchema, jzodPromiseSchema, jzodRecordSchema, jzodReferenceSchema, jzodSetSchema, jzodToTsCode, jzodToTsTypeAliasesAndZodText, jzodTupleSchema, jzodUnionSchema, printTsTypeAlias, printTsTypeAliases };
@@ -0,0 +1,17 @@
1
+ import ts from "typescript";
2
+ import { JzodElement } from "./JzodTsInterface";
3
+ export type TsTypeAliases = {
4
+ [k: string]: ts.TypeAliasDeclaration;
5
+ };
6
+ export interface TsTypeAliasesAndZodText {
7
+ contextTsTypeAliases: {
8
+ [k: string]: ts.TypeAliasDeclaration;
9
+ };
10
+ contextZodText: {
11
+ [k: string]: string;
12
+ };
13
+ mainTsTypeAlias: ts.TypeAliasDeclaration;
14
+ mainZodText: string;
15
+ }
16
+ export declare function jzodToTsTypeAliasesAndZodText(element: JzodElement, typeName?: string): TsTypeAliasesAndZodText;
17
+ export declare function jzodToTsCode(jzodElement: JzodElement, exportPrefix?: boolean, typeName?: string): string;