zod-to-x 2.4.0 → 2.6.0

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
@@ -601,6 +601,21 @@ readonly DiscriminantDataRetrieve = z.lazy(() => // Lazy the zod type itself
601
601
  // | HttpSuccessfulResponse<SomeDtoResult>
602
602
  // | HttpUnsuccessfulResponse;
603
603
  ```
604
+ 4) A default type can be provided as the second argument of `createGenericType`. It can be any supported type: a basic type, an array, a named type, ... Templates with a default type can be omitted in `useGenericType`, and the default type is used instead. The default is applied when the generic type is instantiated, so the output is valid for all supported languages, including the ones without default type parameters (Go, Dart):
605
+ ```ts
606
+ // inside GenericsInfrastructure class
607
+ readonly Page = z.object({
608
+ cursor: createGenericType("TCursor", z.string()),
609
+ content: createGenericType("TContent", this.SomeDtoResult),
610
+ });
611
+
612
+ readonly DefaultPage = useGenericType(this.Page, {});
613
+ readonly PageOfIds = useGenericType(this.Page, { content: z.array(z.string()) });
614
+
615
+ // Output
616
+ // export interface DefaultPage extends Page<string, SomeDtoResult> {}
617
+ // export interface PageOfIds extends Page<string, string[]> {}
618
+ ```
604
619
 
605
620
  Complete definition examples can be found [here](https://github.com/rroumenov/zod-to-x/blob/main/test/common/layered_generics.ts).
606
621
 
@@ -29,22 +29,39 @@ export type ASTNode = Simple.ASTString | Simple.ASTNumber | Simple.ASTBoolean |
29
29
  * Nodes that are always transpiled to a type alias.
30
30
  */
31
31
  export type ASTAliasedTypes = Simple.ASTString | Simple.ASTNumber | Simple.ASTBoolean | Simple.ASTLiteral | Simple.ASTDate | Simple.ASTAny | Complex.ASTMap | Complex.ASTSet | Complex.ASTTuple | Complex.ASTArray;
32
+ /**
33
+ * Translation of a generic template of a generic type instance. Named types are referenced by
34
+ * name (and file, if external), and the rest of types (basic types, arrays, ...) by their AST.
35
+ */
36
+ export type ASTTemplateTranslation = Pick<ASTCommon, "parentFile" | "parentNamespace" | "aliasOf"> & {
37
+ type?: ASTType;
38
+ };
32
39
  /**
33
40
  * Represents a usage of any existing ASTNode. Used to reduce node size.
34
41
  */
35
42
  export declare class ASTDefinition extends ASTCommon {
43
+ static readonly kind = "ASTDefinition";
36
44
  name: string;
45
+ /**
46
+ * Kind of the referenced AST node (e.g. "ASTObject"). See `getASTKind`.
47
+ */
37
48
  instanceType: string;
38
49
  constraints?: Record<string, any>;
39
- templatesTranslation: Pick<ASTCommon, "parentFile" | "parentNamespace" | "aliasOf">[];
50
+ templatesTranslation: ASTTemplateTranslation[];
40
51
  constructor(data: ASTDefinition & ASTCommon);
41
52
  }
42
53
  export type ASTType = ASTNode | ASTDefinition;
54
+ /**
55
+ * Returns the kind of an AST node, which is the name of its class (e.g. "ASTObject"). Unlike
56
+ * `constructor.name`, it is not modified by minifiers.
57
+ */
58
+ export declare function getASTKind(node: unknown): string | undefined;
43
59
  export type ASTNodes = {
44
60
  nodes: Map<string, ASTNode>;
45
61
  warnings: string[];
46
62
  };
47
63
  export declare class ASTGenericType {
64
+ static readonly kind = "ASTGenericType";
48
65
  name: string;
49
66
  constructor(name: string);
50
67
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ASTGenericType = exports.ASTDefinition = exports.ASTCommon = void 0;
4
+ exports.getASTKind = getASTKind;
4
5
  /**
5
6
  * Shared properties for all AST node types.
6
7
  */
@@ -29,9 +30,19 @@ class ASTDefinition extends ASTCommon {
29
30
  }
30
31
  }
31
32
  exports.ASTDefinition = ASTDefinition;
33
+ ASTDefinition.kind = "ASTDefinition";
34
+ /**
35
+ * Returns the kind of an AST node, which is the name of its class (e.g. "ASTObject"). Unlike
36
+ * `constructor.name`, it is not modified by minifiers.
37
+ */
38
+ function getASTKind(node) {
39
+ var _a;
40
+ return (_a = node === null || node === void 0 ? void 0 : node.constructor) === null || _a === void 0 ? void 0 : _a.kind;
41
+ }
32
42
  class ASTGenericType {
33
43
  constructor(name) {
34
44
  this.name = name;
35
45
  }
36
46
  }
37
47
  exports.ASTGenericType = ASTGenericType;
48
+ ASTGenericType.kind = "ASTGenericType";
@@ -1,8 +1,9 @@
1
- import { ASTCommon, ASTType } from "./ast_common";
1
+ import { ASTCommon, ASTTemplateTranslation, ASTType } from "./ast_common";
2
2
  /**
3
3
  * Handle ZodEnum
4
4
  */
5
5
  export declare class ASTEnum extends ASTCommon {
6
+ static readonly kind = "ASTEnum";
6
7
  name: string;
7
8
  values: [string, string | number][];
8
9
  /**
@@ -15,16 +16,19 @@ export declare class ASTEnum extends ASTCommon {
15
16
  * Handle ZodObject
16
17
  */
17
18
  export declare class ASTObject extends ASTCommon {
19
+ static readonly kind = "ASTObject";
18
20
  name: string;
19
21
  properties: Record<string, ASTType>;
20
22
  templates: Set<string>;
21
- templatesTranslation: Pick<ASTCommon, "parentFile" | "parentNamespace" | "aliasOf">[];
23
+ templatesTranslation: ASTTemplateTranslation[];
24
+ genericOf?: string;
22
25
  constructor(data: ASTObject & ASTCommon);
23
26
  }
24
27
  /**
25
28
  * Handle ZodUnion and ZodDiscriminatedUnion
26
29
  */
27
30
  export declare class ASTUnion extends ASTCommon {
31
+ static readonly kind = "ASTUnion";
28
32
  name: string;
29
33
  options: ASTType[];
30
34
  areAllObjects: boolean;
@@ -40,6 +44,7 @@ export declare class ASTUnion extends ASTCommon {
40
44
  * Handle ZodIntersection
41
45
  */
42
46
  export declare class ASTIntersection extends ASTCommon {
47
+ static readonly kind = "ASTIntersection";
43
48
  name: string;
44
49
  left: ASTType;
45
50
  right: ASTType;
@@ -55,6 +60,7 @@ export declare class ASTIntersection extends ASTCommon {
55
60
  * Handle ZodRecord and ZodMap
56
61
  */
57
62
  export declare class ASTMap extends ASTCommon {
63
+ static readonly kind = "ASTMap";
58
64
  name?: string;
59
65
  key: ASTType;
60
66
  value: ASTType;
@@ -65,6 +71,7 @@ export declare class ASTMap extends ASTCommon {
65
71
  * Handle ZodSet
66
72
  */
67
73
  export declare class ASTSet extends ASTCommon {
74
+ static readonly kind = "ASTSet";
68
75
  name?: string;
69
76
  value: ASTType;
70
77
  constructor(data: ASTSet & ASTCommon);
@@ -73,6 +80,7 @@ export declare class ASTSet extends ASTCommon {
73
80
  * Handle ZodTuple
74
81
  */
75
82
  export declare class ASTTuple extends ASTCommon {
83
+ static readonly kind = "ASTTuple";
76
84
  name?: string;
77
85
  items: ASTType[];
78
86
  constructor(data: ASTTuple & ASTCommon);
@@ -81,6 +89,7 @@ export declare class ASTTuple extends ASTCommon {
81
89
  * Handle ZodArray. Only used for type aliases.
82
90
  */
83
91
  export declare class ASTArray extends ASTCommon {
92
+ static readonly kind = "ASTArray";
84
93
  name: string;
85
94
  item: ASTType;
86
95
  constructor(data: ASTArray & ASTCommon);
@@ -14,6 +14,7 @@ class ASTEnum extends ast_common_1.ASTCommon {
14
14
  }
15
15
  }
16
16
  exports.ASTEnum = ASTEnum;
17
+ ASTEnum.kind = "ASTEnum";
17
18
  /**
18
19
  * Handle ZodObject
19
20
  */
@@ -24,9 +25,11 @@ class ASTObject extends ast_common_1.ASTCommon {
24
25
  this.properties = data.properties;
25
26
  this.templates = data.templates;
26
27
  this.templatesTranslation = data.templatesTranslation;
28
+ this.genericOf = data.genericOf;
27
29
  }
28
30
  }
29
31
  exports.ASTObject = ASTObject;
32
+ ASTObject.kind = "ASTObject";
30
33
  /**
31
34
  * Handle ZodUnion and ZodDiscriminatedUnion
32
35
  */
@@ -41,6 +44,7 @@ class ASTUnion extends ast_common_1.ASTCommon {
41
44
  }
42
45
  }
43
46
  exports.ASTUnion = ASTUnion;
47
+ ASTUnion.kind = "ASTUnion";
44
48
  /**
45
49
  * Handle ZodIntersection
46
50
  */
@@ -55,6 +59,7 @@ class ASTIntersection extends ast_common_1.ASTCommon {
55
59
  }
56
60
  }
57
61
  exports.ASTIntersection = ASTIntersection;
62
+ ASTIntersection.kind = "ASTIntersection";
58
63
  /**
59
64
  * Handle ZodRecord and ZodMap
60
65
  */
@@ -68,6 +73,7 @@ class ASTMap extends ast_common_1.ASTCommon {
68
73
  }
69
74
  }
70
75
  exports.ASTMap = ASTMap;
76
+ ASTMap.kind = "ASTMap";
71
77
  /**
72
78
  * Handle ZodSet
73
79
  */
@@ -79,6 +85,7 @@ class ASTSet extends ast_common_1.ASTCommon {
79
85
  }
80
86
  }
81
87
  exports.ASTSet = ASTSet;
88
+ ASTSet.kind = "ASTSet";
82
89
  /**
83
90
  * Handle ZodTuple
84
91
  */
@@ -90,6 +97,7 @@ class ASTTuple extends ast_common_1.ASTCommon {
90
97
  }
91
98
  }
92
99
  exports.ASTTuple = ASTTuple;
100
+ ASTTuple.kind = "ASTTuple";
93
101
  /**
94
102
  * Handle ZodArray. Only used for type aliases.
95
103
  */
@@ -101,3 +109,4 @@ class ASTArray extends ast_common_1.ASTCommon {
101
109
  }
102
110
  }
103
111
  exports.ASTArray = ASTArray;
112
+ ASTArray.kind = "ASTArray";
@@ -3,6 +3,7 @@ import { ASTCommon, ASTDefinition } from "./ast_common";
3
3
  * Handle ZodString
4
4
  */
5
5
  export declare class ASTString extends ASTCommon {
6
+ static readonly kind = "ASTString";
6
7
  name?: string;
7
8
  constraints?: {
8
9
  min?: number;
@@ -15,6 +16,7 @@ export declare class ASTString extends ASTCommon {
15
16
  * Handle ZodNumber and ZodBigInt
16
17
  */
17
18
  export declare class ASTNumber extends ASTCommon {
19
+ static readonly kind = "ASTNumber";
18
20
  name?: string;
19
21
  constraints?: {
20
22
  min?: number;
@@ -27,6 +29,7 @@ export declare class ASTNumber extends ASTCommon {
27
29
  * Handle ZodLiteral
28
30
  */
29
31
  export declare class ASTLiteral extends ASTCommon {
32
+ static readonly kind = "ASTLiteral";
30
33
  name?: string;
31
34
  value: string | number | boolean;
32
35
  parentEnum?: ASTDefinition;
@@ -37,6 +40,7 @@ export declare class ASTLiteral extends ASTCommon {
37
40
  * Handle ZodBoolean
38
41
  */
39
42
  export declare class ASTBoolean extends ASTCommon {
43
+ static readonly kind = "ASTBoolean";
40
44
  name?: string;
41
45
  constructor(data: ASTBoolean & ASTCommon);
42
46
  }
@@ -44,6 +48,7 @@ export declare class ASTBoolean extends ASTCommon {
44
48
  * Handle ZodDate
45
49
  */
46
50
  export declare class ASTDate extends ASTCommon {
51
+ static readonly kind = "ASTDate";
47
52
  name?: string;
48
53
  constructor(data: ASTDate & ASTCommon);
49
54
  }
@@ -51,6 +56,7 @@ export declare class ASTDate extends ASTCommon {
51
56
  * Handle ZodAny
52
57
  */
53
58
  export declare class ASTAny extends ASTCommon {
59
+ static readonly kind = "ASTAny";
54
60
  name?: string;
55
61
  constructor(data: ASTAny & ASTCommon);
56
62
  }
@@ -13,6 +13,7 @@ class ASTString extends ast_common_1.ASTCommon {
13
13
  }
14
14
  }
15
15
  exports.ASTString = ASTString;
16
+ ASTString.kind = "ASTString";
16
17
  /**
17
18
  * Handle ZodNumber and ZodBigInt
18
19
  */
@@ -24,6 +25,7 @@ class ASTNumber extends ast_common_1.ASTCommon {
24
25
  }
25
26
  }
26
27
  exports.ASTNumber = ASTNumber;
28
+ ASTNumber.kind = "ASTNumber";
27
29
  /**
28
30
  * Handle ZodLiteral
29
31
  */
@@ -37,6 +39,7 @@ class ASTLiteral extends ast_common_1.ASTCommon {
37
39
  }
38
40
  }
39
41
  exports.ASTLiteral = ASTLiteral;
42
+ ASTLiteral.kind = "ASTLiteral";
40
43
  /**
41
44
  * Handle ZodBoolean
42
45
  */
@@ -47,6 +50,7 @@ class ASTBoolean extends ast_common_1.ASTCommon {
47
50
  }
48
51
  }
49
52
  exports.ASTBoolean = ASTBoolean;
53
+ ASTBoolean.kind = "ASTBoolean";
50
54
  /**
51
55
  * Handle ZodDate
52
56
  */
@@ -57,6 +61,7 @@ class ASTDate extends ast_common_1.ASTCommon {
57
61
  }
58
62
  }
59
63
  exports.ASTDate = ASTDate;
64
+ ASTDate.kind = "ASTDate";
60
65
  /**
61
66
  * Handle ZodAny
62
67
  */
@@ -67,3 +72,4 @@ class ASTAny extends ast_common_1.ASTCommon {
67
72
  }
68
73
  }
69
74
  exports.ASTAny = ASTAny;
75
+ ASTAny.kind = "ASTAny";
@@ -30,6 +30,11 @@ export declare class Zod2Ast {
30
30
  * Transpilerable nodes of current data model
31
31
  */
32
32
  private nodes;
33
+ /**
34
+ * Source schema of each transpilerable node. Used to detect different schemas sharing the
35
+ * same type name.
36
+ */
37
+ private nodeSchemas;
33
38
  /**
34
39
  * Lazy schemas for further analysis
35
40
  */
@@ -47,6 +52,26 @@ export declare class Zod2Ast {
47
52
  * @returns `true` if the node is an "own property"; otherwise, `false`.
48
53
  */
49
54
  private _isOwnProperty;
55
+ /**
56
+ * Returns the key used to store a transpilerable node. Types imported from other files are
57
+ * namespaced by their file, so they do not clash with local types that share the same name.
58
+ * Aliases of external types are local definitions.
59
+ */
60
+ private _getNodeKey;
61
+ /**
62
+ * Determines if two schemas that share the same type name represent the same definition.
63
+ * Schemas created from the same definition (`.zod2x()`, `.describe()`, `.meta()`, layer
64
+ * aliases) and instances of the same generic type are considered equal. Otherwise, a shallow
65
+ * structural comparison is performed.
66
+ */
67
+ private _isSameDefinition;
68
+ /**
69
+ * Checks if a transpilerable node is already registered.
70
+ *
71
+ * @throws AstTypeNameDefinitionError - If the node key is used by a different schema.
72
+ */
73
+ private _hasNode;
74
+ private _setNode;
50
75
  /**
51
76
  * Check if the layer of the item is compatible with the layer of the schema. If does and the
52
77
  * transpilerable item is in a different file, it returns the file name.