zod-to-x 2.0.0 → 2.0.1-dev.4

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 CHANGED
@@ -60,7 +60,7 @@ Automate the transpilation of data models to save time, reduce errors, and let y
60
60
  ```bash
61
61
  npm install zod-to-x zod
62
62
  ```
63
- (*) [`zod@3.25.0`](https://www.npmjs.com/package/zod/v/3.25.0) version or greather is required.
63
+ (*) [`zod@3.25.28`](https://www.npmjs.com/package/zod/v/3.25.0) version or greather is required.
64
64
 
65
65
  ### 2) Extend Zod using the `extendZod` method after the first [`@zod`](https://github.com/colinhacks/zod) import:
66
66
  ```ts
@@ -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
@@ -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 && metadata.layer.file !== opt.file) {
93
- // Type used from another layer. A new type is created inheriting the
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 = {
@@ -84,9 +84,8 @@ export interface IZod2xMetadata {
84
84
  layer?: IZod2xLayerMetadata;
85
85
  /**
86
86
  * For Layered Modeling.
87
- * When a type of another file is used without modifying it, by default it is sustituted by
88
- * the import without creating a new type. If wanted. it can be forced to create a new type
89
- * which will be the extension of the original type if `zod2xExtendable` is used.
87
+ * When a type of another file is used without modifying it, it is sustituted by its import
88
+ * instead of creating a new type.
90
89
  */
91
90
  aliasOf?: string;
92
91
  parentLayer?: IZod2xLayerMetadata;
@@ -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
- return `boost::variant<${itemsType.join(", ")}>`;
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 = this.getTypeFromExternalNamespace(parentNamespace, aliasOf);
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,25 @@ 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
+ this.addExternalTypeImport(data);
120
+ }
121
+ return true;
122
+ }
123
+ else if (data.aliasOf) {
124
+ this.addExtendedType(data.name, data.parentNamespace, data.aliasOf, {
125
+ type,
126
+ isInternal: true,
127
+ });
128
+ return true;
129
+ }
130
+ return false;
131
+ }
110
132
  runAfter() {
111
133
  this.preImports.add("#pragma once");
112
134
  // Add the output inside the namespace
@@ -159,12 +181,7 @@ class Zod2Cpp extends core_1.Zod2X {
159
181
  return `boost::optional<${type}>`;
160
182
  }
161
183
  transpileAliasedType(data) {
162
- if (this.isExternalTypeImport(data)) {
163
- if (data.aliasOf) {
164
- this.addExtendedType(data.name, data.parentNamespace, data.aliasOf, {
165
- type: "alias",
166
- });
167
- }
184
+ if (this.checkExtendedTypeInclusion(data, "alias")) {
168
185
  return;
169
186
  }
170
187
  let extendedType = undefined;
@@ -185,12 +202,7 @@ class Zod2Cpp extends core_1.Zod2X {
185
202
  * }
186
203
  */
187
204
  transpileEnum(data) {
188
- if (this.isExternalTypeImport(data)) {
189
- if (data.aliasOf) {
190
- this.addExtendedType(data.name, data.parentNamespace, data.aliasOf, {
191
- type: "alias",
192
- });
193
- }
205
+ if (this.checkExtendedTypeInclusion(data, "alias")) {
194
206
  return;
195
207
  }
196
208
  this.addComment(data.description);
@@ -220,10 +232,7 @@ class Zod2Cpp extends core_1.Zod2X {
220
232
  * }
221
233
  */
222
234
  transpileIntersection(data) {
223
- if (this.isExternalTypeImport(data)) {
224
- if (data.aliasOf) {
225
- this.addExtendedType(data.name, data.parentNamespace, data.aliasOf);
226
- }
235
+ if (this.checkExtendedTypeInclusion(data)) {
227
236
  return;
228
237
  }
229
238
  console.warn(`${data.name}: use zod's merge instead of intersection whenever possible.`);
@@ -253,12 +262,7 @@ class Zod2Cpp extends core_1.Zod2X {
253
262
  }
254
263
  /** Ex: using TypeC = boost::variant<TypeA, TypeB> */
255
264
  transpileUnion(data) {
256
- if (this.isExternalTypeImport(data)) {
257
- if (data.aliasOf) {
258
- this.addExtendedType(data.name, data.parentNamespace, data.aliasOf, {
259
- type: "union",
260
- });
261
- }
265
+ if (this.checkExtendedTypeInclusion(data, "union")) {
262
266
  return;
263
267
  }
264
268
  this.addComment(data.description);
@@ -275,10 +279,7 @@ class Zod2Cpp extends core_1.Zod2X {
275
279
  this._createUnionDeserializer(data.name, attributesData, data.discriminantKey);
276
280
  }
277
281
  transpileStruct(data) {
278
- if (this.isExternalTypeImport(data)) {
279
- if (data.aliasOf) {
280
- this.addExtendedType(data.name, data.parentNamespace, data.aliasOf);
281
- }
282
+ if (this.checkExtendedTypeInclusion(data)) {
282
283
  return;
283
284
  }
284
285
  this.addComment(data.description);
@@ -730,7 +731,8 @@ class Zod2Cpp17 extends Zod2Cpp {
730
731
  /** Ex: std::variant<TypeA, TypeB> */
731
732
  this.getUnionType = (itemsType) => {
732
733
  this.imports.add(this.lib.variant);
733
- return `std::variant<${itemsType.join(", ")}>`;
734
+ const unionLines = itemsType.map((type) => `${this.indent[2]}${type}`).join(",\n");
735
+ return `std::variant<\n${unionLines}\n${this.indent[1]}>`;
734
736
  };
735
737
  this.useBoost = false;
736
738
  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.join(" | ");
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 = this.getTypeFromExternalNamespace(parentNamespace, aliasOf);
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,25 @@ 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
+ this.addExternalTypeImport(data);
70
+ }
71
+ return true;
72
+ }
73
+ else if (data.aliasOf) {
74
+ this.addExtendedType(data.name, data.parentNamespace, data.aliasOf, {
75
+ type,
76
+ isInternal: true,
77
+ });
78
+ return true;
79
+ }
80
+ return false;
81
+ }
61
82
  /** Ex: Array<Array<TypeA[]>> */
62
83
  getArrayType(arrayType, arrayDeep) {
63
84
  let output = arrayType.includes("|") || arrayType.includes("&")
@@ -84,12 +105,7 @@ class Zod2Ts extends core_1.Zod2X {
84
105
  return `Record<${keyType}, ${valueType}>`;
85
106
  }
86
107
  transpileAliasedType(data) {
87
- if (this.isExternalTypeImport(data)) {
88
- if (data.aliasOf) {
89
- this.addExtendedType(data.name, data.parentNamespace, data.aliasOf, {
90
- type: "alias",
91
- });
92
- }
108
+ if (this.checkExtendedTypeInclusion(data, "alias")) {
93
109
  return;
94
110
  }
95
111
  let extendedType = undefined;
@@ -110,12 +126,7 @@ class Zod2Ts extends core_1.Zod2X {
110
126
  * }
111
127
  */
112
128
  transpileEnum(data) {
113
- if (this.isExternalTypeImport(data)) {
114
- if (data.aliasOf) {
115
- this.addExtendedType(data.name, data.parentNamespace, data.aliasOf, {
116
- type: "alias",
117
- });
118
- }
129
+ if (this.checkExtendedTypeInclusion(data, "alias")) {
119
130
  return;
120
131
  }
121
132
  this.addComment(data.description);
@@ -147,10 +158,7 @@ class Zod2Ts extends core_1.Zod2X {
147
158
  * */
148
159
  transpileIntersection(data) {
149
160
  var _a;
150
- if (this.isExternalTypeImport(data)) {
151
- if (data.aliasOf) {
152
- this.addExtendedType(data.name, data.parentNamespace, data.aliasOf);
153
- }
161
+ if (this.checkExtendedTypeInclusion(data)) {
154
162
  return;
155
163
  }
156
164
  if (this.opt.outType === "class" && data.newObject) {
@@ -164,10 +172,7 @@ class Zod2Ts extends core_1.Zod2X {
164
172
  }
165
173
  }
166
174
  transpileStruct(data) {
167
- if (this.isExternalTypeImport(data)) {
168
- if (data.aliasOf) {
169
- this.addExtendedType(data.name, data.parentNamespace, data.aliasOf);
170
- }
175
+ if (this.checkExtendedTypeInclusion(data)) {
171
176
  return;
172
177
  }
173
178
  this.addComment(data.description);
@@ -195,12 +200,7 @@ class Zod2Ts extends core_1.Zod2X {
195
200
  * */
196
201
  transpileUnion(data) {
197
202
  var _a;
198
- if (this.isExternalTypeImport(data)) {
199
- if (data.aliasOf) {
200
- this.addExtendedType(data.name, data.parentNamespace, data.aliasOf, {
201
- type: data.discriminantKey === undefined ? "union" : "d-union",
202
- });
203
- }
203
+ if (this.checkExtendedTypeInclusion(data, data.discriminantKey === undefined ? "union" : "d-union")) {
204
204
  return;
205
205
  }
206
206
  if (this.opt.outType === "class" && data.newObject) {
@@ -210,7 +210,7 @@ class Zod2Ts extends core_1.Zod2X {
210
210
  else {
211
211
  this.addComment(data.description);
212
212
  const attributesTypes = data.options.map(this.getAttributeType.bind(this));
213
- this.push0(`export type ${data.name} = ${this.getUnionType(attributesTypes)};\n`);
213
+ this.push0(`export type ${data.name} =\n${this.getUnionType(attributesTypes)};\n`);
214
214
  }
215
215
  }
216
216
  /** Ex:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod-to-x",
3
- "version": "2.0.0",
3
+ "version": "2.0.1-dev.4",
4
4
  "description": "Multi language types generation from Zod schemas.",
5
5
  "main": "dist/index.js",
6
6
  "files": [
@@ -45,7 +45,7 @@
45
45
  "*.ts": "prettier --write"
46
46
  },
47
47
  "peerDependencies": {
48
- "zod": ">=3.25.0"
48
+ "zod": ">=3.25.28"
49
49
  },
50
50
  "dependencies": {
51
51
  "case": "1.6.3"