@contractspec/lib.schema 1.57.0 → 1.59.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.
Files changed (96) hide show
  1. package/dist/EnumType.d.ts +29 -31
  2. package/dist/EnumType.d.ts.map +1 -1
  3. package/dist/EnumType.js +38 -54
  4. package/dist/EnumType.test.d.ts +2 -0
  5. package/dist/EnumType.test.d.ts.map +1 -0
  6. package/dist/FieldType.d.ts +19 -23
  7. package/dist/FieldType.d.ts.map +1 -1
  8. package/dist/FieldType.js +41 -47
  9. package/dist/FieldType.test.d.ts +2 -0
  10. package/dist/FieldType.test.d.ts.map +1 -0
  11. package/dist/GraphQLSchemaType.d.ts +8 -12
  12. package/dist/GraphQLSchemaType.d.ts.map +1 -1
  13. package/dist/GraphQLSchemaType.js +16 -18
  14. package/dist/GraphQLSchemaType.test.d.ts +2 -0
  15. package/dist/GraphQLSchemaType.test.d.ts.map +1 -0
  16. package/dist/JsonSchemaType.d.ts +67 -63
  17. package/dist/JsonSchemaType.d.ts.map +1 -1
  18. package/dist/JsonSchemaType.js +73 -123
  19. package/dist/JsonSchemaType.test.d.ts +2 -0
  20. package/dist/JsonSchemaType.test.d.ts.map +1 -0
  21. package/dist/ScalarTypeEnum.d.ts +23 -27
  22. package/dist/ScalarTypeEnum.d.ts.map +1 -1
  23. package/dist/ScalarTypeEnum.js +235 -235
  24. package/dist/ScalarTypeEnum.test.d.ts +2 -0
  25. package/dist/ScalarTypeEnum.test.d.ts.map +1 -0
  26. package/dist/SchemaModel.d.ts +38 -39
  27. package/dist/SchemaModel.d.ts.map +1 -1
  28. package/dist/SchemaModel.js +28 -55
  29. package/dist/SchemaModel.test.d.ts +2 -0
  30. package/dist/SchemaModel.test.d.ts.map +1 -0
  31. package/dist/SchemaModelType.d.ts +28 -24
  32. package/dist/SchemaModelType.d.ts.map +1 -1
  33. package/dist/SchemaModelType.js +6 -19
  34. package/dist/SchemaModelType.test.d.ts +2 -0
  35. package/dist/SchemaModelType.test.d.ts.map +1 -0
  36. package/dist/ZodSchemaType.d.ts +44 -40
  37. package/dist/ZodSchemaType.d.ts.map +1 -1
  38. package/dist/ZodSchemaType.js +30 -101
  39. package/dist/ZodSchemaType.test.d.ts +2 -0
  40. package/dist/ZodSchemaType.test.d.ts.map +1 -0
  41. package/dist/browser/EnumType.js +40 -0
  42. package/dist/browser/FieldType.js +43 -0
  43. package/dist/browser/GraphQLSchemaType.js +18 -0
  44. package/dist/browser/JsonSchemaType.js +74 -0
  45. package/dist/browser/ScalarTypeEnum.js +236 -0
  46. package/dist/browser/SchemaModel.js +30 -0
  47. package/dist/browser/SchemaModelType.js +7 -0
  48. package/dist/browser/ZodSchemaType.js +31 -0
  49. package/dist/browser/entity/defineEntity.js +128 -0
  50. package/dist/browser/entity/generator.js +283 -0
  51. package/dist/browser/entity/index.js +410 -0
  52. package/dist/browser/entity/types.js +0 -0
  53. package/dist/browser/index.js +840 -0
  54. package/dist/entity/defineEntity.d.ts +55 -34
  55. package/dist/entity/defineEntity.d.ts.map +1 -1
  56. package/dist/entity/defineEntity.js +123 -231
  57. package/dist/entity/defineEntity.test.d.ts +2 -0
  58. package/dist/entity/defineEntity.test.d.ts.map +1 -0
  59. package/dist/entity/generator.d.ts +16 -20
  60. package/dist/entity/generator.d.ts.map +1 -1
  61. package/dist/entity/generator.js +262 -196
  62. package/dist/entity/generator.test.d.ts +2 -0
  63. package/dist/entity/generator.test.d.ts.map +1 -0
  64. package/dist/entity/index.d.ts +4 -4
  65. package/dist/entity/index.d.ts.map +1 -0
  66. package/dist/entity/index.js +410 -4
  67. package/dist/entity/types.d.ts +99 -103
  68. package/dist/entity/types.d.ts.map +1 -1
  69. package/dist/entity/types.js +1 -1
  70. package/dist/index.d.ts +10 -13
  71. package/dist/index.d.ts.map +1 -0
  72. package/dist/index.js +841 -13
  73. package/dist/node/EnumType.js +40 -0
  74. package/dist/node/FieldType.js +43 -0
  75. package/dist/node/GraphQLSchemaType.js +18 -0
  76. package/dist/node/JsonSchemaType.js +74 -0
  77. package/dist/node/ScalarTypeEnum.js +236 -0
  78. package/dist/node/SchemaModel.js +30 -0
  79. package/dist/node/SchemaModelType.js +7 -0
  80. package/dist/node/ZodSchemaType.js +31 -0
  81. package/dist/node/entity/defineEntity.js +128 -0
  82. package/dist/node/entity/generator.js +283 -0
  83. package/dist/node/entity/index.js +410 -0
  84. package/dist/node/entity/types.js +0 -0
  85. package/dist/node/index.js +840 -0
  86. package/package.json +123 -25
  87. package/dist/EnumType.js.map +0 -1
  88. package/dist/FieldType.js.map +0 -1
  89. package/dist/GraphQLSchemaType.js.map +0 -1
  90. package/dist/JsonSchemaType.js.map +0 -1
  91. package/dist/ScalarTypeEnum.js.map +0 -1
  92. package/dist/SchemaModel.js.map +0 -1
  93. package/dist/SchemaModelType.js.map +0 -1
  94. package/dist/ZodSchemaType.js.map +0 -1
  95. package/dist/entity/defineEntity.js.map +0 -1
  96. package/dist/entity/generator.js.map +0 -1
