zod-to-x 2.0.2-dev.2 → 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.
- package/dist/converters/protobuf_v3/runner.d.ts +2 -3
- package/dist/converters/protobuf_v3/runner.js +4 -1
- package/dist/core/ast_node.js +1 -1
- package/dist/lib/zod_ext.d.ts +5 -4
- package/dist/lib/zod_helpers.d.ts +3 -3
- package/dist/lib/zod_helpers.js +4 -4
- package/dist/transpilers/cpp/libs.d.ts +0 -3
- package/dist/transpilers/cpp/libs.js +0 -4
- package/dist/transpilers/cpp/nlohmann.d.ts +1 -0
- package/dist/transpilers/cpp/nlohmann.js +5 -3
- package/dist/transpilers/cpp/runner.d.ts +12 -12
- package/dist/transpilers/cpp/runner.js +29 -28
- package/dist/transpilers/typescript/runner.js +1 -0
- package/package.json +2 -2
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { ZodDiscriminatedUnion, ZodUnion } from "zod";
|
|
2
1
|
import { IZod2AstOpt } from "../../core";
|
|
3
|
-
import { ZodObject } from "../../lib/zod_helpers";
|
|
2
|
+
import { ZodObject, ZodDiscriminatedUnion, ZodUnion } from "../../lib/zod_helpers";
|
|
4
3
|
import { IZod2ProtoV3Opt } from "./options";
|
|
5
4
|
/**
|
|
6
5
|
* Converts a Zod schema into a Protocol Buffers v3 definition.
|
|
@@ -17,5 +16,5 @@ import { IZod2ProtoV3Opt } from "./options";
|
|
|
17
16
|
* definition.
|
|
18
17
|
* @returns The Protocol Buffers v3 definition as a string.
|
|
19
18
|
*/
|
|
20
|
-
export declare function zod2ProtoV3(schema: ZodObject<any> | ZodDiscriminatedUnion
|
|
19
|
+
export declare function zod2ProtoV3(schema: ZodObject<any> | ZodDiscriminatedUnion | ZodUnion<any>, // TODO: fix any to force only ZodObjects
|
|
21
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 (
|
|
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
|
}
|
package/dist/core/ast_node.js
CHANGED
|
@@ -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({
|
package/dist/lib/zod_ext.d.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
|
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
|
|
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.
|
|
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
|
}
|
package/dist/lib/zod_helpers.js
CHANGED
|
@@ -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;
|
|
@@ -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,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(
|
|
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(
|
|
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
|
|
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
|
|
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 (
|
|
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;
|
|
@@ -416,7 +417,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
416
417
|
*/
|
|
417
418
|
_createStructSerializer(parent, childs) {
|
|
418
419
|
const prefix = this.opt.namespace ? `${this.opt.namespace}::` : "";
|
|
419
|
-
this._push0(this.serializers, `inline void to_json(
|
|
420
|
+
this._push0(this.serializers, `inline void to_json(${nlohmann_1.NLOHMANN}& j, const ${parent}& x) {`);
|
|
420
421
|
childs.forEach((i) => {
|
|
421
422
|
if (i.required) {
|
|
422
423
|
this._push1(this.serializers, `j["${i.origName}"] = x.${i.snakeName};`);
|
|
@@ -431,7 +432,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
431
432
|
* @description Generates a JSON deserializer for a struct. Each required field is deserialized
|
|
432
433
|
* using at, while optional fields are handled with get_opt.
|
|
433
434
|
* Ex:
|
|
434
|
-
* inline void from_json(const json& j, MyStruct& x) {
|
|
435
|
+
* inline void from_json(const nlohmann::json& j, MyStruct& x) {
|
|
435
436
|
* x.required_field(j.at("requiredField").get<int>());
|
|
436
437
|
* x.optional_field(get_opt<std::string>(j, "optionalField"));
|
|
437
438
|
* }
|
|
@@ -440,7 +441,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
440
441
|
*/
|
|
441
442
|
_createStructDeserializer(parent, childs) {
|
|
442
443
|
const prefix = this.opt.namespace ? `${this.opt.namespace}::` : "";
|
|
443
|
-
this._push0(this.serializers, `inline void from_json(const
|
|
444
|
+
this._push0(this.serializers, `inline void from_json(const ${nlohmann_1.NLOHMANN}& j, ${parent}& x) {`);
|
|
444
445
|
childs.forEach((i) => {
|
|
445
446
|
if (i.required) {
|
|
446
447
|
this._push1(this.serializers, `x.${i.snakeName} = j.at("${i.origName}").get<${i.typeName}>();`);
|
|
@@ -456,7 +457,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
456
457
|
* access class attributes. Optional fields are checked for existence before
|
|
457
458
|
* serialization.
|
|
458
459
|
* Ex:
|
|
459
|
-
* inline void to_json(json& j, const MyClass& x) {
|
|
460
|
+
* inline void to_json(nlohmann::json& j, const MyClass& x) {
|
|
460
461
|
* j["requiredField"] = x.get_required_field();
|
|
461
462
|
* if (x.get_optional_field()) {
|
|
462
463
|
* j["optionalField"] = x.get_optional_field();
|
|
@@ -467,7 +468,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
467
468
|
*/
|
|
468
469
|
_createClassSerializer(parent, childs) {
|
|
469
470
|
const prefix = this.opt.namespace ? `${this.opt.namespace}::` : "";
|
|
470
|
-
this._push0(this.serializers, `inline void to_json(
|
|
471
|
+
this._push0(this.serializers, `inline void to_json(${nlohmann_1.NLOHMANN}& j, const ${parent}& x) {`);
|
|
471
472
|
childs.forEach((i) => {
|
|
472
473
|
if (i.required) {
|
|
473
474
|
this._push1(this.serializers, `j["${i.origName}"] = x.get_${i.snakeName}();`);
|
|
@@ -482,7 +483,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
482
483
|
* @description Generates a JSON deserializer for a class. The deserializer uses setter methods
|
|
483
484
|
* to populate class attributes.
|
|
484
485
|
* Ex:
|
|
485
|
-
* inline void from_json(const json& j, MyClass& x) {
|
|
486
|
+
* inline void from_json(const nlohmann::json& j, MyClass& x) {
|
|
486
487
|
* x.set_required_field(j.at("requiredField").get<int>());
|
|
487
488
|
* x.set_optional_field(get_opt<std::string>(j, "optionalField"));
|
|
488
489
|
* }
|
|
@@ -491,7 +492,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
491
492
|
*/
|
|
492
493
|
_createClassDeserializer(parent, childs) {
|
|
493
494
|
const prefix = this.opt.namespace ? `${this.opt.namespace}::` : "";
|
|
494
|
-
this._push0(this.serializers, `inline void from_json(const
|
|
495
|
+
this._push0(this.serializers, `inline void from_json(const ${nlohmann_1.NLOHMANN}& j, ${parent}& x) {`);
|
|
495
496
|
childs.forEach((i) => {
|
|
496
497
|
if (i.required) {
|
|
497
498
|
this._push1(this.serializers, `x.set_${i.snakeName}(j.at("${i.origName}").get<${i.typeName}>());`);
|
|
@@ -506,7 +507,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
506
507
|
* @description Generates a JSON serializer for an enum. Maps enum values to strings for
|
|
507
508
|
* serialization, with a default case for unexpected values.
|
|
508
509
|
* Ex:
|
|
509
|
-
* inline void to_json(json& j, const MyEnum& x) {
|
|
510
|
+
* inline void to_json(nlohmann::json& j, const MyEnum& x) {
|
|
510
511
|
* switch (x) {
|
|
511
512
|
* case MyEnum::Value1: j = "VALUE_1"; break;
|
|
512
513
|
* case MyEnum::Value2: j = "VALUE_2"; break;
|
|
@@ -519,7 +520,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
519
520
|
*/
|
|
520
521
|
_createEnumSerializer(parent, childs) {
|
|
521
522
|
this.imports.add(this.lib.exceptions);
|
|
522
|
-
this._push0(this.serializers, `inline void to_json(
|
|
523
|
+
this._push0(this.serializers, `inline void to_json(${nlohmann_1.NLOHMANN}& j, const ${parent}& x) {`);
|
|
523
524
|
this._push1(this.serializers, `switch (x) {`);
|
|
524
525
|
childs.forEach((i) => {
|
|
525
526
|
const value = isNaN(Number(i.origValue)) ? `"${i.origValue}"` : i.origValue;
|
|
@@ -534,7 +535,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
534
535
|
* @description Generates a JSON deserializer for an enum. Maps strings to enum values, with an
|
|
535
536
|
* error for unexpected strings.
|
|
536
537
|
* Ex:
|
|
537
|
-
* inline void from_json(const json& j, MyEnum& x) {
|
|
538
|
+
* inline void from_json(const nlohmann::json& j, MyEnum& x) {
|
|
538
539
|
* if (j == "VALUE_1") x = MyEnum::Value1;
|
|
539
540
|
* else if (j == "VALUE_2") x = MyEnum::Value2;
|
|
540
541
|
* else {
|
|
@@ -546,7 +547,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
546
547
|
*/
|
|
547
548
|
_createEnumDeserializer(parent, childs) {
|
|
548
549
|
this.imports.add(this.lib.exceptions);
|
|
549
|
-
this._push0(this.serializers, `inline void from_json(const
|
|
550
|
+
this._push0(this.serializers, `inline void from_json(const ${nlohmann_1.NLOHMANN}& j, ${parent}& x) {`);
|
|
550
551
|
childs.forEach((i, index) => {
|
|
551
552
|
const value = isNaN(Number(i.origValue)) ? `"${i.origValue}"` : i.origValue;
|
|
552
553
|
if (index === 0) {
|
|
@@ -564,7 +565,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
564
565
|
* multiple types.
|
|
565
566
|
*
|
|
566
567
|
* @example
|
|
567
|
-
* // inline void to_json(json& j, const DerivedType& x) {
|
|
568
|
+
* // inline void to_json(nlohmann::json& j, const DerivedType& x) {
|
|
568
569
|
* // to_json(j, static_cast<const BaseType1&>(x));
|
|
569
570
|
* // to_json(j, static_cast<const BaseType2&>(x));
|
|
570
571
|
* // }
|
|
@@ -573,7 +574,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
573
574
|
* @param itemsType - An array of strings representing the names of the base types to serialize.
|
|
574
575
|
*/
|
|
575
576
|
_createIntersectionSerializer(intersectName, itemsType) {
|
|
576
|
-
this._push0(this.serializers, `inline void to_json(
|
|
577
|
+
this._push0(this.serializers, `inline void to_json(${nlohmann_1.NLOHMANN}& j, const ${intersectName}& x) {`);
|
|
577
578
|
itemsType.forEach((i) => this._push1(this.serializers, `to_json(j, static_cast<const ${i}&>(x));`));
|
|
578
579
|
this._push0(this.serializers, `}\n`);
|
|
579
580
|
}
|
|
@@ -582,7 +583,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
582
583
|
* multiple types.
|
|
583
584
|
*
|
|
584
585
|
* @example
|
|
585
|
-
* // inline void from_json(const json& j, DerivedType& x) {
|
|
586
|
+
* // inline void from_json(const nlohmann::json& j, DerivedType& x) {
|
|
586
587
|
* // from_json(j, static_cast<BaseType1&>(x));
|
|
587
588
|
* // from_json(j, static_cast<BaseType2&>(x));
|
|
588
589
|
* // }
|
|
@@ -591,7 +592,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
591
592
|
* @param itemsType - An array of strings representing the names of the base types to deserialize.
|
|
592
593
|
*/
|
|
593
594
|
_createIntersectionDeserializer(intersectName, itemsType) {
|
|
594
|
-
this._push0(this.serializers, `inline void from_json(const
|
|
595
|
+
this._push0(this.serializers, `inline void from_json(const ${nlohmann_1.NLOHMANN}& j, ${intersectName}& x) {`);
|
|
595
596
|
itemsType.forEach((i) => this._push1(this.serializers, `from_json(j, static_cast<${i}&>(x));`));
|
|
596
597
|
this._push0(this.serializers, `}\n`);
|
|
597
598
|
}
|
|
@@ -606,7 +607,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
606
607
|
* // Given: unionName = "MyUnion", itemsType = {"int", "std::string"}
|
|
607
608
|
* // The generated output might look like:
|
|
608
609
|
* //
|
|
609
|
-
* // inline void to_json(json& j, const MyUnion& x) {
|
|
610
|
+
* // inline void to_json(nlohmann::json& j, const MyUnion& x) {
|
|
610
611
|
* // if (x.type() == typeid(int)) {
|
|
611
612
|
* // j = boost::get<int>(x);
|
|
612
613
|
* // } else if (x.type() == typeid(std::string)) {
|
|
@@ -618,7 +619,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
618
619
|
*/
|
|
619
620
|
_createUnionSerializer(unionName, itemsType) {
|
|
620
621
|
this.imports.add(this.lib.exceptions);
|
|
621
|
-
this._push0(this.serializers, `inline void to_json(
|
|
622
|
+
this._push0(this.serializers, `inline void to_json(${nlohmann_1.NLOHMANN}& j, const ${unionName}& x) {`);
|
|
622
623
|
itemsType.forEach((i, index) => {
|
|
623
624
|
const condition = index === 0 ? "if" : "else if";
|
|
624
625
|
this._push1(this.serializers, `${condition} (x.type() == typeid(${i})) {`);
|
|
@@ -650,7 +651,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
650
651
|
* // {"type": "PhoneContact", "discriminantValue": "phone"} }
|
|
651
652
|
* //
|
|
652
653
|
* // The generated output might look like:
|
|
653
|
-
* // inline void from_json(const json& j, MyUnion& x) {
|
|
654
|
+
* // inline void from_json(const nlohmann::json& j, MyUnion& x) {
|
|
654
655
|
* // const auto& k = j.at("type").get<std::string>();
|
|
655
656
|
* // if (k == "email") {
|
|
656
657
|
* // x = j.get<EmailContact>();
|
|
@@ -666,7 +667,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
666
667
|
* // Given: unionName = "MyUnion", items = { {"type": "int"}, {"type": "std::string"} }
|
|
667
668
|
* //
|
|
668
669
|
* // The generated output might look like:
|
|
669
|
-
* // inline void from_json(const json& j, MyUnion& x) {
|
|
670
|
+
* // inline void from_json(const nlohmann::json& j, MyUnion& x) {
|
|
670
671
|
* // try {
|
|
671
672
|
* // // Try to deserialize as int
|
|
672
673
|
* // x = j.get<int>();
|
|
@@ -687,7 +688,7 @@ class Zod2Cpp extends core_1.Zod2X {
|
|
|
687
688
|
*/
|
|
688
689
|
_createUnionDeserializer(unionName, items, discriminator) {
|
|
689
690
|
this.imports.add(this.lib.exceptions);
|
|
690
|
-
this._push0(this.serializers, `inline void from_json(const
|
|
691
|
+
this._push0(this.serializers, `inline void from_json(const ${nlohmann_1.NLOHMANN}& j, ${unionName}& x) {`);
|
|
691
692
|
const useDiscriminator = discriminator && items.every((i) => i.discriminantValue);
|
|
692
693
|
if (useDiscriminator) {
|
|
693
694
|
this._push1(this.serializers, `const auto& k = j.at("${discriminator}").get<std::string>();`);
|
|
@@ -756,7 +757,7 @@ class Zod2Cpp17 extends Zod2Cpp {
|
|
|
756
757
|
* // Given: unionName = "MyUnion", itemsType = {"int", "std::string"}
|
|
757
758
|
* // The generated output might look like:
|
|
758
759
|
* //
|
|
759
|
-
* // inline void to_json(json& j, const MyUnion& x) {
|
|
760
|
+
* // inline void to_json(nlohmann::json& j, const MyUnion& x) {
|
|
760
761
|
* // std::visit(
|
|
761
762
|
* // [&j](auto&& arg) {
|
|
762
763
|
* // using T = std::decay_t<decltype(arg)>;
|
|
@@ -774,7 +775,7 @@ class Zod2Cpp17 extends Zod2Cpp {
|
|
|
774
775
|
*/
|
|
775
776
|
_createUnionSerializer(unionName, itemsType) {
|
|
776
777
|
this.imports.add(this.lib.exceptions);
|
|
777
|
-
this._push0(this.serializers, `inline void to_json(
|
|
778
|
+
this._push0(this.serializers, `inline void to_json(${nlohmann_1.NLOHMANN}& j, const ${unionName}& x) {`);
|
|
778
779
|
this._push1(this.serializers, `std::visit(`);
|
|
779
780
|
this._push2(this.serializers, `[&j](auto&& arg) {`);
|
|
780
781
|
this._push3(this.serializers, `using T = std::decay_t<decltype(arg)>;`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zod-to-x",
|
|
3
|
-
"version": "2.0.2-dev.
|
|
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": ">=
|
|
48
|
+
"zod": ">=4.0.0"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
51
|
"case": "1.6.3"
|