@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.
Files changed (55) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/index.js +1 -1
  3. package/dist/index.mjs +1 -1
  4. package/package.json +6 -2
  5. package/src/__snapshots__/schema.test.ts.snap +0 -346
  6. package/src/add-additional-properties-to-json-schema.test.ts +0 -289
  7. package/src/convert-async-iterator-to-readable-stream.test.ts +0 -78
  8. package/src/convert-image-model-file-to-data-uri.test.ts +0 -85
  9. package/src/convert-to-form-data.test.ts +0 -167
  10. package/src/create-tool-name-mapping.test.ts +0 -163
  11. package/src/delay.test.ts +0 -212
  12. package/src/delayed-promise.test.ts +0 -132
  13. package/src/download-blob.test.ts +0 -145
  14. package/src/generate-id.test.ts +0 -31
  15. package/src/get-from-api.test.ts +0 -199
  16. package/src/get-runtime-environment-user-agent.test.ts +0 -47
  17. package/src/inject-json-instruction.test.ts +0 -404
  18. package/src/is-url-supported.test.ts +0 -282
  19. package/src/media-type-to-extension.test.ts +0 -26
  20. package/src/normalize-headers.test.ts +0 -64
  21. package/src/parse-json.test.ts +0 -191
  22. package/src/remove-undefined-entries.test.ts +0 -57
  23. package/src/resolve.test.ts +0 -125
  24. package/src/response-handler.test.ts +0 -89
  25. package/src/schema.test-d.ts +0 -11
  26. package/src/schema.test.ts +0 -502
  27. package/src/secure-json-parse.test.ts +0 -59
  28. package/src/to-json-schema/zod3-to-json-schema/parse-def.test.ts +0 -224
  29. package/src/to-json-schema/zod3-to-json-schema/parsers/array.test.ts +0 -98
  30. package/src/to-json-schema/zod3-to-json-schema/parsers/bigint.test.ts +0 -51
  31. package/src/to-json-schema/zod3-to-json-schema/parsers/branded.test.ts +0 -16
  32. package/src/to-json-schema/zod3-to-json-schema/parsers/catch.test.ts +0 -15
  33. package/src/to-json-schema/zod3-to-json-schema/parsers/date.test.ts +0 -97
  34. package/src/to-json-schema/zod3-to-json-schema/parsers/default.test.ts +0 -54
  35. package/src/to-json-schema/zod3-to-json-schema/parsers/effects.test.ts +0 -41
  36. package/src/to-json-schema/zod3-to-json-schema/parsers/intersection.test.ts +0 -92
  37. package/src/to-json-schema/zod3-to-json-schema/parsers/map.test.ts +0 -48
  38. package/src/to-json-schema/zod3-to-json-schema/parsers/native-enum.test.ts +0 -102
  39. package/src/to-json-schema/zod3-to-json-schema/parsers/nullable.test.ts +0 -67
  40. package/src/to-json-schema/zod3-to-json-schema/parsers/number.test.ts +0 -65
  41. package/src/to-json-schema/zod3-to-json-schema/parsers/object.test.ts +0 -149
  42. package/src/to-json-schema/zod3-to-json-schema/parsers/optional.test.ts +0 -147
  43. package/src/to-json-schema/zod3-to-json-schema/parsers/pipe.test.ts +0 -35
  44. package/src/to-json-schema/zod3-to-json-schema/parsers/promise.test.ts +0 -15
  45. package/src/to-json-schema/zod3-to-json-schema/parsers/readonly.test.ts +0 -20
  46. package/src/to-json-schema/zod3-to-json-schema/parsers/record.test.ts +0 -108
  47. package/src/to-json-schema/zod3-to-json-schema/parsers/set.test.ts +0 -20
  48. package/src/to-json-schema/zod3-to-json-schema/parsers/string.test.ts +0 -438
  49. package/src/to-json-schema/zod3-to-json-schema/parsers/tuple.test.ts +0 -33
  50. package/src/to-json-schema/zod3-to-json-schema/parsers/union.test.ts +0 -226
  51. package/src/to-json-schema/zod3-to-json-schema/refs.test.ts +0 -919
  52. package/src/to-json-schema/zod3-to-json-schema/zod3-to-json-schema.test.ts +0 -862
  53. package/src/types/tool.test-d.ts +0 -228
  54. package/src/validate-types.test.ts +0 -105
  55. package/src/with-user-agent-suffix.test.ts +0 -84
