@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.
- package/LICENSE +21 -0
- package/README.md +154 -0
- package/dist/EnumType.d.ts +42 -0
- package/dist/EnumType.d.ts.map +1 -0
- package/dist/EnumType.js +57 -0
- package/dist/EnumType.js.map +1 -0
- package/dist/FieldType.d.ts +31 -0
- package/dist/FieldType.d.ts.map +1 -0
- package/dist/FieldType.js +50 -0
- package/dist/FieldType.js.map +1 -0
- package/dist/GraphQLSchemaType.d.ts +19 -0
- package/dist/GraphQLSchemaType.d.ts.map +1 -0
- package/dist/GraphQLSchemaType.js +21 -0
- package/dist/GraphQLSchemaType.js.map +1 -0
- package/dist/JsonSchemaType.d.ts +112 -0
- package/dist/JsonSchemaType.d.ts.map +1 -0
- package/dist/JsonSchemaType.js +125 -0
- package/dist/JsonSchemaType.js.map +1 -0
- package/dist/ScalarTypeEnum.d.ts +33 -0
- package/dist/ScalarTypeEnum.d.ts.map +1 -0
- package/dist/ScalarTypeEnum.js +237 -0
- package/dist/ScalarTypeEnum.js.map +1 -0
- package/dist/SchemaModel.d.ts +92 -0
- package/dist/SchemaModel.d.ts.map +1 -0
- package/dist/SchemaModel.js +58 -0
- package/dist/SchemaModel.js.map +1 -0
- package/dist/SchemaModelType.d.ts +60 -0
- package/dist/SchemaModelType.d.ts.map +1 -0
- package/dist/SchemaModelType.js +21 -0
- package/dist/SchemaModelType.js.map +1 -0
- package/dist/ZodSchemaType.d.ts +92 -0
- package/dist/ZodSchemaType.d.ts.map +1 -0
- package/dist/ZodSchemaType.js +103 -0
- package/dist/ZodSchemaType.js.map +1 -0
- package/dist/entity/defineEntity.d.ts +93 -0
- package/dist/entity/defineEntity.d.ts.map +1 -0
- package/dist/entity/defineEntity.js +237 -0
- package/dist/entity/defineEntity.js.map +1 -0
- package/dist/entity/generator.d.ts +38 -0
- package/dist/entity/generator.d.ts.map +1 -0
- package/dist/entity/generator.js +218 -0
- package/dist/entity/generator.js.map +1 -0
- package/dist/entity/index.d.ts +4 -0
- package/dist/entity/index.js +5 -0
- package/dist/entity/types.d.ts +146 -0
- package/dist/entity/types.d.ts.map +1 -0
- package/dist/entity/types.js +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +13 -0
- package/package.json +64 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JsonSchemaType.js","names":["z","shape: Record<string, z.ZodType>"],"sources":["../src/JsonSchemaType.ts"],"sourcesContent":["/**\n * JSON Schema wrapper for ContractSpec compatibility.\n *\n * Wraps JSON Schema definitions to implement the SchemaType interface,\n * enabling direct JSON Schema usage in ContractSpec definitions.\n *\n * @module JsonSchemaType\n */\n\nimport * as z from 'zod';\nimport type { SchemaModelType } from './SchemaModelType';\n\n/**\n * JSON Schema definition structure.\n * Supports standard JSON Schema draft-07/2020-12 properties.\n */\nexport interface JsonSchemaDefinition {\n type?: string | string[];\n properties?: Record<string, JsonSchemaDefinition>;\n required?: string[];\n additionalProperties?: boolean | JsonSchemaDefinition;\n items?: JsonSchemaDefinition | JsonSchemaDefinition[];\n enum?: unknown[];\n const?: unknown;\n oneOf?: JsonSchemaDefinition[];\n anyOf?: JsonSchemaDefinition[];\n allOf?: JsonSchemaDefinition[];\n $ref?: string;\n title?: string;\n description?: string;\n default?: unknown;\n format?: string;\n minimum?: number;\n maximum?: number;\n minLength?: number;\n maxLength?: number;\n pattern?: string;\n [key: string]: unknown;\n}\n\n/**\n * Options for JsonSchemaType wrapper.\n */\nexport interface JsonSchemaTypeOptions {\n /** Name for identification */\n name?: string;\n}\n\n/**\n * Wrapper to use JSON Schema as a SchemaType.\n * Converts to Zod at runtime for validation.\n *\n * @example\n * ```typescript\n * const CustomFieldsSchema = fromJsonSchema({\n * type: 'object',\n * additionalProperties: {\n * oneOf: [\n * { type: 'string' },\n * { type: 'number' },\n * { type: 'boolean' },\n * ],\n * },\n * }, { name: 'CustomFields' });\n * ```\n */\nexport class JsonSchemaType implements SchemaModelType<\n Record<string, unknown>\n> {\n private readonly jsonSchema: JsonSchemaDefinition;\n private readonly options: JsonSchemaTypeOptions;\n\n constructor(\n jsonSchema: JsonSchemaDefinition,\n options: JsonSchemaTypeOptions = {}\n ) {\n this.jsonSchema = jsonSchema;\n this.options = options;\n }\n\n /**\n * Convert JSON Schema to Zod schema for runtime validation.\n *\n * Note: This is a simplified conversion. For complex schemas,\n * consider using a dedicated json-schema-to-zod library.\n */\n getZod(): z.ZodType<Record<string, unknown>> {\n // Handle additionalProperties (dictionary/record types)\n if (this.jsonSchema.additionalProperties !== undefined) {\n if (this.jsonSchema.additionalProperties === true) {\n return z.record(z.string(), z.unknown());\n }\n if (typeof this.jsonSchema.additionalProperties === 'object') {\n // For typed additionalProperties, use union or unknown\n return z.record(z.string(), z.unknown());\n }\n if (this.jsonSchema.additionalProperties === false) {\n return z.object({}).strict();\n }\n }\n\n // Handle explicit properties\n if (this.jsonSchema.properties) {\n const shape: Record<string, z.ZodType> = {};\n const required = new Set(this.jsonSchema.required ?? []);\n\n for (const [key, propSchema] of Object.entries(\n this.jsonSchema.properties\n )) {\n const fieldType = this.convertPropertyToZod(propSchema);\n shape[key] = required.has(key) ? fieldType : fieldType.optional();\n }\n\n return z.object(shape).passthrough();\n }\n\n // Default: passthrough object\n return z.object({}).passthrough();\n }\n\n /**\n * Return the original JSON Schema.\n */\n getJsonSchema(): JsonSchemaDefinition {\n return this.jsonSchema;\n }\n\n /**\n * Return GraphQL type info.\n * JSON Schema types map to JSON scalar in GraphQL.\n */\n getPothos(): { type: string; name?: string } {\n return { type: 'JSON', name: this.options.name };\n }\n\n /**\n * Get the configured name for this schema.\n */\n getName(): string | undefined {\n return this.options.name;\n }\n\n /**\n * Convert a single JSON Schema property to Zod.\n */\n private convertPropertyToZod(schema: JsonSchemaDefinition): z.ZodType {\n const type = Array.isArray(schema.type) ? schema.type[0] : schema.type;\n\n switch (type) {\n case 'string':\n return z.string();\n case 'number':\n case 'integer':\n return z.number();\n case 'boolean':\n return z.boolean();\n case 'array':\n if (schema.items && !Array.isArray(schema.items)) {\n return z.array(this.convertPropertyToZod(schema.items));\n }\n return z.array(z.unknown());\n case 'object':\n if (schema.properties) {\n return new JsonSchemaType(schema).getZod();\n }\n return z.record(z.string(), z.unknown());\n case 'null':\n return z.null();\n default:\n return z.unknown();\n }\n }\n}\n\n/**\n * Helper to create a SchemaType from JSON Schema.\n *\n * @param schema - The JSON Schema definition\n * @param options - Optional configuration\n * @returns A SchemaType-compatible wrapper\n *\n * @example\n * ```typescript\n * const MetadataSchema = fromJsonSchema({\n * type: 'object',\n * properties: {\n * createdAt: { type: 'string', format: 'date-time' },\n * updatedAt: { type: 'string', format: 'date-time' },\n * },\n * required: ['createdAt'],\n * });\n * ```\n */\nexport const fromJsonSchema = (\n schema: JsonSchemaDefinition,\n options?: JsonSchemaTypeOptions\n): JsonSchemaType => new JsonSchemaType(schema, options);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkEA,IAAa,iBAAb,MAAa,eAEX;CACA,AAAiB;CACjB,AAAiB;CAEjB,YACE,YACA,UAAiC,EAAE,EACnC;AACA,OAAK,aAAa;AAClB,OAAK,UAAU;;;;;;;;CASjB,SAA6C;AAE3C,MAAI,KAAK,WAAW,yBAAyB,QAAW;AACtD,OAAI,KAAK,WAAW,yBAAyB,KAC3C,QAAOA,IAAE,OAAOA,IAAE,QAAQ,EAAEA,IAAE,SAAS,CAAC;AAE1C,OAAI,OAAO,KAAK,WAAW,yBAAyB,SAElD,QAAOA,IAAE,OAAOA,IAAE,QAAQ,EAAEA,IAAE,SAAS,CAAC;AAE1C,OAAI,KAAK,WAAW,yBAAyB,MAC3C,QAAOA,IAAE,OAAO,EAAE,CAAC,CAAC,QAAQ;;AAKhC,MAAI,KAAK,WAAW,YAAY;GAC9B,MAAMC,QAAmC,EAAE;GAC3C,MAAM,WAAW,IAAI,IAAI,KAAK,WAAW,YAAY,EAAE,CAAC;AAExD,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
+
|
|
5
|
+
/**
|
|
6
|
+
* Factory functions for common scalar FieldTypes with zod/GraphQL/JSON Schema.
|
|
7
|
+
*/
|
|
8
|
+
declare const ScalarTypeEnum: {
|
|
9
|
+
readonly String_unsecure: () => FieldType<string>;
|
|
10
|
+
readonly Int_unsecure: () => FieldType<number>;
|
|
11
|
+
readonly Float_unsecure: () => FieldType<number>;
|
|
12
|
+
readonly Boolean: () => FieldType<boolean>;
|
|
13
|
+
readonly ID: () => FieldType<string>;
|
|
14
|
+
readonly JSON: () => FieldType<unknown>;
|
|
15
|
+
readonly JSONObject: () => FieldType<Record<string, unknown>>;
|
|
16
|
+
readonly Date: () => FieldType<Date, string>;
|
|
17
|
+
readonly DateTime: () => FieldType<Date, string>;
|
|
18
|
+
readonly Time: () => FieldType<string>;
|
|
19
|
+
readonly EmailAddress: () => FieldType<string>;
|
|
20
|
+
readonly URL: () => FieldType<string>;
|
|
21
|
+
readonly PhoneNumber: () => FieldType<string>;
|
|
22
|
+
readonly NonEmptyString: () => FieldType<string>;
|
|
23
|
+
readonly Locale: () => FieldType<string>;
|
|
24
|
+
readonly TimeZone: () => FieldType<string>;
|
|
25
|
+
readonly Latitude: () => FieldType<number>;
|
|
26
|
+
readonly Longitude: () => FieldType<number>;
|
|
27
|
+
readonly Currency: () => FieldType<string>;
|
|
28
|
+
readonly CountryCode: () => FieldType<string>;
|
|
29
|
+
};
|
|
30
|
+
type ScalarTypeEnum = [keyof typeof ScalarTypeEnum];
|
|
31
|
+
//#endregion
|
|
32
|
+
export { ScalarTypeEnum };
|
|
33
|
+
//# sourceMappingURL=ScalarTypeEnum.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ScalarTypeEnum.d.ts","names":[],"sources":["../src/ScalarTypeEnum.ts"],"sourcesContent":[],"mappings":";;;;;;AAkBA;AAEuB,cAFV,cAEU,EAAA;EAaH,SAAA,eAAA,EAAA,GAAA,GAbG,SAaH,CAAA,MAAA,CAAA;EAgBE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QD;AAQG,KAqBP,cAAA,GArBO,CAAA,MAAA,OAqBwB,cArBxB,CAAA"}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { FieldType } from "./FieldType.js";
|
|
2
|
+
import * as z$1 from "zod";
|
|
3
|
+
import { Kind } from "graphql";
|
|
4
|
+
|
|
5
|
+
//#region src/ScalarTypeEnum.ts
|
|
6
|
+
const localeRegex = /^[A-Za-z]{2}(?:-[A-Za-z0-9]{2,8})*$/;
|
|
7
|
+
const timezoneRegex = /^(?:UTC|[A-Za-z_]+\/[A-Za-z_]+)$/;
|
|
8
|
+
const phoneRegex = /^[+]?\d[\d\s().-]{3,}$/;
|
|
9
|
+
const currencyRegex = /^[A-Z]{3}$/;
|
|
10
|
+
const countryRegex = /^[A-Z]{2}$/;
|
|
11
|
+
const latMin = -90;
|
|
12
|
+
const latMax = 90;
|
|
13
|
+
const lonMin = -180;
|
|
14
|
+
const lonMax = 180;
|
|
15
|
+
/**
|
|
16
|
+
* Factory functions for common scalar FieldTypes with zod/GraphQL/JSON Schema.
|
|
17
|
+
*/
|
|
18
|
+
const ScalarTypeEnum = {
|
|
19
|
+
String_unsecure: () => new FieldType({
|
|
20
|
+
name: "String_unsecure",
|
|
21
|
+
description: "Unvalidated string scalar",
|
|
22
|
+
zod: z$1.string(),
|
|
23
|
+
parseValue: (v) => z$1.string().parse(v),
|
|
24
|
+
serialize: (v) => String(v),
|
|
25
|
+
parseLiteral: (ast) => {
|
|
26
|
+
if (ast.kind !== Kind.STRING) throw new TypeError("Invalid literal");
|
|
27
|
+
return ast.value;
|
|
28
|
+
},
|
|
29
|
+
jsonSchema: { type: "string" }
|
|
30
|
+
}),
|
|
31
|
+
Int_unsecure: () => new FieldType({
|
|
32
|
+
name: "Int_unsecure",
|
|
33
|
+
description: "Unvalidated integer scalar",
|
|
34
|
+
zod: z$1.number().int(),
|
|
35
|
+
parseValue: (v) => {
|
|
36
|
+
const num = typeof v === "number" ? v : Number(v);
|
|
37
|
+
return z$1.number().int().parse(num);
|
|
38
|
+
},
|
|
39
|
+
serialize: (v) => Math.trunc(typeof v === "number" ? v : Number(v)),
|
|
40
|
+
parseLiteral: (ast) => {
|
|
41
|
+
if (ast.kind !== Kind.INT) throw new TypeError("Invalid literal");
|
|
42
|
+
return Number(ast.value);
|
|
43
|
+
},
|
|
44
|
+
jsonSchema: { type: "integer" }
|
|
45
|
+
}),
|
|
46
|
+
Float_unsecure: () => new FieldType({
|
|
47
|
+
name: "Float_unsecure",
|
|
48
|
+
description: "Unvalidated float scalar",
|
|
49
|
+
zod: z$1.number(),
|
|
50
|
+
parseValue: (v) => {
|
|
51
|
+
const num = typeof v === "number" ? v : Number(v);
|
|
52
|
+
return z$1.number().parse(num);
|
|
53
|
+
},
|
|
54
|
+
serialize: (v) => Number(v),
|
|
55
|
+
parseLiteral: (ast) => {
|
|
56
|
+
if (ast.kind !== Kind.FLOAT && ast.kind !== Kind.INT) throw new TypeError("Invalid literal");
|
|
57
|
+
return Number(ast.value);
|
|
58
|
+
},
|
|
59
|
+
jsonSchema: { type: "number" }
|
|
60
|
+
}),
|
|
61
|
+
Boolean: () => new FieldType({
|
|
62
|
+
name: "Boolean",
|
|
63
|
+
description: "Unvalidated boolean scalar",
|
|
64
|
+
zod: z$1.boolean(),
|
|
65
|
+
parseValue: (v) => z$1.coerce.boolean().parse(v),
|
|
66
|
+
serialize: (v) => Boolean(v),
|
|
67
|
+
parseLiteral: (ast) => {
|
|
68
|
+
if (ast.kind !== Kind.BOOLEAN) throw new TypeError("Invalid literal");
|
|
69
|
+
return ast.value;
|
|
70
|
+
},
|
|
71
|
+
jsonSchema: { type: "boolean" }
|
|
72
|
+
}),
|
|
73
|
+
ID: () => new FieldType({
|
|
74
|
+
name: "ID",
|
|
75
|
+
description: "Unvalidated id scalar",
|
|
76
|
+
zod: z$1.string(),
|
|
77
|
+
parseValue: (v) => z$1.string().parse(v),
|
|
78
|
+
serialize: (v) => String(v),
|
|
79
|
+
parseLiteral: (ast) => {
|
|
80
|
+
if (ast.kind !== Kind.STRING) throw new TypeError("Invalid literal");
|
|
81
|
+
return ast.value;
|
|
82
|
+
},
|
|
83
|
+
jsonSchema: { type: "string" }
|
|
84
|
+
}),
|
|
85
|
+
JSON: () => new FieldType({
|
|
86
|
+
name: "JSON",
|
|
87
|
+
zod: z$1.any(),
|
|
88
|
+
parseValue: (v) => v,
|
|
89
|
+
serialize: (v) => v,
|
|
90
|
+
jsonSchema: {}
|
|
91
|
+
}),
|
|
92
|
+
JSONObject: () => new FieldType({
|
|
93
|
+
name: "JSONObject",
|
|
94
|
+
zod: z$1.record(z$1.string(), z$1.any()),
|
|
95
|
+
parseValue: (v) => z$1.record(z$1.string(), z$1.any()).parse(v),
|
|
96
|
+
serialize: (v) => v ?? {},
|
|
97
|
+
jsonSchema: { type: "object" }
|
|
98
|
+
}),
|
|
99
|
+
Date: () => new FieldType({
|
|
100
|
+
name: "Date",
|
|
101
|
+
zod: z$1.date(),
|
|
102
|
+
parseValue: (v) => v instanceof Date ? v : new Date(String(v)),
|
|
103
|
+
serialize: (v) => v instanceof Date ? v.toISOString().split("T")[0] : String(v),
|
|
104
|
+
jsonSchema: {
|
|
105
|
+
type: "string",
|
|
106
|
+
format: "date"
|
|
107
|
+
}
|
|
108
|
+
}),
|
|
109
|
+
DateTime: () => new FieldType({
|
|
110
|
+
name: "DateTime",
|
|
111
|
+
zod: z$1.date(),
|
|
112
|
+
parseValue: (v) => v instanceof Date ? v : new Date(String(v)),
|
|
113
|
+
serialize: (v) => {
|
|
114
|
+
return v instanceof Date ? v.toISOString() : String(v);
|
|
115
|
+
},
|
|
116
|
+
jsonSchema: {
|
|
117
|
+
type: "string",
|
|
118
|
+
format: "date-time"
|
|
119
|
+
}
|
|
120
|
+
}),
|
|
121
|
+
Time: () => new FieldType({
|
|
122
|
+
name: "Time",
|
|
123
|
+
zod: z$1.string().regex(/^\d{2}:\d{2}(:\d{2})?$/),
|
|
124
|
+
parseValue: (v) => z$1.string().regex(/^\d{2}:\d{2}(:\d{2})?$/).parse(v),
|
|
125
|
+
serialize: (v) => String(v),
|
|
126
|
+
jsonSchema: {
|
|
127
|
+
type: "string",
|
|
128
|
+
pattern: "^\\d{2}:\\d{2}(:\\d{2})?$"
|
|
129
|
+
}
|
|
130
|
+
}),
|
|
131
|
+
EmailAddress: () => new FieldType({
|
|
132
|
+
name: "EmailAddress",
|
|
133
|
+
zod: z$1.string().email(),
|
|
134
|
+
parseValue: (v) => z$1.string().email().parse(v),
|
|
135
|
+
serialize: (v) => String(v),
|
|
136
|
+
jsonSchema: {
|
|
137
|
+
type: "string",
|
|
138
|
+
format: "email"
|
|
139
|
+
}
|
|
140
|
+
}),
|
|
141
|
+
URL: () => new FieldType({
|
|
142
|
+
name: "URL",
|
|
143
|
+
zod: z$1.string().url(),
|
|
144
|
+
parseValue: (v) => z$1.string().url().parse(v),
|
|
145
|
+
serialize: (v) => String(v),
|
|
146
|
+
jsonSchema: {
|
|
147
|
+
type: "string",
|
|
148
|
+
format: "uri"
|
|
149
|
+
}
|
|
150
|
+
}),
|
|
151
|
+
PhoneNumber: () => new FieldType({
|
|
152
|
+
name: "PhoneNumber",
|
|
153
|
+
zod: z$1.string().regex(phoneRegex),
|
|
154
|
+
parseValue: (v) => z$1.string().regex(phoneRegex).parse(v),
|
|
155
|
+
serialize: (v) => String(v),
|
|
156
|
+
jsonSchema: {
|
|
157
|
+
type: "string",
|
|
158
|
+
pattern: phoneRegex.source
|
|
159
|
+
}
|
|
160
|
+
}),
|
|
161
|
+
NonEmptyString: () => new FieldType({
|
|
162
|
+
name: "NonEmptyString",
|
|
163
|
+
zod: z$1.string().min(1),
|
|
164
|
+
parseValue: (v) => z$1.string().min(1).parse(v),
|
|
165
|
+
serialize: (v) => String(v),
|
|
166
|
+
jsonSchema: {
|
|
167
|
+
type: "string",
|
|
168
|
+
minLength: 1
|
|
169
|
+
}
|
|
170
|
+
}),
|
|
171
|
+
Locale: () => new FieldType({
|
|
172
|
+
name: "Locale",
|
|
173
|
+
zod: z$1.string().regex(localeRegex),
|
|
174
|
+
parseValue: (v) => z$1.string().regex(localeRegex).parse(v),
|
|
175
|
+
serialize: (v) => String(v),
|
|
176
|
+
jsonSchema: {
|
|
177
|
+
type: "string",
|
|
178
|
+
pattern: localeRegex.source
|
|
179
|
+
}
|
|
180
|
+
}),
|
|
181
|
+
TimeZone: () => new FieldType({
|
|
182
|
+
name: "TimeZone",
|
|
183
|
+
zod: z$1.string().regex(timezoneRegex),
|
|
184
|
+
parseValue: (v) => z$1.string().regex(timezoneRegex).parse(v),
|
|
185
|
+
serialize: (v) => String(v),
|
|
186
|
+
jsonSchema: {
|
|
187
|
+
type: "string",
|
|
188
|
+
pattern: timezoneRegex.source
|
|
189
|
+
}
|
|
190
|
+
}),
|
|
191
|
+
Latitude: () => new FieldType({
|
|
192
|
+
name: "Latitude",
|
|
193
|
+
zod: z$1.number().min(latMin).max(latMax),
|
|
194
|
+
parseValue: (v) => z$1.coerce.number().min(latMin).max(latMax).parse(v),
|
|
195
|
+
serialize: (v) => Number(v),
|
|
196
|
+
jsonSchema: {
|
|
197
|
+
type: "number",
|
|
198
|
+
minimum: latMin,
|
|
199
|
+
maximum: latMax
|
|
200
|
+
}
|
|
201
|
+
}),
|
|
202
|
+
Longitude: () => new FieldType({
|
|
203
|
+
name: "Longitude",
|
|
204
|
+
zod: z$1.number().min(lonMin).max(lonMax),
|
|
205
|
+
parseValue: (v) => z$1.coerce.number().min(lonMin).max(lonMax).parse(v),
|
|
206
|
+
serialize: (v) => Number(v),
|
|
207
|
+
jsonSchema: {
|
|
208
|
+
type: "number",
|
|
209
|
+
minimum: lonMin,
|
|
210
|
+
maximum: lonMax
|
|
211
|
+
}
|
|
212
|
+
}),
|
|
213
|
+
Currency: () => new FieldType({
|
|
214
|
+
name: "Currency",
|
|
215
|
+
zod: z$1.string().regex(currencyRegex),
|
|
216
|
+
parseValue: (v) => z$1.string().regex(currencyRegex).parse(v),
|
|
217
|
+
serialize: (v) => String(v),
|
|
218
|
+
jsonSchema: {
|
|
219
|
+
type: "string",
|
|
220
|
+
pattern: currencyRegex.source
|
|
221
|
+
}
|
|
222
|
+
}),
|
|
223
|
+
CountryCode: () => new FieldType({
|
|
224
|
+
name: "CountryCode",
|
|
225
|
+
zod: z$1.string().regex(countryRegex),
|
|
226
|
+
parseValue: (v) => z$1.string().regex(countryRegex).parse(v),
|
|
227
|
+
serialize: (v) => String(v),
|
|
228
|
+
jsonSchema: {
|
|
229
|
+
type: "string",
|
|
230
|
+
pattern: countryRegex.source
|
|
231
|
+
}
|
|
232
|
+
})
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
//#endregion
|
|
236
|
+
export { ScalarTypeEnum };
|
|
237
|
+
//# sourceMappingURL=ScalarTypeEnum.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ScalarTypeEnum.js","names":["z"],"sources":["../src/ScalarTypeEnum.ts"],"sourcesContent":["import * as z from 'zod';\nimport { Kind, type ValueNode } from 'graphql';\nimport { FieldType } from './FieldType';\n\n// Helpers to build standard scalars\nconst localeRegex = /^[A-Za-z]{2}(?:-[A-Za-z0-9]{2,8})*$/;\nconst timezoneRegex = /^(?:UTC|[A-Za-z_]+\\/[A-Za-z_]+)$/;\nconst phoneRegex = /^[+]?\\d[\\d\\s().-]{3,}$/;\nconst currencyRegex = /^[A-Z]{3}$/;\nconst countryRegex = /^[A-Z]{2}$/;\nconst latMin = -90;\nconst latMax = 90;\nconst lonMin = -180;\nconst lonMax = 180;\n\n/**\n * Factory functions for common scalar FieldTypes with zod/GraphQL/JSON Schema.\n */\nexport const ScalarTypeEnum = {\n // primitives (_unsecure)\n String_unsecure: (): FieldType<string> =>\n new FieldType<string>({\n name: 'String_unsecure',\n description: 'Unvalidated string scalar',\n zod: z.string(),\n parseValue: (v) => z.string().parse(v),\n serialize: (v) => String(v),\n parseLiteral: (ast: ValueNode) => {\n if (ast.kind !== Kind.STRING) throw new TypeError('Invalid literal');\n return ast.value;\n },\n jsonSchema: { type: 'string' },\n }),\n Int_unsecure: (): FieldType<number> =>\n new FieldType<number>({\n name: 'Int_unsecure',\n description: 'Unvalidated integer scalar',\n zod: z.number().int(),\n parseValue: (v) => {\n const num = typeof v === 'number' ? v : Number(v as unknown);\n return z.number().int().parse(num);\n },\n serialize: (v) => Math.trunc(typeof v === 'number' ? v : Number(v)),\n parseLiteral: (ast: ValueNode) => {\n if (ast.kind !== Kind.INT) throw new TypeError('Invalid literal');\n return Number(ast.value);\n },\n jsonSchema: { type: 'integer' },\n }),\n Float_unsecure: (): FieldType<number> =>\n new FieldType<number>({\n name: 'Float_unsecure',\n description: 'Unvalidated float scalar',\n zod: z.number(),\n parseValue: (v) => {\n const num = typeof v === 'number' ? v : Number(v as unknown);\n return z.number().parse(num);\n },\n serialize: (v) => Number(v),\n parseLiteral: (ast: ValueNode) => {\n if (ast.kind !== Kind.FLOAT && ast.kind !== Kind.INT)\n throw new TypeError('Invalid literal');\n return Number(ast.value);\n },\n jsonSchema: { type: 'number' },\n }),\n Boolean: (): FieldType<boolean> =>\n new FieldType<boolean>({\n name: 'Boolean',\n description: 'Unvalidated boolean scalar',\n zod: z.boolean(),\n parseValue: (v) => z.coerce.boolean().parse(v),\n serialize: (v) => Boolean(v),\n parseLiteral: (ast: ValueNode) => {\n if (ast.kind !== Kind.BOOLEAN) throw new TypeError('Invalid literal');\n return ast.value;\n },\n jsonSchema: { type: 'boolean' },\n }),\n ID: (): FieldType<string> =>\n new FieldType<string>({\n name: 'ID',\n description: 'Unvalidated id scalar',\n zod: z.string(),\n parseValue: (v) => z.string().parse(v),\n serialize: (v) => String(v),\n parseLiteral: (ast: ValueNode) => {\n if (ast.kind !== Kind.STRING) throw new TypeError('Invalid literal');\n return ast.value;\n },\n jsonSchema: { type: 'string' },\n }),\n\n // Validated custom scalars\n JSON: (): FieldType<unknown> =>\n new FieldType<unknown>({\n name: 'JSON',\n zod: z.any(),\n parseValue: (v) => v,\n serialize: (v) => v,\n jsonSchema: {},\n }),\n JSONObject: (): FieldType<Record<string, unknown>> =>\n new FieldType<Record<string, unknown>>({\n name: 'JSONObject',\n zod: z.record(z.string(), z.any()),\n parseValue: (v) => z.record(z.string(), z.any()).parse(v),\n serialize: (v) => (v ?? {}) as Record<string, unknown>,\n jsonSchema: { type: 'object' },\n }),\n Date: (): FieldType<Date, string> =>\n new FieldType<Date, string>({\n name: 'Date',\n zod: z.date(),\n parseValue: (v) => (v instanceof Date ? v : new Date(String(v))),\n serialize: (v) =>\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n v instanceof Date ? v.toISOString().split('T')[0]! : String(v),\n jsonSchema: { type: 'string', format: 'date' },\n }),\n DateTime: (): FieldType<Date, string> =>\n new FieldType<Date, string>({\n name: 'DateTime',\n zod: z.date(),\n parseValue: (v) => (v instanceof Date ? v : new Date(String(v))),\n serialize: (v) => {\n return v instanceof Date ? v.toISOString() : String(v);\n },\n jsonSchema: { type: 'string', format: 'date-time' },\n }),\n Time: (): FieldType<string> =>\n new FieldType<string>({\n name: 'Time',\n zod: z.string().regex(/^\\d{2}:\\d{2}(:\\d{2})?$/),\n parseValue: (v) =>\n z\n .string()\n .regex(/^\\d{2}:\\d{2}(:\\d{2})?$/)\n .parse(v),\n serialize: (v) => String(v),\n jsonSchema: { type: 'string', pattern: '^\\\\d{2}:\\\\d{2}(:\\\\d{2})?$' },\n }),\n EmailAddress: (): FieldType<string> =>\n new FieldType<string>({\n name: 'EmailAddress',\n zod: z.string().email(),\n parseValue: (v) => z.string().email().parse(v),\n serialize: (v) => String(v),\n jsonSchema: { type: 'string', format: 'email' },\n }),\n URL: (): FieldType<string> =>\n new FieldType<string>({\n name: 'URL',\n zod: z.string().url(),\n parseValue: (v) => z.string().url().parse(v),\n serialize: (v) => String(v),\n jsonSchema: { type: 'string', format: 'uri' },\n }),\n PhoneNumber: (): FieldType<string> =>\n new FieldType<string>({\n name: 'PhoneNumber',\n zod: z.string().regex(phoneRegex),\n parseValue: (v) => z.string().regex(phoneRegex).parse(v),\n serialize: (v) => String(v),\n jsonSchema: { type: 'string', pattern: phoneRegex.source },\n }),\n NonEmptyString: (): FieldType<string> =>\n new FieldType<string>({\n name: 'NonEmptyString',\n zod: z.string().min(1),\n parseValue: (v) => z.string().min(1).parse(v),\n serialize: (v) => String(v),\n jsonSchema: { type: 'string', minLength: 1 },\n }),\n Locale: (): FieldType<string> =>\n new FieldType<string>({\n name: 'Locale',\n zod: z.string().regex(localeRegex),\n parseValue: (v) => z.string().regex(localeRegex).parse(v),\n serialize: (v) => String(v),\n jsonSchema: { type: 'string', pattern: localeRegex.source },\n }),\n TimeZone: (): FieldType<string> =>\n new FieldType<string>({\n name: 'TimeZone',\n zod: z.string().regex(timezoneRegex),\n parseValue: (v) => z.string().regex(timezoneRegex).parse(v),\n serialize: (v) => String(v),\n jsonSchema: { type: 'string', pattern: timezoneRegex.source },\n }),\n Latitude: (): FieldType<number> =>\n new FieldType<number>({\n name: 'Latitude',\n zod: z.number().min(latMin).max(latMax),\n parseValue: (v) => z.coerce.number().min(latMin).max(latMax).parse(v),\n serialize: (v) => Number(v),\n jsonSchema: { type: 'number', minimum: latMin, maximum: latMax },\n }),\n Longitude: (): FieldType<number> =>\n new FieldType<number>({\n name: 'Longitude',\n zod: z.number().min(lonMin).max(lonMax),\n parseValue: (v) => z.coerce.number().min(lonMin).max(lonMax).parse(v),\n serialize: (v) => Number(v),\n jsonSchema: { type: 'number', minimum: lonMin, maximum: lonMax },\n }),\n Currency: (): FieldType<string> =>\n new FieldType<string>({\n name: 'Currency',\n zod: z.string().regex(currencyRegex),\n parseValue: (v) => z.string().regex(currencyRegex).parse(v),\n serialize: (v) => String(v),\n jsonSchema: { type: 'string', pattern: currencyRegex.source },\n }),\n CountryCode: (): FieldType<string> =>\n new FieldType<string>({\n name: 'CountryCode',\n zod: z.string().regex(countryRegex),\n parseValue: (v) => z.string().regex(countryRegex).parse(v),\n serialize: (v) => String(v),\n jsonSchema: { type: 'string', pattern: countryRegex.source },\n }),\n // GeoJSON: (): FieldType<Record<string, unknown>, Record<string, unknown>> =>\n // new FieldType<Record<string, unknown>, Record<string, unknown>>({\n // name: 'GeoJSON',\n // zod: z.object({ type: z.string(), coordinates: z.any() }).passthrough(),\n // parseValue: (v) =>\n // z\n // .object({ type: z.string(), coordinates: z.any() })\n // .passthrough()\n // .parse(v),\n // serialize: (v) => v,\n // jsonSchema: { type: 'object' },\n // }),\n} as const;\nexport type ScalarTypeEnum = [keyof typeof ScalarTypeEnum];\n"],"mappings":";;;;;AAKA,MAAM,cAAc;AACpB,MAAM,gBAAgB;AACtB,MAAM,aAAa;AACnB,MAAM,gBAAgB;AACtB,MAAM,eAAe;AACrB,MAAM,SAAS;AACf,MAAM,SAAS;AACf,MAAM,SAAS;AACf,MAAM,SAAS;;;;AAKf,MAAa,iBAAiB;CAE5B,uBACE,IAAI,UAAkB;EACpB,MAAM;EACN,aAAa;EACb,KAAKA,IAAE,QAAQ;EACf,aAAa,MAAMA,IAAE,QAAQ,CAAC,MAAM,EAAE;EACtC,YAAY,MAAM,OAAO,EAAE;EAC3B,eAAe,QAAmB;AAChC,OAAI,IAAI,SAAS,KAAK,OAAQ,OAAM,IAAI,UAAU,kBAAkB;AACpE,UAAO,IAAI;;EAEb,YAAY,EAAE,MAAM,UAAU;EAC/B,CAAC;CACJ,oBACE,IAAI,UAAkB;EACpB,MAAM;EACN,aAAa;EACb,KAAKA,IAAE,QAAQ,CAAC,KAAK;EACrB,aAAa,MAAM;GACjB,MAAM,MAAM,OAAO,MAAM,WAAW,IAAI,OAAO,EAAa;AAC5D,UAAOA,IAAE,QAAQ,CAAC,KAAK,CAAC,MAAM,IAAI;;EAEpC,YAAY,MAAM,KAAK,MAAM,OAAO,MAAM,WAAW,IAAI,OAAO,EAAE,CAAC;EACnE,eAAe,QAAmB;AAChC,OAAI,IAAI,SAAS,KAAK,IAAK,OAAM,IAAI,UAAU,kBAAkB;AACjE,UAAO,OAAO,IAAI,MAAM;;EAE1B,YAAY,EA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AI,OAAO;EACvC,aAAa,MAAMA,IAAE,OAAO,QAAQ,CAAC,IAAI,OAAO,CAAC,IAAI,OAAO,CAAC,MAAM,EAAE;EACrE,YAAY,MAAM,OAAO,EAAE;EAC3B,YAAY;GAAE,MAAM;GAAU,SAAS;GAAQ,SAAS;GAAQ;EACjE,CAAC;CACJ,iBACE,IAAI,UAAkB;EACpB,MAAM;EACN,KAAKA,IAAE,QAAQ,CAAC,IAAI,OAAO,CAAC,IAAI,OAAO;EACvC,aAAa,MAAMA,IAAE,OAAO,QAAQ,CAAC,IAAI,OAAO,CAAC,IAAI,OAAO,CAAC,MAAM,EAAE;EACrE,YAAY,MAAM,OAAO,EAAE;EAC3B,YAAY;GAAE,MAAM;GAAU,SAAS;GAAQ,SAAS;GAAQ;EACjE,CAAC;CACJ,gBACE,IAAI,UAAkB;EACpB,MAAM;EACN,KAAKA,IAAE,QAAQ,CAAC,MAAM,cAAc;EACpC,aAAa,MAAMA,IAAE,QAAQ,CAAC,MAAM,cAAc,CAAC,MAAM,EAAE;EAC3D,YAAY,MAAM,OAAO,EAAE;EAC3B,YAAY;GAAE,MAAM;GAAU,SAAS,cAAc;GAAQ;EAC9D,CAAC;CACJ,mBACE,IAAI,UAAkB;EACpB,MAAM;EACN,KAAKA,IAAE,QAAQ,CAAC,MAAM,aAAa;EACnC,aAAa,MAAMA,IAAE,QAAQ,CAAC,MAAM,aAAa,CAAC,MAAM,EAAE;EAC1D,YAAY,MAAM,OAAO,EAAE;EAC3B,YAAY;GAAE,MAAM;GAAU,SAAS,aAAa;GAAQ;EAC7D,CAAC;CAaL"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { AnyEnumType } from "./EnumType.js";
|
|
2
|
+
import { AnyFieldType } from "./FieldType.js";
|
|
3
|
+
import { SchemaModelType } from "./SchemaModelType.js";
|
|
4
|
+
import * as z$1 from "zod";
|
|
5
|
+
import { Maybe } from "graphql/jsutils/Maybe";
|
|
6
|
+
|
|
7
|
+
//#region src/SchemaModel.d.ts
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* All types that can be used as field types in a SchemaModel.
|
|
11
|
+
* Supports FieldType, EnumType, nested SchemaModel, or any SchemaType implementation.
|
|
12
|
+
*/
|
|
13
|
+
type FieldLike = AnyFieldType | AnyEnumType | AnySchemaModel | SchemaModelType;
|
|
14
|
+
/** Field configuration for a SchemaModel property. */
|
|
15
|
+
interface SchemaFieldConfig<Type extends FieldLike = FieldLike> {
|
|
16
|
+
type: Type;
|
|
17
|
+
isOptional: boolean;
|
|
18
|
+
/** When present and true, the field is an array */
|
|
19
|
+
isArray?: true;
|
|
20
|
+
}
|
|
21
|
+
type SchemaModelFieldsAnyConfig<Type extends FieldLike = FieldLike> = Record<string, SchemaFieldConfig<Type>>;
|
|
22
|
+
/** Model definition: name and fields. */
|
|
23
|
+
interface SchemaModelConfig<Fields extends SchemaModelFieldsAnyConfig> {
|
|
24
|
+
name: string;
|
|
25
|
+
description?: Maybe<string>;
|
|
26
|
+
fields: Fields;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Named object model built from FieldType/EnumType/SchemaModel fields.
|
|
30
|
+
* Provides zod and GraphQL input helpers, and supports arrays/optional fields.
|
|
31
|
+
*/
|
|
32
|
+
declare class SchemaModel<Fields extends SchemaModelFieldsAnyConfig> implements SchemaModelType {
|
|
33
|
+
readonly config: SchemaModelConfig<Fields>;
|
|
34
|
+
constructor(config: SchemaModelConfig<Fields>);
|
|
35
|
+
/**
|
|
36
|
+
* Build a typed ZodObject from the model fields, preserving each field's
|
|
37
|
+
* Zod schema and optionality at the type level when possible.
|
|
38
|
+
*/
|
|
39
|
+
getZod(): TopLevelZodFromModel<Fields>;
|
|
40
|
+
/** Input object name for GraphQL builder adapters. */
|
|
41
|
+
getPothosInput(): string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Union of all types that can serve as a schema model.
|
|
45
|
+
* This is the main type expected by OperationSpec, EventSpec, FormSpec, etc.
|
|
46
|
+
*
|
|
47
|
+
* Supports:
|
|
48
|
+
* - SchemaModel instances (native ContractSpec types)
|
|
49
|
+
* - Any SchemaType implementation (ZodSchemaType, JsonSchemaType, etc.)
|
|
50
|
+
*/
|
|
51
|
+
type AnySchemaModel = SchemaModel<SchemaModelFieldsAnyConfig> | SchemaModelType;
|
|
52
|
+
/**
|
|
53
|
+
* Type guard to check if a value is a SchemaModel (not just any SchemaType).
|
|
54
|
+
* Use this when you need to access SchemaModel-specific properties like `config`.
|
|
55
|
+
*
|
|
56
|
+
* @param model - The model to check
|
|
57
|
+
* @returns True if the model is a SchemaModel instance
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* if (isSchemaModel(model)) {
|
|
62
|
+
* // TypeScript knows model.config is available
|
|
63
|
+
* console.log(model.config.name);
|
|
64
|
+
* }
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
declare function isSchemaModel(model: AnySchemaModel | null | undefined): model is SchemaModel<SchemaModelFieldsAnyConfig>;
|
|
68
|
+
type ZodSchemaModel<Field extends AnySchemaModel> = z$1.infer<ReturnType<Field['getZod']>>;
|
|
69
|
+
type InferZodFromType<T> = T extends SchemaModel<SchemaModelFieldsAnyConfig> ? z$1.ZodObject<z$1.ZodRawShape> : T extends AnyFieldType ? ReturnType<T['getZod']> : T extends AnyEnumType ? ReturnType<T['getZod']> : T extends SchemaModelType ? ReturnType<T['getZod']> : z$1.ZodUnknown;
|
|
70
|
+
type MaybeArray<Z extends z$1.ZodType, A> = A extends true ? z$1.ZodArray<Z> : Z;
|
|
71
|
+
type MaybeOptional<Z extends z$1.ZodType, O> = O extends true ? z$1.ZodOptional<Z> : Z;
|
|
72
|
+
/**
|
|
73
|
+
* Helper type: derive the Zod shape from the field config.
|
|
74
|
+
* Supports nested SchemaModel and arrays, preserving optionality and list-ness.
|
|
75
|
+
*/
|
|
76
|
+
type FieldIsArray<FC> = FC extends {
|
|
77
|
+
isArray: true;
|
|
78
|
+
} ? true : false;
|
|
79
|
+
type ZodShapeFromFields<F extends SchemaModelFieldsAnyConfig> = { [K in keyof F]: MaybeOptional<MaybeArray<InferZodFromType<F[K]['type']>, FieldIsArray<F[K]>>, F[K]['isOptional']> };
|
|
80
|
+
/**
|
|
81
|
+
* The top-level Zod schema returned by getZod():
|
|
82
|
+
* either ZodObject<...> or ZodArray<ZodObject<...>> when config.isArray.
|
|
83
|
+
*/
|
|
84
|
+
type TopLevelZodFromModel<F extends SchemaModelFieldsAnyConfig> = z$1.ZodObject<ZodShapeFromFields<F>>;
|
|
85
|
+
/**
|
|
86
|
+
* Helper to define a SchemaModel with type inference.
|
|
87
|
+
* Equivalent to `new SchemaModel(config)` but with better ergonomics.
|
|
88
|
+
*/
|
|
89
|
+
declare const defineSchemaModel: <Fields extends SchemaModelFieldsAnyConfig>(config: SchemaModelConfig<Fields>) => SchemaModel<Fields>;
|
|
90
|
+
//#endregion
|
|
91
|
+
export { AnySchemaModel, SchemaFieldConfig, SchemaModel, SchemaModelConfig, SchemaModelFieldsAnyConfig, TopLevelZodFromModel, ZodSchemaModel, ZodShapeFromFields, defineSchemaModel, isSchemaModel };
|
|
92
|
+
//# sourceMappingURL=SchemaModel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SchemaModel.d.ts","names":[],"sources":["../src/SchemaModel.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;AAImD;;KAM9C,SAAA,GAAY,YAAe,GAAA,WAAA,GAAc,cAAd,GAA+B,eAA/B;;AAA+B,UAG9C,iBAH8C,CAAA,aAGf,SAHe,GAGH,SAHG,CAAA,CAAA;EAAe,IAAA,EAItE,IAJsE;EAG7D,UAAA,EAAA,OAAA;EAA+B;EAAY,OAAA,CAAA,EAAA,IAAA;;AAChD,KAMA,0BANA,CAAA,aAMwC,SANxC,GAMoD,SANpD,CAAA,GAOV,MAPU,CAAA,MAAA,EAOK,iBAPL,CAOuB,IAPvB,CAAA,C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pB,EAAA,CAAA,CAAA,GAMkC,CANlC,SAAA,IAAA,GAMmD,GAAA,CAAE,QANrD,CAM8D,CAN9D,CAAA,GAMmE,CANnE;KAOH,aAPa,CAAA,UAOW,GAAA,CAAE,OAPb,EAAA,CAAA,CAAA,GAO2B,CAP3B,SAAA,IAAA,GAQd,GAAA,CAAE,WARY,CAQA,CARA,CAAA,GASd,CATc;;;;;KAeb,YAZkB,CAAA,EAAA,CAAA,GAYC,EAZD,SAAA;EAAX,OAAA,EAAA,IAAA;CACA,GAAA,IAAE,GAAA,KAAA;AAAU,KAaZ,kBAbY,CAAA,UAaiB,0BAbjB,CAAA,GAAA,QAEnB,MAYS,CAZC,GAYG,aAZH,CAaX,UAbW,CAaA,gBAbA,CAaiB,CAbjB,CAamB,CAbnB,CAAA,CAAA,MAAA,CAAA,CAAA,EAagC,YAbhC,CAa6C,CAb7C,CAa+C,CAb/C,CAAA,CAAA,CAAA,EAcX,CAdW,CAcT,CAdS,CAAA,CAAA,YAAA,CAAA,CAAA,EAAW;;;;;AAAkD,KAsBhE,oBAtBgE,CAAA,UAsBjC,0BAtBiC,CAAA,GAuB1E,GAAA,CAAE,SAvBwE,CAuB9D,kBAvB8D,CAuB3C,CAvB2C,CAAA,CAAA;AAAA;;;;AAEtE,cA2BO,iBA3BP,EAAA,CAAA,eA2B2C,0BA3B3C,CAAA,CAAA,MAAA,EA4BI,iBA5BJ,CA4BsB,MA5BtB,CAAA,EAAA,GA6BH,WA7BG,CA6BS,MA7BT,CAAA"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import "./EnumType.js";
|
|
2
|
+
import "./FieldType.js";
|
|
3
|
+
import * as z$1 from "zod";
|
|
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
|
+
*/
|
|
47
|
+
function isSchemaModel(model) {
|
|
48
|
+
return model !== null && model !== void 0 && "config" in model && typeof model.config === "object" && "name" in model.config;
|
|
49
|
+
}
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SchemaModel.js","names":["config: SchemaModelConfig<Fields>","base: z.ZodType","z"],"sources":["../src/SchemaModel.ts"],"sourcesContent":["import { type AnyFieldType } from './FieldType';\nimport { type AnyEnumType } from './EnumType';\nimport { type SchemaModelType } from './SchemaModelType';\nimport * as z from 'zod';\nimport type { Maybe } from 'graphql/jsutils/Maybe';\n\n/**\n * All types that can be used as field types in a SchemaModel.\n * Supports FieldType, EnumType, nested SchemaModel, or any SchemaType implementation.\n */\ntype FieldLike = AnyFieldType | AnyEnumType | AnySchemaModel | SchemaModelType;\n\n/** Field configuration for a SchemaModel property. */\nexport interface SchemaFieldConfig<Type extends FieldLike = FieldLike> {\n type: Type;\n isOptional: boolean;\n /** When present and true, the field is an array */\n isArray?: true;\n}\n\nexport type SchemaModelFieldsAnyConfig<Type extends FieldLike = FieldLike> =\n Record<string, SchemaFieldConfig<Type>>;\n\n/** Model definition: name and fields. */\nexport interface SchemaModelConfig<Fields extends SchemaModelFieldsAnyConfig> {\n name: string;\n description?: Maybe<string>;\n fields: Fields;\n}\n\n/**\n * Named object model built from FieldType/EnumType/SchemaModel fields.\n * Provides zod and GraphQL input helpers, and supports arrays/optional fields.\n */\nexport class SchemaModel<\n Fields extends SchemaModelFieldsAnyConfig,\n> implements SchemaModelType {\n constructor(public readonly config: SchemaModelConfig<Fields>) {}\n\n /**\n * Build a typed ZodObject from the model fields, preserving each field's\n * Zod schema and optionality at the type level when possible.\n */\n getZod(): TopLevelZodFromModel<Fields> {\n const shape = Object.entries(this.config.fields).reduce(\n (acc, [key, def]) => {\n const base: z.ZodType = (\n def.type as unknown as { getZod: () => z.ZodType }\n ).getZod();\n const withArray = def.isArray ? z.array(base) : base;\n (acc as Record<string, z.ZodType>)[key] = def.isOptional\n ? withArray.optional()\n : withArray;\n return acc;\n },\n {} as Record<string, z.ZodType>\n ) as unknown as ZodShapeFromFields<Fields>;\n\n return z.object(shape) as z.ZodObject<ZodShapeFromFields<Fields>>;\n }\n\n /** Input object name for GraphQL builder adapters. */\n getPothosInput() {\n return this.config.name;\n }\n}\n\n/**\n * Union of all types that can serve as a schema model.\n * This is the main type expected by OperationSpec, EventSpec, FormSpec, etc.\n *\n * Supports:\n * - SchemaModel instances (native ContractSpec types)\n * - Any SchemaType implementation (ZodSchemaType, JsonSchemaType, etc.)\n */\nexport type AnySchemaModel =\n | SchemaModel<SchemaModelFieldsAnyConfig>\n | SchemaModelType;\n\n/**\n * Type guard to check if a value is a SchemaModel (not just any SchemaType).\n * Use this when you need to access SchemaModel-specific properties like `config`.\n *\n * @param model - The model to check\n * @returns True if the model is a SchemaModel instance\n *\n * @example\n * ```typescript\n * if (isSchemaModel(model)) {\n * // TypeScript knows model.config is available\n * console.log(model.config.name);\n * }\n * ```\n */\nexport function isSchemaModel(\n model: AnySchemaModel | null | undefined\n): model is SchemaModel<SchemaModelFieldsAnyConfig> {\n return (\n model !== null &&\n model !== undefined &&\n 'config' in model &&\n typeof (model as SchemaModel<SchemaModelFieldsAnyConfig>).config ===\n 'object' &&\n 'name' in (model as SchemaModel<SchemaModelFieldsAnyConfig>).config\n );\n}\n\nexport type ZodSchemaModel<Field extends AnySchemaModel> = z.infer<\n ReturnType<Field['getZod']>\n>;\n\ntype InferZodFromType<T> =\n T extends SchemaModel<SchemaModelFieldsAnyConfig>\n ? z.ZodObject<z.ZodRawShape>\n : T extends AnyFieldType\n ? ReturnType<T['getZod']>\n : T extends AnyEnumType\n ? ReturnType<T['getZod']>\n : T extends SchemaModelType\n ? ReturnType<T['getZod']>\n : z.ZodUnknown;\n\ntype MaybeArray<Z extends z.ZodType, A> = A extends true ? z.ZodArray<Z> : Z;\ntype MaybeOptional<Z extends z.ZodType, O> = O extends true\n ? z.ZodOptional<Z>\n : Z;\n\n/**\n * Helper type: derive the Zod shape from the field config.\n * Supports nested SchemaModel and arrays, preserving optionality and list-ness.\n */\ntype FieldIsArray<FC> = FC extends { isArray: true } ? true : false;\n\nexport type ZodShapeFromFields<F extends SchemaModelFieldsAnyConfig> = {\n [K in keyof F]: MaybeOptional<\n MaybeArray<InferZodFromType<F[K]['type']>, FieldIsArray<F[K]>>,\n F[K]['isOptional']\n >;\n};\n\n/**\n * The top-level Zod schema returned by getZod():\n * either ZodObject<...> or ZodArray<ZodObject<...>> when config.isArray.\n */\nexport type TopLevelZodFromModel<F extends SchemaModelFieldsAnyConfig> =\n z.ZodObject<ZodShapeFromFields<F>>;\n\n/**\n * Helper to define a SchemaModel with type inference.\n * Equivalent to `new SchemaModel(config)` but with better ergonomics.\n */\nexport const defineSchemaModel = <Fields extends SchemaModelFieldsAnyConfig>(\n config: SchemaModelConfig<Fields>\n): SchemaModel<Fields> => new SchemaModel(config);\n"],"mappings":";;;;;;;;;AAkCA,IAAa,cAAb,MAE6B;CAC3B,YAAY,AAAgBA,QAAmC;EAAnC;;;;;;CAM5B,SAAuC;EACrC,MAAM,QAAQ,OAAO,QAAQ,KAAK,OAAO,OAAO,CAAC,QAC9C,KAAK,CAAC,KAAK,SAAS;GACnB,MAAMC,OACJ,IAAI,KACJ,QAAQ;GACV,MAAM,YAAY,IAAI,UAAUC,IAAE,MAAM,KAAK,GAAG;AAChD,GAAC,IAAkC,OAAO,IAAI,aAC1C,UAAU,UAAU,GACpB;AACJ,UAAO;KAET,EAAE,CACH;AAED,SAAOA,IAAE,OAAO,MAAM;;;CAIxB,iBAAiB;AACf,SAAO,KAAK,OAAO;;;;;;;;;;;;;;;;;;AA+BvB,SAAgB,cACd,OACkD;AAClD,QACE,UAAU,QACV,UAAU,UACV,YAAY,SACZ,OAAQ,MAAkD,WACxD,YACF,UAAW,MAAkD;;;;;;AAgDjE,MAAa,qBACX,WACwB,IAAI,YAAY,OAAO"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import * as z$1 from "zod";
|
|
2
|
+
|
|
3
|
+
//#region src/SchemaModelType.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Supported schema output formats for code generation.
|
|
7
|
+
*/
|
|
8
|
+
type SchemaFormat = 'contractspec' | 'zod' | 'json-schema' | 'graphql';
|
|
9
|
+
/**
|
|
10
|
+
* Unified interface for all schema-compatible types.
|
|
11
|
+
* Any type used in ContractSpec schemas must implement this interface.
|
|
12
|
+
*
|
|
13
|
+
* @template T - The TypeScript type this schema represents
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // All of these implement SchemaType:
|
|
18
|
+
* const fieldType = ScalarTypeEnum.String_unsecure();
|
|
19
|
+
* const schemaModel = new SchemaModel({ name: 'User', fields: {...} });
|
|
20
|
+
* const zodWrapper = fromZod(z.object({ name: z.string() }));
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
interface SchemaModelType<T = unknown> {
|
|
24
|
+
/**
|
|
25
|
+
* Return the Zod schema for runtime validation.
|
|
26
|
+
* This is the primary method - all schema types must provide Zod conversion.
|
|
27
|
+
*/
|
|
28
|
+
getZod(): z$1.ZodType<T>;
|
|
29
|
+
/**
|
|
30
|
+
* Return GraphQL type info for Pothos/GraphQL integration.
|
|
31
|
+
* Optional - types without GraphQL representation return undefined.
|
|
32
|
+
*/
|
|
33
|
+
getPothos?(): unknown;
|
|
34
|
+
/**
|
|
35
|
+
* Return JSON Schema representation.
|
|
36
|
+
* Optional - types without JSON Schema representation return undefined.
|
|
37
|
+
*/
|
|
38
|
+
getJsonSchema?(): unknown;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Type guard to check if a value implements the SchemaType interface.
|
|
42
|
+
*
|
|
43
|
+
* @param value - Value to check
|
|
44
|
+
* @returns True if value has a getZod method
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* if (isSchemaType(field.type)) {
|
|
49
|
+
* const zodSchema = field.type.getZod();
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
declare function isSchemaType(value: unknown): value is SchemaModelType;
|
|
54
|
+
/**
|
|
55
|
+
* Type alias for any SchemaType implementation.
|
|
56
|
+
*/
|
|
57
|
+
type AnySchemaType = SchemaModelType<unknown>;
|
|
58
|
+
//#endregion
|
|
59
|
+
export { AnySchemaType, SchemaFormat, SchemaModelType, isSchemaType };
|
|
60
|
+
//# sourceMappingURL=SchemaModelType.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SchemaModelType.d.ts","names":[],"sources":["../src/SchemaModelType.ts"],"sourcesContent":[],"mappings":";;;;;;;KAcY,YAAA;;;;;;;;;;;;;;;UAgBK;;;;;YAKL,GAAA,CAAE,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;iBA4BN,YAAA,2BAAuC;;;;KAY3C,aAAA,GAAgB"}
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
*/
|
|
15
|
+
function isSchemaType(value) {
|
|
16
|
+
return value !== null && typeof value === "object" && "getZod" in value && typeof value.getZod === "function";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
export { isSchemaType };
|
|
21
|
+
//# sourceMappingURL=SchemaModelType.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SchemaModelType.js","names":[],"sources":["../src/SchemaModelType.ts"],"sourcesContent":["/**\n * Unified schema type interface for ContractSpec.\n *\n * This module provides a vendor-agnostic abstraction over different schema\n * definition approaches (SchemaModel, Zod, JSON Schema, GraphQL scalars).\n *\n * @module SchemaType\n */\n\nimport type * as z from 'zod';\n\n/**\n * Supported schema output formats for code generation.\n */\nexport type SchemaFormat = 'contractspec' | 'zod' | 'json-schema' | 'graphql';\n\n/**\n * Unified interface for all schema-compatible types.\n * Any type used in ContractSpec schemas must implement this interface.\n *\n * @template T - The TypeScript type this schema represents\n *\n * @example\n * ```typescript\n * // All of these implement SchemaType:\n * const fieldType = ScalarTypeEnum.String_unsecure();\n * const schemaModel = new SchemaModel({ name: 'User', fields: {...} });\n * const zodWrapper = fromZod(z.object({ name: z.string() }));\n * ```\n */\nexport interface SchemaModelType<T = unknown> {\n /**\n * Return the Zod schema for runtime validation.\n * This is the primary method - all schema types must provide Zod conversion.\n */\n getZod(): z.ZodType<T>;\n\n /**\n * Return GraphQL type info for Pothos/GraphQL integration.\n * Optional - types without GraphQL representation return undefined.\n */\n getPothos?(): unknown;\n\n /**\n * Return JSON Schema representation.\n * Optional - types without JSON Schema representation return undefined.\n */\n getJsonSchema?(): unknown;\n}\n\n/**\n * Type guard to check if a value implements the SchemaType interface.\n *\n * @param value - Value to check\n * @returns True if value has a getZod method\n *\n * @example\n * ```typescript\n * if (isSchemaType(field.type)) {\n * const zodSchema = field.type.getZod();\n * }\n * ```\n */\nexport function isSchemaType(value: unknown): value is SchemaModelType {\n return (\n value !== null &&\n typeof value === 'object' &&\n 'getZod' in value &&\n typeof (value as SchemaModelType).getZod === 'function'\n );\n}\n\n/**\n * Type alias for any SchemaType implementation.\n */\nexport type AnySchemaType = SchemaModelType<unknown>;\n"],"mappings":";;;;;;;;;;;;;;AA+DA,SAAgB,aAAa,OAA0C;AACrE,QACE,UAAU,QACV,OAAO,UAAU,YACjB,YAAY,SACZ,OAAQ,MAA0B,WAAW"}
|