@@ -1,58 +1,31 @@
1
- import "./EnumType.js";
2
- import "./FieldType.js";
3
- import * as z$1 from "zod";
1
+ // @bun
2
+ // src/SchemaModel.ts
3
+ import * as z from "zod";
4
4
 
5
- //#region src/SchemaModel.ts
6
- /**
7
- * Named object model built from FieldType/EnumType/SchemaModel fields.
8
- * Provides zod and GraphQL input helpers, and supports arrays/optional fields.
9
- */
10
- var SchemaModel = class {
11
- constructor(config) {
12
- this.config = config;
13
- }
14
- /**
15
- * Build a typed ZodObject from the model fields, preserving each field's
16
- * Zod schema and optionality at the type level when possible.
17
- */
18
- getZod() {
19
- const shape = Object.entries(this.config.fields).reduce((acc, [key, def]) => {
20
- const base = def.type.getZod();
21
- const withArray = def.isArray ? z$1.array(base) : base;
22
- acc[key] = def.isOptional ? withArray.optional() : withArray;
23
- return acc;
24
- }, {});
25
- return z$1.object(shape);
26
- }
27
- /** Input object name for GraphQL builder adapters. */
28
- getPothosInput() {
29
- return this.config.name;
30
- }
31
- };
32
- /**
33
- * Type guard to check if a value is a SchemaModel (not just any SchemaType).
34
- * Use this when you need to access SchemaModel-specific properties like `config`.
35
- *
36
- * @param model - The model to check
37
- * @returns True if the model is a SchemaModel instance
38
- *
39
- * @example
40
- * ```typescript
41
- * if (isSchemaModel(model)) {
42
- * // TypeScript knows model.config is available
43
- * console.log(model.config.name);
44
- * }
45
- * ```
46
- */
5
+ class SchemaModel {
6
+ config;
7
+ constructor(config) {
8
+ this.config = config;
9
+ }
10
+ getZod() {
11
+ const shape = Object.entries(this.config.fields).reduce((acc, [key, def]) => {
12
+ const base = def.type.getZod();
13
+ const withArray = def.isArray ? z.array(base) : base;
14
+ acc[key] = def.isOptional ? withArray.optional() : withArray;
15
+ return acc;
16
+ }, {});
17
+ return z.object(shape);
18
+ }
19
+ getPothosInput() {
20
+ return this.config.name;
21
+ }
22
+ }
47
23
  function isSchemaModel(model) {
48
- return model !== null && model !== void 0 && "config" in model && typeof model.config === "object" && "name" in model.config;
24
+ return model !== null && model !== undefined && "config" in model && typeof model.config === "object" && "name" in model.config;
49
25
  }
