@contractspec/lib.schema 1.44.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 (50) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +154 -0
  3. package/dist/EnumType.d.ts +42 -0
  4. package/dist/EnumType.d.ts.map +1 -0
  5. package/dist/EnumType.js +57 -0
  6. package/dist/EnumType.js.map +1 -0
  7. package/dist/FieldType.d.ts +31 -0
  8. package/dist/FieldType.d.ts.map +1 -0
  9. package/dist/FieldType.js +50 -0
  10. package/dist/FieldType.js.map +1 -0
  11. package/dist/GraphQLSchemaType.d.ts +19 -0
  12. package/dist/GraphQLSchemaType.d.ts.map +1 -0
  13. package/dist/GraphQLSchemaType.js +21 -0
  14. package/dist/GraphQLSchemaType.js.map +1 -0
  15. package/dist/JsonSchemaType.d.ts +112 -0
  16. package/dist/JsonSchemaType.d.ts.map +1 -0
  17. package/dist/JsonSchemaType.js +125 -0
  18. package/dist/JsonSchemaType.js.map +1 -0
  19. package/dist/ScalarTypeEnum.d.ts +33 -0
  20. package/dist/ScalarTypeEnum.d.ts.map +1 -0
  21. package/dist/ScalarTypeEnum.js +237 -0
  22. package/dist/ScalarTypeEnum.js.map +1 -0
  23. package/dist/SchemaModel.d.ts +92 -0
  24. package/dist/SchemaModel.d.ts.map +1 -0
  25. package/dist/SchemaModel.js +58 -0
  26. package/dist/SchemaModel.js.map +1 -0
  27. package/dist/SchemaModelType.d.ts +60 -0
  28. package/dist/SchemaModelType.d.ts.map +1 -0
  29. package/dist/SchemaModelType.js +21 -0
  30. package/dist/SchemaModelType.js.map +1 -0
  31. package/dist/ZodSchemaType.d.ts +92 -0
  32. package/dist/ZodSchemaType.d.ts.map +1 -0
  33. package/dist/ZodSchemaType.js +103 -0
  34. package/dist/ZodSchemaType.js.map +1 -0
  35. package/dist/entity/defineEntity.d.ts +93 -0
  36. package/dist/entity/defineEntity.d.ts.map +1 -0
  37. package/dist/entity/defineEntity.js +237 -0
  38. package/dist/entity/defineEntity.js.map +1 -0
  39. package/dist/entity/generator.d.ts +38 -0
  40. package/dist/entity/generator.d.ts.map +1 -0
  41. package/dist/entity/generator.js +218 -0
  42. package/dist/entity/generator.js.map +1 -0
  43. package/dist/entity/index.d.ts +4 -0
  44. package/dist/entity/index.js +5 -0
  45. package/dist/entity/types.d.ts +146 -0
  46. package/dist/entity/types.d.ts.map +1 -0
  47. package/dist/entity/types.js +1 -0
  48. package/dist/index.d.ts +13 -0
  49. package/dist/index.js +13 -0
  50. package/package.json +64 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Chaman Ventures, SASU
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,154 @@
1
+ # @contractspec/lib.schema
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@contractspec/lib.schema)](https://www.npmjs.com/package/@contractspec/lib.schema)
4
+ [![npm downloads](https://img.shields.io/npm/dt/@contractspec/lib.schema)](https://www.npmjs.com/package/@contractspec/lib.schema)
5
+ [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/lssm-tech/contractspec)
6
+
7
+
8
+ Website: https://contractspec.io/
9
+
10
+
11
+ **Multi-surface consistency starts here** — Define schemas once, export to Zod, GraphQL, and JSON Schema.
12
+
13
+ A schema dictionary to describe operation I/O once and generate consistent types across all surfaces (API, database, UI). Part of ContractSpec's spec-first approach.
14
+
15
+ ## Exports to:
16
+
17
+ - zod (runtime validation)
18
+ - Pothos (GraphQL type refs)
19
+ - JSON Schema (via `zod-to-json-schema` or custom `getJsonSchema()` on `FieldType`)
20
+
21
+ ## Installation
22
+
23
+ ```bash
24
+ npm install @contractspec/lib.schema
25
+ # or
26
+ bun add @contractspec/lib.schema
27
+ ```
28
+
29
+ The package ships only pre-built `dist/` artifacts and type declarations. Import helpers directly, e.g.:
30
+
31
+ ```ts
32
+ import { SchemaModel, ScalarTypeEnum } from '@contractspec/lib.schema';
33
+ ```
34
+
35
+ ## Primitives
36
+
37
+ - `FieldType<T>` wraps a GraphQLScalarType and carries a zod schema, plus optional JSON Schema definition.
38
+ - `SchemaModel` composes fields into named object models with helpers:
39
+ - `getZod()` → typed `z.ZodObject` preserving each field's schema and optionality
40
+ - `getPothosInput()` → builder name for input object
41
+ - `getPothosInput()` → builder name for input object
42
+ - `getJsonSchema()` → resolved JSON Schema
43
+
44
+ ### Multi-Format Wrappers
45
+
46
+ Use existing schema definitions from other libraries:
47
+ - `ZodSchemaType`: Wraps a raw Zod schema (`z.infer` becomes the TS type).
48
+ - `JsonSchemaType`: Wraps a JSON Schema object.
49
+ - `GraphQLSchemaType`: Wraps a GraphQL SDL string.
50
+
51
+ ### Enums
52
+
53
+ - `EnumType<T>` represents string enums with a single source of truth:
54
+ - `getZod()` → `z.enum([...])`
55
+ - `getPothos()` → GraphQL `GraphQLEnumType`
56
+ - `getJsonSchema()` → `{ type: 'string', enum: [...] }`
57
+ - Create with `defineEnum('Name', ['A','B'] as const)`
58
+
59
+ Example
60
+
61
+ ```ts
62
+ import { defineEnum, SchemaModel, ScalarTypeEnum } from '@contractspec/lib.schema';
63
+
64
+ const Weekday = defineEnum('Weekday', [
65
+ 'MO',
66
+ 'TU',
67
+ 'WE',
68
+ 'TH',
69
+ 'FR',
70
+ 'SA',
71
+ 'SU',
72
+ ] as const);
73
+
74
+ const Rule = new SchemaModel({
75
+ name: 'Rule',
76
+ fields: {
77
+ timezone: { type: ScalarTypeEnum.TimeZone(), isOptional: false },
78
+ frequency: {
79
+ type: defineEnum('RecurrenceFrequency', [
80
+ 'DAILY',
81
+ 'WEEKLY',
82
+ 'MONTHLY',
83
+ 'YEARLY',
84
+ ] as const),
85
+ isOptional: false,
86
+ },
87
+ byWeekday: { type: Weekday, isOptional: true, isArray: true },
88
+ },
89
+ });
90
+ ```
91
+
92
+ ## Usage
93
+
94
+ Define fields with `FieldType` and group them into a `SchemaModel`. Adapters (REST/MCP/GraphQL) can consume these to generate appropriate I/O types while preserving a single source of truth.
95
+
96
+ ## API Overview
97
+
98
+ - **FieldType<TInternal, TExternal = TInternal>**
99
+ - `getZod()` → zod schema
100
+ - `getPothos()` → GraphQL scalar type
101
+ - `getJsonSchema()` → deeply-resolved JSON Schema
102
+ - **EnumType<T>**
103
+ - Construct via `defineEnum('Name', ['A','B'] as const)`
104
+ - `getZod()`, `getPothos()`, `getJsonSchema()`
105
+ - **SchemaModel<Fields>**
106
+ - `getZod()` → typed `z.ZodObject`
107
+ - `getPothosInput()` → input object name for GraphQL builders
108
+ - Each field supports `{ isOptional: boolean, isArray?: true }`
109
+
110
+ ## Patterns & Conventions
111
+
112
+ - Name models with PascalCase and suffix with `Input`/`Result` when ambiguous.
113
+ - Use `ScalarTypeEnum` for common scalars: `NonEmptyString`, `Date`, `DateTime`, `Locale`, `TimeZone`, `Latitude`, `Longitude`, `PhoneNumber`, etc.
114
+ - Prefer enums for constrained strings. Reuse enums across input/output models to ensure consistency.
115
+ - Top-level arrays: define a nested model and set `isArray: true` on the field that holds the list, or create a dedicated list model if needed by adapters.
116
+
117
+ ## Example: Nested models + arrays
118
+
119
+ ```ts
120
+ import { ScalarTypeEnum, SchemaModel, defineEnum } from '@contractspec/lib.schema';
121
+
122
+ const Weekday = defineEnum('Weekday', [
123
+ 'MO',
124
+ 'TU',
125
+ 'WE',
126
+ 'TH',
127
+ 'FR',
128
+ 'SA',
129
+ 'SU',
130
+ ] as const);
131
+
132
+ const Address = new SchemaModel({
133
+ name: 'Address',
134
+ fields: {
135
+ city: { type: ScalarTypeEnum.NonEmptyString(), isOptional: false },
136
+ country: { type: ScalarTypeEnum.CountryCode(), isOptional: false },
137
+ },
138
+ });
139
+
140
+ export const CreateSpotInput = new SchemaModel({
141
+ name: 'CreateSpotInput',
142
+ fields: {
143
+ name: { type: ScalarTypeEnum.NonEmptyString(), isOptional: false },
144
+ latitude: { type: ScalarTypeEnum.Latitude(), isOptional: false },
145
+ longitude: { type: ScalarTypeEnum.Longitude(), isOptional: false },
146
+ availabilityWeekdays: { type: Weekday, isOptional: true, isArray: true },
147
+ address: { type: Address, isOptional: true },
148
+ },
149
+ });
150
+ ```
151
+
152
+ ## JSON Schema export
153
+
154
+ Prefer `getZod()` + `zod-to-json-schema` for consistency. For advanced cases, expose a custom `getJsonSchema()` from `FieldType` or a specialized wrapper.
@@ -0,0 +1,42 @@
1
+ import * as z$1 from "zod";
2
+ import { GraphQLEnumType } from "graphql";
3
+
4
+ //#region src/EnumType.d.ts
5
+
6
+ /**
7
+ * Strongly-typed string enum wrapper with one source of truth for zod, GraphQL, and JSON Schema.
8
+ */
9
+ declare class EnumType<T extends [string, ...string[]]> {
10
+ private readonly name;
11
+ private readonly values;
12
+ private readonly gqlEnum;
13
+ constructor(name: string, values: T);
14
+ /** Enum type name (used by GraphQL and JSON Schema). */
15
+ getName(): string;
16
+ /** Returns the literal tuple of allowed values. */
17
+ getEnumValues(): T;
18
+ /** GraphQL enum instance suitable for Pothos or vanilla GraphQL schemas. */
19
+ getPothos(): GraphQLEnumType;
20
+ /** zod schema representing this enum. */
21
+ getZod(): z$1.ZodEnum<{ [K in T[number]]: K }>;
22
+ /** Minimal JSON representation (alias of getJsonSchema). */
23
+ getJson(): {
24
+ type: 'string';
25
+ enum: T;
26
+ };
27
+ /** JSON Schema for this enum. */
28
+ getJsonSchema(): {
29
+ type: 'string';
30
+ enum: T;
31
+ };
32
+ }
33
+ type AnyEnumType = EnumType<[string, ...string[]]>;
34
+ /**
35
+ * Helper to define an EnumType.
36
+ * @param name Display/type name used across GraphQL and JSON Schema
37
+ * @param values Literal tuple of allowed string values (at least one)
38
+ */
39
+ declare const defineEnum: <T extends [string, ...string[]]>(name: string, values: T) => EnumType<T>;
40
+ //#endregion
41
+ export { AnyEnumType, EnumType, defineEnum };
42
+ //# sourceMappingURL=EnumType.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EnumType.d.ts","names":[],"sources":["../src/EnumType.ts"],"sourcesContent":[],"mappings":";;;;;;;AAMA;AAKoC,cALvB,QAKuB,CAAA,UAAA,CAAA,MAAA,EAAA,GAAA,MAAA,EAAA,CAAA,CAAA,CAAA;EAejB,iBAAA,IAAA;EAMJ,iBAAA,MAAA;EAKe,iBAAA,OAAA;EAAY,WAAA,CAAA,IAAA,EAAA,MAAA,EAAA,MAAA,EA1BN,CA0BM;EAA9B;EAKyB,OAAA,CAAA,CAAA,EAAA,MAAA;EAKM;EAAC,aAAA,CAAA,CAAA,EArBzB,CAqByB;EAKhC;EAOC,SAAA,CAAA,CAAA,EA3BE,eA8BgB;EADrB;EAAC,MAAA,CAAA,CAAA,EAxBC,GAAA,CAAE,OAwBH,CAAA,QAxBmB,CAwBnB,CAAA,MAAA,CAAA,GAxB+B,CAwB/B,EAAA,CAAA;;;;UAnB0B;;;;;UAKM;;;KAK/B,WAAA,GAAc;;;;;;cAOb,oEAEH,MAAC,SAAA"}
@@ -0,0 +1,57 @@
1
+ import * as z$1 from "zod";
2
+ import { GraphQLEnumType } from "graphql";
3
+
4
+ //#region src/EnumType.ts
5
+ /**
6
+ * Strongly-typed string enum wrapper with one source of truth for zod, GraphQL, and JSON Schema.
7
+ */
8
+ var EnumType = class {
9
+ name;
10
+ values;
11
+ gqlEnum;
12
+ constructor(name, values) {
13
+ this.name = name;
14
+ this.values = values;
15
+ this.gqlEnum = new GraphQLEnumType({
16
+ name: this.name,
17
+ values: Object.fromEntries(values.map((v) => [v, { value: v }]))
18
+ });
19
+ }
20
+ /** Enum type name (used by GraphQL and JSON Schema). */
21
+ getName() {
22
+ return this.name;
23
+ }
24
+ /** Returns the literal tuple of allowed values. */
25
+ getEnumValues() {
26
+ return this.values;
27
+ }
28
+ /** GraphQL enum instance suitable for Pothos or vanilla GraphQL schemas. */
29
+ getPothos() {
30
+ return this.gqlEnum;
31
+ }
32
+ /** zod schema representing this enum. */
33
+ getZod() {
34
+ return z$1.enum(this.values);
35
+ }
36
+ /** Minimal JSON representation (alias of getJsonSchema). */
37
+ getJson() {
38
+ return {
39
+ type: "string",
40
+ enum: this.values
41
+ };
42
+ }
43
+ /** JSON Schema for this enum. */
44
+ getJsonSchema() {
45
+ return this.getJson();
46
+ }
47
+ };
48
+ /**
49
+ * Helper to define an EnumType.
50
+ * @param name Display/type name used across GraphQL and JSON Schema
51
+ * @param values Literal tuple of allowed string values (at least one)
52
+ */
53
+ const defineEnum = (name, values) => new EnumType(name, values);
54
+
55
+ //#endregion
56
+ export { EnumType, defineEnum };
57
+ //# sourceMappingURL=EnumType.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EnumType.js","names":["z"],"sources":["../src/EnumType.ts"],"sourcesContent":["import * as z from 'zod';\nimport { GraphQLEnumType } from 'graphql';\n\n/**\n * Strongly-typed string enum wrapper with one source of truth for zod, GraphQL, and JSON Schema.\n */\nexport class EnumType<T extends [string, ...string[]]> {\n private readonly name: string;\n private readonly values: T;\n private readonly gqlEnum: GraphQLEnumType;\n\n constructor(name: string, values: T) {\n this.name = name;\n this.values = values;\n this.gqlEnum = new GraphQLEnumType({\n name: this.name,\n values: Object.fromEntries(values.map((v) => [v, { value: v }] as const)),\n });\n }\n\n /** Enum type name (used by GraphQL and JSON Schema). */\n getName(): string {\n return this.name;\n }\n\n /** Returns the literal tuple of allowed values. */\n getEnumValues(): T {\n return this.values;\n }\n\n // For SchemaModel and adapters expecting a Pothos/GraphQL type-like object\n /** GraphQL enum instance suitable for Pothos or vanilla GraphQL schemas. */\n getPothos(): GraphQLEnumType {\n return this.gqlEnum;\n }\n\n /** zod schema representing this enum. */\n getZod(): z.ZodEnum<{ [K in T[number]]: K }> {\n return z.enum(this.values);\n }\n\n /** Minimal JSON representation (alias of getJsonSchema). */\n getJson(): { type: 'string'; enum: T } {\n return { type: 'string', enum: this.values } as const;\n }\n\n /** JSON Schema for this enum. */\n getJsonSchema(): { type: 'string'; enum: T } {\n return this.getJson();\n }\n}\n\nexport type AnyEnumType = EnumType<[string, ...string[]]>;\n\n/**\n * Helper to define an EnumType.\n * @param name Display/type name used across GraphQL and JSON Schema\n * @param values Literal tuple of allowed string values (at least one)\n */\nexport const defineEnum = <T extends [string, ...string[]]>(\n name: string,\n values: T\n) => new EnumType(name, values);\n"],"mappings":";;;;;;;AAMA,IAAa,WAAb,MAAuD;CACrD,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,YAAY,MAAc,QAAW;AACnC,OAAK,OAAO;AACZ,OAAK,SAAS;AACd,OAAK,UAAU,IAAI,gBAAgB;GACjC,MAAM,KAAK;GACX,QAAQ,OAAO,YAAY,OAAO,KAAK,MAAM,CAAC,GAAG,EAAE,OAAO,GAAG,CAAC,CAAU,CAAC;GAC1E,CAAC;;;CAIJ,UAAkB;AAChB,SAAO,KAAK;;;CAId,gBAAmB;AACjB,SAAO,KAAK;;;CAKd,YAA6B;AAC3B,SAAO,KAAK;;;CAId,SAA6C;AAC3C,SAAOA,IAAE,KAAK,KAAK,OAAO;;;CAI5B,UAAuC;AACrC,SAAO;GAAE,MAAM;GAAU,MAAM,KAAK;GAAQ;;;CAI9C,gBAA6C;AAC3C,SAAO,KAAK,SAAS;;;;;;;;AAWzB,MAAa,cACX,MACA,WACG,IAAI,SAAS,MAAM,OAAO"}
@@ -0,0 +1,31 @@
1
+ import * as z$1 from "zod";
2
+ import { GraphQLScalarType, GraphQLScalarTypeConfig } from "graphql";
3
+
4
+ //#region src/FieldType.d.ts
5
+ interface FieldTypeConfig<TInternal, TExternal = TInternal> extends GraphQLScalarTypeConfig<TInternal, TExternal> {
6
+ zod: z$1.ZodType<TInternal>;
7
+ jsonSchema: unknown | (() => unknown);
8
+ }
9
+ type AnyFieldType = FieldType<any, any>;
10
+ /**
11
+ * GraphQL scalar wrapper that carries zod and JSON Schema metadata.
12
+ *
13
+ * TInternal is the runtime representation; TExternal is the GraphQL output.
14
+ */
15
+ declare class FieldType<TInternal, TExternal = TInternal> extends GraphQLScalarType<TInternal, TExternal> {
16
+ private zodSchema;
17
+ private readonly jsonSchemaDef?;
18
+ constructor(config: FieldTypeConfig<TInternal, TExternal>);
19
+ /** Return the attached zod schema for validation. */
20
+ getZod(): z$1.ZodType<TInternal>;
21
+ /** GraphQL scalar instance usable by Pothos or vanilla GraphQL. */
22
+ getPothos(): GraphQLScalarType<TInternal, TExternal>;
23
+ /** Return the JSON Schema (evaluates factory if provided). */
24
+ getJson(): unknown;
25
+ getJsonSchemaDef(): unknown | (() => unknown);
26
+ getJsonSchema(): unknown;
27
+ }
28
+ type ZodFieldType<Field extends AnyFieldType> = z$1.infer<ReturnType<Field['getZod']>>;
29
+ //#endregion
30
+ export { AnyFieldType, FieldType, FieldTypeConfig, ZodFieldType };
31
+ //# sourceMappingURL=FieldType.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FieldType.d.ts","names":[],"sources":["../src/FieldType.ts"],"sourcesContent":[],"mappings":";;;;UAGiB,uCAEH,mBACJ,wBAAwB,WAAW;OACtC,GAAA,CAAE,QAAQ;EAJA,UAAA,EAAA,OAAe,GAAA,CAAA,GAAA,GAAA,OAAA,CAAA;;AAGE,KAMtB,YAAA,GAAe,SANO,CAAA,GAAA,EAAA,GAAA,CAAA;;;;;;AAMtB,cAOC,SAPW,CAAG,SAAA,EAAS,YAStB,SATsB,CAAA,SAU1B,iBAV0B,CAUR,SAVQ,EAUG,SAVH,CAAA,CAAA;EAOvB,QAAA,SAAS;EAER,iBAAA,aAAA;EACc,WAAA,CAAA,MAAA,EAIN,eAJM,CAIU,SAJV,EAIqB,SAJrB,CAAA;EAAW;EAID,MAAA,CAAA,CAAA,EAO1B,GAAA,CAAE,OAPwB,CAOhB,SAPgB,CAAA;EAAW;EAA3B,SAAA,CAAA,CAAA,EAYP,iBAZO,CAYW,SAZX,EAYsB,SAZtB,CAAA;EAOA;EAAV,OAAE,CAAA,CAAA,EAAA,OAAA;EAKmB,gBAAA,CAAA,CAAA,EAAA,OAAA,GAAA,CAAA,GAAA,GAAA,OAAA,CAAA;EAAW,aAAA,CAAA,CAAA,EAAA,OAAA;;AAhBlC,KAoDE,YApDF,CAAA,cAoD6B,YApD7B,CAAA,GAoD6C,GAAA,CAAE,KApD/C,CAqDR,UArDQ,CAqDG,KArDH,CAAA,QAAA,CAAA,CAAA,CAAA"}
@@ -0,0 +1,50 @@
1
+ import "zod";
2
+ import { GraphQLScalarType } from "graphql";
3
+
4
+ //#region src/FieldType.ts
5
+ /**
6
+ * GraphQL scalar wrapper that carries zod and JSON Schema metadata.
7
+ *
8
+ * TInternal is the runtime representation; TExternal is the GraphQL output.
9
+ */
10
+ var FieldType = class extends GraphQLScalarType {
11
+ zodSchema;
12
+ jsonSchemaDef;
13
+ constructor(config) {
14
+ super(config);
15
+ this.zodSchema = config.zod;
16
+ this.jsonSchemaDef = config.jsonSchema;
17
+ }
18
+ /** Return the attached zod schema for validation. */
19
+ getZod() {
20
+ return this.zodSchema;
21
+ }
22
+ /** GraphQL scalar instance usable by Pothos or vanilla GraphQL. */
23
+ getPothos() {
24
+ return this;
25
+ }
26
+ /** Return the JSON Schema (evaluates factory if provided). */
27
+ getJson() {
28
+ return typeof this.jsonSchemaDef === "function" ? this.jsonSchemaDef() : this.jsonSchemaDef;
29
+ }
30
+ getJsonSchemaDef() {
31
+ return this.jsonSchemaDef;
32
+ }
33
+ getJsonSchema() {
34
+ const deepResolve = (v) => {
35
+ const value = typeof v === "function" ? v() : v;
36
+ if (Array.isArray(value)) return value.map((item) => deepResolve(item));
37
+ if (value && typeof value === "object") {
38
+ const obj = {};
39
+ for (const [k, val] of Object.entries(value)) obj[k] = deepResolve(val);
40
+ return obj;
41
+ }
42
+ return value;
43
+ };
44
+ return deepResolve(this.getJson());
45
+ }
46
+ };
47
+
48
+ //#endregion
49
+ export { FieldType };
50
+ //# sourceMappingURL=FieldType.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FieldType.js","names":["obj: Record<string, unknown>"],"sources":["../src/FieldType.ts"],"sourcesContent":["import * as z from 'zod';\nimport { GraphQLScalarType, type GraphQLScalarTypeConfig } from 'graphql';\n\nexport interface FieldTypeConfig<\n TInternal,\n TExternal = TInternal,\n> extends GraphQLScalarTypeConfig<TInternal, TExternal> {\n zod: z.ZodType<TInternal>;\n jsonSchema: unknown | (() => unknown);\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AnyFieldType = FieldType<any, any>;\n\n/**\n * GraphQL scalar wrapper that carries zod and JSON Schema metadata.\n *\n * TInternal is the runtime representation; TExternal is the GraphQL output.\n */\nexport class FieldType<\n TInternal,\n TExternal = TInternal,\n> extends GraphQLScalarType<TInternal, TExternal> {\n private zodSchema: z.ZodType<TInternal>;\n private readonly jsonSchemaDef?: unknown | (() => unknown);\n\n constructor(config: FieldTypeConfig<TInternal, TExternal>) {\n super(config);\n this.zodSchema = config.zod;\n this.jsonSchemaDef = config.jsonSchema;\n }\n\n /** Return the attached zod schema for validation. */\n getZod(): z.ZodType<TInternal> {\n return this.zodSchema;\n }\n\n /** GraphQL scalar instance usable by Pothos or vanilla GraphQL. */\n getPothos(): GraphQLScalarType<TInternal, TExternal> {\n return this;\n }\n\n /** Return the JSON Schema (evaluates factory if provided). */\n getJson(): unknown {\n return typeof this.jsonSchemaDef === 'function'\n ? (this.jsonSchemaDef as () => unknown)()\n : this.jsonSchemaDef;\n }\n\n // Return the raw jsonSchema config (value or factory)\n getJsonSchemaDef(): unknown | (() => unknown) {\n return this.jsonSchemaDef;\n }\n\n // Return a deep-resolved JSON Schema (evaluates factory and nested factories)\n getJsonSchema(): unknown {\n const deepResolve = (v: unknown): unknown => {\n const value = typeof v === 'function' ? (v as () => unknown)() : v;\n if (Array.isArray(value)) return value.map((item) => deepResolve(item));\n if (value && typeof value === 'object') {\n const obj: Record<string, unknown> = {};\n for (const [k, val] of Object.entries(\n value as Record<string, unknown>\n )) {\n obj[k] = deepResolve(val);\n }\n return obj;\n }\n return value;\n };\n return deepResolve(this.getJson());\n }\n}\n\nexport type ZodFieldType<Field extends AnyFieldType> = z.infer<\n ReturnType<Field['getZod']>\n>;\n"],"mappings":";;;;;;;;;AAmBA,IAAa,YAAb,cAGU,kBAAwC;CAChD,AAAQ;CACR,AAAiB;CAEjB,YAAY,QAA+C;AACzD,QAAM,OAAO;AACb,OAAK,YAAY,OAAO;AACxB,OAAK,gBAAgB,OAAO;;;CAI9B,SAA+B;AAC7B,SAAO,KAAK;;;CAId,YAAqD;AACnD,SAAO;;;CAIT,UAAmB;AACjB,SAAO,OAAO,KAAK,kBAAkB,aAChC,KAAK,eAAiC,GACvC,KAAK;;CAIX,mBAA8C;AAC5C,SAAO,KAAK;;CAId,gBAAyB;EACvB,MAAM,eAAe,MAAwB;GAC3C,MAAM,QAAQ,OAAO,MAAM,aAAc,GAAqB,GAAG;AACjE,OAAI,MAAM,QAAQ,MAAM,CAAE,QAAO,MAAM,KAAK,SAAS,YAAY,KAAK,CAAC;AACvE,OAAI,SAAS,OAAO,UAAU,UAAU;IACtC,MAAMA,MAA+B,EAAE;AACvC,SAAK,MAAM,CAAC,GAAG,QAAQ,OAAO,QAC5B,MACD,CACC,KAAI,KAAK,YAAY,IAAI;AAE3B,WAAO;;AAET,UAAO;;AAET,SAAO,YAAY,KAAK,SAAS,CAAC"}
@@ -0,0 +1,19 @@
1
+ import { SchemaModelType } from "./SchemaModelType.js";
2
+ import { z } from "zod";
3
+
4
+ //#region src/GraphQLSchemaType.d.ts
5
+
6
+ /**
7
+ * Wrapper for GraphQL Type Definitions to be used in ContractSpec Schemas.
8
+ * Note: Runtime validation is limited to 'unknown' (pass-through) as SDL is not parsed at runtime.
9
+ */
10
+ declare class GraphQLSchemaType implements SchemaModelType {
11
+ readonly typeDef: string;
12
+ readonly name: string;
13
+ readonly _isSchemaType = true;
14
+ constructor(typeDef: string, name: string);
15
+ getZod(): z.ZodType;
16
+ }
17
+ //#endregion
18
+ export { GraphQLSchemaType };
19
+ //# sourceMappingURL=GraphQLSchemaType.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GraphQLSchemaType.d.ts","names":[],"sources":["../src/GraphQLSchemaType.ts"],"sourcesContent":[],"mappings":";;;;;;;AAOA;;cAAa,iBAAA,YAA6B;;;;;YAQ9B,CAAA,CAAE"}
@@ -0,0 +1,21 @@
1
+ import { z } from "zod";
2
+
3
+ //#region src/GraphQLSchemaType.ts
4
+ /**
5
+ * Wrapper for GraphQL Type Definitions to be used in ContractSpec Schemas.
6
+ * Note: Runtime validation is limited to 'unknown' (pass-through) as SDL is not parsed at runtime.
7
+ */
8
+ var GraphQLSchemaType = class {
9
+ _isSchemaType = true;
10
+ constructor(typeDef, name) {
11
+ this.typeDef = typeDef;
12
+ this.name = name;
13
+ }
14
+ getZod() {
15
+ return z.unknown();
16
+ }
17
+ };
18
+
19
+ //#endregion
20
+ export { GraphQLSchemaType };
21
+ //# sourceMappingURL=GraphQLSchemaType.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GraphQLSchemaType.js","names":["typeDef: string","name: string"],"sources":["../src/GraphQLSchemaType.ts"],"sourcesContent":["import { z } from 'zod';\nimport type { SchemaModelType } from './SchemaModelType';\n\n/**\n * Wrapper for GraphQL Type Definitions to be used in ContractSpec Schemas.\n * Note: Runtime validation is limited to 'unknown' (pass-through) as SDL is not parsed at runtime.\n */\nexport class GraphQLSchemaType implements SchemaModelType {\n readonly _isSchemaType = true;\n\n constructor(\n public readonly typeDef: string,\n public readonly name: string\n ) {}\n\n getZod(): z.ZodType {\n return z.unknown();\n }\n}\n"],"mappings":";;;;;;;AAOA,IAAa,oBAAb,MAA0D;CACxD,AAAS,gBAAgB;CAEzB,YACE,AAAgBA,SAChB,AAAgBC,MAChB;EAFgB;EACA;;CAGlB,SAAoB;AAClB,SAAO,EAAE,SAAS"}
@@ -0,0 +1,112 @@
1
+ import { SchemaModelType } from "./SchemaModelType.js";
2
+ import * as z$1 from "zod";
3
+
4
+ //#region src/JsonSchemaType.d.ts
5
+
6
+ /**
7
+ * JSON Schema definition structure.
8
+ * Supports standard JSON Schema draft-07/2020-12 properties.
9
+ */
10
+ interface JsonSchemaDefinition {
11
+ type?: string | string[];
12
+ properties?: Record<string, JsonSchemaDefinition>;
13
+ required?: string[];
14
+ additionalProperties?: boolean | JsonSchemaDefinition;
15
+ items?: JsonSchemaDefinition | JsonSchemaDefinition[];
16
+ enum?: unknown[];
17
+ const?: unknown;
18
+ oneOf?: JsonSchemaDefinition[];
19
+ anyOf?: JsonSchemaDefinition[];
20
+ allOf?: JsonSchemaDefinition[];
21
+ $ref?: string;
22
+ title?: string;
23
+ description?: string;
24
+ default?: unknown;
25
+ format?: string;
26
+ minimum?: number;
27
+ maximum?: number;
28
+ minLength?: number;
29
+ maxLength?: number;
30
+ pattern?: string;
31
+ [key: string]: unknown;
32
+ }
33
+ /**
34
+ * Options for JsonSchemaType wrapper.
35
+ */
36
+ interface JsonSchemaTypeOptions {
37
+ /** Name for identification */
38
+ name?: string;
39
+ }
40
+ /**
41
+ * Wrapper to use JSON Schema as a SchemaType.
42
+ * Converts to Zod at runtime for validation.
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * const CustomFieldsSchema = fromJsonSchema({
47
+ * type: 'object',
48
+ * additionalProperties: {
49
+ * oneOf: [
50
+ * { type: 'string' },
51
+ * { type: 'number' },
52
+ * { type: 'boolean' },
53
+ * ],
54
+ * },
55
+ * }, { name: 'CustomFields' });
56
+ * ```
57
+ */
58
+ declare class JsonSchemaType implements SchemaModelType<Record<string, unknown>> {
59
+ private readonly jsonSchema;
60
+ private readonly options;
61
+ constructor(jsonSchema: JsonSchemaDefinition, options?: JsonSchemaTypeOptions);
62
+ /**
63
+ * Convert JSON Schema to Zod schema for runtime validation.
64
+ *
65
+ * Note: This is a simplified conversion. For complex schemas,
66
+ * consider using a dedicated json-schema-to-zod library.
67
+ */
68
+ getZod(): z$1.ZodType<Record<string, unknown>>;
69
+ /**
70
+ * Return the original JSON Schema.
71
+ */
72
+ getJsonSchema(): JsonSchemaDefinition;
73
+ /**
74
+ * Return GraphQL type info.
75
+ * JSON Schema types map to JSON scalar in GraphQL.
76
+ */
77
+ getPothos(): {
78
+ type: string;
79
+ name?: string;
80
+ };
81
+ /**
82
+ * Get the configured name for this schema.
83
+ */
84
+ getName(): string | undefined;
85
+ /**
86
+ * Convert a single JSON Schema property to Zod.
87
+ */
88
+ private convertPropertyToZod;
89
+ }
90
+ /**
91
+ * Helper to create a SchemaType from JSON Schema.
92
+ *
93
+ * @param schema - The JSON Schema definition
94
+ * @param options - Optional configuration
95
+ * @returns A SchemaType-compatible wrapper
96
+ *
97
+ * @example
98
+ * ```typescript
99
+ * const MetadataSchema = fromJsonSchema({
100
+ * type: 'object',
101
+ * properties: {
102
+ * createdAt: { type: 'string', format: 'date-time' },
103
+ * updatedAt: { type: 'string', format: 'date-time' },
104
+ * },
105
+ * required: ['createdAt'],
106
+ * });
107
+ * ```
108
+ */
109
+ declare const fromJsonSchema: (schema: JsonSchemaDefinition, options?: JsonSchemaTypeOptions) => JsonSchemaType;
110
+ //#endregion
111
+ export { JsonSchemaDefinition, JsonSchemaType, JsonSchemaTypeOptions, fromJsonSchema };
112
+ //# sourceMappingURL=JsonSchemaType.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"JsonSchemaType.d.ts","names":[],"sources":["../src/JsonSchemaType.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AA2CiB,UA3BA,oBAAA,CA2BqB;EAuBzB,IAAA,CAAA,EAAA,MAAA,GAAA,MAAe,EAAA;EAC1B,UAAA,CAAA,EAjDa,MAiDb,CAAA,MAAA,EAjD4B,oBAiD5B,CAAA;EAMc,QAAA,CAAA,EAAA,MAAA,EAAA;EACH,oBAAA,CAAA,EAAA,OAAA,GAtDsB,oBAsDtB;EAYS,KAAA,CAAA,EAjEZ,oBAiEY,GAjEW,oBAiEX,EAAA;EAAV,IAAE,CAAA,EAAA,OAAA,EAAA;EAqCK,KAAA,CAAA,EAAA,OAAA;EAzDoB,KAAA,CAAA,EA1C7B,oBA0C6B,EAAA;EAAe,KAAA,CAAA,EAzC5C,oBAyC4C,EAAA;EA+HzC,KAAA,CAAA,EAvKH,oBA0K8C,EAAA;EAF9C,IAAA,CAAA,EAAA,MAAA;EACE,KAAA,CAAA,EAAA,MAAA;EACT,WAAA,CAAA,EAAA,MAAA;EAAqD,OAAA,CAAA,EAAA,OAAA;;;;;;;;;;;;UAzJvC,qBAAA;;;;;;;;;;;;;;;;;;;;;;cAuBJ,cAAA,YAA0B,gBACrC;;;0BAMc,gCACH;;;;;;;YAYD,GAAA,CAAE,QAAQ;;;;mBAqCH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAsEN,yBACH,gCACE,0BACT"}
@@ -0,0 +1,125 @@
1
+ import * as z$1 from "zod";
2
+
3
+ //#region src/JsonSchemaType.ts
4
+ /**
5
+ * JSON Schema wrapper for ContractSpec compatibility.
6
+ *
7
+ * Wraps JSON Schema definitions to implement the SchemaType interface,
8
+ * enabling direct JSON Schema usage in ContractSpec definitions.
9
+ *
10
+ * @module JsonSchemaType
11
+ */
12
+ /**
13
+ * Wrapper to use JSON Schema as a SchemaType.
14
+ * Converts to Zod at runtime for validation.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const CustomFieldsSchema = fromJsonSchema({
19
+ * type: 'object',
20
+ * additionalProperties: {
21
+ * oneOf: [
22
+ * { type: 'string' },
23
+ * { type: 'number' },
24
+ * { type: 'boolean' },
25
+ * ],
26
+ * },
27
+ * }, { name: 'CustomFields' });
28
+ * ```
29
+ */
30
+ var JsonSchemaType = class JsonSchemaType {
31
+ jsonSchema;
32
+ options;
33
+ constructor(jsonSchema, options = {}) {
34
+ this.jsonSchema = jsonSchema;
35
+ this.options = options;
36
+ }
37
+ /**
38
+ * Convert JSON Schema to Zod schema for runtime validation.
39
+ *
40
+ * Note: This is a simplified conversion. For complex schemas,
41
+ * consider using a dedicated json-schema-to-zod library.
42
+ */
43
+ getZod() {
44
+ if (this.jsonSchema.additionalProperties !== void 0) {
45
+ if (this.jsonSchema.additionalProperties === true) return z$1.record(z$1.string(), z$1.unknown());
46
+ if (typeof this.jsonSchema.additionalProperties === "object") return z$1.record(z$1.string(), z$1.unknown());
47
+ if (this.jsonSchema.additionalProperties === false) return z$1.object({}).strict();
48
+ }
49
+ if (this.jsonSchema.properties) {
50
+ const shape = {};
51
+ const required = new Set(this.jsonSchema.required ?? []);
52
+ for (const [key, propSchema] of Object.entries(this.jsonSchema.properties)) {
53
+ const fieldType = this.convertPropertyToZod(propSchema);
54
+ shape[key] = required.has(key) ? fieldType : fieldType.optional();
55
+ }
56
+ return z$1.object(shape).passthrough();
57
+ }
58
+ return z$1.object({}).passthrough();
59
+ }
60
+ /**
61
+ * Return the original JSON Schema.
62
+ */
63
+ getJsonSchema() {
64
+ return this.jsonSchema;
65
+ }
66
+ /**
67
+ * Return GraphQL type info.
68
+ * JSON Schema types map to JSON scalar in GraphQL.
69
+ */
70
+ getPothos() {
71
+ return {
72
+ type: "JSON",
73
+ name: this.options.name
74
+ };
75
+ }
76
+ /**
77
+ * Get the configured name for this schema.
78
+ */
79
+ getName() {
80
+ return this.options.name;
81
+ }
82
+ /**
83
+ * Convert a single JSON Schema property to Zod.
84
+ */
85
+ convertPropertyToZod(schema) {
86
+ switch (Array.isArray(schema.type) ? schema.type[0] : schema.type) {
87
+ case "string": return z$1.string();
88
+ case "number":
89
+ case "integer": return z$1.number();
90
+ case "boolean": return z$1.boolean();
91
+ case "array":
92
+ if (schema.items && !Array.isArray(schema.items)) return z$1.array(this.convertPropertyToZod(schema.items));
93
+ return z$1.array(z$1.unknown());
94
+ case "object":
95
+ if (schema.properties) return new JsonSchemaType(schema).getZod();
96
+ return z$1.record(z$1.string(), z$1.unknown());
97
+ case "null": return z$1.null();
98
+ default: return z$1.unknown();
99
+ }
100
+ }
101
+ };
102
+ /**
103
+ * Helper to create a SchemaType from JSON Schema.
104
+ *
105
+ * @param schema - The JSON Schema definition
106
+ * @param options - Optional configuration
107
+ * @returns A SchemaType-compatible wrapper
108
+ *
109
+ * @example
110
+ * ```typescript
111
+ * const MetadataSchema = fromJsonSchema({
112
+ * type: 'object',
113
+ * properties: {
114
+ * createdAt: { type: 'string', format: 'date-time' },
115
+ * updatedAt: { type: 'string', format: 'date-time' },
116
+ * },
117
+ * required: ['createdAt'],
118
+ * });
119
+ * ```
120
+ */
121
+ const fromJsonSchema = (schema, options) => new JsonSchemaType(schema, options);
122
+
123
+ //#endregion
124
+ export { JsonSchemaType, fromJsonSchema };
125
+ //# sourceMappingURL=JsonSchemaType.js.map