@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.
- package/dist/EnumType.d.ts +29 -31
- package/dist/EnumType.d.ts.map +1 -1
- package/dist/EnumType.js +38 -54
- package/dist/EnumType.test.d.ts +2 -0
- package/dist/EnumType.test.d.ts.map +1 -0
- package/dist/FieldType.d.ts +19 -23
- package/dist/FieldType.d.ts.map +1 -1
- package/dist/FieldType.js +41 -47
- package/dist/FieldType.test.d.ts +2 -0
- package/dist/FieldType.test.d.ts.map +1 -0
- package/dist/GraphQLSchemaType.d.ts +8 -12
- package/dist/GraphQLSchemaType.d.ts.map +1 -1
- package/dist/GraphQLSchemaType.js +16 -18
- package/dist/GraphQLSchemaType.test.d.ts +2 -0
- package/dist/GraphQLSchemaType.test.d.ts.map +1 -0
- package/dist/JsonSchemaType.d.ts +67 -63
- package/dist/JsonSchemaType.d.ts.map +1 -1
- package/dist/JsonSchemaType.js +73 -123
- package/dist/JsonSchemaType.test.d.ts +2 -0
- package/dist/JsonSchemaType.test.d.ts.map +1 -0
- package/dist/ScalarTypeEnum.d.ts +23 -27
- package/dist/ScalarTypeEnum.d.ts.map +1 -1
- package/dist/ScalarTypeEnum.js +235 -235
- package/dist/ScalarTypeEnum.test.d.ts +2 -0
- package/dist/ScalarTypeEnum.test.d.ts.map +1 -0
- package/dist/SchemaModel.d.ts +38 -39
- package/dist/SchemaModel.d.ts.map +1 -1
- package/dist/SchemaModel.js +28 -55
- package/dist/SchemaModel.test.d.ts +2 -0
- package/dist/SchemaModel.test.d.ts.map +1 -0
- package/dist/SchemaModelType.d.ts +28 -24
- package/dist/SchemaModelType.d.ts.map +1 -1
- package/dist/SchemaModelType.js +6 -19
- package/dist/SchemaModelType.test.d.ts +2 -0
- package/dist/SchemaModelType.test.d.ts.map +1 -0
- package/dist/ZodSchemaType.d.ts +44 -40
- package/dist/ZodSchemaType.d.ts.map +1 -1
- package/dist/ZodSchemaType.js +30 -101
- package/dist/ZodSchemaType.test.d.ts +2 -0
- package/dist/ZodSchemaType.test.d.ts.map +1 -0
- package/dist/browser/EnumType.js +40 -0
- package/dist/browser/FieldType.js +43 -0
- package/dist/browser/GraphQLSchemaType.js +18 -0
- package/dist/browser/JsonSchemaType.js +74 -0
- package/dist/browser/ScalarTypeEnum.js +236 -0
- package/dist/browser/SchemaModel.js +30 -0
- package/dist/browser/SchemaModelType.js +7 -0
- package/dist/browser/ZodSchemaType.js +31 -0
- package/dist/browser/entity/defineEntity.js +128 -0
- package/dist/browser/entity/generator.js +283 -0
- package/dist/browser/entity/index.js +410 -0
- package/dist/browser/entity/types.js +0 -0
- package/dist/browser/index.js +840 -0
- package/dist/entity/defineEntity.d.ts +55 -34
- package/dist/entity/defineEntity.d.ts.map +1 -1
- package/dist/entity/defineEntity.js +123 -231
- package/dist/entity/defineEntity.test.d.ts +2 -0
- package/dist/entity/defineEntity.test.d.ts.map +1 -0
- package/dist/entity/generator.d.ts +16 -20
- package/dist/entity/generator.d.ts.map +1 -1
- package/dist/entity/generator.js +262 -196
- package/dist/entity/generator.test.d.ts +2 -0
- package/dist/entity/generator.test.d.ts.map +1 -0
- package/dist/entity/index.d.ts +4 -4
- package/dist/entity/index.d.ts.map +1 -0
- package/dist/entity/index.js +410 -4
- package/dist/entity/types.d.ts +99 -103
- package/dist/entity/types.d.ts.map +1 -1
- package/dist/entity/types.js +1 -1
- package/dist/index.d.ts +10 -13
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +841 -13
- package/dist/node/EnumType.js +40 -0
- package/dist/node/FieldType.js +43 -0
- package/dist/node/GraphQLSchemaType.js +18 -0
- package/dist/node/JsonSchemaType.js +74 -0
- package/dist/node/ScalarTypeEnum.js +236 -0
- package/dist/node/SchemaModel.js +30 -0
- package/dist/node/SchemaModelType.js +7 -0
- package/dist/node/ZodSchemaType.js +31 -0
- package/dist/node/entity/defineEntity.js +128 -0
- package/dist/node/entity/generator.js +283 -0
- package/dist/node/entity/index.js +410 -0
- package/dist/node/entity/types.js +0 -0
- package/dist/node/index.js +840 -0
- package/package.json +123 -25
- package/dist/EnumType.js.map +0 -1
- package/dist/FieldType.js.map +0 -1
- package/dist/GraphQLSchemaType.js.map +0 -1
- package/dist/JsonSchemaType.js.map +0 -1
- package/dist/ScalarTypeEnum.js.map +0 -1
- package/dist/SchemaModel.js.map +0 -1
- package/dist/SchemaModelType.js.map +0 -1
- package/dist/ZodSchemaType.js.map +0 -1
- package/dist/entity/defineEntity.js.map +0 -1
- package/dist/entity/generator.js.map +0 -1
package/dist/SchemaModel.js
CHANGED
|
@@ -1,58 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import * as z
|
|
1
|
+
// @bun
|
|
2
|
+
// src/SchemaModel.ts
|
|
3
|
+
import * as z from "zod";
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
24
|
+
return model !== null && model !== undefined && "config" in model && typeof model.config === "object" && "name" in model.config;
|
|
49
25
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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 @@
|
|
|
1
|
+
{"version":3,"file":"SchemaModel.test.d.ts","sourceRoot":"","sources":["../src/SchemaModel.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,10 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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","
|
|
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"}
|
package/dist/SchemaModelType.js
CHANGED
|
@@ -1,21 +1,8 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
4
|
+
return value !== null && typeof value === "object" && "getZod" in value && typeof value.getZod === "function";
|
|
17
5
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
//# sourceMappingURL=SchemaModelType.js.map
|
|
6
|
+
export {
|
|
7
|
+
isSchemaType
|
|
8
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SchemaModelType.test.d.ts","sourceRoot":"","sources":["../src/SchemaModelType.test.ts"],"names":[],"mappings":""}
|
package/dist/ZodSchemaType.d.ts
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
|
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","
|
|
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"}
|
package/dist/ZodSchemaType.js
CHANGED
|
@@ -1,103 +1,32 @@
|
|
|
1
|
-
|
|
1
|
+
// @bun
|
|
2
|
+
// src/ZodSchemaType.ts
|
|
3
|
+
import * as z from "zod";
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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 @@
|
|
|
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
|
+
};
|