zod-to-x 1.4.2 → 1.4.3
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.d.ts +7 -3
- package/dist/core/ast_node.js +41 -15
- package/dist/core/ast_types.d.ts +1 -6
- package/dist/core/transpiler.d.ts +9 -1
- package/dist/core/transpiler.js +23 -5
- package/dist/transpilers/cpp/runner.js +4 -4
- package/dist/transpilers/typescript/runner.js +4 -4
- package/package.json +1 -1
package/dist/core/ast_node.d.ts
CHANGED
|
@@ -44,9 +44,13 @@ export declare class Zod2Ast {
|
|
|
44
44
|
private _getTranspilerableFile;
|
|
45
45
|
/**
|
|
46
46
|
* Transpilerable items are treated as references in the AST
|
|
47
|
-
* @param ref
|
|
48
|
-
* @param refType
|
|
49
|
-
* @param discriminantValue
|
|
47
|
+
* @param ref - Output type name
|
|
48
|
+
* @param refType - Type of the output type
|
|
49
|
+
* @param discriminantValue - Discriminant value (for ZodDiscriminatedUnion)
|
|
50
|
+
* @param parentNamespace - For Layered modeling, the namespace of the parent type if does not
|
|
51
|
+
* belong to the same file.
|
|
52
|
+
* @param parentFile - For Layered modeling, the file of the parent type if does not belong to
|
|
53
|
+
* the same file.
|
|
50
54
|
* @returns
|
|
51
55
|
*/
|
|
52
56
|
private _createDefinition;
|
package/dist/core/ast_node.js
CHANGED
|
@@ -27,17 +27,38 @@ class Zod2Ast {
|
|
|
27
27
|
*/
|
|
28
28
|
_getTranspilerableFile(itemName, metadata) {
|
|
29
29
|
var _a;
|
|
30
|
-
|
|
31
|
-
if (this.opt.layer && layer) {
|
|
32
|
-
if (
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
let layer;
|
|
31
|
+
if (this.opt.layer !== undefined && (metadata === null || metadata === void 0 ? void 0 : metadata.layer) !== undefined) {
|
|
32
|
+
if (metadata.layer.file === this.opt.layer.file) {
|
|
33
|
+
// Case 1: Only layer exists and belongs to the same file
|
|
34
|
+
// Case 2: Layer (belongs to same file) and parentLayer exist
|
|
35
|
+
// Behaviour: New type is created extending the parent layer (if any)
|
|
36
|
+
layer = (_a = metadata.parentLayer) !== null && _a !== void 0 ? _a : metadata.layer;
|
|
37
|
+
if (this.opt.layer.index < layer.index) {
|
|
38
|
+
throw new errors_1.BadLayerDefinitionError(`${itemName}: Layer with number ${this.opt.layer.index} can only use models` +
|
|
39
|
+
`from the same or lower layer. Found layer with number ${layer.index}`);
|
|
40
|
+
}
|
|
41
|
+
if (this.opt.layer.file !== layer.file) {
|
|
42
|
+
return {
|
|
43
|
+
parentFile: layer.file,
|
|
44
|
+
parentNamespace: layer.namespace,
|
|
45
|
+
parentTypeName: metadata === null || metadata === void 0 ? void 0 : metadata.parentTypeName,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
35
48
|
}
|
|
36
|
-
|
|
49
|
+
else {
|
|
50
|
+
// Case 3: Only layer exists and belongs to a different file
|
|
51
|
+
// Case 4: Layer (belongs to different file) and parentLayer exist
|
|
52
|
+
// Behaviour: Type is imported from Layer file
|
|
53
|
+
layer = metadata.layer;
|
|
54
|
+
if (this.opt.layer.index < layer.index) {
|
|
55
|
+
throw new errors_1.BadLayerDefinitionError(`${itemName}: Layer with number ${this.opt.layer.index} can only use models` +
|
|
56
|
+
`from the same or lower layer. Found layer with number ${layer.index}`);
|
|
57
|
+
}
|
|
37
58
|
return {
|
|
38
59
|
parentFile: layer.file,
|
|
39
60
|
parentNamespace: layer.namespace,
|
|
40
|
-
parentTypeName:
|
|
61
|
+
parentTypeName: undefined,
|
|
41
62
|
};
|
|
42
63
|
}
|
|
43
64
|
}
|
|
@@ -45,18 +66,23 @@ class Zod2Ast {
|
|
|
45
66
|
}
|
|
46
67
|
/**
|
|
47
68
|
* Transpilerable items are treated as references in the AST
|
|
48
|
-
* @param ref
|
|
49
|
-
* @param refType
|
|
50
|
-
* @param discriminantValue
|
|
69
|
+
* @param ref - Output type name
|
|
70
|
+
* @param refType - Type of the output type
|
|
71
|
+
* @param discriminantValue - Discriminant value (for ZodDiscriminatedUnion)
|
|
72
|
+
* @param parentNamespace - For Layered modeling, the namespace of the parent type if does not
|
|
73
|
+
* belong to the same file.
|
|
74
|
+
* @param parentFile - For Layered modeling, the file of the parent type if does not belong to
|
|
75
|
+
* the same file.
|
|
51
76
|
* @returns
|
|
52
77
|
*/
|
|
53
|
-
_createDefinition(ref, refType, discriminantValue, parentNamespace) {
|
|
78
|
+
_createDefinition(ref, refType, discriminantValue, parentNamespace, parentFile) {
|
|
54
79
|
return {
|
|
55
80
|
type: "definition",
|
|
56
81
|
reference: ref,
|
|
57
82
|
referenceType: refType,
|
|
58
83
|
discriminantValue,
|
|
59
84
|
parentNamespace,
|
|
85
|
+
parentFile,
|
|
60
86
|
};
|
|
61
87
|
}
|
|
62
88
|
/**
|
|
@@ -170,7 +196,7 @@ class Zod2Ast {
|
|
|
170
196
|
if (!this.nodes.has(name)) {
|
|
171
197
|
this.nodes.set(name, item);
|
|
172
198
|
}
|
|
173
|
-
return this._createDefinition(name, zodTypeName, undefined, parentNamespace);
|
|
199
|
+
return this._createDefinition(name, zodTypeName, undefined, parentNamespace, parentFile);
|
|
174
200
|
}
|
|
175
201
|
_getObjectAst(schema, opt) {
|
|
176
202
|
const { name, zodTypeName, parentFile, parentNamespace, parentTypeName } = this._getNames(schema, "ZodObject type must have a typeName. Use zod2x method to provide one.");
|
|
@@ -205,7 +231,7 @@ class Zod2Ast {
|
|
|
205
231
|
}
|
|
206
232
|
}
|
|
207
233
|
}
|
|
208
|
-
return this._createDefinition(name, zodTypeName, discriminantValue, parentTypeName ? undefined : parentNamespace);
|
|
234
|
+
return this._createDefinition(name, zodTypeName, discriminantValue, parentTypeName ? undefined : parentNamespace, parentTypeName ? undefined : parentFile);
|
|
209
235
|
}
|
|
210
236
|
_getUnionAst(schema) {
|
|
211
237
|
const def = schema._def;
|
|
@@ -241,7 +267,7 @@ class Zod2Ast {
|
|
|
241
267
|
if (name && !this.nodes.has(name)) {
|
|
242
268
|
this.nodes.set(name, item);
|
|
243
269
|
}
|
|
244
|
-
return this._createDefinition(name, zodTypeName, undefined, parentTypeName ? undefined : parentNamespace);
|
|
270
|
+
return this._createDefinition(name, zodTypeName, undefined, parentTypeName ? undefined : parentNamespace, parentTypeName ? undefined : parentFile);
|
|
245
271
|
}
|
|
246
272
|
_getIntersectionAst(schema) {
|
|
247
273
|
const def = schema._def;
|
|
@@ -274,7 +300,7 @@ class Zod2Ast {
|
|
|
274
300
|
if (name && !this.nodes.has(name)) {
|
|
275
301
|
this.nodes.set(name, item);
|
|
276
302
|
}
|
|
277
|
-
return this._createDefinition(name, zodTypeName, undefined, parentTypeName ? undefined : parentNamespace);
|
|
303
|
+
return this._createDefinition(name, zodTypeName, undefined, parentTypeName ? undefined : parentNamespace, parentTypeName ? undefined : parentFile);
|
|
278
304
|
}
|
|
279
305
|
/**
|
|
280
306
|
* Build the AST node of provided Zod Schema
|
package/dist/core/ast_types.d.ts
CHANGED
|
@@ -116,12 +116,7 @@ export type ASTDefintion = ASTCommon & {
|
|
|
116
116
|
reference: string;
|
|
117
117
|
referenceType: ZodFirstPartyTypeKind;
|
|
118
118
|
discriminantValue?: string;
|
|
119
|
-
|
|
120
|
-
* Namespace where the transpilerable model is defined. Used to use import statements in the
|
|
121
|
-
* transpiled code.
|
|
122
|
-
*/
|
|
123
|
-
parentNamespace?: string;
|
|
124
|
-
};
|
|
119
|
+
} & Omit<ASTLayerMetadata, "parentTypeName">;
|
|
125
120
|
/**
|
|
126
121
|
* Represents a general AST node, encompassing various Zod schema types.
|
|
127
122
|
*/
|
|
@@ -187,6 +187,14 @@ export declare abstract class Zod2X<T extends IZodToXOpt> {
|
|
|
187
187
|
* @returns A string representing the type in the target language.
|
|
188
188
|
*/
|
|
189
189
|
protected getAttributeType(token: ASTNode | TranspilerableTypes): string;
|
|
190
|
+
/**
|
|
191
|
+
* Determines whether a given type is an external type import.
|
|
192
|
+
*
|
|
193
|
+
* @param item - An object containing the `parentFile` and `parentNamespace`
|
|
194
|
+
* properties of the type to evaluate.
|
|
195
|
+
* @returns `true` if the type is an external type import; otherwise, `false`.
|
|
196
|
+
*/
|
|
197
|
+
protected isExternalTypeImport(item: Pick<TranspilerableTypes, "parentFile" | "parentNamespace">): boolean;
|
|
190
198
|
/**
|
|
191
199
|
* Adds an external type import to the transpiler's imports if the provided transpiled item
|
|
192
200
|
* is located into another file and namespace, and if the `useImports` option is not disabled.
|
|
@@ -195,7 +203,7 @@ export declare abstract class Zod2X<T extends IZodToXOpt> {
|
|
|
195
203
|
* about the type to be imported, including its parent file and namespace.
|
|
196
204
|
* @returns `true` if the import was successfully added, otherwise `false`.
|
|
197
205
|
*/
|
|
198
|
-
protected addExternalTypeImport(item: TranspilerableTypes): boolean;
|
|
206
|
+
protected addExternalTypeImport(item: Pick<TranspilerableTypes, "parentFile" | "parentNamespace">): boolean;
|
|
199
207
|
/**
|
|
200
208
|
* Transpiles a single item from the transpiler queue.
|
|
201
209
|
* @param item - The transpilerable type to transpile.
|
package/dist/core/transpiler.js
CHANGED
|
@@ -55,10 +55,16 @@ class Zod2X {
|
|
|
55
55
|
varType = token.name;
|
|
56
56
|
}
|
|
57
57
|
else if (token.type === "definition") {
|
|
58
|
-
|
|
59
|
-
this.
|
|
60
|
-
|
|
61
|
-
: token.
|
|
58
|
+
if (this.opt.useImports === true && token.parentNamespace) {
|
|
59
|
+
varType = this.getTypeFromExternalNamespace(token.parentNamespace, token.reference);
|
|
60
|
+
this.addExternalTypeImport({
|
|
61
|
+
parentNamespace: token.parentNamespace,
|
|
62
|
+
parentFile: token.parentFile,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
varType = token.reference;
|
|
67
|
+
}
|
|
62
68
|
}
|
|
63
69
|
else if (token.type === zod_1.ZodFirstPartyTypeKind.ZodString) {
|
|
64
70
|
varType = this.getStringType();
|
|
@@ -106,6 +112,18 @@ class Zod2X {
|
|
|
106
112
|
}
|
|
107
113
|
return token.arrayDimension ? this.getArrayType(varType, token.arrayDimension) : varType;
|
|
108
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* Determines whether a given type is an external type import.
|
|
117
|
+
*
|
|
118
|
+
* @param item - An object containing the `parentFile` and `parentNamespace`
|
|
119
|
+
* properties of the type to evaluate.
|
|
120
|
+
* @returns `true` if the type is an external type import; otherwise, `false`.
|
|
121
|
+
*/
|
|
122
|
+
isExternalTypeImport(item) {
|
|
123
|
+
return (item.parentFile !== undefined &&
|
|
124
|
+
item.parentNamespace !== undefined &&
|
|
125
|
+
this.opt.useImports !== false);
|
|
126
|
+
}
|
|
109
127
|
/**
|
|
110
128
|
* Adds an external type import to the transpiler's imports if the provided transpiled item
|
|
111
129
|
* is located into another file and namespace, and if the `useImports` option is not disabled.
|
|
@@ -115,7 +133,7 @@ class Zod2X {
|
|
|
115
133
|
* @returns `true` if the import was successfully added, otherwise `false`.
|
|
116
134
|
*/
|
|
117
135
|
addExternalTypeImport(item) {
|
|
118
|
-
if (
|
|
136
|
+
if (this.isExternalTypeImport(item)) {
|
|
119
137
|
this.imports.add(this.addImportFromFile(item.parentFile, item.parentNamespace));
|
|
120
138
|
return true;
|
|
121
139
|
}
|
|
@@ -166,7 +166,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
166
166
|
* }
|
|
167
167
|
*/
|
|
168
168
|
transpileEnum(data) {
|
|
169
|
-
if (this.
|
|
169
|
+
if (this.isExternalTypeImport(data)) {
|
|
170
170
|
return;
|
|
171
171
|
}
|
|
172
172
|
this.addComment(data.description);
|
|
@@ -196,7 +196,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
196
196
|
* }
|
|
197
197
|
*/
|
|
198
198
|
transpileIntersection(data) {
|
|
199
|
-
if (this.
|
|
199
|
+
if (this.isExternalTypeImport(data)) {
|
|
200
200
|
if (data.parentTypeName) {
|
|
201
201
|
this.addExtendedType(data.name, data.parentNamespace, data.parentTypeName);
|
|
202
202
|
}
|
|
@@ -232,7 +232,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
232
232
|
}
|
|
233
233
|
/** Ex: using TypeC = boost::variant<TypeA, TypeB> */
|
|
234
234
|
transpileUnion(data) {
|
|
235
|
-
if (this.
|
|
235
|
+
if (this.isExternalTypeImport(data)) {
|
|
236
236
|
if (data.parentTypeName) {
|
|
237
237
|
this.addExtendedType(data.name, data.parentNamespace, data.parentTypeName, true);
|
|
238
238
|
}
|
|
@@ -251,7 +251,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
251
251
|
this._createUnionDeserializer(data.name, attributesData, data.discriminantKey);
|
|
252
252
|
}
|
|
253
253
|
transpileStruct(data) {
|
|
254
|
-
if (this.
|
|
254
|
+
if (this.isExternalTypeImport(data)) {
|
|
255
255
|
if (data.parentTypeName) {
|
|
256
256
|
this.addExtendedType(data.name, data.parentNamespace, data.parentTypeName);
|
|
257
257
|
}
|
|
@@ -91,7 +91,7 @@ class Zod2Ts extends core_1.Zod2X {
|
|
|
91
91
|
* }
|
|
92
92
|
*/
|
|
93
93
|
transpileEnum(data) {
|
|
94
|
-
if (this.
|
|
94
|
+
if (this.isExternalTypeImport(data)) {
|
|
95
95
|
return;
|
|
96
96
|
}
|
|
97
97
|
this.addComment(data.description);
|
|
@@ -123,7 +123,7 @@ class Zod2Ts extends core_1.Zod2X {
|
|
|
123
123
|
* */
|
|
124
124
|
transpileIntersection(data) {
|
|
125
125
|
var _a;
|
|
126
|
-
if (this.
|
|
126
|
+
if (this.isExternalTypeImport(data)) {
|
|
127
127
|
if (data.parentTypeName) {
|
|
128
128
|
this.addExtendedType(data.name, data.parentNamespace, data.parentTypeName);
|
|
129
129
|
}
|
|
@@ -140,7 +140,7 @@ class Zod2Ts extends core_1.Zod2X {
|
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
142
|
transpileStruct(data) {
|
|
143
|
-
if (this.
|
|
143
|
+
if (this.isExternalTypeImport(data)) {
|
|
144
144
|
if (data.parentTypeName) {
|
|
145
145
|
this.addExtendedType(data.name, data.parentNamespace, data.parentTypeName);
|
|
146
146
|
}
|
|
@@ -171,7 +171,7 @@ class Zod2Ts extends core_1.Zod2X {
|
|
|
171
171
|
* */
|
|
172
172
|
transpileUnion(data) {
|
|
173
173
|
var _a;
|
|
174
|
-
if (this.
|
|
174
|
+
if (this.isExternalTypeImport(data)) {
|
|
175
175
|
if (data.parentTypeName) {
|
|
176
176
|
this.addExtendedType(data.name, data.parentNamespace, data.parentTypeName, {
|
|
177
177
|
isUnion: data.type === zod_1.ZodFirstPartyTypeKind.ZodUnion,
|