-
Notifications
You must be signed in to change notification settings - Fork 412
Expand file tree
/
Copy pathvalidate.ts
More file actions
249 lines (231 loc) · 9.9 KB
/
validate.ts
File metadata and controls
249 lines (231 loc) · 9.9 KB
1
2
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
31
32
33
34
35
36
37
38
39
40
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import { z } from "zod";
import { success, error } from '../result';
import { TypeChatJsonValidator } from '../typechat';
/**
* Returns a JSON validator for a given Zod schema. The schema is supplied as an object where each property provides
* a name for an associated Zod type. The `validate` method of the returned object validates a JSON object against the
* supplied schema, the `getSchemaText` method obtains the TypeScript source text representation of the schema, and
* the `getTypeName` method obtains the name of the given target type in the schema.
* @param schema A schema object where each property provides a name for an associated Zod type.
* @param targetType The name in the schema of the target type for JSON validation.
* @returns A `TypeChatJsonValidator<z.TypeOf<T[K]>>`, where T is the schema and K is the target type name.
*/
export function createZodJsonValidator<T extends Record<string, z.ZodType>, K extends keyof T & string>(schema: T, typeName: K): TypeChatJsonValidator<z.TypeOf<T[K]>> {
let schemaText: string;
const validator: TypeChatJsonValidator<z.TypeOf<T[K]>> = {
getSchemaText: () => schemaText ??= getZodSchemaAsTypeScript(schema),
getTypeName: () => typeName,
validate
};
return validator;
function validate(jsonObject: object) {
const result = schema[typeName].safeParse(jsonObject);
if (!result.success) {
return error(result.error.issues.map(({ path, message }) => `${path.map(key => `[${JSON.stringify(key)}]`).join("")}: ${message}`).join("\""));
}
return success(result.data as z.TypeOf<T[K]>);
}
}
function getTypeKind(type: z.ZodType) {
return (type._def as z.ZodTypeDef & { typeName: z.ZodFirstPartyTypeKind }).typeName;
}
function getTypeIdentity(type: z.ZodType): object {
switch (getTypeKind(type)) {
case z.ZodFirstPartyTypeKind.ZodObject:
return (type._def as z.ZodObjectDef).shape();
case z.ZodFirstPartyTypeKind.ZodEnum:
return (type._def as z.ZodEnumDef).values;
case z.ZodFirstPartyTypeKind.ZodUnion:
return (type._def as z.ZodUnionDef).options;
}
return type;
}
const enum TypePrecedence {
Union = 0,
Intersection = 1,
Object = 2
}
function getTypePrecedence(type: z.ZodType): TypePrecedence {
switch (getTypeKind(type)) {
case z.ZodFirstPartyTypeKind.ZodEnum:
case z.ZodFirstPartyTypeKind.ZodUnion:
case z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion:
return TypePrecedence.Union;
case z.ZodFirstPartyTypeKind.ZodIntersection:
return TypePrecedence.Intersection;
}
return TypePrecedence.Object;
}
/**
* Returns the TypeScript source code corresponding to a Zod schema. The schema is supplied as an object where each
* property provides a name for an associated Zod type. The return value is a string containing the TypeScript source
* code corresponding to the schema. Each property of the schema object is emitted as a named `interface` or `type`
* declaration for the associated type and is referenced by that name in the emitted type declarations. Other types
* referenced in the schema are emitted in their structural form.
* @param schema A schema object where each property provides a name for an associated Zod type.
* @returns The TypeScript source code corresponding to the schema.
*/
export function getZodSchemaAsTypeScript(schema: Record<string, z.ZodType>): string {
let result = "";
let startOfLine = true;
let indent = 0;
const entries = Array.from(Object.entries(schema));
let namedTypes = new Map<object, string>(entries.map(([name, type]) => [getTypeIdentity(type), name]));
for (const [name, type] of entries) {
if (result) {
appendNewLine();
}
const description = type._def.description;
if (description) {
for (const comment of description.split("\n")) {
append(`// ${comment}`);
appendNewLine();
}
}
if (getTypeKind(type) === z.ZodFirstPartyTypeKind.ZodObject) {
append(`interface ${name} `);
appendObjectType(type as z.ZodObject<z.ZodRawShape>);
}
else {
append(`type ${name} = `);
appendTypeDefinition(type);
append(";");
}
appendNewLine();
}
return result;
function append(s: string) {
if (startOfLine) {
result += " ".repeat(indent);
startOfLine = false;
}
result += s;
}
function appendNewLine() {
append("\n");
startOfLine = true;
}
function appendType(type: z.ZodType, minPrecedence = 0) {
const name = namedTypes.get(getTypeIdentity(type));
if (name) {
append(name);
}
else {
const parenthesize = getTypePrecedence(type) < minPrecedence;
if (parenthesize) append("(");
appendTypeDefinition(type);
if (parenthesize) append(")");
}
}
function appendTypeDefinition(type: z.ZodType) {
switch (getTypeKind(type)) {
case z.ZodFirstPartyTypeKind.ZodString:
return append("string");
case z.ZodFirstPartyTypeKind.ZodNumber:
return append("number");
case z.ZodFirstPartyTypeKind.ZodBoolean:
return append("boolean");
case z.ZodFirstPartyTypeKind.ZodDate:
return append("Date");
case z.ZodFirstPartyTypeKind.ZodUndefined:
return append("undefined");
case z.ZodFirstPartyTypeKind.ZodNull:
return append("null");
case z.ZodFirstPartyTypeKind.ZodUnknown:
return append("unknown");
case z.ZodFirstPartyTypeKind.ZodArray:
return appendArrayType(type);
case z.ZodFirstPartyTypeKind.ZodObject:
return appendObjectType(type);
case z.ZodFirstPartyTypeKind.ZodUnion:
return appendUnionOrIntersectionTypes((type._def as z.ZodUnionDef).options, TypePrecedence.Union);
case z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion:
return appendUnionOrIntersectionTypes([...(type._def as z.ZodDiscriminatedUnionDef<string>).options.values()], TypePrecedence.Union);
case z.ZodFirstPartyTypeKind.ZodIntersection:
return appendUnionOrIntersectionTypes((type._def as z.ZodUnionDef).options, TypePrecedence.Intersection);
case z.ZodFirstPartyTypeKind.ZodTuple:
return appendTupleType(type);
case z.ZodFirstPartyTypeKind.ZodRecord:
return appendRecordType(type);
case z.ZodFirstPartyTypeKind.ZodLiteral:
return appendLiteral((type._def as z.ZodLiteralDef).value);
case z.ZodFirstPartyTypeKind.ZodEnum:
return append((type._def as z.ZodEnumDef).values.map(value => JSON.stringify(value)).join(" | "));
case z.ZodFirstPartyTypeKind.ZodOptional:
return appendUnionOrIntersectionTypes([(type._def as z.ZodOptionalDef).innerType, z.undefined()], TypePrecedence.Union);
case z.ZodFirstPartyTypeKind.ZodReadonly:
return appendReadonlyType(type);
}
append("any");
}
function appendArrayType(arrayType: z.ZodType) {
appendType((arrayType._def as z.ZodArrayDef).type, TypePrecedence.Object);
append("[]");
}
function appendObjectType(objectType: z.ZodType) {
append("{");
appendNewLine();
indent++;
for (let [name, type] of Object.entries((objectType._def as z.ZodObjectDef).shape())) {
const comment = type.description;
append(name);
if (getTypeKind(type) === z.ZodFirstPartyTypeKind.ZodOptional) {
append("?");
type = (type._def as z.ZodOptionalDef).innerType;
}
append(": ");
appendType(type);
append(";");
if (comment) append(` // ${comment}`);
appendNewLine();
}
indent--;
append("}");
}
function appendUnionOrIntersectionTypes(types: readonly z.ZodType[], minPrecedence: TypePrecedence) {
let first = true;
for (const type of types) {
if (!first) append(minPrecedence === TypePrecedence.Intersection ? " & " : " | ");
appendType(type, minPrecedence);
first = false;
}
}
function appendTupleType(tupleType: z.ZodType) {
append("[");
let first = true;
for (let type of (tupleType._def as z.ZodTupleDef<z.ZodTupleItems, z.ZodType>).items) {
if (!first) append(", ");
if (getTypeKind(type) === z.ZodFirstPartyTypeKind.ZodOptional) {
appendType((type._def as z.ZodOptionalDef).innerType, TypePrecedence.Object);
append("?");
}
else {
appendType(type);
}
first = false;
}
const rest = (tupleType._def as z.ZodTupleDef<z.ZodTupleItems, z.ZodType>).rest;
if (rest) {
if (!first) append(", ");
append("...");
appendType(rest, TypePrecedence.Object);
append("[]");
}
append("]");
}
function appendRecordType(recordType: z.ZodType) {
append("Record<");
appendType((recordType._def as z.ZodRecordDef).keyType);
append(", ");
appendType((recordType._def as z.ZodRecordDef).valueType);
append(">");
}
function appendLiteral(value: unknown) {
append(typeof value === "string" || typeof value === "number" || typeof value === "boolean" ? JSON.stringify(value) : "any");
}
function appendReadonlyType(readonlyType: z.ZodType) {
append("Readonly<");
appendType((readonlyType._def as z.ZodReadonlyDef).innerType);
append(">");
}
}