zod-to-x 1.4.7-dev.7 → 2.0.1-dev.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/README.md +21 -69
- package/dist/converters/index.d.ts +0 -1
- package/dist/converters/index.js +1 -3
- package/dist/core/ast-types/ast_complex.d.ts +1 -1
- package/dist/core/ast-types/ast_complex.js +1 -1
- package/dist/core/ast_node.js +65 -63
- package/dist/lib/zod_ext.d.ts +26 -50
- package/dist/lib/zod_ext.js +7 -13
- package/dist/lib/zod_helpers.d.ts +55 -36
- package/dist/lib/zod_helpers.js +81 -69
- package/dist/transpilers/cpp/runner.js +0 -1
- package/dist/transpilers/typescript/runner.js +0 -1
- package/package.json +3 -4
- package/dist/converters/json_schema_definitions.d.ts +0 -20
- package/dist/converters/json_schema_definitions.js +0 -70
package/README.md
CHANGED
|
@@ -16,7 +16,9 @@
|
|
|
16
16
|
|
|
17
17
|
|
|
18
18
|
|
|
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.
|
|
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
|
+
|
|
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.
|
|
20
22
|
|
|
21
23
|
|
|
22
24
|
|
|
@@ -34,7 +36,6 @@
|
|
|
34
36
|
- [Typescript](#1-typescript)
|
|
35
37
|
- [C++](#2-c)
|
|
36
38
|
- [Additional utils](#additional-utils)
|
|
37
|
-
- [JSON Schema definitions](#1-zod2jsonschemadefinitions)
|
|
38
39
|
- [Protobuf V3 generation](#2-zod2protov3)
|
|
39
40
|
- [Mapping of supported Zod Types by Language](#mapping-of-supported-zod-types-by-langauge)
|
|
40
41
|
- [Considerations](#considerations)
|
|
@@ -45,7 +46,7 @@
|
|
|
45
46
|
Managing data consistency across multiple layers and languages is a common challenge in modern software development. [`@zod-to-x`](https://github.com/rroumenov/zod-to-x) solves this by allowing you to define your data models once and effortlessly transpile them into multiple programming languages. Here’s why it stands out:
|
|
46
47
|
|
|
47
48
|
1. **Centralized Data Definition**
|
|
48
|
-
Define your data structures in one place using the powerful [`@zod`](https://github.com/colinhacks/zod) library. This eliminates redundancy, reduces inconsistencies, and simplifies maintenance across your entire codebase, all while allowing you to continue leveraging any npm package in the [`@zod`](https://github.com/colinhacks/zod) ecosystem
|
|
49
|
+
Define your data structures in one place using the powerful [`@zod`](https://github.com/colinhacks/zod) library. This eliminates redundancy, reduces inconsistencies, and simplifies maintenance across your entire codebase, all while allowing you to continue leveraging any npm package in the [`@zod`](https://github.com/colinhacks/zod) ecosystem.
|
|
49
50
|
|
|
50
51
|
2. **Multi-Language Compatibility**
|
|
51
52
|
Generate data models for TypeScript, Protobuf V3 and C++ (with languages like Golang on the roadmap). No more manually rewriting models for different platforms.
|
|
@@ -59,18 +60,17 @@ Automate the transpilation of data models to save time, reduce errors, and let y
|
|
|
59
60
|
```bash
|
|
60
61
|
npm install zod-to-x zod
|
|
61
62
|
```
|
|
62
|
-
(*) [`zod@3.
|
|
63
|
+
(*) [`zod@3.25.28`](https://www.npmjs.com/package/zod/v/3.25.0) version or greather is required.
|
|
63
64
|
|
|
64
65
|
### 2) Extend Zod using the `extendZod` method after the first [`@zod`](https://github.com/colinhacks/zod) import:
|
|
65
66
|
```ts
|
|
66
|
-
import { z } from "zod";
|
|
67
|
+
import { z } from "zod/v4";
|
|
67
68
|
import { extendZod } from "zod-to-x";
|
|
68
69
|
extendZod(z);
|
|
69
70
|
```
|
|
70
71
|
|
|
71
72
|
This extension appends a `zod2x` method to:
|
|
72
73
|
- ZodEnum
|
|
73
|
-
- ZodNativeEnum
|
|
74
74
|
- ZodObject
|
|
75
75
|
- ZodUnion
|
|
76
76
|
- ZodDiscriminatedUnion
|
|
@@ -79,7 +79,7 @@ This extension appends a `zod2x` method to:
|
|
|
79
79
|
|
|
80
80
|
## Quick start
|
|
81
81
|
```ts
|
|
82
|
-
import { z } from "zod";
|
|
82
|
+
import { z } from "zod/v4";
|
|
83
83
|
import { extendZod, Zod2Ast, Zod2XTranspilers } from "zod-to-x";
|
|
84
84
|
extendZod(z); // The extend step can be skipped if it has already been done.
|
|
85
85
|
|
|
@@ -87,9 +87,9 @@ extendZod(z); // The extend step can be skipped if it has already been done.
|
|
|
87
87
|
* 1) Define a data model using Zod schemas
|
|
88
88
|
*/
|
|
89
89
|
const VisitorSchema = z.object({
|
|
90
|
-
id: z.
|
|
90
|
+
id: z.uuid(),
|
|
91
91
|
name: z.string(),
|
|
92
|
-
email: z.
|
|
92
|
+
email: z.email(),
|
|
93
93
|
visitDate: z.date(),
|
|
94
94
|
comments: z.string().optional(),
|
|
95
95
|
}).zod2x("Visitor"); // Add your expected output type name using the 'zod2x' method.
|
|
@@ -185,7 +185,7 @@ const Intersection = z.intersection(DataA, DataB).zod2x("Intersection");
|
|
|
185
185
|
|
|
186
186
|
### Tips for discriminated unions
|
|
187
187
|
To enhance data modeling and the serialization/deserialization of a discriminated union type, two key steps should be considered:
|
|
188
|
-
- Using `ZodEnum`
|
|
188
|
+
- Using `ZodEnum` the define the discriminator key options.
|
|
189
189
|
- Using `ZodLiteral` to define the specific value of the discriminator key.
|
|
190
190
|
|
|
191
191
|
Example of discriminant definition:
|
|
@@ -265,6 +265,7 @@ To achieve this, new components are included:
|
|
|
265
265
|
- **file**: Specifies the expected output file where the transpiled types will be saved.
|
|
266
266
|
- **externalInheritance**: When a type from one layer is imported into another layer without modifications, it is transpiled as a new type inheriting from the imported type. This ensures type consistency across layers while maintaining reusability. See example (4) below. The default value is `true`.
|
|
267
267
|
- **basicTypes**: Since `v1.4.6`, primitive data types and arrays are also transpiled when using Layered modeling **if they are declared as property inside the layer class**. They are output as aliases of the types they represent. Setting it to `false` will disable this behavior (except for arrays, which are always transpiled). Default is `true`.
|
|
268
|
+
- **skipLayerInterface**: Before `v2.0.0`, a global interface was transpiled with all layer properties. With `externalInheritance` and `basicType`, this behavior has started to become obsolete, so it is not included by default. If needed, set to `false`.
|
|
268
269
|
- **Zod2XMixin**: A function that enables the creation of layers by extending multiple data models, thereby simplifying their definition and organization.
|
|
269
270
|
|
|
270
271
|
### Usage example
|
|
@@ -275,13 +276,13 @@ class UserModels extends Zod2XModel {
|
|
|
275
276
|
|
|
276
277
|
userRole = z.enum(["Admin", "User"]).zod2x("UserRole"); // (*)
|
|
277
278
|
|
|
278
|
-
userEmail = z.
|
|
279
|
+
userEmail = z.email(); // This will be transpiled as an alias.
|
|
279
280
|
|
|
280
281
|
userEntity = z.object({
|
|
281
|
-
id: z.
|
|
282
|
+
id: z.uuid(),
|
|
282
283
|
name: z.string().min(1),
|
|
283
284
|
email: this.userEmail,
|
|
284
|
-
age: z.
|
|
285
|
+
age: z.int().nonnegative().optional(),
|
|
285
286
|
role: this.userRole,
|
|
286
287
|
}); // (*)
|
|
287
288
|
}
|
|
@@ -304,11 +305,6 @@ console.log(userModels.transpile(Zod2XTranspilers.Zod2Ts));
|
|
|
304
305
|
// role: UserRole;
|
|
305
306
|
// }
|
|
306
307
|
|
|
307
|
-
// export interface UserModels {
|
|
308
|
-
// userRole: UserRole;
|
|
309
|
-
// userEntity: UserEntity;
|
|
310
|
-
// }
|
|
311
|
-
|
|
312
308
|
```
|
|
313
309
|
**(*)** Thanks to the Layer decorator, the use of `zod2x` for type naming can now be omitted, simplifying and clarifying the process of model definition. By default, it will take the property name and apply a Pascal case convention (example: *myProperty* -> *MyProperty*).
|
|
314
310
|
You can still use it if you want to enforce a different type name.
|
|
@@ -350,11 +346,6 @@ console.log(userDtos.transpile(Zod2XTranspilers.Zod2Ts))
|
|
|
350
346
|
// updatedAt: Date;
|
|
351
347
|
// }
|
|
352
348
|
|
|
353
|
-
// export interface UserDtos {
|
|
354
|
-
// createUserUseCaseDto: CreateUserUseCaseDto;
|
|
355
|
-
// createUserUseCaseResultDto: CreateUserUseCaseResultDto;
|
|
356
|
-
// }
|
|
357
|
-
|
|
358
349
|
```
|
|
359
350
|
**(*)** Any modification of an existing model (in this case, `userEntity`) will lose the relation with that model, but not its children. In the case of the `CreateUserUseCaseDto` type, the `role` field will remain linked to the existing one in the `user.entity` file.
|
|
360
351
|
|
|
@@ -407,11 +398,6 @@ console.log(userDtos.transpile(Zod2XTranspilers.Zod2Ts))
|
|
|
407
398
|
// createdAt: Date;
|
|
408
399
|
// updatedAt: Date;
|
|
409
400
|
// }
|
|
410
|
-
|
|
411
|
-
// export interface UserDtos {
|
|
412
|
-
// createUserUseCaseDto: CreateUserUseCaseDto;
|
|
413
|
-
// createUserUseCaseResultDto: CreateUserUseCaseResultDto;
|
|
414
|
-
// }
|
|
415
401
|
```
|
|
416
402
|
|
|
417
403
|
4 - Difference of using **externalInheritance** (defaults) or not.
|
|
@@ -437,11 +423,6 @@ class UserDtos extends Zod2XModel {
|
|
|
437
423
|
|
|
438
424
|
// export interface CreateUserUseCaseResultDto extends USER.UserEntity {}
|
|
439
425
|
|
|
440
|
-
// export interface UserDtos {
|
|
441
|
-
// createUserUseCaseDto: CreateUserUseCaseDto;
|
|
442
|
-
// createUserUseCaseResultDto: CreateUserUseCaseResultDto;
|
|
443
|
-
// }
|
|
444
|
-
|
|
445
426
|
// ---------------
|
|
446
427
|
// If `USER.UserEntity` were a Union or a Discriminated Union, the output would be a Type equivalent to `USER.UserEntity` rather than an Interface that extends it.
|
|
447
428
|
|
|
@@ -449,7 +430,7 @@ class UserDtos extends Zod2XModel {
|
|
|
449
430
|
|
|
450
431
|
```ts
|
|
451
432
|
// Output without externalInheritance
|
|
452
|
-
@Application({ namespace: "USER_DTOS", file: "user.dtos", externalInheritance: false})
|
|
433
|
+
@Application({ namespace: "USER_DTOS", file: "user.dtos", externalInheritance: false, skipLayerInterface: false})
|
|
453
434
|
class UserDtos extends Zod2XModel {
|
|
454
435
|
|
|
455
436
|
createUserUseCaseDto = userModels.userEntity.omit({ id: true });
|
|
@@ -473,7 +454,7 @@ class UserDtos extends Zod2XModel {
|
|
|
473
454
|
// }
|
|
474
455
|
|
|
475
456
|
// ---------------
|
|
476
|
-
// In this case, the type of `createUserUseCaseResultDto` is inferred from the parent model (`UserDtos
|
|
457
|
+
// In this case, the type of `createUserUseCaseResultDto` is inferred from the parent model (`UserDtos`, only if skipLayerInterface = false), but there is no explicit definition of the type itself.
|
|
477
458
|
```
|
|
478
459
|
|
|
479
460
|
### Custom Layers
|
|
@@ -533,35 +514,7 @@ Common options:
|
|
|
533
514
|
## Additional utils
|
|
534
515
|
Additional useful tools to convert Zod Schemas into different formats.
|
|
535
516
|
|
|
536
|
-
### 1) `
|
|
537
|
-
In case of use of libraries like [`@zod-to-json-schema`](https://github.com/StefanTerdell/zod-to-json-schema), the provided zod extension can also be used as a JSON Schema definitions mapper. Check out this [input example](https://github.com/rroumenov/zod-to-x/blob/main/test/test_zod2jschema_def/user_schema.ts) and its [output](https://github.com/rroumenov/zod-to-x/blob/main/test/test_zod2jschema_def/user_schema.json) (\*).
|
|
538
|
-
|
|
539
|
-
<sup>*(\*) Output is generated using definitions from `zod2JsonSchemaDefinitions` and [`@zod-to-json-schema`](https://github.com/StefanTerdell/zod-to-json-schema) to create the schema with them.*</sup>
|
|
540
|
-
|
|
541
|
-
```ts
|
|
542
|
-
import { z } from 'zod';
|
|
543
|
-
import { extendZod, Zod2XConverters } from 'zod-to-x';
|
|
544
|
-
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
545
|
-
extendZod(z);
|
|
546
|
-
|
|
547
|
-
// Example using model from `Quick-start` section
|
|
548
|
-
const visitorDefinitions = Zod2XConverters.zod2JsonSchemaDefinitions(VisitorSchema);
|
|
549
|
-
const visitorJsonSchema = zodToJsonSchema(
|
|
550
|
-
VisitorSchema,
|
|
551
|
-
{definitions: visitorDefinitions}
|
|
552
|
-
);
|
|
553
|
-
console.log(visitorJsonSchema); // JSON Schema with definitions
|
|
554
|
-
|
|
555
|
-
// Example using model from `Layer modeling` section
|
|
556
|
-
const createUserDtoDefinitions = Zod2XConverters.zod2JsonSchemaDefinitions(userDtos.createUserUseCaseDto);
|
|
557
|
-
const createUserDtoJsonSchema = zodToJsonSchema(
|
|
558
|
-
userDtos.createUserUseCaseDto,
|
|
559
|
-
{definitions: createUserDtoDefinitions}
|
|
560
|
-
);
|
|
561
|
-
console.log(createUserDtoJsonSchema); // JSON Schema with definitions
|
|
562
|
-
```
|
|
563
|
-
|
|
564
|
-
### 2) `zod2ProtoV3`
|
|
517
|
+
### 1) `zod2ProtoV3`
|
|
565
518
|
In case of use of Google protobuf to improve communication performance, you can automatically generate `proto` files directly from your models. Check out this [input example](https://github.com/rroumenov/zod-to-x/blob/main/test/test_zod2proto3/proto3_supported_schemas.ts) and its [output](https://github.com/rroumenov/zod-to-x/blob/main/test/test_zod2proto3/proto3_supported_schemas.expect.proto)
|
|
566
519
|
|
|
567
520
|
- Options:
|
|
@@ -575,9 +528,8 @@ In case of use of Google protobuf to improve communication performance, you can
|
|
|
575
528
|
- ZodTuple is supported only for items of the same type.
|
|
576
529
|
|
|
577
530
|
```ts
|
|
578
|
-
import { z } from
|
|
531
|
+
import { z } from "zod/v4";
|
|
579
532
|
import { extendZod, Zod2XConverters } from 'zod-to-x';
|
|
580
|
-
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
581
533
|
extendZod(z);
|
|
582
534
|
|
|
583
535
|
// Example using model from `Quick-start` section
|
|
@@ -602,13 +554,13 @@ For a detailed mapping of supported Zod types across supported targets, please r
|
|
|
602
554
|
- In Layered modeling, the transpilation of primitive types can be completely disabled or combined by defining them outside the layer class:
|
|
603
555
|
```ts
|
|
604
556
|
// External primitive
|
|
605
|
-
const stringUUID = z.
|
|
557
|
+
const stringUUID = z.uuid();
|
|
606
558
|
|
|
607
559
|
@Domain({ namespace: "USER", file: "user.entity" })
|
|
608
560
|
class UserModels extends Zod2XModel {
|
|
609
561
|
|
|
610
562
|
// Internal primitive
|
|
611
|
-
userEmail = z.
|
|
563
|
+
userEmail = z.email(); // This will be transpiled as an alias. It is a layer property.
|
|
612
564
|
|
|
613
565
|
userEntity = z.object({
|
|
614
566
|
id: stringUUID, // "stringUUID" will not be transpiled as an alias. It is not a layer property.
|
|
@@ -623,7 +575,7 @@ export const userModels = new UserModels();
|
|
|
623
575
|
```ts
|
|
624
576
|
// Consider the previous UserModels
|
|
625
577
|
|
|
626
|
-
import { z } from "zod";
|
|
578
|
+
import { z } from "zod/v4";
|
|
627
579
|
|
|
628
580
|
@Application({ namespace: "USER_DTOS", file: "user.dtos", externalInheritance: false})
|
|
629
581
|
class UserDtos extends Zod2XModel {
|
package/dist/converters/index.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.zod2ProtoV3 = void 0;
|
|
4
4
|
var runner_1 = require("./protobuf_v3/runner");
|
|
5
5
|
Object.defineProperty(exports, "zod2ProtoV3", { enumerable: true, get: function () { return runner_1.zod2ProtoV3; } });
|
|
6
|
-
var json_schema_definitions_1 = require("./json_schema_definitions");
|
|
7
|
-
Object.defineProperty(exports, "zod2JsonSchemaDefinitions", { enumerable: true, get: function () { return json_schema_definitions_1.zod2JsonSchemaDefinitions; } });
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ASTArray = exports.ASTTuple = exports.ASTSet = exports.ASTMap = exports.ASTIntersection = exports.ASTUnion = exports.ASTObject = exports.ASTEnum = void 0;
|
|
4
4
|
const ast_common_1 = require("./ast_common");
|
|
5
5
|
/**
|
|
6
|
-
* Handle ZodEnum
|
|
6
|
+
* Handle ZodEnum
|
|
7
7
|
*/
|
|
8
8
|
class ASTEnum extends ast_common_1.ASTCommon {
|
|
9
9
|
constructor(data) {
|
package/dist/core/ast_node.js
CHANGED
|
@@ -106,22 +106,13 @@ class Zod2Ast {
|
|
|
106
106
|
* is either a string or a number.
|
|
107
107
|
*/
|
|
108
108
|
_getEnumValues(schema) {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
return Object.entries(schema.enum)
|
|
118
|
-
.filter(([key, _value]) => isNaN(Number(key)))
|
|
119
|
-
.map(([key, value]) => {
|
|
120
|
-
// Creates a string key if it starts with number.
|
|
121
|
-
key = isNaN(Number(key.at(0))) ? key : `"${key}"`;
|
|
122
|
-
return [key, value];
|
|
123
|
-
});
|
|
124
|
-
}
|
|
109
|
+
return Object.entries(schema.enum)
|
|
110
|
+
.filter(([key, _value]) => isNaN(Number(key)))
|
|
111
|
+
.map(([key, value]) => {
|
|
112
|
+
// Creates a string key if it starts with number.
|
|
113
|
+
key = isNaN(Number(key.at(0))) ? key : `"${key}"`;
|
|
114
|
+
return [key, value];
|
|
115
|
+
});
|
|
125
116
|
}
|
|
126
117
|
/**
|
|
127
118
|
* Intersects the properties of two AST nodes and returns the combined properties.
|
|
@@ -208,21 +199,26 @@ class Zod2Ast {
|
|
|
208
199
|
if (!name) {
|
|
209
200
|
let itemProperties = ["Unknown type"];
|
|
210
201
|
if (zod_helpers_1.ZodHelpers.isZodObject(schema)) {
|
|
211
|
-
itemProperties = Object.keys(schema.
|
|
202
|
+
itemProperties = Object.keys(schema.def.shape);
|
|
212
203
|
}
|
|
213
204
|
else if (zod_helpers_1.ZodHelpers.isZodAnyUnionType(schema)) {
|
|
214
|
-
itemProperties = schema.
|
|
205
|
+
itemProperties = schema.def.options.map((i) => {
|
|
206
|
+
var _a;
|
|
207
|
+
return ((_a = i._zod2x) === null || _a === void 0 ? void 0 : _a.typeName) || i.def.type;
|
|
208
|
+
});
|
|
215
209
|
}
|
|
216
210
|
else if (zod_helpers_1.ZodHelpers.isZodIntersection(schema)) {
|
|
211
|
+
const left = schema.def.left;
|
|
212
|
+
const right = schema.def.right;
|
|
217
213
|
itemProperties = [
|
|
218
|
-
((_b =
|
|
219
|
-
((_c =
|
|
214
|
+
((_b = left._zod2x) === null || _b === void 0 ? void 0 : _b.typeName) || left.def.type,
|
|
215
|
+
((_c = right._zod2x) === null || _c === void 0 ? void 0 : _c.typeName) || right.def.type,
|
|
220
216
|
];
|
|
221
217
|
}
|
|
222
|
-
else if (zod_helpers_1.ZodHelpers.
|
|
218
|
+
else if (zod_helpers_1.ZodHelpers.isZodEnum(schema)) {
|
|
223
219
|
itemProperties = this._getEnumValues(schema).map((i) => i[0]);
|
|
224
220
|
}
|
|
225
|
-
throw new errors_1.AstTypeNameDefinitionError(`${schema.
|
|
221
|
+
throw new errors_1.AstTypeNameDefinitionError(`${schema.def.type} type must have a typeName. If Layered modeling is used, ` +
|
|
226
222
|
`avoid nesting schemas definitions. Otherwise, use zod2x method to provide one. ` +
|
|
227
223
|
`Affected type properties: ${itemProperties.join(", ")}`);
|
|
228
224
|
}
|
|
@@ -238,11 +234,12 @@ class Zod2Ast {
|
|
|
238
234
|
* @returns The AST definition for the provided enum schema.
|
|
239
235
|
*/
|
|
240
236
|
_getEnumAst(schema, opt) {
|
|
237
|
+
var _a;
|
|
241
238
|
const { name, parentFile, parentNamespace, aliasOf } = this._getNames(schema);
|
|
242
239
|
const item = new core_1.ASTEnum({
|
|
243
240
|
name,
|
|
244
241
|
values: this._getEnumValues(schema),
|
|
245
|
-
description: schema.
|
|
242
|
+
description: (_a = schema.meta()) === null || _a === void 0 ? void 0 : _a.description,
|
|
246
243
|
parentFile,
|
|
247
244
|
parentNamespace,
|
|
248
245
|
aliasOf,
|
|
@@ -263,9 +260,10 @@ class Zod2Ast {
|
|
|
263
260
|
* @returns The AST definition for the provided Zod object schema.
|
|
264
261
|
*/
|
|
265
262
|
_getObjectAst(schema, opt) {
|
|
263
|
+
var _a;
|
|
266
264
|
const { name, parentFile, parentNamespace, aliasOf } = this._getNames(schema);
|
|
267
265
|
let discriminantValue = undefined;
|
|
268
|
-
const shape = schema.
|
|
266
|
+
const shape = schema.def.shape;
|
|
269
267
|
if (!this.nodes.has(name)) {
|
|
270
268
|
const properties = {};
|
|
271
269
|
for (const key in shape) {
|
|
@@ -277,7 +275,7 @@ class Zod2Ast {
|
|
|
277
275
|
this.nodes.set(name, new core_1.ASTObject({
|
|
278
276
|
name,
|
|
279
277
|
properties,
|
|
280
|
-
description: schema.description,
|
|
278
|
+
description: (_a = schema.meta()) === null || _a === void 0 ? void 0 : _a.description,
|
|
281
279
|
parentFile,
|
|
282
280
|
parentNamespace,
|
|
283
281
|
aliasOf,
|
|
@@ -314,16 +312,17 @@ class Zod2Ast {
|
|
|
314
312
|
* @returns The AST definition for the given Zod union schema.
|
|
315
313
|
*/
|
|
316
314
|
_getUnionAst(schema) {
|
|
317
|
-
|
|
315
|
+
var _a, _b, _c;
|
|
316
|
+
const def = schema.def;
|
|
318
317
|
const discriminator = zod_helpers_1.ZodHelpers.isZodDiscriminatedUnion(schema)
|
|
319
|
-
? schema.
|
|
318
|
+
? schema.def.discriminator
|
|
320
319
|
: undefined;
|
|
321
320
|
const { name, parentFile, parentNamespace, aliasOf } = this._getNames(schema);
|
|
322
321
|
const item = new core_1.ASTUnion({
|
|
323
322
|
name,
|
|
324
323
|
options: def.options.map((i) => this._zodToAST(i, { discriminantKey: discriminator })),
|
|
325
324
|
areAllObjects: def.options.every((i) => zod_helpers_1.ZodHelpers.isZodObject(i)),
|
|
326
|
-
description: schema.description,
|
|
325
|
+
description: (_a = schema.meta()) === null || _a === void 0 ? void 0 : _a.description,
|
|
327
326
|
discriminantKey: discriminator,
|
|
328
327
|
parentFile,
|
|
329
328
|
parentNamespace,
|
|
@@ -344,7 +343,7 @@ class Zod2Ast {
|
|
|
344
343
|
item.newObject = new core_1.ASTObject({
|
|
345
344
|
name,
|
|
346
345
|
properties: this._unionAstNodes(item.options).properties,
|
|
347
|
-
description: (schema.description ? `${schema.description} - ` : "") +
|
|
346
|
+
description: (((_b = schema.meta()) === null || _b === void 0 ? void 0 : _b.description) ? `${(_c = schema.meta()) === null || _c === void 0 ? void 0 : _c.description} - ` : "") +
|
|
348
347
|
`Built from union of ` +
|
|
349
348
|
`${item.options.map((i) => i.name).join(", ")}`,
|
|
350
349
|
});
|
|
@@ -362,14 +361,16 @@ class Zod2Ast {
|
|
|
362
361
|
* @returns An ASTDefinition representing the intersection of the two Zod types.
|
|
363
362
|
*/
|
|
364
363
|
_getIntersectionAst(schema) {
|
|
365
|
-
|
|
364
|
+
var _a, _b, _c;
|
|
365
|
+
const def = schema.def;
|
|
366
366
|
const { name, parentFile, parentNamespace, aliasOf } = this._getNames(schema);
|
|
367
367
|
const item = new core_1.ASTIntersection({
|
|
368
368
|
name,
|
|
369
369
|
left: this._zodToAST(def.left),
|
|
370
370
|
right: this._zodToAST(def.right),
|
|
371
|
-
areAllObjects: zod_helpers_1.ZodHelpers.isZodObject(def.left) &&
|
|
372
|
-
|
|
371
|
+
areAllObjects: zod_helpers_1.ZodHelpers.isZodObject(def.left) &&
|
|
372
|
+
zod_helpers_1.ZodHelpers.isZodObject(def.right),
|
|
373
|
+
description: (_a = schema.meta()) === null || _a === void 0 ? void 0 : _a.description,
|
|
373
374
|
parentFile,
|
|
374
375
|
parentNamespace,
|
|
375
376
|
aliasOf,
|
|
@@ -385,7 +386,7 @@ class Zod2Ast {
|
|
|
385
386
|
item.newObject = new core_1.ASTObject({
|
|
386
387
|
name,
|
|
387
388
|
properties: this._intersectAstNodes(item.left, item.right).properties,
|
|
388
|
-
description: (schema.description ? `${schema.description} - ` : "") +
|
|
389
|
+
description: (((_b = schema.meta()) === null || _b === void 0 ? void 0 : _b.description) ? `${(_c = schema.meta()) === null || _c === void 0 ? void 0 : _c.description} - ` : "") +
|
|
389
390
|
`Built from intersection of ` +
|
|
390
391
|
`${item.left.name} and ` +
|
|
391
392
|
`${item.right.name}`,
|
|
@@ -404,11 +405,12 @@ class Zod2Ast {
|
|
|
404
405
|
* @returns The AST definition for the array schema.
|
|
405
406
|
*/
|
|
406
407
|
_getArrayAst(schema, innerSchema) {
|
|
408
|
+
var _a;
|
|
407
409
|
const { name, parentFile, parentNamespace, aliasOf } = this._getNames(schema);
|
|
408
410
|
const item = new core_1.ASTArray({
|
|
409
411
|
name,
|
|
410
412
|
item: innerSchema,
|
|
411
|
-
description: schema.description,
|
|
413
|
+
description: (_a = schema.meta()) === null || _a === void 0 ? void 0 : _a.description,
|
|
412
414
|
parentFile,
|
|
413
415
|
parentNamespace,
|
|
414
416
|
aliasOf,
|
|
@@ -439,51 +441,50 @@ class Zod2Ast {
|
|
|
439
441
|
* @returns
|
|
440
442
|
*/
|
|
441
443
|
_zodToAST(schema, opt) {
|
|
442
|
-
var _a, _b, _c, _d, _e;
|
|
443
|
-
const def = schema._def;
|
|
444
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
|
|
444
445
|
if (zod_helpers_1.ZodHelpers.isZodString(schema)) {
|
|
445
|
-
return this._getAliasAst(schema, new core_1.ASTString({ description: schema.description }));
|
|
446
|
+
return this._getAliasAst(schema, new core_1.ASTString({ description: (_a = schema.meta()) === null || _a === void 0 ? void 0 : _a.description }));
|
|
446
447
|
}
|
|
447
448
|
else if (zod_helpers_1.ZodHelpers.isZodAnyNumberType(schema)) {
|
|
448
449
|
return this._getAliasAst(schema, new core_1.ASTNumber({
|
|
449
|
-
description: schema.description,
|
|
450
|
+
description: (_b = schema.meta()) === null || _b === void 0 ? void 0 : _b.description,
|
|
450
451
|
constraints: zod_helpers_1.ZodHelpers.getZodNumberConstraints(schema),
|
|
451
452
|
}));
|
|
452
453
|
}
|
|
453
454
|
else if (zod_helpers_1.ZodHelpers.isZodBoolean(schema)) {
|
|
454
|
-
return this._getAliasAst(schema, new core_1.ASTBoolean({ description: schema.description }));
|
|
455
|
+
return this._getAliasAst(schema, new core_1.ASTBoolean({ description: (_c = schema.meta()) === null || _c === void 0 ? void 0 : _c.description }));
|
|
455
456
|
}
|
|
456
457
|
else if (zod_helpers_1.ZodHelpers.isZodDate(schema)) {
|
|
457
|
-
return this._getAliasAst(schema, new core_1.ASTDate({ description: schema.description }));
|
|
458
|
+
return this._getAliasAst(schema, new core_1.ASTDate({ description: (_d = schema.meta()) === null || _d === void 0 ? void 0 : _d.description }));
|
|
458
459
|
}
|
|
459
460
|
else if (zod_helpers_1.ZodHelpers.isZodAny(schema)) {
|
|
460
|
-
return this._getAliasAst(schema, new core_1.ASTAny({ description: schema.description }));
|
|
461
|
+
return this._getAliasAst(schema, new core_1.ASTAny({ description: (_e = schema.meta()) === null || _e === void 0 ? void 0 : _e.description }));
|
|
461
462
|
}
|
|
462
463
|
else if (zod_helpers_1.ZodHelpers.isZodNullable(schema)) {
|
|
463
464
|
const subSchema = this._zodToAST(schema.unwrap());
|
|
464
465
|
subSchema.isNullable = true;
|
|
465
|
-
subSchema.description = schema.description || subSchema.description;
|
|
466
|
+
subSchema.description = ((_f = schema.meta()) === null || _f === void 0 ? void 0 : _f.description) || subSchema.description;
|
|
466
467
|
return subSchema;
|
|
467
468
|
}
|
|
468
469
|
else if (zod_helpers_1.ZodHelpers.isZodOptional(schema)) {
|
|
469
470
|
const subSchema = this._zodToAST(schema.unwrap());
|
|
470
471
|
subSchema.isOptional = true;
|
|
471
|
-
subSchema.description = schema.description || subSchema.description;
|
|
472
|
+
subSchema.description = ((_g = schema.meta()) === null || _g === void 0 ? void 0 : _g.description) || subSchema.description;
|
|
472
473
|
return subSchema;
|
|
473
474
|
}
|
|
474
475
|
else if (zod_helpers_1.ZodHelpers.isZodDefault(schema)) {
|
|
475
|
-
const subSchema = this._zodToAST(def.
|
|
476
|
-
subSchema.description = schema.description || subSchema.description;
|
|
476
|
+
const subSchema = this._zodToAST(schema.def.defaultValue());
|
|
477
|
+
subSchema.description = ((_h = schema.meta()) === null || _h === void 0 ? void 0 : _h.description) || subSchema.description;
|
|
477
478
|
return subSchema;
|
|
478
479
|
}
|
|
479
480
|
else if (zod_helpers_1.ZodHelpers.isZodArray(schema)) {
|
|
480
481
|
const isParentArray = (opt === null || opt === void 0 ? void 0 : opt.calledFromArray) !== true;
|
|
481
|
-
const subSchema = this._zodToAST(
|
|
482
|
-
subSchema.description = schema.description || subSchema.description;
|
|
482
|
+
const subSchema = this._zodToAST(schema.element, { calledFromArray: true });
|
|
483
|
+
subSchema.description = ((_j = schema.meta()) === null || _j === void 0 ? void 0 : _j.description) || subSchema.description;
|
|
483
484
|
subSchema.arrayDimension = Number.isInteger(subSchema.arrayDimension)
|
|
484
485
|
? ++subSchema.arrayDimension
|
|
485
486
|
: 1;
|
|
486
|
-
if (isParentArray && ((
|
|
487
|
+
if (isParentArray && ((_k = schema._zod2x) === null || _k === void 0 ? void 0 : _k.typeName)) {
|
|
487
488
|
return this._getArrayAst(schema, subSchema);
|
|
488
489
|
}
|
|
489
490
|
else {
|
|
@@ -492,22 +493,23 @@ class Zod2Ast {
|
|
|
492
493
|
}
|
|
493
494
|
else if (zod_helpers_1.ZodHelpers.isZodSet(schema)) {
|
|
494
495
|
return this._getAliasAst(schema, new core_1.ASTSet({
|
|
495
|
-
value: this._zodToAST(def.valueType),
|
|
496
|
-
description: schema.description,
|
|
496
|
+
value: this._zodToAST(schema.def.valueType),
|
|
497
|
+
description: (_l = schema.meta()) === null || _l === void 0 ? void 0 : _l.description,
|
|
497
498
|
}));
|
|
498
499
|
}
|
|
499
500
|
else if (zod_helpers_1.ZodHelpers.isZodLiteral(schema)) {
|
|
500
501
|
let parentEnum = undefined;
|
|
501
502
|
let parentEnumKey = undefined;
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
503
|
+
// TODO: Support multiple values
|
|
504
|
+
if ((_m = schema._zod2x) === null || _m === void 0 ? void 0 : _m.parentEnum) {
|
|
505
|
+
parentEnumKey = (_p = this._getEnumValues((_o = schema._zod2x) === null || _o === void 0 ? void 0 : _o.parentEnum).find((i) => i[1] === schema.def.values.at(0))) === null || _p === void 0 ? void 0 : _p[0];
|
|
506
|
+
parentEnum = this._zodToAST((_q = schema._zod2x) === null || _q === void 0 ? void 0 : _q.parentEnum, {
|
|
505
507
|
isInjectedEnum: true,
|
|
506
508
|
});
|
|
507
509
|
}
|
|
508
510
|
return new core_1.ASTLiteral({
|
|
509
|
-
value: def.
|
|
510
|
-
description: schema.description,
|
|
511
|
+
value: schema.def.values.at(0), // TODO: Support multiple values
|
|
512
|
+
description: (_r = schema.meta()) === null || _r === void 0 ? void 0 : _r.description,
|
|
511
513
|
parentEnum: parentEnum,
|
|
512
514
|
parentEnumKey,
|
|
513
515
|
});
|
|
@@ -515,23 +517,23 @@ class Zod2Ast {
|
|
|
515
517
|
else if (zod_helpers_1.ZodHelpers.isZodAnyMapType(schema)) {
|
|
516
518
|
return this._getAliasAst(schema, new core_1.ASTMap({
|
|
517
519
|
type: zod_helpers_1.ZodHelpers.isZodRecord(schema) ? "record" : "map",
|
|
518
|
-
key: this._zodToAST(
|
|
519
|
-
value: this._zodToAST(
|
|
520
|
-
description: schema.description,
|
|
520
|
+
key: this._zodToAST(schema.keyType),
|
|
521
|
+
value: this._zodToAST(schema.valueType),
|
|
522
|
+
description: (_s = schema.meta()) === null || _s === void 0 ? void 0 : _s.description,
|
|
521
523
|
}));
|
|
522
524
|
}
|
|
523
525
|
else if (zod_helpers_1.ZodHelpers.isZodLazy(schema)) {
|
|
524
526
|
/** Lazy items use to be recursive schemas of its own, so the are trated as another
|
|
525
527
|
* definition */
|
|
526
|
-
const lazySchema = def.getter();
|
|
528
|
+
const lazySchema = schema.def.getter();
|
|
527
529
|
const lazyPointer = this._createDefinition({ name: "pending" });
|
|
528
530
|
this.lazyPointers.push([lazyPointer, lazySchema]);
|
|
529
531
|
return lazyPointer;
|
|
530
532
|
}
|
|
531
533
|
else if (zod_helpers_1.ZodHelpers.isZodTuple(schema)) {
|
|
532
534
|
return this._getAliasAst(schema, new core_1.ASTTuple({
|
|
533
|
-
items: def.items.map(this._zodToAST.bind(this)),
|
|
534
|
-
description: schema.description,
|
|
535
|
+
items: schema.def.items.map(this._zodToAST.bind(this)),
|
|
536
|
+
description: (_t = schema.meta()) === null || _t === void 0 ? void 0 : _t.description,
|
|
535
537
|
}));
|
|
536
538
|
/**
|
|
537
539
|
*
|
|
@@ -541,7 +543,7 @@ class Zod2Ast {
|
|
|
541
543
|
*
|
|
542
544
|
* */
|
|
543
545
|
}
|
|
544
|
-
else if (zod_helpers_1.ZodHelpers.
|
|
546
|
+
else if (zod_helpers_1.ZodHelpers.isZodEnum(schema)) {
|
|
545
547
|
return this._getEnumAst(schema, opt);
|
|
546
548
|
}
|
|
547
549
|
else if (zod_helpers_1.ZodHelpers.isZodObject(schema)) {
|
|
@@ -556,7 +558,7 @@ class Zod2Ast {
|
|
|
556
558
|
else {
|
|
557
559
|
logger_1.log.warn(`Unsupported Zod type: ${JSON.stringify(schema)}`);
|
|
558
560
|
return new core_1.ASTAny({
|
|
559
|
-
description: `Unsupported Zod type: ${schema.
|
|
561
|
+
description: `Unsupported Zod type: ${schema.def.type}`,
|
|
560
562
|
});
|
|
561
563
|
}
|
|
562
564
|
}
|