@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,226 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { z } from 'zod/v3';
|
|
3
|
+
import { parseUnionDef } from './union';
|
|
4
|
+
import { getRefs } from '../refs';
|
|
5
|
+
import { JSONSchema7 } from '@ai-sdk/provider';
|
|
6
|
+
|
|
7
|
+
describe('union', () => {
|
|
8
|
+
it('Should be possible to get a simple type array from a union of only unvalidated primitives', () => {
|
|
9
|
+
const parsedSchema = parseUnionDef(
|
|
10
|
+
z.union([z.string(), z.number(), z.boolean(), z.null()])._def,
|
|
11
|
+
getRefs(),
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
expect(parsedSchema).toStrictEqual({
|
|
15
|
+
type: ['string', 'number', 'boolean', 'null'],
|
|
16
|
+
} satisfies JSONSchema7);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('Should be possible to get a simple type array with enum values from a union of literals', () => {
|
|
20
|
+
const parsedSchema = parseUnionDef(
|
|
21
|
+
z.union([
|
|
22
|
+
z.literal('string'),
|
|
23
|
+
z.literal(123),
|
|
24
|
+
z.literal(true),
|
|
25
|
+
z.literal(null),
|
|
26
|
+
z.literal(BigInt(50)),
|
|
27
|
+
])._def,
|
|
28
|
+
getRefs(),
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
expect(parsedSchema).toStrictEqual({
|
|
32
|
+
type: ['string', 'number', 'boolean', 'null', 'integer'],
|
|
33
|
+
enum: ['string', 123, true, null, BigInt(50) as unknown as number],
|
|
34
|
+
} satisfies JSONSchema7);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('Should be possible to get an anyOf array with enum values from a union of literals', () => {
|
|
38
|
+
const parsedSchema = parseUnionDef(
|
|
39
|
+
z.union([
|
|
40
|
+
z.literal(undefined),
|
|
41
|
+
z.literal(Symbol('abc')),
|
|
42
|
+
// @ts-expect-error Ok
|
|
43
|
+
z.literal(function () {}),
|
|
44
|
+
])._def,
|
|
45
|
+
getRefs(),
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
expect(parsedSchema).toStrictEqual({
|
|
49
|
+
anyOf: [
|
|
50
|
+
{
|
|
51
|
+
type: 'object',
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
type: 'object',
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
type: 'object',
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
} satisfies JSONSchema7);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('Should be possible to create a union with objects, arrays and validated primitives as an anyOf', () => {
|
|
64
|
+
const parsedSchema = parseUnionDef(
|
|
65
|
+
z.union([
|
|
66
|
+
z.object({ herp: z.string(), derp: z.boolean() }),
|
|
67
|
+
z.array(z.number()),
|
|
68
|
+
z.string().min(3),
|
|
69
|
+
z.number(),
|
|
70
|
+
])._def,
|
|
71
|
+
getRefs(),
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
expect(parsedSchema).toStrictEqual({
|
|
75
|
+
anyOf: [
|
|
76
|
+
{
|
|
77
|
+
type: 'object',
|
|
78
|
+
properties: {
|
|
79
|
+
herp: {
|
|
80
|
+
type: 'string',
|
|
81
|
+
},
|
|
82
|
+
derp: {
|
|
83
|
+
type: 'boolean',
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
required: ['herp', 'derp'],
|
|
87
|
+
additionalProperties: false,
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
type: 'array',
|
|
91
|
+
items: {
|
|
92
|
+
type: 'number',
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
type: 'string',
|
|
97
|
+
minLength: 3,
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
type: 'number',
|
|
101
|
+
},
|
|
102
|
+
],
|
|
103
|
+
} satisfies JSONSchema7);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('should be possible to deref union schemas', () => {
|
|
107
|
+
const recurring = z.object({ foo: z.boolean() });
|
|
108
|
+
|
|
109
|
+
const union = z.union([recurring, recurring, recurring]);
|
|
110
|
+
|
|
111
|
+
const jsonSchema = parseUnionDef(union._def, getRefs());
|
|
112
|
+
|
|
113
|
+
expect(jsonSchema).toStrictEqual({
|
|
114
|
+
anyOf: [
|
|
115
|
+
{
|
|
116
|
+
type: 'object',
|
|
117
|
+
properties: {
|
|
118
|
+
foo: {
|
|
119
|
+
type: 'boolean',
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
required: ['foo'],
|
|
123
|
+
additionalProperties: false,
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
$ref: '#/anyOf/0',
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
$ref: '#/anyOf/0',
|
|
130
|
+
},
|
|
131
|
+
],
|
|
132
|
+
} satisfies JSONSchema7);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('nullable primitives should come out fine', () => {
|
|
136
|
+
const union = z.union([z.string(), z.null()]);
|
|
137
|
+
|
|
138
|
+
const jsonSchema = parseUnionDef(union._def, getRefs());
|
|
139
|
+
|
|
140
|
+
expect(jsonSchema).toStrictEqual({
|
|
141
|
+
type: ['string', 'null'],
|
|
142
|
+
} satisfies JSONSchema7);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('should join a union of Zod enums into a single enum', () => {
|
|
146
|
+
const union = z.union([z.enum(['a', 'b', 'c']), z.enum(['c', 'd', 'e'])]);
|
|
147
|
+
|
|
148
|
+
const jsonSchema = parseUnionDef(union._def, getRefs());
|
|
149
|
+
|
|
150
|
+
expect(jsonSchema).toStrictEqual({
|
|
151
|
+
type: 'string',
|
|
152
|
+
enum: ['a', 'b', 'c', 'd', 'e'],
|
|
153
|
+
} satisfies JSONSchema7);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('should work with discriminated union type', () => {
|
|
157
|
+
const discUnion = z.discriminatedUnion('kek', [
|
|
158
|
+
z.object({ kek: z.literal('A'), lel: z.boolean() }),
|
|
159
|
+
z.object({ kek: z.literal('B'), lel: z.number() }),
|
|
160
|
+
]);
|
|
161
|
+
|
|
162
|
+
const jsonSchema = parseUnionDef(discUnion._def, getRefs());
|
|
163
|
+
|
|
164
|
+
expect(jsonSchema).toStrictEqual({
|
|
165
|
+
anyOf: [
|
|
166
|
+
{
|
|
167
|
+
type: 'object',
|
|
168
|
+
properties: {
|
|
169
|
+
kek: {
|
|
170
|
+
type: 'string',
|
|
171
|
+
const: 'A',
|
|
172
|
+
},
|
|
173
|
+
lel: {
|
|
174
|
+
type: 'boolean',
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
required: ['kek', 'lel'],
|
|
178
|
+
additionalProperties: false,
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
type: 'object',
|
|
182
|
+
properties: {
|
|
183
|
+
kek: {
|
|
184
|
+
type: 'string',
|
|
185
|
+
const: 'B',
|
|
186
|
+
},
|
|
187
|
+
lel: {
|
|
188
|
+
type: 'number',
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
required: ['kek', 'lel'],
|
|
192
|
+
additionalProperties: false,
|
|
193
|
+
},
|
|
194
|
+
],
|
|
195
|
+
} satisfies JSONSchema7);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('should not ignore descriptions in literal unions', () => {
|
|
199
|
+
expect(
|
|
200
|
+
parseUnionDef(
|
|
201
|
+
z.union([z.literal(true), z.literal('herp'), z.literal(3)])._def,
|
|
202
|
+
getRefs(),
|
|
203
|
+
),
|
|
204
|
+
).toStrictEqual({
|
|
205
|
+
type: ['boolean', 'string', 'number'],
|
|
206
|
+
enum: [true, 'herp', 3],
|
|
207
|
+
} satisfies JSONSchema7);
|
|
208
|
+
|
|
209
|
+
expect(
|
|
210
|
+
parseUnionDef(
|
|
211
|
+
z.union([
|
|
212
|
+
z.literal(true),
|
|
213
|
+
z.literal('herp').describe('derp'),
|
|
214
|
+
z.literal(3),
|
|
215
|
+
])._def,
|
|
216
|
+
getRefs(),
|
|
217
|
+
),
|
|
218
|
+
).toStrictEqual({
|
|
219
|
+
anyOf: [
|
|
220
|
+
{ type: 'boolean', const: true },
|
|
221
|
+
{ type: 'string', const: 'herp', description: 'derp' },
|
|
222
|
+
{ type: 'number', const: 3 },
|
|
223
|
+
],
|
|
224
|
+
} satisfies JSONSchema7);
|
|
225
|
+
});
|
|
226
|
+
});
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ZodDiscriminatedUnionDef,
|
|
3
|
+
ZodLiteralDef,
|
|
4
|
+
ZodTypeAny,
|
|
5
|
+
ZodUnionDef,
|
|
6
|
+
} from 'zod/v3';
|
|
7
|
+
import { parseDef } from '../parse-def';
|
|
8
|
+
import { JsonSchema7Type } from '../parse-types';
|
|
9
|
+
import { Refs } from '../refs';
|
|
10
|
+
|
|
11
|
+
export const primitiveMappings = {
|
|
12
|
+
ZodString: 'string',
|
|
13
|
+
ZodNumber: 'number',
|
|
14
|
+
ZodBigInt: 'integer',
|
|
15
|
+
ZodBoolean: 'boolean',
|
|
16
|
+
ZodNull: 'null',
|
|
17
|
+
} as const;
|
|
18
|
+
type ZodPrimitive = keyof typeof primitiveMappings;
|
|
19
|
+
type JsonSchema7Primitive =
|
|
20
|
+
(typeof primitiveMappings)[keyof typeof primitiveMappings];
|
|
21
|
+
|
|
22
|
+
export type JsonSchema7UnionType =
|
|
23
|
+
| JsonSchema7PrimitiveUnionType
|
|
24
|
+
| JsonSchema7AnyOfType;
|
|
25
|
+
|
|
26
|
+
type JsonSchema7PrimitiveUnionType =
|
|
27
|
+
| {
|
|
28
|
+
type: JsonSchema7Primitive | JsonSchema7Primitive[];
|
|
29
|
+
}
|
|
30
|
+
| {
|
|
31
|
+
type: JsonSchema7Primitive | JsonSchema7Primitive[];
|
|
32
|
+
enum: (string | number | bigint | boolean | null)[];
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
type JsonSchema7AnyOfType = {
|
|
36
|
+
anyOf: JsonSchema7Type[];
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export function parseUnionDef(
|
|
40
|
+
def: ZodUnionDef | ZodDiscriminatedUnionDef<any, any>,
|
|
41
|
+
refs: Refs,
|
|
42
|
+
): JsonSchema7PrimitiveUnionType | JsonSchema7AnyOfType | undefined {
|
|
43
|
+
const options: readonly ZodTypeAny[] =
|
|
44
|
+
def.options instanceof Map ? Array.from(def.options.values()) : def.options;
|
|
45
|
+
|
|
46
|
+
// This blocks tries to look ahead a bit to produce nicer looking schemas with type array instead of anyOf.
|
|
47
|
+
if (
|
|
48
|
+
options.every(
|
|
49
|
+
x =>
|
|
50
|
+
x._def.typeName in primitiveMappings &&
|
|
51
|
+
(!x._def.checks || !x._def.checks.length),
|
|
52
|
+
)
|
|
53
|
+
) {
|
|
54
|
+
// all types in union are primitive and lack checks, so might as well squash into {type: [...]}
|
|
55
|
+
|
|
56
|
+
const types = options.reduce((types: JsonSchema7Primitive[], x) => {
|
|
57
|
+
const type = primitiveMappings[x._def.typeName as ZodPrimitive]; //Can be safely casted due to row 43
|
|
58
|
+
return type && !types.includes(type) ? [...types, type] : types;
|
|
59
|
+
}, []);
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
type: types.length > 1 ? types : types[0],
|
|
63
|
+
};
|
|
64
|
+
} else if (
|
|
65
|
+
options.every(x => x._def.typeName === 'ZodLiteral' && !x.description)
|
|
66
|
+
) {
|
|
67
|
+
// all options literals
|
|
68
|
+
|
|
69
|
+
const types = options.reduce(
|
|
70
|
+
(acc: JsonSchema7Primitive[], x: { _def: ZodLiteralDef }) => {
|
|
71
|
+
const type = typeof x._def.value;
|
|
72
|
+
switch (type) {
|
|
73
|
+
case 'string':
|
|
74
|
+
case 'number':
|
|
75
|
+
case 'boolean':
|
|
76
|
+
return [...acc, type];
|
|
77
|
+
case 'bigint':
|
|
78
|
+
return [...acc, 'integer' as const];
|
|
79
|
+
case 'object':
|
|
80
|
+
if (x._def.value === null) return [...acc, 'null' as const];
|
|
81
|
+
case 'symbol':
|
|
82
|
+
case 'undefined':
|
|
83
|
+
case 'function':
|
|
84
|
+
default:
|
|
85
|
+
return acc;
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
[],
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
if (types.length === options.length) {
|
|
92
|
+
// all the literals are primitive, as far as null can be considered primitive
|
|
93
|
+
|
|
94
|
+
const uniqueTypes = types.filter((x, i, a) => a.indexOf(x) === i);
|
|
95
|
+
return {
|
|
96
|
+
type: uniqueTypes.length > 1 ? uniqueTypes : uniqueTypes[0],
|
|
97
|
+
enum: options.reduce(
|
|
98
|
+
(acc, x) => {
|
|
99
|
+
return acc.includes(x._def.value) ? acc : [...acc, x._def.value];
|
|
100
|
+
},
|
|
101
|
+
[] as (string | number | bigint | boolean | null)[],
|
|
102
|
+
),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
} else if (options.every(x => x._def.typeName === 'ZodEnum')) {
|
|
106
|
+
return {
|
|
107
|
+
type: 'string',
|
|
108
|
+
enum: options.reduce(
|
|
109
|
+
(acc: string[], x) => [
|
|
110
|
+
...acc,
|
|
111
|
+
...x._def.values.filter((x: string) => !acc.includes(x)),
|
|
112
|
+
],
|
|
113
|
+
[],
|
|
114
|
+
),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return asAnyOf(def, refs);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const asAnyOf = (
|
|
122
|
+
def: ZodUnionDef | ZodDiscriminatedUnionDef<any, any>,
|
|
123
|
+
refs: Refs,
|
|
124
|
+
): JsonSchema7PrimitiveUnionType | JsonSchema7AnyOfType | undefined => {
|
|
125
|
+
const anyOf = (
|
|
126
|
+
(def.options instanceof Map
|
|
127
|
+
? Array.from(def.options.values())
|
|
128
|
+
: def.options) as any[]
|
|
129
|
+
)
|
|
130
|
+
.map((x, i) =>
|
|
131
|
+
parseDef(x._def, {
|
|
132
|
+
...refs,
|
|
133
|
+
currentPath: [...refs.currentPath, 'anyOf', `${i}`],
|
|
134
|
+
}),
|
|
135
|
+
)
|
|
136
|
+
.filter(
|
|
137
|
+
(x): x is JsonSchema7Type =>
|
|
138
|
+
!!x &&
|
|
139
|
+
(!refs.strictUnions ||
|
|
140
|
+
(typeof x === 'object' && Object.keys(x).length > 0)),
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
return anyOf.length ? { anyOf } : undefined;
|
|
144
|
+
};
|