50
- /**
51
- * Helper to define a SchemaModel with type inference.
52
- * Equivalent to `new SchemaModel(config)` but with better ergonomics.
53
- */
54
- const defineSchemaModel = (config) => new SchemaModel(config);
55
-
56
- //#endregion
57
- export { SchemaModel, defineSchemaModel, isSchemaModel };
58
- //# sourceMappingURL=SchemaModel.js.map
26
+ var defineSchemaModel = (config) => new SchemaModel(config);
27
+ export {
28
+ isSchemaModel,
29
+ defineSchemaModel,
30
+ SchemaModel
31
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=SchemaModel.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SchemaModel.test.d.ts","sourceRoot":"","sources":["../src/SchemaModel.test.ts"],"names":[],"mappings":""}
@@ -1,10 +1,16 @@
1
- import * as z$1 from "zod";
2
-
3
- //#region src/SchemaModelType.d.ts
1
+ /**
2
+ * Unified schema type interface for ContractSpec.
3
+ *
4
+ * This module provides a vendor-agnostic abstraction over different schema
5
+ * definition approaches (SchemaModel, Zod, JSON Schema, GraphQL scalars).
6
+ *
7
+ * @module SchemaType
8
+ */
9
+ import type * as z from 'zod';
4
10
  /**
5
11
  * Supported schema output formats for code generation.
6
12
  */
7
- type SchemaFormat = 'contractspec' | 'zod' | 'json-schema' | 'graphql';
13
+ export type SchemaFormat = 'contractspec' | 'zod' | 'json-schema' | 'graphql';
8
14
  /**
9
15
  * Unified interface for all schema-compatible types.
10
16
  * Any type used in ContractSpec schemas must implement this interface.
@@ -19,22 +25,22 @@ type SchemaFormat = 'contractspec' | 'zod' | 'json-schema' | 'graphql';
19
25
  * const zodWrapper = fromZod(z.object({ name: z.string() }));
20
26
  * ```
21
27
  */
22
- interface SchemaModelType<T = unknown> {
23
- /**
24
- * Return the Zod schema for runtime validation.
25
- * This is the primary method - all schema types must provide Zod conversion.
26
- */
27
- getZod(): z$1.ZodType<T>;
28
- /**
29
- * Return GraphQL type info for Pothos/GraphQL integration.
30
- * Optional - types without GraphQL representation return undefined.
31
- */
32
- getPothos?(): unknown;
33
- /**
34
- * Return JSON Schema representation.
35
- * Optional - types without JSON Schema representation return undefined.
36
- */
37
- getJsonSchema?(): unknown;
28
+ export interface SchemaModelType<T = unknown> {
29
+ /**
30
+ * Return the Zod schema for runtime validation.
31
+ * This is the primary method - all schema types must provide Zod conversion.
32
+ */
33
+ getZod(): z.ZodType<T>;
34
+ /**
35
+ * Return GraphQL type info for Pothos/GraphQL integration.
36
+ * Optional - types without GraphQL representation return undefined.
37
+ */
38
+ getPothos?(): unknown;
39
+ /**
40
+ * Return JSON Schema representation.
41
+ * Optional - types without JSON Schema representation return undefined.
42
+ */
43
+ getJsonSchema?(): unknown;
38
44
  }
39
45
  /**
40
46
  * Type guard to check if a value implements the SchemaType interface.
@@ -49,11 +55,9 @@ interface SchemaModelType<T = unknown> {
49
55
  * }
50
56
  * ```
51
57
  */
52
- declare function isSchemaType(value: unknown): value is SchemaModelType;
58
+ export declare function isSchemaType(value: unknown): value is SchemaModelType;
53
59
  /**
54
60
  * Type alias for any SchemaType implementation.
55
61
  */
56
- type AnySchemaType = SchemaModelType<unknown>;
57
- //#endregion
58
- export { AnySchemaType, SchemaFormat, SchemaModelType, isSchemaType };
62
+ export type AnySchemaType = SchemaModelType<unknown>;
59
63
  //# sourceMappingURL=SchemaModelType.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"SchemaModelType.d.ts","names":[],"sources":["../src/SchemaModelType.ts"],"mappings":";;;;;;KAcY,YAAA;;;;;;;;;AAiDZ;;;;;;UAjCiB,eAAA;EAiCqD;;AAYtE;;EAxCE,MAAA,IAAU,GAAA,CAAE,OAAA,CAAQ,CAAA;EAwCM;;;;EAlC1B,SAAA;;;;;EAMA,aAAA;AAAA;;;;;;;;;;;;;;iBAgBc,YAAA,CAAa,KAAA,YAAiB,KAAA,IAAS,eAAA;;;;KAY3C,aAAA,GAAgB,eAAA"}
1
+ {"version":3,"file":"SchemaModelType.d.ts","sourceRoot":"","sources":["../src/SchemaModelType.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,KAAK,CAAC,MAAM,KAAK,CAAC;AAE9B;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,cAAc,GAAG,KAAK,GAAG,aAAa,GAAG,SAAS,CAAC;AAE9E;;;;;;;;;;;;;GAaG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC,GAAG,OAAO;IAC1C;;;OAGG;IACH,MAAM,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IAEvB;;;OAGG;IACH,SAAS,CAAC,IAAI,OAAO,CAAC;IAEtB;;;OAGG;IACH,aAAa,CAAC,IAAI,OAAO,CAAC;CAC3B;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,eAAe,CAOrE;AAED;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,eAAe,CAAC,OAAO,CAAC,CAAC"}
@@ -1,21 +1,8 @@
1
- //#region src/SchemaModelType.ts
2
- /**
3
- * Type guard to check if a value implements the SchemaType interface.
4
- *
5
- * @param value - Value to check
6
- * @returns True if value has a getZod method
7
- *
8
- * @example
9
- * ```typescript
10
- * if (isSchemaType(field.type)) {
11
- * const zodSchema = field.type.getZod();
12
- * }
13
- * ```
14
- */
1
+ // @bun
2
+ // src/SchemaModelType.ts
15
3
  function isSchemaType(value) {
16
- return value !== null && typeof value === "object" && "getZod" in value && typeof value.getZod === "function";
4
+ return value !== null && typeof value === "object" && "getZod" in value && typeof value.getZod === "function";
17
5
  }
18
-
19
- //#endregion
20
- export { isSchemaType };
21
- //# sourceMappingURL=SchemaModelType.js.map
6
+ export {
7
+ isSchemaType
8
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=SchemaModelType.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SchemaModelType.test.d.ts","sourceRoot":"","sources":["../src/SchemaModelType.test.ts"],"names":[],"mappings":""}
@@ -1,15 +1,21 @@
1
- import { SchemaModelType } from "./SchemaModelType.js";
2
- import * as z$1 from "zod";
3
-
4
- //#region src/ZodSchemaType.d.ts
1
+ /**
2
+ * Zod schema wrapper for ContractSpec compatibility.
3
+ *
4
+ * Wraps raw Zod schemas to implement the SchemaType interface,
5
+ * enabling direct Zod usage in ContractSpec definitions.
6
+ *
7
+ * @module ZodSchemaType
8
+ */
9
+ import * as z from 'zod';
10
+ import type { SchemaModelType } from './SchemaModelType';
5
11
  /**
6
12
  * Options for ZodSchemaType wrapper.
7
13
  */
8
- interface ZodSchemaTypeOptions {
9
- /** Name for GraphQL/JSON Schema representation */
10
- name?: string;
11
- /** Description for documentation */
12
- description?: string;
14
+ export interface ZodSchemaTypeOptions {
15
+ /** Name for GraphQL/JSON Schema representation */
16
+ name?: string;
17
+ /** Description for documentation */
18
+ description?: string;
13
19
  }
14
20
  /**
15
21
  * Wrapper to use a raw Zod schema as a SchemaType.
@@ -37,34 +43,34 @@ interface ZodSchemaTypeOptions {
37
43
  * });
38
44
  * ```
39
45
  */
40
- declare class ZodSchemaType<T extends z$1.ZodType> implements SchemaModelType<z$1.infer<T>> {
41
- private readonly schema;
42
- private readonly options;
43
- constructor(schema: T, options?: ZodSchemaTypeOptions);
44
- /**
45
- * Return the wrapped Zod schema.
46
- */
47
- getZod(): z$1.ZodType<z$1.infer<T>>;
48
- /**
49
- * Return JSON Schema representation using Zod's toJSONSchema.
50
- */
51
- getJsonSchema(): unknown;
52
- /**
53
- * Return GraphQL type info.
54
- * For complex Zod types, defaults to JSON scalar.
55
- */
56
- getPothos(): {
57
- type: string;
58
- name?: string;
59
- };
60
- /**
61
- * Get the configured name for this schema.
62
- */
63
- getName(): string | undefined;
64
- /**
65
- * Get the configured description for this schema.
66
- */
67
- getDescription(): string | undefined;
46
+ export declare class ZodSchemaType<T extends z.ZodType> implements SchemaModelType<z.infer<T>> {
47
+ private readonly schema;
48
+ private readonly options;
49
+ constructor(schema: T, options?: ZodSchemaTypeOptions);
50
+ /**
51
+ * Return the wrapped Zod schema.
52
+ */
53
+ getZod(): z.ZodType<z.infer<T>>;
54
+ /**
55
+ * Return JSON Schema representation using Zod's toJSONSchema.
56
+ */
57
+ getJsonSchema(): unknown;
58
+ /**
59
+ * Return GraphQL type info.
60
+ * For complex Zod types, defaults to JSON scalar.
61
+ */
62
+ getPothos(): {
63
+ type: string;
64
+ name?: string;
65
+ };
66
+ /**
67
+ * Get the configured name for this schema.
68
+ */
69
+ getName(): string | undefined;
70
+ /**
71
+ * Get the configured description for this schema.
72
+ */
73
+ getDescription(): string | undefined;
68
74
  }
69
75
  /**
70
76
  * Helper to wrap a raw Zod schema as a SchemaType.
@@ -85,7 +91,5 @@ declare class ZodSchemaType<T extends z$1.ZodType> implements SchemaModelType<z$
85
91
  * );
86
92
  * ```
87
93
  */
88
- declare const fromZod: <T extends z$1.ZodType>(schema: T, options?: ZodSchemaTypeOptions) => ZodSchemaType<T>;
89
- //#endregion
90
- export { ZodSchemaType, ZodSchemaTypeOptions, fromZod };
94
+ export declare const fromZod: <T extends z.ZodType>(schema: T, options?: ZodSchemaTypeOptions) => ZodSchemaType<T>;
91
95
  //# sourceMappingURL=ZodSchemaType.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ZodSchemaType.d.ts","names":[],"sources":["../src/ZodSchemaType.ts"],"mappings":";;;;;;;UAeiB,oBAAA;EAkCf;EAhCA,IAAA;EAqCgC;EAnChC,WAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;cA6BW,aAAA,WAAwB,GAAA,CAAE,OAAA,aAAoB,eAAA,CACzD,GAAA,CAAE,KAAA,CAAM,CAAA;EAAA,iBAES,MAAA;EAAA,iBACA,OAAA;cAEL,MAAA,EAAQ,CAAA,EAAG,OAAA,GAAS,oBAAA;EAqClB;;AAwBhB;EArDE,MAAA,CAAA,GAAU,GAAA,CAAE,OAAA,CAAQ,GAAA,CAAE,KAAA,CAAM,CAAA;EAwD2B;;;EAjDvD,aAAA,CAAA;EAiDe;;;;EAzCf,SAAA,CAAA;IAAe,IAAA;IAAc,IAAA;EAAA;EAuC7B;;;EAhCA,OAAA,CAAA;EAkCe;;;EA3Bf,cAAA,CAAA;AAAA;;;;;;;;;;;;;;;;;;;;cAwBW,OAAA,aAAqB,GAAA,CAAE,OAAA,EAClC,MAAA,EAAQ,CAAA,EACR,OAAA,GAAU,oBAAA,KACT,aAAA,CAAc,CAAA"}
1
+ {"version":3,"file":"ZodSchemaType.d.ts","sourceRoot":"","sources":["../src/ZodSchemaType.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AACzB,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEzD;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,kDAAkD;IAClD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,oCAAoC;IACpC,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,qBAAa,aAAa,CAAC,CAAC,SAAS,CAAC,CAAC,OAAO,CAAE,YAAW,eAAe,CACxE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CACX;IACC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAI;IAC3B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAuB;gBAEnC,MAAM,EAAE,CAAC,EAAE,OAAO,GAAE,oBAAyB;IAKzD;;OAEG;IACH,MAAM,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAI/B;;OAEG;IACH,aAAa,IAAI,OAAO;IAIxB;;;OAGG;IACH,SAAS,IAAI;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE;IAI5C;;OAEG;IACH,OAAO,IAAI,MAAM,GAAG,SAAS;IAI7B;;OAEG;IACH,cAAc,IAAI,MAAM,GAAG,SAAS;CAGrC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,OAAO,GAAI,CAAC,SAAS,CAAC,CAAC,OAAO,EACzC,QAAQ,CAAC,EACT,UAAU,oBAAoB,KAC7B,aAAa,CAAC,CAAC,CAAuC,CAAC"}
@@ -1,103 +1,32 @@
1
- import * as z$1 from "zod";
1
+ // @bun
2
+ // src/ZodSchemaType.ts
3
+ import * as z from "zod";
2
4
 
3
- //#region src/ZodSchemaType.ts
4
- /**
5
- * Zod schema wrapper for ContractSpec compatibility.
6
- *
7
- * Wraps raw Zod schemas to implement the SchemaType interface,
8
- * enabling direct Zod usage in ContractSpec definitions.
9
- *
10
- * @module ZodSchemaType
11
- */
12
- /**
13
- * Wrapper to use a raw Zod schema as a SchemaType.
14
- * Enables schema definition directly with Zod for maximum flexibility.
15
- *
16
- * @template T - The Zod schema type being wrapped
17
- *
18
- * @example
19
- * ```typescript
20
- * const UserSchema = fromZod(
21
- * z.object({
22
- * id: z.string().uuid(),
23
- * name: z.string().min(1),
24
- * email: z.string().email(),
25
- * }),
26
- * { name: 'User' }
27
- * );
28
- *
29
- * // Use in SchemaModel
30
- * const CreateUserInput = new SchemaModel({
31
- * name: 'CreateUserInput',
32
- * fields: {
33
- * user: { type: UserSchema, isOptional: false },
34
- * },
35
- * });
36
- * ```
37
- */
38
- var ZodSchemaType = class {
39
- schema;
40
- options;
41
- constructor(schema, options = {}) {
42
- this.schema = schema;
43
- this.options = options;
44
- }
45
- /**
46
- * Return the wrapped Zod schema.
47
- */
48
- getZod() {
49
- return this.schema;
50
- }
51
- /**
52
- * Return JSON Schema representation using Zod's toJSONSchema.
53
- */
54
- getJsonSchema() {
55
- return z$1.toJSONSchema(this.schema);
56
- }
57
- /**
58
- * Return GraphQL type info.
59
- * For complex Zod types, defaults to JSON scalar.
60
- */
61
- getPothos() {
62
- return {
63
- type: "JSON",
64
- name: this.options.name
65
- };
66
- }
67
- /**
68
- * Get the configured name for this schema.
69
- */
70
- getName() {
71
- return this.options.name;
72
- }
73
- /**
74
- * Get the configured description for this schema.
75
- */
76
- getDescription() {
77
- return this.options.description;
78
- }
5
+ class ZodSchemaType {
6
+ schema;
7
+ options;
8
+ constructor(schema, options = {}) {
9
+ this.schema = schema;
10
+ this.options = options;
11
+ }
12
+ getZod() {
13
+ return this.schema;
14
+ }
15
+ getJsonSchema() {
16
+ return z.toJSONSchema(this.schema);
17
+ }
18
+ getPothos() {
19
+ return { type: "JSON", name: this.options.name };
20
+ }
21
+ getName() {
22
+ return this.options.name;
23
+ }
24
+ getDescription() {
25
+ return this.options.description;
26
+ }
27
+ }
28
+ var fromZod = (schema, options) => new ZodSchemaType(schema, options);
29
+ export {
30
+ fromZod,
31
+ ZodSchemaType
79
32
  };
80
- /**
81
- * Helper to wrap a raw Zod schema as a SchemaType.
82
- *
83
- * @param schema - The Zod schema to wrap
84
- * @param options - Optional configuration
85
- * @returns A SchemaType-compatible wrapper
86
- *
87
- * @example
88
- * ```typescript
89
- * const AddressSchema = fromZod(
90
- * z.object({
91
- * street: z.string(),
92
- * city: z.string(),
93
- * country: z.string(),
94
- * }),
95
- * { name: 'Address', description: 'Physical address' }
96
- * );
97
- * ```
98
- */
99
- const fromZod = (schema, options) => new ZodSchemaType(schema, options);
100
-
101
- //#endregion
102
- export { ZodSchemaType, fromZod };
103
- //# sourceMappingURL=ZodSchemaType.js.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=ZodSchemaType.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ZodSchemaType.test.d.ts","sourceRoot":"","sources":["../src/ZodSchemaType.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,40 @@
1
+ // src/EnumType.ts
2
+ import * as z from "zod";
3
+ import { GraphQLEnumType } from "graphql";
4
+
5
+ class EnumType {
6
+ name;
7
+ values;
8
+ gqlEnum;
9
+ constructor(name, values) {
10
+ this.name = name;
11
+ this.values = values;
12
+ this.gqlEnum = new GraphQLEnumType({
13
+ name: this.name,
14
+ values: Object.fromEntries(values.map((v) => [v, { value: v }]))
15
+ });
16
+ }
17
+ getName() {
18
+ return this.name;
19
+ }
20
+ getEnumValues() {
21
+ return this.values;
22
+ }
23
+ getPothos() {
24
+ return this.gqlEnum;
25
+ }
26
+ getZod() {
27
+ return z.enum(this.values);
28
+ }
29
+ getJson() {
30
+ return { type: "string", enum: this.values };
31
+ }
32
+ getJsonSchema() {
33
+ return this.getJson();
34
+ }
35
+ }
36
+ var defineEnum = (name, values) => new EnumType(name, values);
37
+ export {
38
+ defineEnum,
39
+ EnumType
40
+ };
@@ -0,0 +1,43 @@
1
+ // src/FieldType.ts
2
+ import { GraphQLScalarType } from "graphql";
3
+
4
+ class FieldType extends GraphQLScalarType {
5
+ zodSchema;
6
+ jsonSchemaDef;
7
+ constructor(config) {
8
+ super(config);
9
+ this.zodSchema = config.zod;
10
+ this.jsonSchemaDef = config.jsonSchema;
11
+ }
12
+ getZod() {
13
+ return this.zodSchema;
14
+ }
15
+ getPothos() {
16
+ return this;
17
+ }
18
+ getJson() {
19
+ return typeof this.jsonSchemaDef === "function" ? this.jsonSchemaDef() : this.jsonSchemaDef;
20
+ }
21
+ getJsonSchemaDef() {
22
+ return this.jsonSchemaDef;
23
+ }
24
+ getJsonSchema() {
25
+ const deepResolve = (v) => {
26
+ const value = typeof v === "function" ? v() : v;
27
+ if (Array.isArray(value))
28
+ return value.map((item) => deepResolve(item));
29
+ if (value && typeof value === "object") {
30
+ const obj = {};
31
+ for (const [k, val] of Object.entries(value)) {
32
+ obj[k] = deepResolve(val);
33
+ }
34
+ return obj;
35
+ }
36
+ return value;
37
+ };
38
+ return deepResolve(this.getJson());
39
+ }
40
+ }
41
+ export {
42
+ FieldType
43
+ };
@@ -0,0 +1,18 @@
1
+ // src/GraphQLSchemaType.ts
2
+ import { z } from "zod";
3
+
4
+ class GraphQLSchemaType {
5
+ typeDef;
6
+ name;
7
+ _isSchemaType = true;
8
+ constructor(typeDef, name) {
9
+ this.typeDef = typeDef;
10
+ this.name = name;
11
+ }
12
+ getZod() {
13
+ return z.unknown();
14
+ }
15
+ }
16
+ export {
17
+ GraphQLSchemaType
18
+ };
@@ -0,0 +1,74 @@
1
+ // src/JsonSchemaType.ts
2
+ import * as z from "zod";
3
+
4
+ class JsonSchemaType {
5
+ jsonSchema;
6
+ options;
7
+ constructor(jsonSchema, options = {}) {
8
+ this.jsonSchema = jsonSchema;
9
+ this.options = options;
10
+ }
11
+ getZod() {
12
+ if (this.jsonSchema.additionalProperties !== undefined) {
13
+ if (this.jsonSchema.additionalProperties === true) {
14
+ return z.record(z.string(), z.unknown());
15
+ }
16
+ if (typeof this.jsonSchema.additionalProperties === "object") {
17
+ return z.record(z.string(), z.unknown());
18
+ }
19
+ if (this.jsonSchema.additionalProperties === false) {
20
+ return z.object({}).strict();
21
+ }
22
+ }
23
+ if (this.jsonSchema.properties) {
24
+ const shape = {};
25
+ const required = new Set(this.jsonSchema.required ?? []);
26
+ for (const [key, propSchema] of Object.entries(this.jsonSchema.properties)) {
27
+ const fieldType = this.convertPropertyToZod(propSchema);
28
+ shape[key] = required.has(key) ? fieldType : fieldType.optional();
29
+ }
30
+ return z.object(shape).passthrough();
31
+ }
32
+ return z.object({}).passthrough();
33
+ }
34
+ getJsonSchema() {
35
+ return this.jsonSchema;
36
+ }
37
+ getPothos() {
38
+ return { type: "JSON", name: this.options.name };
39
+ }
40
+ getName() {
41
+ return this.options.name;
42
+ }
43
+ convertPropertyToZod(schema) {
44
+ const type = Array.isArray(schema.type) ? schema.type[0] : schema.type;
45
+ switch (type) {
46
+ case "string":
47
+ return z.string();
48
+ case "number":
49
+ case "integer":
50
+ return z.number();
51
+ case "boolean":
52
+ return z.boolean();
53
+ case "array":
54
+ if (schema.items && !Array.isArray(schema.items)) {
55
+ return z.array(this.convertPropertyToZod(schema.items));
56
+ }
57
+ return z.array(z.unknown());
58
+ case "object":
59
+ if (schema.properties) {
60
+ return new JsonSchemaType(schema).getZod();
61
+ }
62
+ return z.record(z.string(), z.unknown());
63
+ case "null":
64
+ return z.null();
65
+ default:
66
+ return z.unknown();
67
+ }
68
+ }
69
+ }
70
+ var fromJsonSchema = (schema, options) => new JsonSchemaType(schema, options);
71
+ export {
72
+ fromJsonSchema,
73
+ JsonSchemaType
74
+ };