zod-to-x 1.4.7-dev.1 → 1.4.7-dev.6
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/core/ast_node.js +12 -1
- package/dist/layered-modeling/layer.js +3 -3
- package/dist/lib/zod_ext.d.ts +7 -3
- package/dist/lib/zod_helpers.d.ts +3 -0
- package/dist/lib/zod_helpers.js +50 -23
- package/dist/transpilers/cpp/runner.d.ts +3 -1
- package/dist/transpilers/cpp/runner.js +30 -29
- package/dist/transpilers/typescript/runner.d.ts +3 -1
- package/dist/transpilers/typescript/runner.js +28 -29
- package/package.json +6 -6
package/dist/core/ast_node.js
CHANGED
|
@@ -58,6 +58,11 @@ class Zod2Ast {
|
|
|
58
58
|
aliasOf: metadata === null || metadata === void 0 ? void 0 : metadata.aliasOf,
|
|
59
59
|
};
|
|
60
60
|
}
|
|
61
|
+
else {
|
|
62
|
+
return {
|
|
63
|
+
aliasOf: metadata === null || metadata === void 0 ? void 0 : metadata.aliasOf,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
61
66
|
}
|
|
62
67
|
else {
|
|
63
68
|
// Case 3: Only layer exists and belongs to a different file
|
|
@@ -266,6 +271,9 @@ class Zod2Ast {
|
|
|
266
271
|
for (const key in shape) {
|
|
267
272
|
properties[key] = this._zodToAST(shape[key]);
|
|
268
273
|
}
|
|
274
|
+
if (opt === null || opt === void 0 ? void 0 : opt.skipLayerClass) {
|
|
275
|
+
return {}; // Layer classes are not transpilerable
|
|
276
|
+
}
|
|
269
277
|
this.nodes.set(name, new core_1.ASTObject({
|
|
270
278
|
name,
|
|
271
279
|
properties,
|
|
@@ -558,7 +566,10 @@ class Zod2Ast {
|
|
|
558
566
|
* @returns Transpilerable nodes.
|
|
559
567
|
*/
|
|
560
568
|
build(schema) {
|
|
561
|
-
|
|
569
|
+
var _a, _b, _c;
|
|
570
|
+
this._zodToAST(schema, {
|
|
571
|
+
skipLayerClass: ((_a = this.opt) === null || _a === void 0 ? void 0 : _a.layer) !== undefined && ((_c = (_b = this.opt) === null || _b === void 0 ? void 0 : _b.layer) === null || _c === void 0 ? void 0 : _c.skipLayerInterface) !== false,
|
|
572
|
+
});
|
|
562
573
|
while (this.lazyPointers.length > 0) {
|
|
563
574
|
const [pointer, schema] = this.lazyPointers.shift();
|
|
564
575
|
const lazyResolve = this._zodToAST(schema);
|
|
@@ -81,7 +81,6 @@ function Layer(opt) {
|
|
|
81
81
|
zodItem["_zod2x"] = metadata;
|
|
82
82
|
}
|
|
83
83
|
else if (!metadata.typeName) {
|
|
84
|
-
// Only possible if `zod2xExtendable` is used.
|
|
85
84
|
metadata.typeName = name;
|
|
86
85
|
}
|
|
87
86
|
if (metadata.layer === undefined) {
|
|
@@ -89,8 +88,9 @@ function Layer(opt) {
|
|
|
89
88
|
// zod2x was used before.
|
|
90
89
|
metadata.layer = opt;
|
|
91
90
|
}
|
|
92
|
-
if (opt.externalInheritance !== false &&
|
|
93
|
-
|
|
91
|
+
if (opt.externalInheritance !== false &&
|
|
92
|
+
(metadata.layer.file !== opt.file || name !== metadata.typeName)) {
|
|
93
|
+
// Using an existing type. A new type is created that inherits from the
|
|
94
94
|
// original type.
|
|
95
95
|
zodItem = zod_helpers_1.ZodHelpers.cloneZod(zodItem);
|
|
96
96
|
zodItem._zod2x = {
|
package/dist/lib/zod_ext.d.ts
CHANGED
|
@@ -60,6 +60,11 @@ export interface IZod2xLayerMetadata {
|
|
|
60
60
|
* intersection). Default is true.
|
|
61
61
|
*/
|
|
62
62
|
basicTypes?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Indicates if the layer class should be included as another node for transpilation.
|
|
65
|
+
* Default is true.
|
|
66
|
+
*/
|
|
67
|
+
skipLayerInterface?: boolean;
|
|
63
68
|
}
|
|
64
69
|
export interface IZod2xMetadata {
|
|
65
70
|
/**
|
|
@@ -78,9 +83,8 @@ export interface IZod2xMetadata {
|
|
|
78
83
|
layer?: IZod2xLayerMetadata;
|
|
79
84
|
/**
|
|
80
85
|
* For Layered Modeling.
|
|
81
|
-
* When a type of another file is used without modifying it,
|
|
82
|
-
*
|
|
83
|
-
* which will be the extension of the original type if `zod2xExtendable` is used.
|
|
86
|
+
* When a type of another file is used without modifying it, it is sustituted by its import
|
|
87
|
+
* instead of creating a new type.
|
|
84
88
|
*/
|
|
85
89
|
aliasOf?: string;
|
|
86
90
|
parentLayer?: IZod2xLayerMetadata;
|
|
@@ -7,6 +7,9 @@ type ZodNumberConstraints = {
|
|
|
7
7
|
max?: number;
|
|
8
8
|
isInt: boolean;
|
|
9
9
|
};
|
|
10
|
+
/**
|
|
11
|
+
* Zod's typeName is checked insted of instanceof to resolve Bun incomatibilities.
|
|
12
|
+
*/
|
|
10
13
|
export declare class ZodHelpers {
|
|
11
14
|
static isZodType(i: ZodTypeAny): boolean;
|
|
12
15
|
static isZodAny(i: ZodTypeAny): i is z.ZodAny;
|
package/dist/lib/zod_helpers.js
CHANGED
|
@@ -3,75 +3,102 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ZodHelpers = void 0;
|
|
4
4
|
const zod_1 = require("zod");
|
|
5
5
|
const zod_ext_1 = require("./zod_ext");
|
|
6
|
+
/**
|
|
7
|
+
* Zod's typeName is checked insted of instanceof to resolve Bun incomatibilities.
|
|
8
|
+
*/
|
|
6
9
|
class ZodHelpers {
|
|
7
10
|
static isZodType(i) {
|
|
8
|
-
|
|
11
|
+
var _a;
|
|
12
|
+
return zod_1.ZodFirstPartyTypeKind[(_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName] !== undefined;
|
|
9
13
|
}
|
|
10
14
|
static isZodAny(i) {
|
|
11
|
-
|
|
15
|
+
var _a;
|
|
16
|
+
return ((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) === zod_1.ZodFirstPartyTypeKind.ZodAny;
|
|
12
17
|
}
|
|
13
18
|
static isZodString(i) {
|
|
14
|
-
|
|
19
|
+
var _a;
|
|
20
|
+
return ((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) === zod_1.ZodFirstPartyTypeKind.ZodString;
|
|
15
21
|
}
|
|
16
22
|
static isZodNumber(i) {
|
|
17
|
-
|
|
23
|
+
var _a;
|
|
24
|
+
return ((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) === zod_1.ZodFirstPartyTypeKind.ZodNumber;
|
|
18
25
|
}
|
|
19
26
|
static isZodBigInt(i) {
|
|
20
|
-
|
|
27
|
+
var _a;
|
|
28
|
+
return ((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) === zod_1.ZodFirstPartyTypeKind.ZodBigInt;
|
|
21
29
|
}
|
|
22
30
|
static isZodLiteral(i) {
|
|
23
|
-
|
|
31
|
+
var _a;
|
|
32
|
+
return ((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) === zod_1.ZodFirstPartyTypeKind.ZodLiteral;
|
|
24
33
|
}
|
|
25
34
|
static isZodBoolean(i) {
|
|
26
|
-
|
|
35
|
+
var _a;
|
|
36
|
+
return ((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) === zod_1.ZodFirstPartyTypeKind.ZodBoolean;
|
|
27
37
|
}
|
|
28
38
|
static isZodDate(i) {
|
|
29
|
-
|
|
39
|
+
var _a;
|
|
40
|
+
return ((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) === zod_1.ZodFirstPartyTypeKind.ZodDate;
|
|
30
41
|
}
|
|
31
42
|
static isZodEnum(i) {
|
|
32
|
-
|
|
43
|
+
var _a;
|
|
44
|
+
return ((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) === zod_1.ZodFirstPartyTypeKind.ZodEnum;
|
|
33
45
|
}
|
|
34
46
|
static isZodUnion(i) {
|
|
35
|
-
|
|
47
|
+
var _a;
|
|
48
|
+
return ((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) === zod_1.ZodFirstPartyTypeKind.ZodUnion;
|
|
36
49
|
}
|
|
37
50
|
static isZodDiscriminatedUnion(i) {
|
|
38
|
-
|
|
51
|
+
var _a;
|
|
52
|
+
return (((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) ===
|
|
53
|
+
zod_1.ZodFirstPartyTypeKind.ZodDiscriminatedUnion);
|
|
39
54
|
}
|
|
40
55
|
static isZodNativeEnum(i) {
|
|
41
|
-
|
|
56
|
+
var _a;
|
|
57
|
+
return ((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) === zod_1.ZodFirstPartyTypeKind.ZodNativeEnum;
|
|
42
58
|
}
|
|
43
59
|
static isZodIntersection(i) {
|
|
44
|
-
|
|
60
|
+
var _a;
|
|
61
|
+
return (((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) === zod_1.ZodFirstPartyTypeKind.ZodIntersection);
|
|
45
62
|
}
|
|
46
63
|
static isZodObject(i) {
|
|
47
|
-
|
|
64
|
+
var _a;
|
|
65
|
+
return ((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) === zod_1.ZodFirstPartyTypeKind.ZodObject;
|
|
48
66
|
}
|
|
49
67
|
static isZodLazy(i) {
|
|
50
|
-
|
|
68
|
+
var _a;
|
|
69
|
+
return ((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) === zod_1.ZodFirstPartyTypeKind.ZodLazy;
|
|
51
70
|
}
|
|
52
71
|
static isZodRecord(i) {
|
|
53
|
-
|
|
72
|
+
var _a;
|
|
73
|
+
return ((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) === zod_1.ZodFirstPartyTypeKind.ZodRecord;
|
|
54
74
|
}
|
|
55
75
|
static isZodMap(i) {
|
|
56
|
-
|
|
76
|
+
var _a;
|
|
77
|
+
return ((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) === zod_1.ZodFirstPartyTypeKind.ZodMap;
|
|
57
78
|
}
|
|
58
79
|
static isZodArray(i) {
|
|
59
|
-
|
|
80
|
+
var _a;
|
|
81
|
+
return ((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) === zod_1.ZodFirstPartyTypeKind.ZodArray;
|
|
60
82
|
}
|
|
61
83
|
static isZodSet(i) {
|
|
62
|
-
|
|
84
|
+
var _a;
|
|
85
|
+
return ((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) === zod_1.ZodFirstPartyTypeKind.ZodSet;
|
|
63
86
|
}
|
|
64
87
|
static isZodTuple(i) {
|
|
65
|
-
|
|
88
|
+
var _a;
|
|
89
|
+
return ((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) === zod_1.ZodFirstPartyTypeKind.ZodTuple;
|
|
66
90
|
}
|
|
67
91
|
static isZodOptional(i) {
|
|
68
|
-
|
|
92
|
+
var _a;
|
|
93
|
+
return ((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) === zod_1.ZodFirstPartyTypeKind.ZodOptional;
|
|
69
94
|
}
|
|
70
95
|
static isZodNullable(i) {
|
|
71
|
-
|
|
96
|
+
var _a;
|
|
97
|
+
return ((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) === zod_1.ZodFirstPartyTypeKind.ZodNullable;
|
|
72
98
|
}
|
|
73
99
|
static isZodDefault(i) {
|
|
74
|
-
|
|
100
|
+
var _a;
|
|
101
|
+
return ((_a = i === null || i === void 0 ? void 0 : i._def) === null || _a === void 0 ? void 0 : _a.typeName) === zod_1.ZodFirstPartyTypeKind.ZodDefault;
|
|
75
102
|
}
|
|
76
103
|
static isZodAnyUnionType(i) {
|
|
77
104
|
return this.isZodUnion(i) || this.isZodDiscriminatedUnion(i);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ASTAliasedTypes, ASTEnum, ASTIntersection, ASTObject, ASTUnion, Zod2X } from "../../core";
|
|
1
|
+
import { ASTAliasedTypes, ASTEnum, ASTIntersection, ASTNode, ASTObject, ASTUnion, Zod2X } from "../../core";
|
|
2
2
|
import { IZod2CppOpt } from "./options";
|
|
3
3
|
/**
|
|
4
4
|
* @description Transpiler for Zod schemas to C++11 code.
|
|
@@ -25,7 +25,9 @@ export declare class Zod2Cpp extends Zod2X<IZod2CppOpt> {
|
|
|
25
25
|
protected getTypeFromExternalNamespace(namespace: string, typeName: string): string;
|
|
26
26
|
protected addExtendedType(name: string, parentNamespace: string, aliasOf: string, opt?: {
|
|
27
27
|
type?: "union" | "alias";
|
|
28
|
+
isInternal?: boolean;
|
|
28
29
|
}): void;
|
|
30
|
+
protected checkExtendedTypeInclusion(data: ASTNode, type?: "union" | "alias"): boolean;
|
|
29
31
|
protected runAfter(): void;
|
|
30
32
|
protected getComment: (data: string, indent?: string) => string;
|
|
31
33
|
protected getDateType: () => string;
|
|
@@ -44,7 +44,8 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
44
44
|
/** Ex: boost::variant<TypeA, TypeB> */
|
|
45
45
|
this.getUnionType = (itemsType) => {
|
|
46
46
|
this.imports.add(this.lib.variant);
|
|
47
|
-
|
|
47
|
+
const unionLines = itemsType.map((type) => `${this.indent[2]}${type}`).join(",\n");
|
|
48
|
+
return `boost::variant<\n${unionLines}\n${this.indent[1]}>`;
|
|
48
49
|
};
|
|
49
50
|
/** Ex: depends on number range (if any). One of:
|
|
50
51
|
* - std::uint32_t
|
|
@@ -94,7 +95,9 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
94
95
|
return `${namespace}::${typeName}`;
|
|
95
96
|
}
|
|
96
97
|
addExtendedType(name, parentNamespace, aliasOf, opt) {
|
|
97
|
-
const extendedType =
|
|
98
|
+
const extendedType = (opt === null || opt === void 0 ? void 0 : opt.isInternal)
|
|
99
|
+
? aliasOf
|
|
100
|
+
: this.getTypeFromExternalNamespace(parentNamespace, aliasOf);
|
|
98
101
|
if ((opt === null || opt === void 0 ? void 0 : opt.type) === "union" || (opt === null || opt === void 0 ? void 0 : opt.type) === "alias") {
|
|
99
102
|
this.push0(`using ${name} = ${extendedType};\n`);
|
|
100
103
|
}
|
|
@@ -107,6 +110,24 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
107
110
|
}
|
|
108
111
|
}
|
|
109
112
|
}
|
|
113
|
+
checkExtendedTypeInclusion(data, type) {
|
|
114
|
+
if (this.isExternalTypeImport(data)) {
|
|
115
|
+
if (data.aliasOf) {
|
|
116
|
+
this.addExtendedType(data.name, data.parentNamespace, data.aliasOf, {
|
|
117
|
+
type,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
else if (data.aliasOf) {
|
|
123
|
+
this.addExtendedType(data.name, data.parentNamespace, data.aliasOf, {
|
|
124
|
+
type,
|
|
125
|
+
isInternal: true,
|
|
126
|
+
});
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
110
131
|
runAfter() {
|
|
111
132
|
this.preImports.add("#pragma once");
|
|
112
133
|
// Add the output inside the namespace
|
|
@@ -159,12 +180,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
159
180
|
return `boost::optional<${type}>`;
|
|
160
181
|
}
|
|
161
182
|
transpileAliasedType(data) {
|
|
162
|
-
if (this.
|
|
163
|
-
if (data.aliasOf) {
|
|
164
|
-
this.addExtendedType(data.name, data.parentNamespace, data.aliasOf, {
|
|
165
|
-
type: "alias",
|
|
166
|
-
});
|
|
167
|
-
}
|
|
183
|
+
if (this.checkExtendedTypeInclusion(data, "alias")) {
|
|
168
184
|
return;
|
|
169
185
|
}
|
|
170
186
|
let extendedType = undefined;
|
|
@@ -185,12 +201,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
185
201
|
* }
|
|
186
202
|
*/
|
|
187
203
|
transpileEnum(data) {
|
|
188
|
-
if (this.
|
|
189
|
-
if (data.aliasOf) {
|
|
190
|
-
this.addExtendedType(data.name, data.parentNamespace, data.aliasOf, {
|
|
191
|
-
type: "alias",
|
|
192
|
-
});
|
|
193
|
-
}
|
|
204
|
+
if (this.checkExtendedTypeInclusion(data, "alias")) {
|
|
194
205
|
return;
|
|
195
206
|
}
|
|
196
207
|
this.addComment(data.description);
|
|
@@ -220,10 +231,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
220
231
|
* }
|
|
221
232
|
*/
|
|
222
233
|
transpileIntersection(data) {
|
|
223
|
-
if (this.
|
|
224
|
-
if (data.aliasOf) {
|
|
225
|
-
this.addExtendedType(data.name, data.parentNamespace, data.aliasOf);
|
|
226
|
-
}
|
|
234
|
+
if (this.checkExtendedTypeInclusion(data)) {
|
|
227
235
|
return;
|
|
228
236
|
}
|
|
229
237
|
console.warn(`${data.name}: use zod's merge instead of intersection whenever possible.`);
|
|
@@ -253,12 +261,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
253
261
|
}
|
|
254
262
|
/** Ex: using TypeC = boost::variant<TypeA, TypeB> */
|
|
255
263
|
transpileUnion(data) {
|
|
256
|
-
if (this.
|
|
257
|
-
if (data.aliasOf) {
|
|
258
|
-
this.addExtendedType(data.name, data.parentNamespace, data.aliasOf, {
|
|
259
|
-
type: "union",
|
|
260
|
-
});
|
|
261
|
-
}
|
|
264
|
+
if (this.checkExtendedTypeInclusion(data, "union")) {
|
|
262
265
|
return;
|
|
263
266
|
}
|
|
264
267
|
this.addComment(data.description);
|
|
@@ -275,10 +278,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
275
278
|
this._createUnionDeserializer(data.name, attributesData, data.discriminantKey);
|
|
276
279
|
}
|
|
277
280
|
transpileStruct(data) {
|
|
278
|
-
if (this.
|
|
279
|
-
if (data.aliasOf) {
|
|
280
|
-
this.addExtendedType(data.name, data.parentNamespace, data.aliasOf);
|
|
281
|
-
}
|
|
281
|
+
if (this.checkExtendedTypeInclusion(data)) {
|
|
282
282
|
return;
|
|
283
283
|
}
|
|
284
284
|
this.addComment(data.description);
|
|
@@ -730,7 +730,8 @@ class Zod2Cpp17 extends Zod2Cpp {
|
|
|
730
730
|
/** Ex: std::variant<TypeA, TypeB> */
|
|
731
731
|
this.getUnionType = (itemsType) => {
|
|
732
732
|
this.imports.add(this.lib.variant);
|
|
733
|
-
|
|
733
|
+
const unionLines = itemsType.map((type) => `${this.indent[2]}${type}`).join(",\n");
|
|
734
|
+
return `std::variant<\n${unionLines}\n${this.indent[1]}>`;
|
|
734
735
|
};
|
|
735
736
|
this.useBoost = false;
|
|
736
737
|
this.lib = (0, libs_1.getLibs)(this.useBoost);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ASTAliasedTypes, ASTEnum, ASTIntersection, ASTObject, ASTUnion, Zod2X } from "../../core";
|
|
1
|
+
import { ASTAliasedTypes, ASTEnum, ASTIntersection, ASTNode, ASTObject, ASTUnion, Zod2X } from "../../core";
|
|
2
2
|
import { IZod2TsOpt } from "./options";
|
|
3
3
|
export declare class Zod2Ts extends Zod2X<IZod2TsOpt> {
|
|
4
4
|
constructor(opt?: IZod2TsOpt);
|
|
@@ -8,7 +8,9 @@ export declare class Zod2Ts extends Zod2X<IZod2TsOpt> {
|
|
|
8
8
|
protected getTypeFromExternalNamespace(namespace: string, typeName: string): string;
|
|
9
9
|
protected addExtendedType(name: string, parentNamespace: string, aliasOf: string, opt?: {
|
|
10
10
|
type?: "union" | "d-union" | "alias";
|
|
11
|
+
isInternal?: boolean;
|
|
11
12
|
}): void;
|
|
13
|
+
protected checkExtendedTypeInclusion(data: ASTNode, type?: "alias" | "union" | "d-union"): boolean;
|
|
12
14
|
protected getComment: (data: string, indent?: string) => string;
|
|
13
15
|
protected getAnyType: () => string;
|
|
14
16
|
protected getBooleanType: () => string;
|
|
@@ -20,7 +20,7 @@ class Zod2Ts extends core_1.Zod2X {
|
|
|
20
20
|
/** Ex: [TypeA, TypeB] */
|
|
21
21
|
this.getTupleType = (itemsType) => `[${itemsType.join(", ")}]`;
|
|
22
22
|
/** Ex: TypeA | TypeB */
|
|
23
|
-
this.getUnionType = (itemsType) => itemsType.
|
|
23
|
+
this.getUnionType = (itemsType) => itemsType.map((type) => `${this.indent[1]}| ${type}`).join("\n");
|
|
24
24
|
/** Ex: TypeA & TypeB */
|
|
25
25
|
this.getIntersectionType = (itemsType) => itemsType.join(" & ");
|
|
26
26
|
this.getNumberType = () => "number";
|
|
@@ -37,7 +37,9 @@ class Zod2Ts extends core_1.Zod2X {
|
|
|
37
37
|
return `${namespace}.${typeName}`;
|
|
38
38
|
}
|
|
39
39
|
addExtendedType(name, parentNamespace, aliasOf, opt) {
|
|
40
|
-
const extendedType =
|
|
40
|
+
const extendedType = (opt === null || opt === void 0 ? void 0 : opt.isInternal)
|
|
41
|
+
? aliasOf
|
|
42
|
+
: this.getTypeFromExternalNamespace(parentNamespace, aliasOf);
|
|
41
43
|
if ((opt === null || opt === void 0 ? void 0 : opt.type) === "alias") {
|
|
42
44
|
this.push0(`export type ${name} = ${extendedType};\n`);
|
|
43
45
|
}
|
|
@@ -58,6 +60,24 @@ class Zod2Ts extends core_1.Zod2X {
|
|
|
58
60
|
}
|
|
59
61
|
}
|
|
60
62
|
}
|
|
63
|
+
checkExtendedTypeInclusion(data, type) {
|
|
64
|
+
if (this.isExternalTypeImport(data)) {
|
|
65
|
+
if (data.aliasOf) {
|
|
66
|
+
this.addExtendedType(data.name, data.parentNamespace, data.aliasOf, {
|
|
67
|
+
type,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
else if (data.aliasOf) {
|
|
73
|
+
this.addExtendedType(data.name, data.parentNamespace, data.aliasOf, {
|
|
74
|
+
type,
|
|
75
|
+
isInternal: true,
|
|
76
|
+
});
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
61
81
|
/** Ex: Array<Array<TypeA[]>> */
|
|
62
82
|
getArrayType(arrayType, arrayDeep) {
|
|
63
83
|
let output = arrayType.includes("|") || arrayType.includes("&")
|
|
@@ -84,12 +104,7 @@ class Zod2Ts extends core_1.Zod2X {
|
|
|
84
104
|
return `Record<${keyType}, ${valueType}>`;
|
|
85
105
|
}
|
|
86
106
|
transpileAliasedType(data) {
|
|
87
|
-
if (this.
|
|
88
|
-
if (data.aliasOf) {
|
|
89
|
-
this.addExtendedType(data.name, data.parentNamespace, data.aliasOf, {
|
|
90
|
-
type: "alias",
|
|
91
|
-
});
|
|
92
|
-
}
|
|
107
|
+
if (this.checkExtendedTypeInclusion(data, "alias")) {
|
|
93
108
|
return;
|
|
94
109
|
}
|
|
95
110
|
let extendedType = undefined;
|
|
@@ -110,12 +125,7 @@ class Zod2Ts extends core_1.Zod2X {
|
|
|
110
125
|
* }
|
|
111
126
|
*/
|
|
112
127
|
transpileEnum(data) {
|
|
113
|
-
if (this.
|
|
114
|
-
if (data.aliasOf) {
|
|
115
|
-
this.addExtendedType(data.name, data.parentNamespace, data.aliasOf, {
|
|
116
|
-
type: "alias",
|
|
117
|
-
});
|
|
118
|
-
}
|
|
128
|
+
if (this.checkExtendedTypeInclusion(data, "alias")) {
|
|
119
129
|
return;
|
|
120
130
|
}
|
|
121
131
|
this.addComment(data.description);
|
|
@@ -147,10 +157,7 @@ class Zod2Ts extends core_1.Zod2X {
|
|
|
147
157
|
* */
|
|
148
158
|
transpileIntersection(data) {
|
|
149
159
|
var _a;
|
|
150
|
-
if (this.
|
|
151
|
-
if (data.aliasOf) {
|
|
152
|
-
this.addExtendedType(data.name, data.parentNamespace, data.aliasOf);
|
|
153
|
-
}
|
|
160
|
+
if (this.checkExtendedTypeInclusion(data)) {
|
|
154
161
|
return;
|
|
155
162
|
}
|
|
156
163
|
if (this.opt.outType === "class" && data.newObject) {
|
|
@@ -164,10 +171,7 @@ class Zod2Ts extends core_1.Zod2X {
|
|
|
164
171
|
}
|
|
165
172
|
}
|
|
166
173
|
transpileStruct(data) {
|
|
167
|
-
if (this.
|
|
168
|
-
if (data.aliasOf) {
|
|
169
|
-
this.addExtendedType(data.name, data.parentNamespace, data.aliasOf);
|
|
170
|
-
}
|
|
174
|
+
if (this.checkExtendedTypeInclusion(data)) {
|
|
171
175
|
return;
|
|
172
176
|
}
|
|
173
177
|
this.addComment(data.description);
|
|
@@ -195,12 +199,7 @@ class Zod2Ts extends core_1.Zod2X {
|
|
|
195
199
|
* */
|
|
196
200
|
transpileUnion(data) {
|
|
197
201
|
var _a;
|
|
198
|
-
if (this.
|
|
199
|
-
if (data.aliasOf) {
|
|
200
|
-
this.addExtendedType(data.name, data.parentNamespace, data.aliasOf, {
|
|
201
|
-
type: data.discriminantKey === undefined ? "union" : "d-union",
|
|
202
|
-
});
|
|
203
|
-
}
|
|
202
|
+
if (this.checkExtendedTypeInclusion(data, data.discriminantKey === undefined ? "union" : "d-union")) {
|
|
204
203
|
return;
|
|
205
204
|
}
|
|
206
205
|
if (this.opt.outType === "class" && data.newObject) {
|
|
@@ -210,7 +209,7 @@ class Zod2Ts extends core_1.Zod2X {
|
|
|
210
209
|
else {
|
|
211
210
|
this.addComment(data.description);
|
|
212
211
|
const attributesTypes = data.options.map(this.getAttributeType.bind(this));
|
|
213
|
-
this.push0(`export type ${data.name}
|
|
212
|
+
this.push0(`export type ${data.name} =\n${this.getUnionType(attributesTypes)};\n`);
|
|
214
213
|
}
|
|
215
214
|
}
|
|
216
215
|
/** Ex:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zod-to-x",
|
|
3
|
-
"version": "1.4.7-dev.
|
|
3
|
+
"version": "1.4.7-dev.6",
|
|
4
4
|
"description": "Multi language types generation from Zod schemas.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"files": [
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"build": "npm run clear_dist && tsc && tsc-alias",
|
|
12
12
|
"build:debug": "npm run clear_dist && tsc --project tsconfig.dev.json && tsc-alias",
|
|
13
13
|
"format:check": "prettier --check .",
|
|
14
|
-
"test": "find ./test -name \"err-*\" -delete &&
|
|
14
|
+
"test": "find ./test -name \"err-*\" -delete && vitest --run",
|
|
15
15
|
"test:cpp": "bash ./test/test_zod2cpp/test_cpp.sh",
|
|
16
16
|
"ts-run": "ts-node -r tsconfig-paths/register",
|
|
17
17
|
"prepare": "husky"
|
|
@@ -51,18 +51,18 @@
|
|
|
51
51
|
"case": "1.6.3"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
|
-
"@
|
|
54
|
+
"@vitest/coverage-v8": "^3.1.3",
|
|
55
|
+
"@vitest/ui": "^3.1.3",
|
|
56
|
+
"diff": "^8.0.1",
|
|
55
57
|
"husky": "^9.1.7",
|
|
56
|
-
"jest": "^29.7.0",
|
|
57
|
-
"jest-diff": "^29.7.0",
|
|
58
58
|
"lint-staged": "^15.2.11",
|
|
59
59
|
"prettier": "^3.4.2",
|
|
60
60
|
"protobufjs": "7.4.0",
|
|
61
|
-
"ts-jest": "^29.2.5",
|
|
62
61
|
"ts-node": "^10.9.2",
|
|
63
62
|
"tsc-alias": "^1.8.10",
|
|
64
63
|
"tsconfig-paths": "4.2.0",
|
|
65
64
|
"typescript": "^5.6.3",
|
|
65
|
+
"vitest": "^3.1.3",
|
|
66
66
|
"zod-to-json-schema": "^3.21.4"
|
|
67
67
|
}
|
|
68
68
|
}
|