@ai-sdk/provider-utils 4.0.8 → 4.0.9
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 +6 -2
- package/src/__snapshots__/schema.test.ts.snap +0 -346
- package/src/add-additional-properties-to-json-schema.test.ts +0 -289
- package/src/convert-async-iterator-to-readable-stream.test.ts +0 -78
- package/src/convert-image-model-file-to-data-uri.test.ts +0 -85
- package/src/convert-to-form-data.test.ts +0 -167
- package/src/create-tool-name-mapping.test.ts +0 -163
- package/src/delay.test.ts +0 -212
- package/src/delayed-promise.test.ts +0 -132
- package/src/download-blob.test.ts +0 -145
- package/src/generate-id.test.ts +0 -31
- package/src/get-from-api.test.ts +0 -199
- package/src/get-runtime-environment-user-agent.test.ts +0 -47
- package/src/inject-json-instruction.test.ts +0 -404
- package/src/is-url-supported.test.ts +0 -282
- package/src/media-type-to-extension.test.ts +0 -26
- package/src/normalize-headers.test.ts +0 -64
- package/src/parse-json.test.ts +0 -191
- package/src/remove-undefined-entries.test.ts +0 -57
- package/src/resolve.test.ts +0 -125
- package/src/response-handler.test.ts +0 -89
- package/src/schema.test-d.ts +0 -11
- package/src/schema.test.ts +0 -502
- package/src/secure-json-parse.test.ts +0 -59
- package/src/to-json-schema/zod3-to-json-schema/parse-def.test.ts +0 -224
- package/src/to-json-schema/zod3-to-json-schema/parsers/array.test.ts +0 -98
- package/src/to-json-schema/zod3-to-json-schema/parsers/bigint.test.ts +0 -51
- package/src/to-json-schema/zod3-to-json-schema/parsers/branded.test.ts +0 -16
- package/src/to-json-schema/zod3-to-json-schema/parsers/catch.test.ts +0 -15
- package/src/to-json-schema/zod3-to-json-schema/parsers/date.test.ts +0 -97
- package/src/to-json-schema/zod3-to-json-schema/parsers/default.test.ts +0 -54
- package/src/to-json-schema/zod3-to-json-schema/parsers/effects.test.ts +0 -41
- package/src/to-json-schema/zod3-to-json-schema/parsers/intersection.test.ts +0 -92
- package/src/to-json-schema/zod3-to-json-schema/parsers/map.test.ts +0 -48
- package/src/to-json-schema/zod3-to-json-schema/parsers/native-enum.test.ts +0 -102
- package/src/to-json-schema/zod3-to-json-schema/parsers/nullable.test.ts +0 -67
- package/src/to-json-schema/zod3-to-json-schema/parsers/number.test.ts +0 -65
- package/src/to-json-schema/zod3-to-json-schema/parsers/object.test.ts +0 -149
- package/src/to-json-schema/zod3-to-json-schema/parsers/optional.test.ts +0 -147
- package/src/to-json-schema/zod3-to-json-schema/parsers/pipe.test.ts +0 -35
- package/src/to-json-schema/zod3-to-json-schema/parsers/promise.test.ts +0 -15
- package/src/to-json-schema/zod3-to-json-schema/parsers/readonly.test.ts +0 -20
- package/src/to-json-schema/zod3-to-json-schema/parsers/record.test.ts +0 -108
- package/src/to-json-schema/zod3-to-json-schema/parsers/set.test.ts +0 -20
- package/src/to-json-schema/zod3-to-json-schema/parsers/string.test.ts +0 -438
- package/src/to-json-schema/zod3-to-json-schema/parsers/tuple.test.ts +0 -33
- package/src/to-json-schema/zod3-to-json-schema/parsers/union.test.ts +0 -226
- package/src/to-json-schema/zod3-to-json-schema/refs.test.ts +0 -919
- package/src/to-json-schema/zod3-to-json-schema/zod3-to-json-schema.test.ts +0 -862
- package/src/types/tool.test-d.ts +0 -228
- package/src/validate-types.test.ts +0 -105
- package/src/with-user-agent-suffix.test.ts +0 -84
package/src/schema.test.ts
DELETED
|
@@ -1,502 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
// Licensed under BSD-3-Clause (this file only)
|
|
2
|
-
// Code adapted from https://github.com/fastify/secure-json-parse/blob/783fcb1b5434709466759847cec974381939673a/test/index.test.js
|
|
3
|
-
//
|
|
4
|
-
// Copyright (c) Vercel, Inc. (https://vercel.com)
|
|
5
|
-
// Copyright (c) 2019 The Fastify Team
|
|
6
|
-
// Copyright (c) 2019, Sideway Inc, and project contributors
|
|
7
|
-
// All rights reserved.
|
|
8
|
-
//
|
|
9
|
-
// The complete list of contributors can be found at:
|
|
10
|
-
// - https://github.com/hapijs/bourne/graphs/contributors
|
|
11
|
-
// - https://github.com/fastify/secure-json-parse/graphs/contributors
|
|
12
|
-
// - https://github.com/vercel/ai/commits/main/packages/provider-utils/src/secure-parse-json.test.ts
|
|
13
|
-
//
|
|
14
|
-
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
15
|
-
//
|
|
16
|
-
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
17
|
-
//
|
|
18
|
-
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
19
|
-
//
|
|
20
|
-
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
21
|
-
//
|
|
22
|
-
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
23
|
-
|
|
24
|
-
import { describe, it, expect } from 'vitest';
|
|
25
|
-
import { secureJsonParse } from './secure-json-parse';
|
|
26
|
-
|
|
27
|
-
describe('secureJsonParse', () => {
|
|
28
|
-
it('parses object string', () => {
|
|
29
|
-
expect(secureJsonParse('{"a": 5, "b": 6}')).toStrictEqual(
|
|
30
|
-
JSON.parse('{"a": 5, "b": 6}'),
|
|
31
|
-
);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it('parses null string', () => {
|
|
35
|
-
expect(secureJsonParse('null')).toStrictEqual(JSON.parse('null'));
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it('parses 0 string', () => {
|
|
39
|
-
expect(secureJsonParse('0')).toStrictEqual(JSON.parse('0'));
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it('parses string string', () => {
|
|
43
|
-
expect(secureJsonParse('"X"')).toStrictEqual(JSON.parse('"X"'));
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('errors on constructor property', () => {
|
|
47
|
-
const text =
|
|
48
|
-
'{ "a": 5, "b": 6, "constructor": { "x": 7 }, "c": { "d": 0, "e": "text", "__proto__": { "y": 8 }, "f": { "g": 2 } } }';
|
|
49
|
-
|
|
50
|
-
expect(() => secureJsonParse(text)).toThrow(SyntaxError);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it('errors on proto property', () => {
|
|
54
|
-
const text =
|
|
55
|
-
'{ "a": 5, "b": 6, "__proto__": { "x": 7 }, "c": { "d": 0, "e": "text", "__proto__": { "y": 8 }, "f": { "g": 2 } } }';
|
|
56
|
-
|
|
57
|
-
expect(() => secureJsonParse(text)).toThrow(SyntaxError);
|
|
58
|
-
});
|
|
59
|
-
});
|