zod-to-x 2.0.1-dev.4 → 2.0.2-dev.2

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
@@ -18,7 +18,7 @@
18
18
 
19
19
  [`@zod-to-x`](https://github.com/rroumenov/zod-to-x`) is a Zod-based library designed to establish a centralized methodology for defining data structures. It allows you to transpile these definitions into various programming languages in a clear and straightforward way. This tool addresses a fundamental requirement of any software: having a clear understanding of the data it handles.
20
20
 
21
- <span style="color: red;">**Important Announcement:**</span> `zod-to-x@2.0.0` has been released, introducing migration to Zod V4. At this stage, only the existent behavior has been migrated, while new features like Literal Templates are still under analysis. Additionally, `zod-to-x@1.X.Y` will continue to be maintained for Zod V3, and any new transpilation languages will also be supported in version 1.
21
+ <span style="color: red;">**Important Announcement:**</span> `zod-to-x@2.0.0` has been released, introducing migration to Zod V4. At this stage, only the existent behavior has been migrated, while new features like Literal Templates are still under analysis. Only the complete Zod V4 version will be supported, **not v4-mini**. Additionally, `zod-to-x@1.X.Y` will continue to be maintained for Zod V3, and any new transpilation languages will also be supported in version 1.
22
22
 
23
23
 
24
24
 
@@ -521,8 +521,9 @@ In case of use of Google protobuf to improve communication performance, you can
521
521
  - **packageName**: Name of the protobuf file package.
522
522
  - **header**: Text to add as a comment at the beginning of the output.
523
523
  - **indent**: Number of spaces to use for indentation in the generated code. Defaults to 4 if not specified.
524
- - **includeComments**: Determines whether to include comments in the transpiled code. Defaults to true.
524
+ - **includeComments**: Determines whether to include comments in the transpiled code. Defaults to `true`.
525
525
  - **keepKeys**: Specifies whether property names should follow the Google Protobuf naming convention (false) or remain as originally defined (true). The default is `false`.
526
+ - **encodeDoubleAsInt**: Double values will be represented as integers in the proto file. Defaults to `false`.
526
527
 
527
528
  - Limitations:
528
529
  - ZodTuple is supported only for items of the same type.
@@ -9,5 +9,11 @@ export interface IZod2ProtoV3Opt extends IZodToXOpt {
9
9
  * language's naming conventions. If set to true, the original property names are preserved.
10
10
  */
11
11
  keepKeys?: boolean;
12
+ /**
13
+ * If true, double values will be represented as integers in the generated protobuf file.
14
+ * This could be useful when integer representation could be done with int32 or smaller because
15
+ * output will be more compact. Default is false.
16
+ */
17
+ encodeDoubleAsInt?: boolean;
12
18
  }
13
19
  export declare const defaultOpts: IZod2ProtoV3Opt;
@@ -5,5 +5,6 @@ exports.defaultOpts = {
5
5
  includeComments: true,
6
6
  indent: 4,
7
7
  keepKeys: false,
8
+ encodeDoubleAsInt: false,
8
9
  useImports: false, // Not required for protobuf files
9
10
  };
@@ -1,3 +1,4 @@
1
+ import { ZodDiscriminatedUnion, ZodUnion } from "zod";
1
2
  import { IZod2AstOpt } from "../../core";
2
3
  import { ZodObject } from "../../lib/zod_helpers";
3
4
  import { IZod2ProtoV3Opt } from "./options";
@@ -16,4 +17,5 @@ import { IZod2ProtoV3Opt } from "./options";
16
17
  * definition.
17
18
  * @returns The Protocol Buffers v3 definition as a string.
18
19
  */
19
- export declare function zod2ProtoV3(schema: ZodObject<any>, opt?: Pick<IZod2AstOpt, "strict"> & Pick<IZod2ProtoV3Opt, "packageName" | "keepKeys" | "header" | "indent" | "includeComments">): string;
20
+ export declare function zod2ProtoV3(schema: ZodObject<any> | ZodDiscriminatedUnion<string, any> | ZodUnion<any>, // TODO: fix any to force only ZodObjects
21
+ opt?: Pick<IZod2AstOpt, "strict"> & Pick<IZod2ProtoV3Opt, "packageName" | "keepKeys" | "header" | "indent" | "includeComments" | "encodeDoubleAsInt">): string;
@@ -34,7 +34,7 @@ class Zod2ProtoV3 extends core_1.Zod2X {
34
34
  this.getBooleanType = () => "bool";
35
35
  this.getStringType = () => "string";
36
36
  this.getNumberType = (isInt, range) => {
37
- if (!isInt) {
37
+ if (!isInt && this.opt.encodeDoubleAsInt !== true) {
38
38
  return "double";
39
39
  }
40
40
  if ((range === null || range === void 0 ? void 0 : range.min) >= number_limits_1.UINT32_RANGES[0]) {
@@ -243,7 +243,8 @@ class Zod2ProtoV3 extends core_1.Zod2X {
243
243
  * definition.
244
244
  * @returns The Protocol Buffers v3 definition as a string.
245
245
  */
246
- function zod2ProtoV3(schema, opt = {}) {
246
+ function zod2ProtoV3(schema, // TODO: fix any to force only ZodObjects
247
+ opt = {}) {
247
248
  const astNode = new core_1.Zod2Ast({ strict: opt.strict }).build(schema);
248
249
  return new Zod2ProtoV3(opt).transpile(astNode);
249
250
  }
@@ -415,13 +415,14 @@ class Zod2Cpp extends core_1.Zod2X {
415
415
  * @param childs - Structure attributes data.
416
416
  */
417
417
  _createStructSerializer(parent, childs) {
418
+ const prefix = this.opt.namespace ? `${this.opt.namespace}::` : "";
418
419
  this._push0(this.serializers, `inline void to_json(json& j, const ${parent}& x) {`);
419
420
  childs.forEach((i) => {
420
421
  if (i.required) {
421
422
  this._push1(this.serializers, `j["${i.origName}"] = x.${i.snakeName};`);
422
423
  }
423
424
  else {
424
- this._push1(this.serializers, `set_opt<${i.typeName}>(j, "${i.origName}", x.${i.snakeName});`);
425
+ this._push1(this.serializers, `${prefix}set_opt<${i.typeName}>(j, "${i.origName}", x.${i.snakeName});`);
425
426
  }
426
427
  });
427
428
  this._push0(this.serializers, "}\n");
@@ -438,13 +439,14 @@ class Zod2Cpp extends core_1.Zod2X {
438
439
  * @param childs - Structure attributes data.
439
440
  */
440
441
  _createStructDeserializer(parent, childs) {
442
+ const prefix = this.opt.namespace ? `${this.opt.namespace}::` : "";
441
443
  this._push0(this.serializers, `inline void from_json(const json& j, ${parent}& x) {`);
442
444
  childs.forEach((i) => {
443
445
  if (i.required) {
444
446
  this._push1(this.serializers, `x.${i.snakeName} = j.at("${i.origName}").get<${i.typeName}>();`);
445
447
  }
446
448
  else {
447
- this._push1(this.serializers, `x.${i.snakeName} = get_opt<${i.typeName}>(j, "${i.origName}");`);
449
+ this._push1(this.serializers, `x.${i.snakeName} = ${prefix}get_opt<${i.typeName}>(j, "${i.origName}");`);
448
450
  }
449
451
  });
450
452
  this._push0(this.serializers, "}\n");
@@ -464,13 +466,14 @@ class Zod2Cpp extends core_1.Zod2X {
464
466
  * @param childs - Structure attributes data.
465
467
  */
466
468
  _createClassSerializer(parent, childs) {
469
+ const prefix = this.opt.namespace ? `${this.opt.namespace}::` : "";
467
470
  this._push0(this.serializers, `inline void to_json(json& j, const ${parent}& x) {`);
468
471
  childs.forEach((i) => {
469
472
  if (i.required) {
470
473
  this._push1(this.serializers, `j["${i.origName}"] = x.get_${i.snakeName}();`);
471
474
  }
472
475
  else {
473
- this._push1(this.serializers, `set_opt<${i.typeName}>(j, "${i.origName}", x.get_${i.snakeName}());`);
476
+ this._push1(this.serializers, `${prefix}set_opt<${i.typeName}>(j, "${i.origName}", x.get_${i.snakeName}());`);
474
477
  }
475
478
  });
476
479
  this._push0(this.serializers, "}\n");
@@ -487,13 +490,14 @@ class Zod2Cpp extends core_1.Zod2X {
487
490
  * @param childs - Structure attributes data.
488
491
  */
489
492
  _createClassDeserializer(parent, childs) {
493
+ const prefix = this.opt.namespace ? `${this.opt.namespace}::` : "";
490
494
  this._push0(this.serializers, `inline void from_json(const json& j, ${parent}& x) {`);
491
495
  childs.forEach((i) => {
492
496
  if (i.required) {
493
497
  this._push1(this.serializers, `x.set_${i.snakeName}(j.at("${i.origName}").get<${i.typeName}>());`);
494
498
  }
495
499
  else {
496
- this._push1(this.serializers, `x.set_${i.snakeName}(get_opt<${i.typeName}>(j, "${i.origName}"));`);
500
+ this._push1(this.serializers, `x.set_${i.snakeName}(${prefix}get_opt<${i.typeName}>(j, "${i.origName}"));`);
497
501
  }
498
502
  });
499
503
  this._push0(this.serializers, "}\n");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod-to-x",
3
- "version": "2.0.1-dev.4",
3
+ "version": "2.0.2-dev.2",
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"
48
+ "zod": ">=3.25.28 <3.25.50"
49
49
  },
50
50
  "dependencies": {
51
51
  "case": "1.6.3"