-
Notifications
You must be signed in to change notification settings - Fork 137
/
schema.ts
98 lines (91 loc) · 2.96 KB
/
schema.ts
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
/**
* Copyright 2024 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { TypeOf, ZodType, ZodEffects, ZodTypeAny, AnyZodObject, input } from "zod";
import { zodToJsonSchema, Options } from "zod-to-json-schema";
import { Ajv, SchemaObject, ValidateFunction, Options as AjvOptions } from "ajv";
import addFormats from "ajv-formats";
import { findFirstPair } from "@/internals/helpers/string.js";
import { FrameworkErrorOptions, ValueError } from "@/errors.js";
import { jsonrepair } from "jsonrepair";
export type AnyToolSchemaLike = AnyZodObject | SchemaObject;
export type AnySchemaLike = ZodTypeAny | SchemaObject;
export type FromSchemaLike<T> = T extends ZodTypeAny ? TypeOf<T> : unknown;
export type FromSchemaLikeRaw<T> = T extends ZodTypeAny ? input<T> : unknown;
export function validateSchema<T extends AnySchemaLike>(
schema: T | ZodEffects<any>,
errorOptions?: FrameworkErrorOptions,
): asserts schema is T {
if (schema && schema instanceof ZodEffects) {
throw new ValueError(
"zod effects (refine, superRefine, transform, ...) cannot be converted to JSONSchema!",
[],
errorOptions,
);
}
}
export function toJsonSchema<T extends AnySchemaLike>(
schema: T,
options?: Partial<Options>,
): SchemaObject {
validateSchema(schema);
if (schema instanceof ZodType) {
return zodToJsonSchema(schema, options);
}
return schema;
}
export function createSchemaValidator<T extends AnySchemaLike>(
schema: T,
options?: AjvOptions,
): ValidateFunction<FromSchemaLike<T>> {
const jsonSchema = toJsonSchema(schema);
const ajv = new Ajv({
coerceTypes: "array",
useDefaults: true,
strict: false,
strictSchema: false,
strictTuples: true,
strictNumbers: true,
strictTypes: true,
strictRequired: true,
parseDate: true,
allowDate: true,
allowUnionTypes: true,
...options,
});
addFormats.default(ajv);
return ajv.compile<FromSchemaLike<T>>(jsonSchema);
}
interface ParseBrokenJsonOptions {
pair?: [string, string] | null;
}
export function parseBrokenJson(input: string | undefined, options?: ParseBrokenJsonOptions) {
input = (input ?? "")?.trim();
try {
try {
return JSON.parse(input);
} catch {
const pair = options?.pair;
if (pair) {
const { outer } = findFirstPair(input, pair) ?? { outer: input };
return JSON.parse(jsonrepair(outer));
} else {
return JSON.parse(jsonrepair(input));
}
}
} catch {
return null;
}
}