zod-to-x 2.0.1 → 2.0.2-dev.5

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,5 +1,5 @@
1
1
  import { IZod2AstOpt } from "../../core";
2
- import { ZodObject } from "../../lib/zod_helpers";
2
+ import { ZodObject, ZodDiscriminatedUnion, ZodUnion } from "../../lib/zod_helpers";
3
3
  import { IZod2ProtoV3Opt } from "./options";
4
4
  /**
5
5
  * Converts a Zod schema into a Protocol Buffers v3 definition.
@@ -16,4 +16,5 @@ import { IZod2ProtoV3Opt } from "./options";
16
16
  * definition.
17
17
  * @returns The Protocol Buffers v3 definition as a string.
18
18
  */
19
- export declare function zod2ProtoV3(schema: ZodObject<any>, opt?: Pick<IZod2AstOpt, "strict"> & Pick<IZod2ProtoV3Opt, "packageName" | "keepKeys" | "header" | "indent" | "includeComments" | "encodeDoubleAsInt">): string;
19
+ export declare function zod2ProtoV3(schema: ZodObject<any> | ZodDiscriminatedUnion | ZodUnion<any>, // TODO: fix any to force only ZodObjects
20
+ opt?: Pick<IZod2AstOpt, "strict"> & Pick<IZod2ProtoV3Opt, "packageName" | "keepKeys" | "header" | "indent" | "includeComments" | "encodeDoubleAsInt">): string;
@@ -160,7 +160,10 @@ class Zod2ProtoV3 extends core_1.Zod2X {
160
160
  this.addComment(data.description);
161
161
  this.push0(`message ${data.name} {`);
162
162
  Object.entries(data.properties).forEach(([key, value], index) => {
163
- if (value.description && !this.isTranspilerable(value)) {
163
+ if (this.opt.includeComments &&
164
+ value.description &&
165
+ !value.name &&
166
+ !this.isTranspilerable(value)) {
164
167
  // Avoid duplicated descriptions for transpiled items.
165
168
  this.addComment(value.description, `\n${this.indent[1]}`);
166
169
  }
@@ -243,7 +246,8 @@ class Zod2ProtoV3 extends core_1.Zod2X {
243
246
  * definition.
244
247
  * @returns The Protocol Buffers v3 definition as a string.
245
248
  */
246
- function zod2ProtoV3(schema, opt = {}) {
249
+ function zod2ProtoV3(schema, // TODO: fix any to force only ZodObjects
250
+ opt = {}) {
247
251
  const astNode = new core_1.Zod2Ast({ strict: opt.strict }).build(schema);
248
252
  return new Zod2ProtoV3(opt).transpile(astNode);
249
253
  }
@@ -315,7 +315,7 @@ class Zod2Ast {
315
315
  var _a, _b, _c;
316
316
  const def = schema.def;
317
317
  const discriminator = zod_helpers_1.ZodHelpers.isZodDiscriminatedUnion(schema)
318
- ? schema.def.discriminator
318
+ ? schema._zod.def.discriminator
319
319
  : undefined;
320
320
  const { name, parentFile, parentNamespace, aliasOf } = this._getNames(schema);
321
321
  const item = new core_1.ASTUnion({
@@ -118,7 +118,8 @@ declare module "zod/v4" {
118
118
  */
119
119
  zod2x<K extends keyof Pick<IZod2xMetadata, "typeName">>(this: ZodObject<Shape, Config>, key: K, value: IZod2xMetadata[K]): this;
120
120
  }
121
- interface ZodEnum<T extends util.EnumLike = util.EnumLike> extends ZodType {
121
+ /** @ts-ignore Cast variance */
122
+ interface ZodEnum<out T extends util.EnumLike = util.EnumLike> extends ZodType<core.$ZodEnumInternals<T>>, core.$ZodEnum<T> {
122
123
  /**
123
124
  * Creates a new Zod enum with the specified `typeName` metadata property.
124
125
  *
@@ -166,7 +167,7 @@ declare module "zod/v4" {
166
167
  */
167
168
  zod2x<K extends keyof Pick<IZod2xMetadata, "typeName">>(this: ZodDiscriminatedUnion<Options>, key: K, value: IZod2xMetadata[K]): this;
168
169
  }
169
- interface ZodUnion<T extends readonly core.$ZodType[] = readonly core.$ZodType[]> extends ZodType {
170
+ interface ZodUnion<T extends readonly core.SomeType[] = readonly core.$ZodType[]> extends ZodType {
170
171
  /**
171
172
  * Creates a new Zod union with the specified `typeName` metadata property.
172
173
  *
@@ -190,7 +191,7 @@ declare module "zod/v4" {
190
191
  */
191
192
  zod2x<K extends keyof Pick<IZod2xMetadata, "typeName">>(this: ZodUnion<T>, key: K, value: IZod2xMetadata[K]): this;
192
193
  }
193
- interface ZodIntersection<A extends core.$ZodType = core.$ZodType, B extends core.$ZodType = core.$ZodType> extends ZodType {
194
+ interface ZodIntersection<A extends core.SomeType = core.$ZodType, B extends core.SomeType = core.$ZodType> extends ZodType {
194
195
  /**
195
196
  * Creates a new Zod intersection with the specified `typeName` metadata property.
196
197
  *
@@ -214,7 +215,7 @@ declare module "zod/v4" {
214
215
  */
215
216
  zod2x<K extends keyof Pick<IZod2xMetadata, "typeName">>(this: ZodIntersection<A, B>, key: K, value: IZod2xMetadata[K]): this;
216
217
  }
217
- interface ZodLiteral<T extends util.Primitive = util.Primitive> extends ZodType {
218
+ interface ZodLiteral<T extends util.Literal = util.Literal> extends ZodType {
218
219
  /**
219
220
  * Creates a new Zod literal with the specified `parentEnum` metadata property.
220
221
  *
@@ -1,5 +1,5 @@
1
1
  import { z, ZodEnum, ZodNumber, ZodObject, ZodType } from "zod/v4";
2
- export type { ZodArray, ZodType, ZodIntersection, ZodObject, ZodEnum } from "zod/v4";
2
+ export type { ZodArray, ZodType, ZodIntersection, ZodObject, ZodEnum, ZodDiscriminatedUnion, ZodUnion, } from "zod/v4";
3
3
  export type ZodAnyUnionType = z.ZodUnion<any> | z.ZodDiscriminatedUnion<any>;
4
4
  type ZodNumberConstraints = {
5
5
  min?: number;
@@ -54,7 +54,7 @@ export declare class ZodHelpers {
54
54
  static isZodOptional(i: ZodType): i is z.ZodOptional<any>;
55
55
  static isZodNullable(i: ZodType): i is z.ZodNullable<any>;
56
56
  static isZodDefault(i: ZodType): i is z.ZodDefault<any>;
57
- static isZodAnyUnionType(i: ZodType): i is z.ZodUnion<any> | z.ZodDiscriminatedUnion<readonly z.core.$ZodType<unknown, unknown>[]>;
57
+ static isZodAnyUnionType(i: ZodType): i is z.ZodUnion<any> | z.ZodDiscriminatedUnion<readonly z.core.$ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>[], string>;
58
58
  static isZodAnyNumberType(i: ZodType): i is z.ZodNumber | z.ZodBigInt;
59
59
  static isZodAnyMapType(i: ZodType): i is z.ZodRecord<any, any> | z.ZodMap<any, any>;
60
60
  /**
@@ -72,7 +72,7 @@ export declare class ZodHelpers {
72
72
  * @returns
73
73
  */
74
74
  static isTranspilerableAliasedZodType(zodType: string | ZodType, onlyArray?: boolean): boolean;
75
- static cloneZod(i: ZodType): z.ZodType<unknown, unknown>;
75
+ static cloneZod(i: ZodType): z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
76
76
  static createZodObject(properties: Map<string, ZodType>): ZodObject<any>;
77
77
  static getZodNumberConstraints(i: ZodNumber | z.ZodBigInt): ZodNumberConstraints;
78
78
  }
@@ -66,14 +66,14 @@ class ZodHelpers {
66
66
  return ((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodEnum;
67
67
  }
68
68
  static isZodUnion(i) {
69
- var _a;
69
+ var _a, _b, _c;
70
70
  return (((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodUnion &&
71
- i.def.discriminator === undefined);
71
+ ((_c = (_b = i._zod) === null || _b === void 0 ? void 0 : _b.def) === null || _c === void 0 ? void 0 : _c.discriminator) === undefined);
72
72
  }
73
73
  static isZodDiscriminatedUnion(i) {
74
- var _a;
74
+ var _a, _b, _c;
75
75
  return (((_a = i === null || i === void 0 ? void 0 : i.def) === null || _a === void 0 ? void 0 : _a.type) === ZodFirstPartyTypeKind.ZodUnion &&
76
- i.def.discriminator !== undefined);
76
+ ((_c = (_b = i._zod) === null || _b === void 0 ? void 0 : _b.def) === null || _c === void 0 ? void 0 : _c.discriminator) !== undefined);
77
77
  }
78
78
  static isZodIntersection(i) {
79
79
  var _a;
@@ -15,6 +15,3 @@ export declare function getLibs(useBoost: boolean): {
15
15
  optional: string;
16
16
  variant: string;
17
17
  };
18
- export declare const USING: {
19
- nlohmann: string;
20
- };
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.USING = void 0;
4
3
  exports.getLibs = getLibs;
5
4
  /**
6
5
  * @description C++ standard library imports
@@ -23,6 +22,3 @@ function getLibs(useBoost) {
23
22
  variant: useBoost ? "#include <boost/variant.hpp>" : "#include <variant>",
24
23
  };
25
24
  }
26
- exports.USING = {
27
- nlohmann: "using nlohmann::json;",
28
- };
@@ -1,3 +1,4 @@
1
+ export declare const NLOHMANN = "nlohmann::json";
1
2
  /**
2
3
  * @description Generates a reusable C++ utility for handling optional fields in JSON serialization
3
4
  * and deserialization using the nlohmann::json library and boost::optional.
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NLOHMANN = void 0;
3
4
  exports.getNlohmannOptionalHelper = getNlohmannOptionalHelper;
5
+ exports.NLOHMANN = "nlohmann::json";
4
6
  /**
5
7
  * @description Generates a reusable C++ utility for handling optional fields in JSON serialization
6
8
  * and deserialization using the nlohmann::json library and boost::optional.
@@ -15,7 +17,7 @@ function getNlohmannOptionalHelper(indent, includeNulls, useBoost, namespace) {
15
17
  const setOpt = includeNulls
16
18
  ? [
17
19
  `template <typename T>`,
18
- `void set_opt(json& j, const std::string& key, const ${optType}<T>& opt) {`,
20
+ `void set_opt(${exports.NLOHMANN}& j, const std::string& key, const ${optType}<T>& opt) {`,
19
21
  `${" ".repeat(indent)}if (opt) {`,
20
22
  `${" ".repeat(indent * 2)}j[key] = *opt;`,
21
23
  `${" ".repeat(indent)}}`,
@@ -27,7 +29,7 @@ function getNlohmannOptionalHelper(indent, includeNulls, useBoost, namespace) {
27
29
  ]
28
30
  : [
29
31
  `template <typename T>`,
30
- `void set_opt(json& j, const std::string& key, const ${optType}<T>& opt) {`,
32
+ `void set_opt(${exports.NLOHMANN}& j, const std::string& key, const ${optType}<T>& opt) {`,
31
33
  `${" ".repeat(indent)}if (opt) {`,
32
34
  `${" ".repeat(indent * 2)}j[key] = *opt;`,
33
35
  `${" ".repeat(indent)}}`,
@@ -38,7 +40,7 @@ function getNlohmannOptionalHelper(indent, includeNulls, useBoost, namespace) {
38
40
  `#ifndef NLOHMANN_OPTIONAL_HELPER_${namespace}`,
39
41
  `#define NLOHMANN_OPTIONAL_HELPER_${namespace}`,
40
42
  "template <typename T>",
41
- `${optType}<T> get_opt(const json& j, const std::string& key) {`,
43
+ `${optType}<T> get_opt(const ${exports.NLOHMANN}& j, const std::string& key) {`,
42
44
  `${" ".repeat(indent)}auto it = j.find(key);`,
43
45
  `${" ".repeat(indent)}if (it != j.end() && !it->is_null()) {`,
44
46
  `${" ".repeat(indent * 2)}return it->get<T>();`,
@@ -131,7 +131,7 @@ export declare class Zod2Cpp extends Zod2X<IZod2CppOpt> {
131
131
  * @description Generates a JSON serializer for a struct. Each required field is serialized
132
132
  * directly, and optional fields are checked for existence before serialization.
133
133
  * Ex:
134
- * inline void to_json(json& j, const MyStruct& x) {
134
+ * inline void to_json(nlohmann::json& j, const MyStruct& x) {
135
135
  * j["requiredField"] = x.required_field;
136
136
  * if (x.optional_field) {
137
137
  * j["optionalField"] = x.optional_field;
@@ -145,7 +145,7 @@ export declare class Zod2Cpp extends Zod2X<IZod2CppOpt> {
145
145
  * @description Generates a JSON deserializer for a struct. Each required field is deserialized
146
146
  * using at, while optional fields are handled with get_opt.
147
147
  * Ex:
148
- * inline void from_json(const json& j, MyStruct& x) {
148
+ * inline void from_json(const nlohmann::json& j, MyStruct& x) {
149
149
  * x.required_field(j.at("requiredField").get<int>());
150
150
  * x.optional_field(get_opt<std::string>(j, "optionalField"));
151
151
  * }
@@ -158,7 +158,7 @@ export declare class Zod2Cpp extends Zod2X<IZod2CppOpt> {
158
158
  * access class attributes. Optional fields are checked for existence before
159
159
  * serialization.
160
160
  * Ex:
161
- * inline void to_json(json& j, const MyClass& x) {
161
+ * inline void to_json(nlohmann::json& j, const MyClass& x) {
162
162
  * j["requiredField"] = x.get_required_field();
163
163
  * if (x.get_optional_field()) {
164
164
  * j["optionalField"] = x.get_optional_field();
@@ -172,7 +172,7 @@ export declare class Zod2Cpp extends Zod2X<IZod2CppOpt> {
172
172
  * @description Generates a JSON deserializer for a class. The deserializer uses setter methods
173
173
  * to populate class attributes.
174
174
  * Ex:
175
- * inline void from_json(const json& j, MyClass& x) {
175
+ * inline void from_json(const nlohmann::json& j, MyClass& x) {
176
176
  * x.set_required_field(j.at("requiredField").get<int>());
177
177
  * x.set_optional_field(get_opt<std::string>(j, "optionalField"));
178
178
  * }
@@ -184,7 +184,7 @@ export declare class Zod2Cpp extends Zod2X<IZod2CppOpt> {
184
184
  * @description Generates a JSON serializer for an enum. Maps enum values to strings for
185
185
  * serialization, with a default case for unexpected values.
186
186
  * Ex:
187
- * inline void to_json(json& j, const MyEnum& x) {
187
+ * inline void to_json(nlohmann::json& j, const MyEnum& x) {
188
188
  * switch (x) {
189
189
  * case MyEnum::Value1: j = "VALUE_1"; break;
190
190
  * case MyEnum::Value2: j = "VALUE_2"; break;
@@ -200,7 +200,7 @@ export declare class Zod2Cpp extends Zod2X<IZod2CppOpt> {
200
200
  * @description Generates a JSON deserializer for an enum. Maps strings to enum values, with an
201
201
  * error for unexpected strings.
202
202
  * Ex:
203
- * inline void from_json(const json& j, MyEnum& x) {
203
+ * inline void from_json(const nlohmann::json& j, MyEnum& x) {
204
204
  * if (j == "VALUE_1") x = MyEnum::Value1;
205
205
  * else if (j == "VALUE_2") x = MyEnum::Value2;
206
206
  * else {
@@ -216,7 +216,7 @@ export declare class Zod2Cpp extends Zod2X<IZod2CppOpt> {
216
216
  * multiple types.
217
217
  *
218
218
  * @example
219
- * // inline void to_json(json& j, const DerivedType& x) {
219
+ * // inline void to_json(nlohmann::json& j, const DerivedType& x) {
220
220
  * // to_json(j, static_cast<const BaseType1&>(x));
221
221
  * // to_json(j, static_cast<const BaseType2&>(x));
222
222
  * // }
@@ -230,7 +230,7 @@ export declare class Zod2Cpp extends Zod2X<IZod2CppOpt> {
230
230
  * multiple types.
231
231
  *
232
232
  * @example
233
- * // inline void from_json(const json& j, DerivedType& x) {
233
+ * // inline void from_json(const nlohmann::json& j, DerivedType& x) {
234
234
  * // from_json(j, static_cast<BaseType1&>(x));
235
235
  * // from_json(j, static_cast<BaseType2&>(x));
236
236
  * // }
@@ -250,7 +250,7 @@ export declare class Zod2Cpp extends Zod2X<IZod2CppOpt> {
250
250
  * // Given: unionName = "MyUnion", itemsType = {"int", "std::string"}
251
251
  * // The generated output might look like:
252
252
  * //
253
- * // inline void to_json(json& j, const MyUnion& x) {
253
+ * // inline void to_json(nlohmann::json& j, const MyUnion& x) {
254
254
  * // if (x.type() == typeid(int)) {
255
255
  * // j = boost::get<int>(x);
256
256
  * // } else if (x.type() == typeid(std::string)) {
@@ -281,7 +281,7 @@ export declare class Zod2Cpp extends Zod2X<IZod2CppOpt> {
281
281
  * // {"type": "PhoneContact", "discriminantValue": "phone"} }
282
282
  * //
283
283
  * // The generated output might look like:
284
- * // inline void from_json(const json& j, MyUnion& x) {
284
+ * // inline void from_json(const nlohmann::json& j, MyUnion& x) {
285
285
  * // const auto& k = j.at("type").get<std::string>();
286
286
  * // if (k == "email") {
287
287
  * // x = j.get<EmailContact>();
@@ -297,7 +297,7 @@ export declare class Zod2Cpp extends Zod2X<IZod2CppOpt> {
297
297
  * // Given: unionName = "MyUnion", items = { {"type": "int"}, {"type": "std::string"} }
298
298
  * //
299
299
  * // The generated output might look like:
300
- * // inline void from_json(const json& j, MyUnion& x) {
300
+ * // inline void from_json(const nlohmann::json& j, MyUnion& x) {
301
301
  * // try {
302
302
  * // // Try to deserialize as int
303
303
  * // x = j.get<int>();
@@ -342,7 +342,7 @@ export declare class Zod2Cpp17 extends Zod2Cpp {
342
342
  * // Given: unionName = "MyUnion", itemsType = {"int", "std::string"}
343
343
  * // The generated output might look like:
344
344
  * //
345
- * // inline void to_json(json& j, const MyUnion& x) {
345
+ * // inline void to_json(nlohmann::json& j, const MyUnion& x) {
346
346
  * // std::visit(
347
347
  * // [&j](auto&& arg) {
348
348
  * // using T = std::decay_t<decltype(arg)>;
@@ -34,7 +34,7 @@ class Zod2Cpp extends core_1.Zod2X {
34
34
  };
35
35
  this.getAnyType = () => {
36
36
  this.imports.add(this.lib.nlohmann);
37
- return "json";
37
+ return nlohmann_1.NLOHMANN;
38
38
  };
39
39
  /** Ex: std::set<TypeA> */
40
40
  this.getSetType = (itemType) => {
@@ -146,9 +146,6 @@ class Zod2Cpp extends core_1.Zod2X {
146
146
  this.output.push(...this.serializers);
147
147
  this.output.push("}");
148
148
  }
149
- if (this.imports.has(this.lib.nlohmann)) {
150
- this.postImports.add(`${libs_1.USING.nlohmann}`);
151
- }
152
149
  }
153
150
  /** Ex: std::vector<std::vector<TypeA>> */
154
151
  getArrayType(arrayType, arrayDeep) {
@@ -185,6 +182,7 @@ class Zod2Cpp extends core_1.Zod2X {
185
182
  return;
186
183
  }
187
184
  let extendedType = undefined;
185
+ this.addComment(data.description);
188
186
  if (data instanceof core_1.ASTArray) {
189
187
  extendedType = this.getAttributeType(data.item);
190
188
  }
@@ -366,7 +364,10 @@ class Zod2Cpp extends core_1.Zod2X {
366
364
  _transpileMember(memberName, memberNode) {
367
365
  let keyType = this.getAttributeType(memberNode);
368
366
  const origType = keyType;
369
- if (memberNode.description && !memberNode.name && !this.isTranspilerable(memberNode)) {
367
+ if (this.opt.includeComments &&
368
+ memberNode.description &&
369
+ !memberNode.name &&
370
+ !this.isTranspilerable(memberNode)) {
370
371
  // Avoid duplicated descriptions for transpiled items.
371
372
  this.push1("");
372
373
  this.addComment(memberNode.description, `${this.indent[1]}`);
@@ -405,7 +406,7 @@ class Zod2Cpp extends core_1.Zod2X {
405
406
  * @description Generates a JSON serializer for a struct. Each required field is serialized
406
407
  * directly, and optional fields are checked for existence before serialization.
407
408
  * Ex:
408
- * inline void to_json(json& j, const MyStruct& x) {
409
+ * inline void to_json(nlohmann::json& j, const MyStruct& x) {
409
410
  * j["requiredField"] = x.required_field;
410
411
  * if (x.optional_field) {
411
412
  * j["optionalField"] = x.optional_field;
@@ -415,13 +416,14 @@ class Zod2Cpp extends core_1.Zod2X {
415
416
  * @param childs - Structure attributes data.
416
417
  */
417
418
  _createStructSerializer(parent, childs) {
418
- this._push0(this.serializers, `inline void to_json(json& j, const ${parent}& x) {`);
419
+ const prefix = this.opt.namespace ? `${this.opt.namespace}::` : "";
420
+ this._push0(this.serializers, `inline void to_json(${nlohmann_1.NLOHMANN}& j, const ${parent}& x) {`);
419
421
  childs.forEach((i) => {
420
422
  if (i.required) {
421
423
  this._push1(this.serializers, `j["${i.origName}"] = x.${i.snakeName};`);
422
424
  }
423
425
  else {
424
- this._push1(this.serializers, `set_opt<${i.typeName}>(j, "${i.origName}", x.${i.snakeName});`);
426
+ this._push1(this.serializers, `${prefix}set_opt<${i.typeName}>(j, "${i.origName}", x.${i.snakeName});`);
425
427
  }
426
428
  });
427
429
  this._push0(this.serializers, "}\n");
@@ -430,7 +432,7 @@ class Zod2Cpp extends core_1.Zod2X {
430
432
  * @description Generates a JSON deserializer for a struct. Each required field is deserialized
431
433
  * using at, while optional fields are handled with get_opt.
432
434
  * Ex:
433
- * inline void from_json(const json& j, MyStruct& x) {
435
+ * inline void from_json(const nlohmann::json& j, MyStruct& x) {
434
436
  * x.required_field(j.at("requiredField").get<int>());
435
437
  * x.optional_field(get_opt<std::string>(j, "optionalField"));
436
438
  * }
@@ -438,13 +440,14 @@ class Zod2Cpp extends core_1.Zod2X {
438
440
  * @param childs - Structure attributes data.
439
441
  */
440
442
  _createStructDeserializer(parent, childs) {
441
- this._push0(this.serializers, `inline void from_json(const json& j, ${parent}& x) {`);
443
+ const prefix = this.opt.namespace ? `${this.opt.namespace}::` : "";
444
+ this._push0(this.serializers, `inline void from_json(const ${nlohmann_1.NLOHMANN}& j, ${parent}& x) {`);
442
445
  childs.forEach((i) => {
443
446
  if (i.required) {
444
447
  this._push1(this.serializers, `x.${i.snakeName} = j.at("${i.origName}").get<${i.typeName}>();`);
445
448
  }
446
449
  else {
447
- this._push1(this.serializers, `x.${i.snakeName} = get_opt<${i.typeName}>(j, "${i.origName}");`);
450
+ this._push1(this.serializers, `x.${i.snakeName} = ${prefix}get_opt<${i.typeName}>(j, "${i.origName}");`);
448
451
  }
449
452
  });
450
453
  this._push0(this.serializers, "}\n");
@@ -454,7 +457,7 @@ class Zod2Cpp extends core_1.Zod2X {
454
457
  * access class attributes. Optional fields are checked for existence before
455
458
  * serialization.
456
459
  * Ex:
457
- * inline void to_json(json& j, const MyClass& x) {
460
+ * inline void to_json(nlohmann::json& j, const MyClass& x) {
458
461
  * j["requiredField"] = x.get_required_field();
459
462
  * if (x.get_optional_field()) {
460
463
  * j["optionalField"] = x.get_optional_field();
@@ -464,13 +467,14 @@ class Zod2Cpp extends core_1.Zod2X {
464
467
  * @param childs - Structure attributes data.
465
468
  */
466
469
  _createClassSerializer(parent, childs) {
467
- this._push0(this.serializers, `inline void to_json(json& j, const ${parent}& x) {`);
470
+ const prefix = this.opt.namespace ? `${this.opt.namespace}::` : "";
471
+ this._push0(this.serializers, `inline void to_json(${nlohmann_1.NLOHMANN}& j, const ${parent}& x) {`);
468
472
  childs.forEach((i) => {
469
473
  if (i.required) {
470
474
  this._push1(this.serializers, `j["${i.origName}"] = x.get_${i.snakeName}();`);
471
475
  }
472
476
  else {
473
- this._push1(this.serializers, `set_opt<${i.typeName}>(j, "${i.origName}", x.get_${i.snakeName}());`);
477
+ this._push1(this.serializers, `${prefix}set_opt<${i.typeName}>(j, "${i.origName}", x.get_${i.snakeName}());`);
474
478
  }
475
479
  });
476
480
  this._push0(this.serializers, "}\n");
@@ -479,7 +483,7 @@ class Zod2Cpp extends core_1.Zod2X {
479
483
  * @description Generates a JSON deserializer for a class. The deserializer uses setter methods
480
484
  * to populate class attributes.
481
485
  * Ex:
482
- * inline void from_json(const json& j, MyClass& x) {
486
+ * inline void from_json(const nlohmann::json& j, MyClass& x) {
483
487
  * x.set_required_field(j.at("requiredField").get<int>());
484
488
  * x.set_optional_field(get_opt<std::string>(j, "optionalField"));
485
489
  * }
@@ -487,13 +491,14 @@ class Zod2Cpp extends core_1.Zod2X {
487
491
  * @param childs - Structure attributes data.
488
492
  */
489
493
  _createClassDeserializer(parent, childs) {
490
- this._push0(this.serializers, `inline void from_json(const json& j, ${parent}& x) {`);
494
+ const prefix = this.opt.namespace ? `${this.opt.namespace}::` : "";
495
+ this._push0(this.serializers, `inline void from_json(const ${nlohmann_1.NLOHMANN}& j, ${parent}& x) {`);
491
496
  childs.forEach((i) => {
492
497
  if (i.required) {
493
498
  this._push1(this.serializers, `x.set_${i.snakeName}(j.at("${i.origName}").get<${i.typeName}>());`);
494
499
  }
495
500
  else {
496
- this._push1(this.serializers, `x.set_${i.snakeName}(get_opt<${i.typeName}>(j, "${i.origName}"));`);
501
+ this._push1(this.serializers, `x.set_${i.snakeName}(${prefix}get_opt<${i.typeName}>(j, "${i.origName}"));`);
497
502
  }
498
503
  });
499
504
  this._push0(this.serializers, "}\n");
@@ -502,7 +507,7 @@ class Zod2Cpp extends core_1.Zod2X {
502
507
  * @description Generates a JSON serializer for an enum. Maps enum values to strings for
503
508
  * serialization, with a default case for unexpected values.
504
509
  * Ex:
505
- * inline void to_json(json& j, const MyEnum& x) {
510
+ * inline void to_json(nlohmann::json& j, const MyEnum& x) {
506
511
  * switch (x) {
507
512
  * case MyEnum::Value1: j = "VALUE_1"; break;
508
513
  * case MyEnum::Value2: j = "VALUE_2"; break;
@@ -515,7 +520,7 @@ class Zod2Cpp extends core_1.Zod2X {
515
520
  */
516
521
  _createEnumSerializer(parent, childs) {
517
522
  this.imports.add(this.lib.exceptions);
518
- this._push0(this.serializers, `inline void to_json(json& j, const ${parent}& x) {`);
523
+ this._push0(this.serializers, `inline void to_json(${nlohmann_1.NLOHMANN}& j, const ${parent}& x) {`);
519
524
  this._push1(this.serializers, `switch (x) {`);
520
525
  childs.forEach((i) => {
521
526
  const value = isNaN(Number(i.origValue)) ? `"${i.origValue}"` : i.origValue;
@@ -530,7 +535,7 @@ class Zod2Cpp extends core_1.Zod2X {
530
535
  * @description Generates a JSON deserializer for an enum. Maps strings to enum values, with an
531
536
  * error for unexpected strings.
532
537
  * Ex:
533
- * inline void from_json(const json& j, MyEnum& x) {
538
+ * inline void from_json(const nlohmann::json& j, MyEnum& x) {
534
539
  * if (j == "VALUE_1") x = MyEnum::Value1;
535
540
  * else if (j == "VALUE_2") x = MyEnum::Value2;
536
541
  * else {
@@ -542,7 +547,7 @@ class Zod2Cpp extends core_1.Zod2X {
542
547
  */
543
548
  _createEnumDeserializer(parent, childs) {
544
549
  this.imports.add(this.lib.exceptions);
545
- this._push0(this.serializers, `inline void from_json(const json& j, ${parent}& x) {`);
550
+ this._push0(this.serializers, `inline void from_json(const ${nlohmann_1.NLOHMANN}& j, ${parent}& x) {`);
546
551
  childs.forEach((i, index) => {
547
552
  const value = isNaN(Number(i.origValue)) ? `"${i.origValue}"` : i.origValue;
548
553
  if (index === 0) {
@@ -560,7 +565,7 @@ class Zod2Cpp extends core_1.Zod2X {
560
565
  * multiple types.
561
566
  *
562
567
  * @example
563
- * // inline void to_json(json& j, const DerivedType& x) {
568
+ * // inline void to_json(nlohmann::json& j, const DerivedType& x) {
564
569
  * // to_json(j, static_cast<const BaseType1&>(x));
565
570
  * // to_json(j, static_cast<const BaseType2&>(x));
566
571
  * // }
@@ -569,7 +574,7 @@ class Zod2Cpp extends core_1.Zod2X {
569
574
  * @param itemsType - An array of strings representing the names of the base types to serialize.
570
575
  */
571
576
  _createIntersectionSerializer(intersectName, itemsType) {
572
- this._push0(this.serializers, `inline void to_json(json& j, const ${intersectName}& x) {`);
577
+ this._push0(this.serializers, `inline void to_json(${nlohmann_1.NLOHMANN}& j, const ${intersectName}& x) {`);
573
578
  itemsType.forEach((i) => this._push1(this.serializers, `to_json(j, static_cast<const ${i}&>(x));`));
574
579
  this._push0(this.serializers, `}\n`);
575
580
  }
@@ -578,7 +583,7 @@ class Zod2Cpp extends core_1.Zod2X {
578
583
  * multiple types.
579
584
  *
580
585
  * @example
581
- * // inline void from_json(const json& j, DerivedType& x) {
586
+ * // inline void from_json(const nlohmann::json& j, DerivedType& x) {
582
587
  * // from_json(j, static_cast<BaseType1&>(x));
583
588
  * // from_json(j, static_cast<BaseType2&>(x));
584
589
  * // }
@@ -587,7 +592,7 @@ class Zod2Cpp extends core_1.Zod2X {
587
592
  * @param itemsType - An array of strings representing the names of the base types to deserialize.
588
593
  */
589
594
  _createIntersectionDeserializer(intersectName, itemsType) {
590
- this._push0(this.serializers, `inline void from_json(const json& j, ${intersectName}& x) {`);
595
+ this._push0(this.serializers, `inline void from_json(const ${nlohmann_1.NLOHMANN}& j, ${intersectName}& x) {`);
591
596
  itemsType.forEach((i) => this._push1(this.serializers, `from_json(j, static_cast<${i}&>(x));`));
592
597
  this._push0(this.serializers, `}\n`);
593
598
  }
@@ -602,7 +607,7 @@ class Zod2Cpp extends core_1.Zod2X {
602
607
  * // Given: unionName = "MyUnion", itemsType = {"int", "std::string"}
603
608
  * // The generated output might look like:
604
609
  * //
605
- * // inline void to_json(json& j, const MyUnion& x) {
610
+ * // inline void to_json(nlohmann::json& j, const MyUnion& x) {
606
611
  * // if (x.type() == typeid(int)) {
607
612
  * // j = boost::get<int>(x);
608
613
  * // } else if (x.type() == typeid(std::string)) {
@@ -614,7 +619,7 @@ class Zod2Cpp extends core_1.Zod2X {
614
619
  */
615
620
  _createUnionSerializer(unionName, itemsType) {
616
621
  this.imports.add(this.lib.exceptions);
617
- this._push0(this.serializers, `inline void to_json(json& j, const ${unionName}& x) {`);
622
+ this._push0(this.serializers, `inline void to_json(${nlohmann_1.NLOHMANN}& j, const ${unionName}& x) {`);
618
623
  itemsType.forEach((i, index) => {
619
624
  const condition = index === 0 ? "if" : "else if";
620
625
  this._push1(this.serializers, `${condition} (x.type() == typeid(${i})) {`);
@@ -646,7 +651,7 @@ class Zod2Cpp extends core_1.Zod2X {
646
651
  * // {"type": "PhoneContact", "discriminantValue": "phone"} }
647
652
  * //
648
653
  * // The generated output might look like:
649
- * // inline void from_json(const json& j, MyUnion& x) {
654
+ * // inline void from_json(const nlohmann::json& j, MyUnion& x) {
650
655
  * // const auto& k = j.at("type").get<std::string>();
651
656
  * // if (k == "email") {
652
657
  * // x = j.get<EmailContact>();
@@ -662,7 +667,7 @@ class Zod2Cpp extends core_1.Zod2X {
662
667
  * // Given: unionName = "MyUnion", items = { {"type": "int"}, {"type": "std::string"} }
663
668
  * //
664
669
  * // The generated output might look like:
665
- * // inline void from_json(const json& j, MyUnion& x) {
670
+ * // inline void from_json(const nlohmann::json& j, MyUnion& x) {
666
671
  * // try {
667
672
  * // // Try to deserialize as int
668
673
  * // x = j.get<int>();
@@ -683,7 +688,7 @@ class Zod2Cpp extends core_1.Zod2X {
683
688
  */
684
689
  _createUnionDeserializer(unionName, items, discriminator) {
685
690
  this.imports.add(this.lib.exceptions);
686
- this._push0(this.serializers, `inline void from_json(const json& j, ${unionName}& x) {`);
691
+ this._push0(this.serializers, `inline void from_json(const ${nlohmann_1.NLOHMANN}& j, ${unionName}& x) {`);
687
692
  const useDiscriminator = discriminator && items.every((i) => i.discriminantValue);
688
693
  if (useDiscriminator) {
689
694
  this._push1(this.serializers, `const auto& k = j.at("${discriminator}").get<std::string>();`);
@@ -752,7 +757,7 @@ class Zod2Cpp17 extends Zod2Cpp {
752
757
  * // Given: unionName = "MyUnion", itemsType = {"int", "std::string"}
753
758
  * // The generated output might look like:
754
759
  * //
755
- * // inline void to_json(json& j, const MyUnion& x) {
760
+ * // inline void to_json(nlohmann::json& j, const MyUnion& x) {
756
761
  * // std::visit(
757
762
  * // [&j](auto&& arg) {
758
763
  * // using T = std::decay_t<decltype(arg)>;
@@ -770,7 +775,7 @@ class Zod2Cpp17 extends Zod2Cpp {
770
775
  */
771
776
  _createUnionSerializer(unionName, itemsType) {
772
777
  this.imports.add(this.lib.exceptions);
773
- this._push0(this.serializers, `inline void to_json(json& j, const ${unionName}& x) {`);
778
+ this._push0(this.serializers, `inline void to_json(${nlohmann_1.NLOHMANN}& j, const ${unionName}& x) {`);
774
779
  this._push1(this.serializers, `std::visit(`);
775
780
  this._push2(this.serializers, `[&j](auto&& arg) {`);
776
781
  this._push3(this.serializers, `using T = std::decay_t<decltype(arg)>;`);
@@ -109,6 +109,7 @@ class Zod2Ts extends core_1.Zod2X {
109
109
  return;
110
110
  }
111
111
  let extendedType = undefined;
112
+ this.addComment(data.description);
112
113
  if (data instanceof core_1.ASTArray) {
113
114
  extendedType = this.getAttributeType(data.item);
114
115
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod-to-x",
3
- "version": "2.0.1",
3
+ "version": "2.0.2-dev.5",
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.28 <3.25.50"
48
+ "zod": ">=4.0.0"
49
49
  },
50
50
  "dependencies": {
51
51
  "case": "1.6.3"