@ai-sdk/provider-utils 4.0.5 → 4.0.6
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/CHANGELOG.md +8 -0
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +4 -2
- package/src/__snapshots__/schema.test.ts.snap +346 -0
- package/src/add-additional-properties-to-json-schema.test.ts +289 -0
- package/src/add-additional-properties-to-json-schema.ts +53 -0
- package/src/combine-headers.ts +11 -0
- package/src/convert-async-iterator-to-readable-stream.test.ts +78 -0
- package/src/convert-async-iterator-to-readable-stream.ts +47 -0
- package/src/convert-image-model-file-to-data-uri.test.ts +85 -0
- package/src/convert-image-model-file-to-data-uri.ts +19 -0
- package/src/convert-to-form-data.test.ts +167 -0
- package/src/convert-to-form-data.ts +61 -0
- package/src/create-tool-name-mapping.test.ts +163 -0
- package/src/create-tool-name-mapping.ts +66 -0
- package/src/delay.test.ts +212 -0
- package/src/delay.ts +47 -0
- package/src/delayed-promise.test.ts +132 -0
- package/src/delayed-promise.ts +61 -0
- package/src/download-blob.test.ts +145 -0
- package/src/download-blob.ts +31 -0
- package/src/download-error.ts +39 -0
- package/src/extract-response-headers.ts +9 -0
- package/src/fetch-function.ts +4 -0
- package/src/generate-id.test.ts +31 -0
- package/src/generate-id.ts +57 -0
- package/src/get-error-message.ts +15 -0
- package/src/get-from-api.test.ts +199 -0
- package/src/get-from-api.ts +97 -0
- package/src/get-runtime-environment-user-agent.test.ts +47 -0
- package/src/get-runtime-environment-user-agent.ts +24 -0
- package/src/handle-fetch-error.ts +39 -0
- package/src/index.ts +67 -0
- package/src/inject-json-instruction.test.ts +404 -0
- package/src/inject-json-instruction.ts +63 -0
- package/src/is-abort-error.ts +8 -0
- package/src/is-async-iterable.ts +3 -0
- package/src/is-non-nullable.ts +12 -0
- package/src/is-url-supported.test.ts +282 -0
- package/src/is-url-supported.ts +40 -0
- package/src/load-api-key.ts +45 -0
- package/src/load-optional-setting.ts +30 -0
- package/src/load-setting.ts +62 -0
- package/src/maybe-promise-like.ts +3 -0
- package/src/media-type-to-extension.test.ts +26 -0
- package/src/media-type-to-extension.ts +22 -0
- package/src/normalize-headers.test.ts +64 -0
- package/src/normalize-headers.ts +38 -0
- package/src/parse-json-event-stream.ts +33 -0
- package/src/parse-json.test.ts +191 -0
- package/src/parse-json.ts +122 -0
- package/src/parse-provider-options.ts +32 -0
- package/src/post-to-api.ts +166 -0
- package/src/provider-tool-factory.ts +125 -0
- package/src/remove-undefined-entries.test.ts +57 -0
- package/src/remove-undefined-entries.ts +12 -0
- package/src/resolve.test.ts +125 -0
- package/src/resolve.ts +17 -0
- package/src/response-handler.test.ts +89 -0
- package/src/response-handler.ts +187 -0
- package/src/schema.test-d.ts +11 -0
- package/src/schema.test.ts +502 -0
- package/src/schema.ts +267 -0
- package/src/secure-json-parse.test.ts +59 -0
- package/src/secure-json-parse.ts +92 -0
- package/src/test/convert-array-to-async-iterable.ts +9 -0
- package/src/test/convert-array-to-readable-stream.ts +15 -0
- package/src/test/convert-async-iterable-to-array.ts +9 -0
- package/src/test/convert-readable-stream-to-array.ts +14 -0
- package/src/test/convert-response-stream-to-array.ts +9 -0
- package/src/test/index.ts +7 -0
- package/src/test/is-node-version.ts +4 -0
- package/src/test/mock-id.ts +8 -0
- package/src/to-json-schema/zod3-to-json-schema/LICENSE +16 -0
- package/src/to-json-schema/zod3-to-json-schema/README.md +24 -0
- package/src/to-json-schema/zod3-to-json-schema/get-relative-path.ts +7 -0
- package/src/to-json-schema/zod3-to-json-schema/index.ts +1 -0
- package/src/to-json-schema/zod3-to-json-schema/options.ts +98 -0
- package/src/to-json-schema/zod3-to-json-schema/parse-def.test.ts +224 -0
- package/src/to-json-schema/zod3-to-json-schema/parse-def.ts +109 -0
- package/src/to-json-schema/zod3-to-json-schema/parse-types.ts +57 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/any.ts +5 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/array.test.ts +98 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/array.ts +38 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/bigint.test.ts +51 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/bigint.ts +44 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/boolean.ts +7 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/branded.test.ts +16 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/branded.ts +7 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/catch.test.ts +15 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/catch.ts +7 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/date.test.ts +97 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/date.ts +64 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/default.test.ts +54 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/default.ts +14 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/effects.test.ts +41 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/effects.ts +14 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/enum.ts +13 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/intersection.test.ts +92 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/intersection.ts +52 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/literal.ts +29 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/map.test.ts +48 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/map.ts +47 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/native-enum.test.ts +102 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/native-enum.ts +31 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/never.ts +9 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/null.ts +9 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/nullable.test.ts +67 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/nullable.ts +42 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/number.test.ts +65 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/number.ts +44 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/object.test.ts +149 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/object.ts +88 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/optional.test.ts +147 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/optional.ts +23 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/pipe.test.ts +35 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/pipeline.ts +29 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/promise.test.ts +15 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/promise.ts +11 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/readonly.test.ts +20 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/readonly.ts +7 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/record.test.ts +108 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/record.ts +71 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/set.test.ts +20 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/set.ts +35 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/string.test.ts +438 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/string.ts +426 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/tuple.test.ts +33 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/tuple.ts +61 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/undefined.ts +11 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/union.test.ts +226 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/union.ts +144 -0
- package/src/to-json-schema/zod3-to-json-schema/parsers/unknown.ts +7 -0
- package/src/to-json-schema/zod3-to-json-schema/refs.test.ts +919 -0
- package/src/to-json-schema/zod3-to-json-schema/refs.ts +39 -0
- package/src/to-json-schema/zod3-to-json-schema/select-parser.ts +115 -0
- package/src/to-json-schema/zod3-to-json-schema/zod3-to-json-schema.test.ts +862 -0
- package/src/to-json-schema/zod3-to-json-schema/zod3-to-json-schema.ts +93 -0
- package/src/types/assistant-model-message.ts +39 -0
- package/src/types/content-part.ts +379 -0
- package/src/types/data-content.ts +4 -0
- package/src/types/execute-tool.ts +27 -0
- package/src/types/index.ts +40 -0
- package/src/types/model-message.ts +14 -0
- package/src/types/provider-options.ts +9 -0
- package/src/types/system-model-message.ts +20 -0
- package/src/types/tool-approval-request.ts +16 -0
- package/src/types/tool-approval-response.ts +27 -0
- package/src/types/tool-call.ts +31 -0
- package/src/types/tool-model-message.ts +23 -0
- package/src/types/tool-result.ts +35 -0
- package/src/types/tool.test-d.ts +193 -0
- package/src/types/tool.ts +324 -0
- package/src/types/user-model-message.ts +22 -0
- package/src/uint8-utils.ts +26 -0
- package/src/validate-types.test.ts +105 -0
- package/src/validate-types.ts +81 -0
- package/src/version.ts +6 -0
- package/src/with-user-agent-suffix.test.ts +84 -0
- package/src/with-user-agent-suffix.ts +27 -0
- package/src/without-trailing-slash.ts +3 -0
- package/LICENSE +0 -13
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import * as z4 from 'zod/v4';
|
|
3
|
+
import { safeParseJSON } from './parse-json';
|
|
4
|
+
import { asSchema, StandardSchema, zodSchema } from './schema';
|
|
5
|
+
|
|
6
|
+
describe('zodSchema', () => {
|
|
7
|
+
describe('zod/v4', () => {
|
|
8
|
+
describe('json schema conversion', () => {
|
|
9
|
+
it('should create a schema with simple types', () => {
|
|
10
|
+
const schema = zodSchema(
|
|
11
|
+
z4.object({
|
|
12
|
+
text: z4.string(),
|
|
13
|
+
number: z4.number(),
|
|
14
|
+
}),
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
expect(schema.jsonSchema).toMatchSnapshot();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should support optional fields in object', () => {
|
|
21
|
+
const schema = zodSchema(
|
|
22
|
+
z4.object({
|
|
23
|
+
required: z4.string(),
|
|
24
|
+
optional: z4.string().optional(),
|
|
25
|
+
}),
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
expect(schema.jsonSchema).toMatchSnapshot();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('should support optional fields with descriptions in object', () => {
|
|
32
|
+
const schema = zodSchema(
|
|
33
|
+
z4.object({
|
|
34
|
+
required: z4.string().describe('Required description'),
|
|
35
|
+
optional: z4.string().optional().describe('Optional description'),
|
|
36
|
+
}),
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
expect(schema.jsonSchema).toMatchSnapshot();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should support arrays', () => {
|
|
43
|
+
const schema = zodSchema(
|
|
44
|
+
z4.object({
|
|
45
|
+
items: z4.array(z4.string()),
|
|
46
|
+
}),
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
expect(schema.jsonSchema).toMatchSnapshot();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('should support optional arrays', () => {
|
|
53
|
+
const schema = zodSchema(
|
|
54
|
+
z4.object({
|
|
55
|
+
items: z4.array(z4.string()).optional(),
|
|
56
|
+
}),
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
expect(schema.jsonSchema).toMatchSnapshot();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should support required enums', () => {
|
|
63
|
+
const schema = zodSchema(
|
|
64
|
+
z4.object({
|
|
65
|
+
type: z4.enum(['a', 'b', 'c']),
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
expect(schema.jsonSchema).toMatchSnapshot();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should support optional enums', () => {
|
|
73
|
+
const schema = zodSchema(
|
|
74
|
+
z4.object({
|
|
75
|
+
type: z4.enum(['a', 'b', 'c']).optional(),
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
expect(schema.jsonSchema).toMatchSnapshot();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('should duplicate referenced schemas (and not use references) by default', () => {
|
|
83
|
+
const Inner = z4.object({
|
|
84
|
+
text: z4.string(),
|
|
85
|
+
number: z4.number(),
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const schema = zodSchema(
|
|
89
|
+
z4.object({
|
|
90
|
+
group1: z4.array(Inner),
|
|
91
|
+
group2: z4.array(Inner),
|
|
92
|
+
}),
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
expect(schema.jsonSchema).toMatchSnapshot();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('should use references when useReferences is true', () => {
|
|
99
|
+
const Inner = z4.object({
|
|
100
|
+
text: z4.string(),
|
|
101
|
+
number: z4.number(),
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
const schema = zodSchema(
|
|
105
|
+
z4.object({
|
|
106
|
+
group1: z4.array(Inner),
|
|
107
|
+
group2: z4.array(Inner),
|
|
108
|
+
}),
|
|
109
|
+
{ useReferences: true },
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
expect(schema.jsonSchema).toMatchSnapshot();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('should use recursive references with z.lazy when useReferences is true', () => {
|
|
116
|
+
const baseCategorySchema = z4.object({
|
|
117
|
+
name: z4.string(),
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
type Category = z4.infer<typeof baseCategorySchema> & {
|
|
121
|
+
subcategories: Category[];
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const categorySchema: z4.ZodType<Category> = baseCategorySchema.extend({
|
|
125
|
+
subcategories: z4.lazy(() => categorySchema.array()),
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const schema = zodSchema(
|
|
129
|
+
z4.object({
|
|
130
|
+
category: categorySchema,
|
|
131
|
+
}),
|
|
132
|
+
{ useReferences: true },
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
expect(schema.jsonSchema).toMatchSnapshot();
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('should support nullable', () => {
|
|
139
|
+
const schema = zodSchema(
|
|
140
|
+
z4.object({
|
|
141
|
+
location: z4.string().nullable(),
|
|
142
|
+
}),
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
expect(schema.jsonSchema).toMatchSnapshot();
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('.literal and .enum', () => {
|
|
149
|
+
const schema = zodSchema(
|
|
150
|
+
z4.object({
|
|
151
|
+
text: z4.literal('hello'),
|
|
152
|
+
number: z4.enum(['one', 'two', 'three']),
|
|
153
|
+
}),
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
expect(schema.jsonSchema).toMatchSnapshot();
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('should generate JSON schema for input when transform is used', async () => {
|
|
160
|
+
const schema = zodSchema(
|
|
161
|
+
z4.object({
|
|
162
|
+
user: z4.object({
|
|
163
|
+
id: z4
|
|
164
|
+
.string()
|
|
165
|
+
.transform(val => parseInt(val, 10))
|
|
166
|
+
.pipe(z4.number()),
|
|
167
|
+
name: z4.string(),
|
|
168
|
+
}),
|
|
169
|
+
}),
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
expect(schema.jsonSchema).toMatchSnapshot();
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
describe('output validation', () => {
|
|
177
|
+
it('should validate output with transform', async () => {
|
|
178
|
+
const schema = zodSchema(
|
|
179
|
+
z4.object({
|
|
180
|
+
user: z4.object({
|
|
181
|
+
id: z4
|
|
182
|
+
.string()
|
|
183
|
+
.transform(val => parseInt(val, 10))
|
|
184
|
+
.pipe(z4.number()),
|
|
185
|
+
name: z4.string(),
|
|
186
|
+
}),
|
|
187
|
+
}),
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
const result = await safeParseJSON({
|
|
191
|
+
text: '{"user": {"id": "123", "name": "John"}}',
|
|
192
|
+
schema,
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
expect(result).toStrictEqual({
|
|
196
|
+
success: true,
|
|
197
|
+
value: { user: { id: 123, name: 'John' } },
|
|
198
|
+
rawValue: { user: { id: '123', name: 'John' } },
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
describe('StandardSchema (StandardJSONSchemaV1)', () => {
|
|
206
|
+
// Helper to create a StandardSchema mock
|
|
207
|
+
function createStandardSchema<T>(options: {
|
|
208
|
+
jsonSchema: object;
|
|
209
|
+
validate: (value: unknown) => Promise<{ value: T } | { issues: Error[] }>;
|
|
210
|
+
}): StandardSchema<T> {
|
|
211
|
+
return {
|
|
212
|
+
'~standard': {
|
|
213
|
+
version: 1,
|
|
214
|
+
vendor: 'custom',
|
|
215
|
+
validate: options.validate,
|
|
216
|
+
jsonSchema: {
|
|
217
|
+
input: () => options.jsonSchema,
|
|
218
|
+
output: () => options.jsonSchema,
|
|
219
|
+
},
|
|
220
|
+
},
|
|
221
|
+
} as StandardSchema<T>;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
describe('json schema conversion', () => {
|
|
225
|
+
it('should return the JSON schema from input()', async () => {
|
|
226
|
+
const standardSchema = createStandardSchema<{
|
|
227
|
+
name: string;
|
|
228
|
+
age: number;
|
|
229
|
+
}>({
|
|
230
|
+
jsonSchema: {
|
|
231
|
+
type: 'object',
|
|
232
|
+
properties: {
|
|
233
|
+
name: { type: 'string' },
|
|
234
|
+
age: { type: 'number' },
|
|
235
|
+
},
|
|
236
|
+
required: ['name', 'age'],
|
|
237
|
+
},
|
|
238
|
+
validate: async value => ({
|
|
239
|
+
value: value as { name: string; age: number },
|
|
240
|
+
}),
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
const schema = asSchema(standardSchema);
|
|
244
|
+
|
|
245
|
+
expect(await schema.jsonSchema).toStrictEqual({
|
|
246
|
+
type: 'object',
|
|
247
|
+
additionalProperties: false,
|
|
248
|
+
properties: {
|
|
249
|
+
name: { type: 'string' },
|
|
250
|
+
age: { type: 'number' },
|
|
251
|
+
},
|
|
252
|
+
required: ['name', 'age'],
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it('should pass target draft-07 to jsonSchema.input()', async () => {
|
|
257
|
+
let capturedTarget: string | undefined;
|
|
258
|
+
|
|
259
|
+
const standardSchema: StandardSchema<{ text: string }> = {
|
|
260
|
+
'~standard': {
|
|
261
|
+
version: 1,
|
|
262
|
+
vendor: 'custom',
|
|
263
|
+
validate: async value => ({ value: value as { text: string } }),
|
|
264
|
+
jsonSchema: {
|
|
265
|
+
input: (options: { target?: string } = {}) => {
|
|
266
|
+
capturedTarget = options.target;
|
|
267
|
+
return {
|
|
268
|
+
type: 'object',
|
|
269
|
+
properties: { text: { type: 'string' } },
|
|
270
|
+
};
|
|
271
|
+
},
|
|
272
|
+
output: () => ({
|
|
273
|
+
type: 'object',
|
|
274
|
+
properties: { text: { type: 'string' } },
|
|
275
|
+
}),
|
|
276
|
+
},
|
|
277
|
+
},
|
|
278
|
+
} as StandardSchema<{ text: string }>;
|
|
279
|
+
|
|
280
|
+
const schema = asSchema(standardSchema);
|
|
281
|
+
const jsonSchema = await schema.jsonSchema;
|
|
282
|
+
|
|
283
|
+
expect(capturedTarget).toBe('draft-07');
|
|
284
|
+
expect(jsonSchema).toStrictEqual({
|
|
285
|
+
type: 'object',
|
|
286
|
+
additionalProperties: false,
|
|
287
|
+
properties: { text: { type: 'string' } },
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it('should support nested objects', async () => {
|
|
292
|
+
const standardSchema = createStandardSchema<{
|
|
293
|
+
user: { name: string; email: string };
|
|
294
|
+
}>({
|
|
295
|
+
jsonSchema: {
|
|
296
|
+
type: 'object',
|
|
297
|
+
properties: {
|
|
298
|
+
user: {
|
|
299
|
+
type: 'object',
|
|
300
|
+
properties: {
|
|
301
|
+
name: { type: 'string' },
|
|
302
|
+
email: { type: 'string' },
|
|
303
|
+
},
|
|
304
|
+
required: ['name', 'email'],
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
required: ['user'],
|
|
308
|
+
},
|
|
309
|
+
validate: async value => ({
|
|
310
|
+
value: value as { user: { name: string; email: string } },
|
|
311
|
+
}),
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
const schema = asSchema(standardSchema);
|
|
315
|
+
|
|
316
|
+
expect(await schema.jsonSchema).toStrictEqual({
|
|
317
|
+
type: 'object',
|
|
318
|
+
additionalProperties: false,
|
|
319
|
+
properties: {
|
|
320
|
+
user: {
|
|
321
|
+
type: 'object',
|
|
322
|
+
additionalProperties: false,
|
|
323
|
+
properties: {
|
|
324
|
+
name: { type: 'string' },
|
|
325
|
+
email: { type: 'string' },
|
|
326
|
+
},
|
|
327
|
+
required: ['name', 'email'],
|
|
328
|
+
},
|
|
329
|
+
},
|
|
330
|
+
required: ['user'],
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it('should support arrays', async () => {
|
|
335
|
+
const standardSchema = createStandardSchema<{ items: string[] }>({
|
|
336
|
+
jsonSchema: {
|
|
337
|
+
type: 'object',
|
|
338
|
+
properties: {
|
|
339
|
+
items: {
|
|
340
|
+
type: 'array',
|
|
341
|
+
items: { type: 'string' },
|
|
342
|
+
},
|
|
343
|
+
},
|
|
344
|
+
required: ['items'],
|
|
345
|
+
},
|
|
346
|
+
validate: async value => ({ value: value as { items: string[] } }),
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
const schema = asSchema(standardSchema);
|
|
350
|
+
|
|
351
|
+
expect(await schema.jsonSchema).toStrictEqual({
|
|
352
|
+
type: 'object',
|
|
353
|
+
additionalProperties: false,
|
|
354
|
+
properties: {
|
|
355
|
+
items: {
|
|
356
|
+
type: 'array',
|
|
357
|
+
items: { type: 'string' },
|
|
358
|
+
},
|
|
359
|
+
},
|
|
360
|
+
required: ['items'],
|
|
361
|
+
});
|
|
362
|
+
});
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
describe('output validation', () => {
|
|
366
|
+
it('should validate and return value for valid input', async () => {
|
|
367
|
+
const standardSchema = createStandardSchema<{
|
|
368
|
+
name: string;
|
|
369
|
+
age: number;
|
|
370
|
+
}>({
|
|
371
|
+
jsonSchema: {
|
|
372
|
+
type: 'object',
|
|
373
|
+
properties: {
|
|
374
|
+
name: { type: 'string' },
|
|
375
|
+
age: { type: 'number' },
|
|
376
|
+
},
|
|
377
|
+
},
|
|
378
|
+
validate: async value => {
|
|
379
|
+
const obj = value as any;
|
|
380
|
+
if (
|
|
381
|
+
typeof obj === 'object' &&
|
|
382
|
+
obj !== null &&
|
|
383
|
+
typeof obj.name === 'string' &&
|
|
384
|
+
typeof obj.age === 'number'
|
|
385
|
+
) {
|
|
386
|
+
return { value: obj };
|
|
387
|
+
}
|
|
388
|
+
return { issues: [new Error('Invalid input')] };
|
|
389
|
+
},
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
const schema = asSchema(standardSchema);
|
|
393
|
+
const result = await schema.validate!({ name: 'John', age: 30 });
|
|
394
|
+
|
|
395
|
+
expect(result).toStrictEqual({
|
|
396
|
+
success: true,
|
|
397
|
+
value: { name: 'John', age: 30 },
|
|
398
|
+
});
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
it('should return error for invalid input', async () => {
|
|
402
|
+
const standardSchema = createStandardSchema<{
|
|
403
|
+
name: string;
|
|
404
|
+
age: number;
|
|
405
|
+
}>({
|
|
406
|
+
jsonSchema: {
|
|
407
|
+
type: 'object',
|
|
408
|
+
properties: {
|
|
409
|
+
name: { type: 'string' },
|
|
410
|
+
age: { type: 'number' },
|
|
411
|
+
},
|
|
412
|
+
},
|
|
413
|
+
validate: async value => {
|
|
414
|
+
const obj = value as any;
|
|
415
|
+
if (
|
|
416
|
+
typeof obj === 'object' &&
|
|
417
|
+
obj !== null &&
|
|
418
|
+
typeof obj.name === 'string' &&
|
|
419
|
+
typeof obj.age === 'number'
|
|
420
|
+
) {
|
|
421
|
+
return { value: obj };
|
|
422
|
+
}
|
|
423
|
+
return { issues: [new Error('Invalid input')] };
|
|
424
|
+
},
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
const schema = asSchema(standardSchema);
|
|
428
|
+
const result = await schema.validate!({
|
|
429
|
+
name: 'John',
|
|
430
|
+
age: 'not a number',
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
expect(result.success).toBe(false);
|
|
434
|
+
if (!result.success) {
|
|
435
|
+
expect(result.error).toBeInstanceOf(Error);
|
|
436
|
+
expect(result.error.message).toContain('Type validation failed');
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
it('should support transform in validation', async () => {
|
|
441
|
+
const standardSchema = createStandardSchema<{ id: number; name: string }>(
|
|
442
|
+
{
|
|
443
|
+
jsonSchema: {
|
|
444
|
+
type: 'object',
|
|
445
|
+
properties: {
|
|
446
|
+
id: { type: 'string' }, // Input is string
|
|
447
|
+
name: { type: 'string' },
|
|
448
|
+
},
|
|
449
|
+
},
|
|
450
|
+
validate: async value => {
|
|
451
|
+
const obj = value as any;
|
|
452
|
+
// Transform string id to number
|
|
453
|
+
return {
|
|
454
|
+
value: {
|
|
455
|
+
id: parseInt(obj.id, 10),
|
|
456
|
+
name: obj.name,
|
|
457
|
+
},
|
|
458
|
+
};
|
|
459
|
+
},
|
|
460
|
+
},
|
|
461
|
+
);
|
|
462
|
+
|
|
463
|
+
const schema = asSchema(standardSchema);
|
|
464
|
+
const result = await schema.validate!({ id: '123', name: 'John' });
|
|
465
|
+
|
|
466
|
+
expect(result).toStrictEqual({
|
|
467
|
+
success: true,
|
|
468
|
+
value: { id: 123, name: 'John' },
|
|
469
|
+
});
|
|
470
|
+
});
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
describe('asSchema detection', () => {
|
|
474
|
+
it('should detect non-zod standard schema by vendor', async () => {
|
|
475
|
+
const standardSchema: StandardSchema<{ text: string }> = {
|
|
476
|
+
'~standard': {
|
|
477
|
+
version: 1,
|
|
478
|
+
vendor: 'valibot', // non-zod vendor
|
|
479
|
+
validate: async value => ({ value: value as { text: string } }),
|
|
480
|
+
jsonSchema: {
|
|
481
|
+
input: () => ({
|
|
482
|
+
type: 'object',
|
|
483
|
+
properties: { text: { type: 'string' } },
|
|
484
|
+
}),
|
|
485
|
+
output: () => ({
|
|
486
|
+
type: 'object',
|
|
487
|
+
properties: { text: { type: 'string' } },
|
|
488
|
+
}),
|
|
489
|
+
},
|
|
490
|
+
},
|
|
491
|
+
} as StandardSchema<{ text: string }>;
|
|
492
|
+
|
|
493
|
+
const schema = asSchema(standardSchema);
|
|
494
|
+
|
|
495
|
+
expect(await schema.jsonSchema).toStrictEqual({
|
|
496
|
+
type: 'object',
|
|
497
|
+
additionalProperties: false,
|
|
498
|
+
properties: { text: { type: 'string' } },
|
|
499
|
+
});
|
|
500
|
+
});
|
|
501
|
+
});
|
|
502
|
+
});
|