@@ -1,224 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { z } from 'zod/v3';
3
- import { parseDef } from './parse-def';
4
- import { getRefs } from './refs';
5
- import { JSONSchema7 } from '@ai-sdk/provider';
6
-
7
- describe('parseDef', () => {
8
- it('should return a proper json schema with some common types without validation', () => {
9
- const zodSchema = z.object({
10
- requiredString: z.string(),
11
- optionalString: z.string().optional(),
12
- literalString: z.literal('literalStringValue'),
13
- stringArray: z.array(z.string()),
14
- stringEnum: z.enum(['stringEnumOptionA', 'stringEnumOptionB']),
15
- tuple: z.tuple([z.string(), z.number(), z.boolean()]),
16
- record: z.record(z.boolean()),
17
- requiredNumber: z.number(),
18
- optionalNumber: z.number().optional(),
19
- numberOrNull: z.number().nullable(),
20
- numberUnion: z.union([z.literal(1), z.literal(2), z.literal(3)]),
21
- mixedUnion: z.union([
22
- z.literal('abc'),
23
- z.literal(123),
24
- z.object({ nowItGetsAnnoying: z.literal(true) }),
25
- ]),
26
- objectOrNull: z.object({ myString: z.string() }).nullable(),
27
- passthrough: z.object({ myString: z.string() }).passthrough(),
28
- });
29
-
30
- const parsedSchema = parseDef(zodSchema._def, getRefs());
31
-
32
- expect(parsedSchema).toStrictEqual({
33
- type: 'object',
34
- properties: {
35
- requiredString: {
36
- type: 'string',
37
- },
38
- optionalString: {
39
- type: 'string',
40
- },
41
- literalString: {
42
- type: 'string',
43
- const: 'literalStringValue',
44
- },
45
- stringArray: {
46
- type: 'array',
47
- items: {
48
- type: 'string',
49
- },
50
- },
51
- stringEnum: {
52
- type: 'string',
53
- enum: ['stringEnumOptionA', 'stringEnumOptionB'],
54
- },
55
- tuple: {
56
- type: 'array',
57
- minItems: 3,
58
- items: [
59
- {
60
- type: 'string',
61
- },
62
- {
63
- type: 'number',
64
- },
65
- {
66
- type: 'boolean',
67
- },
68
- ],
69
- maxItems: 3,
70
- },
71
- record: {
72
- type: 'object',
73
- additionalProperties: {
74
- type: 'boolean',
75
- },
76
- },
77
- requiredNumber: {
78
- type: 'number',
79
- },
80
- optionalNumber: {
81
- type: 'number',
82
- },
83
- numberOrNull: {
84
- type: ['number', 'null'],
85
- },
86
- numberUnion: {
87
- type: 'number',
88
- enum: [1, 2, 3],
89
- },
90
- mixedUnion: {
91
- anyOf: [
92
- {
93
- type: 'string',
94
- const: 'abc',
95
- },
96
- {
97
- type: 'number',
98
- const: 123,
99
- },
100
- {
101
- type: 'object',
102
- properties: {
103
- nowItGetsAnnoying: {
104
- type: 'boolean',
105
- const: true,
106
- },
107
- },
108
- required: ['nowItGetsAnnoying'],
109
- additionalProperties: false,
110
- },
111
- ],
112
- },
113
- objectOrNull: {
114
- anyOf: [
115
- {
116
- type: 'object',
117
- properties: {
118
- myString: {
119
- type: 'string',
120
- },
121
- },
122
- required: ['myString'],
123
- additionalProperties: false,
124
- },
125
- {
126
- type: 'null',
127
- },
128
- ],
129
- },
130
- passthrough: {
131
- type: 'object',
132
- properties: {
133
- myString: {
134
- type: 'string',
135
- },
136
- },
137
- required: ['myString'],
138
- additionalProperties: true,
139
- },
140
- },
141
- required: [
142
- 'requiredString',
143
- 'literalString',
144
- 'stringArray',
145
- 'stringEnum',
146
- 'tuple',
147
- 'record',
148
- 'requiredNumber',
149
- 'numberOrNull',
150
- 'numberUnion',
151
- 'mixedUnion',
152
- 'objectOrNull',
153
- 'passthrough',
154
- ],
155
- additionalProperties: false,
156
- } satisfies JSONSchema7);
157
- });
158
-
159
- it('should handle a nullable string properly', () => {
160
- const shorthand = z.string().nullable();
161
- const union = z.union([z.string(), z.null()]);
162
-
163
- expect(parseDef(shorthand._def, getRefs())).toStrictEqual({
164
- type: ['string', 'null'],
165
- } satisfies JSONSchema7);
166
- expect(parseDef(union._def, getRefs())).toStrictEqual({
167
- type: ['string', 'null'],
168
- } satisfies JSONSchema7);
169
- });
170
-
171
- it('should be possible to use branded string', () => {
172
- const schema = z.string().brand<'x'>();
173
- const parsedSchema = parseDef(schema._def, getRefs());
174
-
175
- expect(parsedSchema).toStrictEqual({
176
- type: 'string',
177
- } satisfies JSONSchema7);
178
- });
179
-
180
- it('should be possible to use readonly', () => {
181
- const parsedSchema = parseDef(z.object({}).readonly()._def, getRefs());
182
-
183
- expect(parsedSchema).toStrictEqual({
184
- type: 'object',
185
- properties: {},
186
- additionalProperties: false,
187
- } satisfies JSONSchema7);
188
- });
189
-
190
- it('should be possible to use catch', () => {
191
- const parsedSchema = parseDef(z.number().catch(5)._def, getRefs());
192
-
193
- expect(parsedSchema).toStrictEqual({
194
- type: 'number',
195
- } satisfies JSONSchema7);
196
- });
197
-
198
- it('should be possible to use pipeline', () => {
199
- const schema = z.number().pipe(z.number().int());
200
-
201
- expect(parseDef(schema._def, getRefs())).toStrictEqual({
202
- allOf: [{ type: 'number' }, { type: 'integer' }],
203
- } satisfies JSONSchema7);
204
- });
205
-
206
- it('should get undefined for function', () => {
207
- const parsedSchema = parseDef(z.function()._def, getRefs());
208
- expect(parsedSchema).toBeUndefined();
209
- });
210
-
211
- it('should get undefined for void', () => {
212
- const parsedSchema = parseDef(z.void()._def, getRefs());
213
- expect(parsedSchema).toBeUndefined();
214
- });
215
-
216
- it('nested lazy', () => {
217
- const zodSchema = z.lazy(() => z.lazy(() => z.string()));
218
- const parsed = parseDef(zodSchema._def, getRefs());
219
-
220
- expect(parsed).toStrictEqual({
221
- type: 'string',
222
- } satisfies JSONSchema7);
223
- });
224
- });
@@ -1,98 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { JSONSchema7 } from '@ai-sdk/provider';
3
- import { z } from 'zod/v3';
4
- import { parseArrayDef } from './array';
5
- import { getRefs } from '../refs';
6
-
7
- describe('array', () => {
8
- it('should be possible to describe a simple array', () => {
9
- const parsedSchema = parseArrayDef(z.array(z.string())._def, getRefs());
10
-
11
- expect(parsedSchema).toStrictEqual({
12
- type: 'array',
13
- items: {
14
- type: 'string',
15
- },
16
- } satisfies JSONSchema7);
17
- });
18
-
19
- it('should be possible to describe a simple array with any item', () => {
20
- const parsedSchema = parseArrayDef(z.array(z.any())._def, getRefs());
21
-
22
- expect(parsedSchema).toStrictEqual({
23
- type: 'array',
24
- } satisfies JSONSchema7);
25
- });
26
-
27
- it('should be possible to describe a string array with a minimum and maximum length', () => {
28
- const parsedSchema = parseArrayDef(
29
- z.array(z.string()).min(2).max(4)._def,
30
- getRefs(),
31
- );
32
-
33
- expect(parsedSchema).toStrictEqual({
34
- type: 'array',
35
- items: {
36
- type: 'string',
37
- },
38
- minItems: 2,
39
- maxItems: 4,
40
- } satisfies JSONSchema7);
41
- });
42
-
43
- it('should be possible to describe a string array with an exact length', () => {
44
- const parsedSchema = parseArrayDef(
45
- z.array(z.string()).length(5)._def,
46
- getRefs(),
47
- );
48
-
49
- expect(parsedSchema).toStrictEqual({
50
- type: 'array',
51
- items: {
52
- type: 'string',
53
- },
54
- minItems: 5,
55
- maxItems: 5,
56
- } satisfies JSONSchema7);
57
- });
58
-
59
- it('should be possible to describe a string array with a minimum length of 1 by using nonempty', () => {
60
- const parsedSchema = parseArrayDef(
61
- z.array(z.any()).nonempty()._def,
62
- getRefs(),
63
- );
64
-
65
- expect(parsedSchema).toStrictEqual({
66
- type: 'array',
67
- minItems: 1,
68
- } satisfies JSONSchema7);
69
- });
70
-
71
- it('should be possible do properly reference array items', () => {
72
- const willHaveBeenSeen = z.object({ hello: z.string() });
73
- const unionSchema = z.union([willHaveBeenSeen, willHaveBeenSeen]);
74
- const arraySchema = z.array(unionSchema);
75
- const jsonSchema = parseArrayDef(arraySchema._def, getRefs());
76
-
77
- expect(jsonSchema).toStrictEqual({
78
- items: {
79
- anyOf: [
80
- {
81
- additionalProperties: false,
82
- properties: {
83
- hello: {
84
- type: 'string',
85
- },
86
- },
87
- required: ['hello'],
88
- type: 'object',
89
- },
90
- {
91
- $ref: '#/items/anyOf/0',
92
- },
93
- ],
94
- },
95
- type: 'array',
96
- } satisfies JSONSchema7);
97
- });
98
- });
@@ -1,51 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { JSONSchema7 } from '@ai-sdk/provider';
3
- import { z } from 'zod/v3';
4
- import { parseBigintDef } from './bigint';
5
-
6
- describe('bigint', () => {
7
- it('should be possible to use bigint', () => {
8
- const parsedSchema = parseBigintDef(z.bigint()._def);
9
-
10
- expect(parsedSchema).toStrictEqual({
11
- type: 'integer',
12
- format: 'int64',
13
- } satisfies JSONSchema7);
14
- });
15
-
16
- it('should be possible to define gt/lt', () => {
17
- const parsedSchema = parseBigintDef(
18
- z.bigint().gte(BigInt(10)).lte(BigInt(20))._def,
19
- );
20
-
21
- expect(parsedSchema).toStrictEqual({
22
- type: 'integer',
23
- format: 'int64',
24
- minimum: BigInt(10) as any, // json schema type does not support bigint
25
- maximum: BigInt(20) as any, // json schema type does not support bigint
26
- } satisfies JSONSchema7);
27
- });
28
-
29
- it('should be possible to define gt/lt', () => {
30
- const parsedSchema = parseBigintDef(
31
- z.bigint().gt(BigInt(10)).lt(BigInt(20))._def,
32
- );
33
-
34
- expect(parsedSchema).toStrictEqual({
35
- type: 'integer',
36
- format: 'int64',
37
- exclusiveMinimum: BigInt(10) as any, // json schema type does not support bigint
38
- exclusiveMaximum: BigInt(20) as any, // json schema type does not support bigint
39
- } satisfies JSONSchema7);
40
- });
41
-
42
- it('should be possible to define multipleOf', () => {
43
- const parsedSchema = parseBigintDef(z.bigint().multipleOf(BigInt(5))._def);
44
-
45
- expect(parsedSchema).toStrictEqual({
46
- type: 'integer',
47
- format: 'int64',
48
- multipleOf: BigInt(5) as any, // json schema type does not support bigint
49
- } satisfies JSONSchema7);
50
- });
51
- });
@@ -1,16 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { z } from 'zod/v3';
3
- import { parseBrandedDef } from './branded';
4
- import { getRefs } from '../refs';
5
- import { JSONSchema7 } from '@ai-sdk/provider';
6
-
7
- describe('branded', () => {
8
- it('should be possible to use branded string', () => {
9
- const schema = z.string().brand<'x'>();
10
- const parsedSchema = parseBrandedDef(schema._def, getRefs());
11
-
12
- expect(parsedSchema).toStrictEqual({
13
- type: 'string',
14
- } satisfies JSONSchema7);
15
- });
16
- });
@@ -1,15 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { z } from 'zod/v3';
3
- import { parseCatchDef } from './catch';
4
- import { getRefs } from '../refs';
5
- import { JSONSchema7 } from '@ai-sdk/provider';
6
-
7
- describe('catch', () => {
8
- it('should be possible to use catch', () => {
9
- const parsedSchema = parseCatchDef(z.number().catch(5)._def, getRefs());
10
-
11
- expect(parsedSchema).toStrictEqual({
12
- type: 'number',
13
- } satisfies JSONSchema7);
14
- });
15
- });
@@ -1,97 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { z } from 'zod/v3';
3
- import { parseDateDef } from './date';
4
- import { getRefs } from '../refs';
5
- import { JSONSchema7 } from '@ai-sdk/provider';
6
-
7
- describe('Date validations', it => {
8
- it('should be possible to date as a string type', () => {
9
- const zodDateSchema = z.date();
10
- const parsedSchemaWithOption = parseDateDef(
11
- zodDateSchema._def,
12
- getRefs({ dateStrategy: 'string' }),
13
- );
14
- const parsedSchemaFromDefault = parseDateDef(zodDateSchema._def, getRefs());
15
-
16
- const jsonSchema: JSONSchema7 = {
17
- type: 'string',
18
- format: 'date-time',
19
- };
20
-
21
- expect(parsedSchemaWithOption).toStrictEqual(jsonSchema);
22
- expect(parsedSchemaFromDefault).toStrictEqual(jsonSchema);
23
- });
24
-
25
- it('should be possible to describe minimum date', () => {
26
- const zodDateSchema = z
27
- .date()
28
- .min(new Date('1970-01-02'), { message: 'Too old' });
29
- const parsedSchema = parseDateDef(
30
- zodDateSchema._def,
31
- getRefs({ dateStrategy: 'integer' }),
32
- );
33
-
34
- expect(parsedSchema).toStrictEqual({
35
- type: 'integer',
36
- format: 'unix-time',
37
- minimum: 86400000,
38
- } satisfies JSONSchema7);
39
- });
40
-
41
- it('should be possible to describe maximum date', () => {
42
- const zodDateSchema = z.date().max(new Date('1970-01-02'));
43
- const parsedSchema = parseDateDef(
44
- zodDateSchema._def,
45
- getRefs({ dateStrategy: 'integer' }),
46
- );
47
-
48
- expect(parsedSchema).toStrictEqual({
49
- type: 'integer',
50
- format: 'unix-time',
51
- maximum: 86400000,
52
- } satisfies JSONSchema7);
53
- });
54
-
55
- it('should be possible to describe both maximum and minimum date', () => {
56
- const zodDateSchema = z
57
- .date()
58
- .min(new Date('1970-01-02'))
59
- .max(new Date('1972-01-02'));
60
- const parsedSchema = parseDateDef(
61
- zodDateSchema._def,
62
- getRefs({ dateStrategy: 'integer' }),
63
- );
64
-
65
- expect(parsedSchema).toStrictEqual({
66
- type: 'integer',
67
- format: 'unix-time',
68
- minimum: 86400000,
69
- maximum: 63158400000,
70
- } satisfies JSONSchema7);
71
- });
72
-
73
- it('multiple choices of strategy should result in anyOf', () => {
74
- const zodDateSchema = z.date();
75
- const parsedSchema = parseDateDef(
76
- zodDateSchema._def,
77
- getRefs({ dateStrategy: ['format:date-time', 'format:date', 'integer'] }),
78
- );
79
-
80
- expect(parsedSchema).toStrictEqual({
81
- anyOf: [
82
- {
83
- type: 'string',
84
- format: 'date-time',
85
- },
86
- {
87
- type: 'string',
88
- format: 'date',
89
- },
90
- {
91
- type: 'integer',
92
- format: 'unix-time',
93
- },
94
- ],
95
- } satisfies JSONSchema7);
96
- });
97
- });
@@ -1,54 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { z } from 'zod/v3';
3
- import { parseDefaultDef } from './default';
4
- import { getRefs } from '../refs';
5
- import { JSONSchema7 } from '@ai-sdk/provider';
6
-
7
- describe('default', () => {
8
- it('should be possible to use default on objects', () => {
9
- const parsedSchema = parseDefaultDef(
10
- z.object({ foo: z.boolean() }).default({ foo: true })._def,
11
- getRefs(),
12
- );
13
-
14
- expect(parsedSchema).toStrictEqual({
15
- type: 'object',
16
- additionalProperties: false,
17
- required: ['foo'],
18
- properties: {
19
- foo: {
20
- type: 'boolean',
21
- },
22
- },
23
- default: {
24
- foo: true,
25
- },
26
- } satisfies JSONSchema7);
27
- });
28
-
29
- it('should be possible to use default on primitives', () => {
30
- const parsedSchema = parseDefaultDef(
31
- z.string().default('default')._def,
32
- getRefs(),
33
- );
34
-
35
- expect(parsedSchema).toStrictEqual({
36
- type: 'string',
37
- default: 'default',
38
- } satisfies JSONSchema7);
39
- });
40
-
41
- it('default with transform', () => {
42
- const stringWithDefault = z
43
- .string()
44
- .transform(val => val.toUpperCase())
45
- .default('default');
46
-
47
- const parsedSchema = parseDefaultDef(stringWithDefault._def, getRefs());
48
-
49
- expect(parsedSchema).toStrictEqual({
50
- type: 'string',
51
- default: 'default',
52
- } satisfies JSONSchema7);
53
- });
54
- });
@@ -1,41 +0,0 @@
1
- import { describe, it, expect, test } from 'vitest';
2
- import { z } from 'zod/v3';
3
- import { parseEffectsDef } from './effects';
4
- import { getRefs } from '../refs';
5
- import { JSONSchema7 } from '@ai-sdk/provider';
6
-
7
- describe('effects', () => {
8
- it('should be possible to use refine', () => {
9
- const parsedSchema = parseEffectsDef(
10
- z.number().refine(x => x + 1)._def,
11
- getRefs(),
12
- );
13
-
14
- expect(parsedSchema).toStrictEqual({
15
- type: 'number',
16
- } satisfies JSONSchema7);
17
- });
18
-
19
- it('should default to the input type', () => {
20
- const schema = z.string().transform(arg => parseInt(arg));
21
-
22
- const jsonSchema = parseEffectsDef(schema._def, getRefs());
23
-
24
- expect(jsonSchema).toStrictEqual({
25
- type: 'string',
26
- } satisfies JSONSchema7);
27
- });
28
-
29
- test("should return object based on 'any' strategy", () => {
30
- const schema = z.string().transform(arg => parseInt(arg));
31
-
32
- const jsonSchema = parseEffectsDef(
33
- schema._def,
34
- getRefs({
35
- effectStrategy: 'any',
36
- }),
37
- );
38
-
39
- expect(jsonSchema).toStrictEqual({} satisfies JSONSchema7);
40
- });
41
- });
@@ -1,92 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { z } from 'zod/v3';
3
- import { parseIntersectionDef } from './intersection';
4
- import { getRefs } from '../refs';
5
- import { JSONSchema7 } from '@ai-sdk/provider';
6
-
7
- describe('intersection', () => {
8
- it('should be possible to use intersections', () => {
9
- const intersection = z.intersection(z.string().min(1), z.string().max(3));
10
-
11
- const jsonSchema = parseIntersectionDef(intersection._def, getRefs());
12
-
13
- expect(jsonSchema).toStrictEqual({
14
- allOf: [
15
- {
16
- type: 'string',
17
- minLength: 1,
18
- },
19
- {
20
- type: 'string',
21
- maxLength: 3,
22
- },
23
- ],
24
- } satisfies JSONSchema7);
25
- });
26
-
27
- it('should be possible to deref intersections', () => {
28
- const schema = z.string();
29
- const intersection = z.intersection(schema, schema);
30
- const jsonSchema = parseIntersectionDef(intersection._def, getRefs());
31
-
32
- expect(jsonSchema).toStrictEqual({
33
- allOf: [
34
- {
35
- type: 'string',
36
- },
37
- {
38
- $ref: '#/allOf/0',
39
- },
40
- ],
41
- } satisfies JSONSchema7);
42
- });
43
-
44
- it('should return `unevaluatedProperties` only if all of the multiple sub-schemas have additionalProperties set to false', () => {
45
- const schema1 = z.object({
46
- foo: z.string(),
47
- });
48
- const schema2 = z.object({
49
- bar: z.string(),
50
- });
51
- const schema3 = z
52
- .object({
53
- baz: z.string(),
54
- })
55
- .passthrough();
56
- const intersection = schema1.and(schema2).and(schema3);
57
- const jsonSchema = parseIntersectionDef(intersection._def, getRefs());
58
-
59
- expect(jsonSchema).toStrictEqual({
60
- allOf: [
61
- {
62
- properties: {
63
- foo: {
64
- type: 'string',
65
- },
66
- },
67
- required: ['foo'],
68
- type: 'object',
69
- },
70
- {
71
- properties: {
72
- bar: {
73
- type: 'string',
74
- },
75
- },
76
- required: ['bar'],
77
- type: 'object',
78
- },
79
- {
80
- additionalProperties: true,
81
- properties: {
82
- baz: {
83
- type: 'string',
84
- },
85
- },
86
- required: ['baz'],
87
- type: 'object',
88
- },
89
- ],
90
- } satisfies JSONSchema7);
91
- });
92
- });