@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.
Files changed (163) 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 +4 -2
  5. package/src/__snapshots__/schema.test.ts.snap +346 -0
  6. package/src/add-additional-properties-to-json-schema.test.ts +289 -0
  7. package/src/add-additional-properties-to-json-schema.ts +53 -0
  8. package/src/combine-headers.ts +11 -0
  9. package/src/convert-async-iterator-to-readable-stream.test.ts +78 -0
  10. package/src/convert-async-iterator-to-readable-stream.ts +47 -0
  11. package/src/convert-image-model-file-to-data-uri.test.ts +85 -0
  12. package/src/convert-image-model-file-to-data-uri.ts +19 -0
  13. package/src/convert-to-form-data.test.ts +167 -0
  14. package/src/convert-to-form-data.ts +61 -0
  15. package/src/create-tool-name-mapping.test.ts +163 -0
  16. package/src/create-tool-name-mapping.ts +66 -0
  17. package/src/delay.test.ts +212 -0
  18. package/src/delay.ts +47 -0
  19. package/src/delayed-promise.test.ts +132 -0
  20. package/src/delayed-promise.ts +61 -0
  21. package/src/download-blob.test.ts +145 -0
  22. package/src/download-blob.ts +31 -0
  23. package/src/download-error.ts +39 -0
  24. package/src/extract-response-headers.ts +9 -0
  25. package/src/fetch-function.ts +4 -0
  26. package/src/generate-id.test.ts +31 -0
  27. package/src/generate-id.ts +57 -0
  28. package/src/get-error-message.ts +15 -0
  29. package/src/get-from-api.test.ts +199 -0
  30. package/src/get-from-api.ts +97 -0
  31. package/src/get-runtime-environment-user-agent.test.ts +47 -0
  32. package/src/get-runtime-environment-user-agent.ts +24 -0
  33. package/src/handle-fetch-error.ts +39 -0
  34. package/src/index.ts +67 -0
  35. package/src/inject-json-instruction.test.ts +404 -0
  36. package/src/inject-json-instruction.ts +63 -0
  37. package/src/is-abort-error.ts +8 -0
  38. package/src/is-async-iterable.ts +3 -0
  39. package/src/is-non-nullable.ts +12 -0
  40. package/src/is-url-supported.test.ts +282 -0
  41. package/src/is-url-supported.ts +40 -0
  42. package/src/load-api-key.ts +45 -0
  43. package/src/load-optional-setting.ts +30 -0
  44. package/src/load-setting.ts +62 -0
  45. package/src/maybe-promise-like.ts +3 -0
  46. package/src/media-type-to-extension.test.ts +26 -0
  47. package/src/media-type-to-extension.ts +22 -0
  48. package/src/normalize-headers.test.ts +64 -0
  49. package/src/normalize-headers.ts +38 -0
  50. package/src/parse-json-event-stream.ts +33 -0
  51. package/src/parse-json.test.ts +191 -0
  52. package/src/parse-json.ts +122 -0
  53. package/src/parse-provider-options.ts +32 -0
  54. package/src/post-to-api.ts +166 -0
  55. package/src/provider-tool-factory.ts +125 -0
  56. package/src/remove-undefined-entries.test.ts +57 -0
  57. package/src/remove-undefined-entries.ts +12 -0
  58. package/src/resolve.test.ts +125 -0
  59. package/src/resolve.ts +17 -0
  60. package/src/response-handler.test.ts +89 -0
  61. package/src/response-handler.ts +187 -0
  62. package/src/schema.test-d.ts +11 -0
  63. package/src/schema.test.ts +502 -0
  64. package/src/schema.ts +267 -0
  65. package/src/secure-json-parse.test.ts +59 -0
  66. package/src/secure-json-parse.ts +92 -0
  67. package/src/test/convert-array-to-async-iterable.ts +9 -0
  68. package/src/test/convert-array-to-readable-stream.ts +15 -0
  69. package/src/test/convert-async-iterable-to-array.ts +9 -0
  70. package/src/test/convert-readable-stream-to-array.ts +14 -0
  71. package/src/test/convert-response-stream-to-array.ts +9 -0
  72. package/src/test/index.ts +7 -0
  73. package/src/test/is-node-version.ts +4 -0
  74. package/src/test/mock-id.ts +8 -0
  75. package/src/to-json-schema/zod3-to-json-schema/LICENSE +16 -0
  76. package/src/to-json-schema/zod3-to-json-schema/README.md +24 -0
  77. package/src/to-json-schema/zod3-to-json-schema/get-relative-path.ts +7 -0
  78. package/src/to-json-schema/zod3-to-json-schema/index.ts +1 -0
  79. package/src/to-json-schema/zod3-to-json-schema/options.ts +98 -0
  80. package/src/to-json-schema/zod3-to-json-schema/parse-def.test.ts +224 -0
  81. package/src/to-json-schema/zod3-to-json-schema/parse-def.ts +109 -0
  82. package/src/to-json-schema/zod3-to-json-schema/parse-types.ts +57 -0
  83. package/src/to-json-schema/zod3-to-json-schema/parsers/any.ts +5 -0
  84. package/src/to-json-schema/zod3-to-json-schema/parsers/array.test.ts +98 -0
  85. package/src/to-json-schema/zod3-to-json-schema/parsers/array.ts +38 -0
  86. package/src/to-json-schema/zod3-to-json-schema/parsers/bigint.test.ts +51 -0
  87. package/src/to-json-schema/zod3-to-json-schema/parsers/bigint.ts +44 -0
  88. package/src/to-json-schema/zod3-to-json-schema/parsers/boolean.ts +7 -0
  89. package/src/to-json-schema/zod3-to-json-schema/parsers/branded.test.ts +16 -0
  90. package/src/to-json-schema/zod3-to-json-schema/parsers/branded.ts +7 -0
  91. package/src/to-json-schema/zod3-to-json-schema/parsers/catch.test.ts +15 -0
  92. package/src/to-json-schema/zod3-to-json-schema/parsers/catch.ts +7 -0
  93. package/src/to-json-schema/zod3-to-json-schema/parsers/date.test.ts +97 -0
  94. package/src/to-json-schema/zod3-to-json-schema/parsers/date.ts +64 -0
  95. package/src/to-json-schema/zod3-to-json-schema/parsers/default.test.ts +54 -0
  96. package/src/to-json-schema/zod3-to-json-schema/parsers/default.ts +14 -0
  97. package/src/to-json-schema/zod3-to-json-schema/parsers/effects.test.ts +41 -0
  98. package/src/to-json-schema/zod3-to-json-schema/parsers/effects.ts +14 -0
  99. package/src/to-json-schema/zod3-to-json-schema/parsers/enum.ts +13 -0
  100. package/src/to-json-schema/zod3-to-json-schema/parsers/intersection.test.ts +92 -0
  101. package/src/to-json-schema/zod3-to-json-schema/parsers/intersection.ts +52 -0
  102. package/src/to-json-schema/zod3-to-json-schema/parsers/literal.ts +29 -0
  103. package/src/to-json-schema/zod3-to-json-schema/parsers/map.test.ts +48 -0
  104. package/src/to-json-schema/zod3-to-json-schema/parsers/map.ts +47 -0
  105. package/src/to-json-schema/zod3-to-json-schema/parsers/native-enum.test.ts +102 -0
  106. package/src/to-json-schema/zod3-to-json-schema/parsers/native-enum.ts +31 -0
  107. package/src/to-json-schema/zod3-to-json-schema/parsers/never.ts +9 -0
  108. package/src/to-json-schema/zod3-to-json-schema/parsers/null.ts +9 -0
  109. package/src/to-json-schema/zod3-to-json-schema/parsers/nullable.test.ts +67 -0
  110. package/src/to-json-schema/zod3-to-json-schema/parsers/nullable.ts +42 -0
  111. package/src/to-json-schema/zod3-to-json-schema/parsers/number.test.ts +65 -0
  112. package/src/to-json-schema/zod3-to-json-schema/parsers/number.ts +44 -0
  113. package/src/to-json-schema/zod3-to-json-schema/parsers/object.test.ts +149 -0
  114. package/src/to-json-schema/zod3-to-json-schema/parsers/object.ts +88 -0
  115. package/src/to-json-schema/zod3-to-json-schema/parsers/optional.test.ts +147 -0
  116. package/src/to-json-schema/zod3-to-json-schema/parsers/optional.ts +23 -0
  117. package/src/to-json-schema/zod3-to-json-schema/parsers/pipe.test.ts +35 -0
  118. package/src/to-json-schema/zod3-to-json-schema/parsers/pipeline.ts +29 -0
  119. package/src/to-json-schema/zod3-to-json-schema/parsers/promise.test.ts +15 -0
  120. package/src/to-json-schema/zod3-to-json-schema/parsers/promise.ts +11 -0
  121. package/src/to-json-schema/zod3-to-json-schema/parsers/readonly.test.ts +20 -0
  122. package/src/to-json-schema/zod3-to-json-schema/parsers/readonly.ts +7 -0
  123. package/src/to-json-schema/zod3-to-json-schema/parsers/record.test.ts +108 -0
  124. package/src/to-json-schema/zod3-to-json-schema/parsers/record.ts +71 -0
  125. package/src/to-json-schema/zod3-to-json-schema/parsers/set.test.ts +20 -0
  126. package/src/to-json-schema/zod3-to-json-schema/parsers/set.ts +35 -0
  127. package/src/to-json-schema/zod3-to-json-schema/parsers/string.test.ts +438 -0
  128. package/src/to-json-schema/zod3-to-json-schema/parsers/string.ts +426 -0
  129. package/src/to-json-schema/zod3-to-json-schema/parsers/tuple.test.ts +33 -0
  130. package/src/to-json-schema/zod3-to-json-schema/parsers/tuple.ts +61 -0
  131. package/src/to-json-schema/zod3-to-json-schema/parsers/undefined.ts +11 -0
  132. package/src/to-json-schema/zod3-to-json-schema/parsers/union.test.ts +226 -0
  133. package/src/to-json-schema/zod3-to-json-schema/parsers/union.ts +144 -0
  134. package/src/to-json-schema/zod3-to-json-schema/parsers/unknown.ts +7 -0
  135. package/src/to-json-schema/zod3-to-json-schema/refs.test.ts +919 -0
  136. package/src/to-json-schema/zod3-to-json-schema/refs.ts +39 -0
  137. package/src/to-json-schema/zod3-to-json-schema/select-parser.ts +115 -0
  138. package/src/to-json-schema/zod3-to-json-schema/zod3-to-json-schema.test.ts +862 -0
  139. package/src/to-json-schema/zod3-to-json-schema/zod3-to-json-schema.ts +93 -0
  140. package/src/types/assistant-model-message.ts +39 -0
  141. package/src/types/content-part.ts +379 -0
  142. package/src/types/data-content.ts +4 -0
  143. package/src/types/execute-tool.ts +27 -0
  144. package/src/types/index.ts +40 -0
  145. package/src/types/model-message.ts +14 -0
  146. package/src/types/provider-options.ts +9 -0
  147. package/src/types/system-model-message.ts +20 -0
  148. package/src/types/tool-approval-request.ts +16 -0
  149. package/src/types/tool-approval-response.ts +27 -0
  150. package/src/types/tool-call.ts +31 -0
  151. package/src/types/tool-model-message.ts +23 -0
  152. package/src/types/tool-result.ts +35 -0
  153. package/src/types/tool.test-d.ts +193 -0
  154. package/src/types/tool.ts +324 -0
  155. package/src/types/user-model-message.ts +22 -0
  156. package/src/uint8-utils.ts +26 -0
  157. package/src/validate-types.test.ts +105 -0
  158. package/src/validate-types.ts +81 -0
  159. package/src/version.ts +6 -0
  160. package/src/with-user-agent-suffix.test.ts +84 -0
  161. package/src/with-user-agent-suffix.ts +27 -0
  162. package/src/without-trailing-slash.ts +3 -0
  163. package/LICENSE +0 -13
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @ai-sdk/provider-utils
2
2
 
3
+ ## 4.0.6
4
+
5
+ ### Patch Changes
6
+
7
+ - 1b11dcb: chore(ai): include sources in npm package
8
+ - Updated dependencies [1b11dcb]
9
+ - @ai-sdk/provider@3.0.3
10
+
3
11
  ## 4.0.5
4
12
 
5
13
  ### Patch Changes
package/dist/index.js CHANGED
@@ -475,7 +475,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
475
475
  }
476
476
 
477
477
  // src/version.ts
478
- var VERSION = true ? "4.0.5" : "0.0.0-test";
478
+ var VERSION = true ? "4.0.6" : "0.0.0-test";
479
479
 
480
480
  // src/get-from-api.ts
481
481
  var getOriginalFetch = () => globalThis.fetch;
package/dist/index.mjs CHANGED
@@ -382,7 +382,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
382
382
  }
383
383
 
384
384
  // src/version.ts
385
- var VERSION = true ? "4.0.5" : "0.0.0-test";
385
+ var VERSION = true ? "4.0.6" : "0.0.0-test";
386
386
 
387
387
  // src/get-from-api.ts
388
388
  var getOriginalFetch = () => globalThis.fetch;
package/package.json CHANGED
@@ -1,13 +1,15 @@
1
1
  {
2
2
  "name": "@ai-sdk/provider-utils",
3
- "version": "4.0.5",
3
+ "version": "4.0.6",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.mjs",
8
8
  "types": "./dist/index.d.ts",
9
+ "source": "./src/index.ts",
9
10
  "files": [
10
11
  "dist/**/*",
12
+ "src",
11
13
  "CHANGELOG.md",
12
14
  "README.md",
13
15
  "test.d.ts"
@@ -29,7 +31,7 @@
29
31
  "dependencies": {
30
32
  "@standard-schema/spec": "^1.1.0",
31
33
  "eventsource-parser": "^3.0.6",
32
- "@ai-sdk/provider": "3.0.2"
34
+ "@ai-sdk/provider": "3.0.3"
33
35
  },
34
36
  "devDependencies": {
35
37
  "@types/node": "20.17.24",
@@ -0,0 +1,346 @@
1
+ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
+
3
+ exports[`zodSchema > zod/v4 > json schema conversion > .literal and .enum 1`] = `
4
+ {
5
+ "$schema": "http://json-schema.org/draft-07/schema#",
6
+ "additionalProperties": false,
7
+ "properties": {
8
+ "number": {
9
+ "enum": [
10
+ "one",
11
+ "two",
12
+ "three",
13
+ ],
14
+ "type": "string",
15
+ },
16
+ "text": {
17
+ "const": "hello",
18
+ "type": "string",
19
+ },
20
+ },
21
+ "required": [
22
+ "text",
23
+ "number",
24
+ ],
25
+ "type": "object",
26
+ }
27
+ `;
28
+
29
+ exports[`zodSchema > zod/v4 > json schema conversion > should create a schema with simple types 1`] = `
30
+ {
31
+ "$schema": "http://json-schema.org/draft-07/schema#",
32
+ "additionalProperties": false,
33
+ "properties": {
34
+ "number": {
35
+ "type": "number",
36
+ },
37
+ "text": {
38
+ "type": "string",
39
+ },
40
+ },
41
+ "required": [
42
+ "text",
43
+ "number",
44
+ ],
45
+ "type": "object",
46
+ }
47
+ `;
48
+
49
+ exports[`zodSchema > zod/v4 > json schema conversion > should duplicate referenced schemas (and not use references) by default 1`] = `
50
+ {
51
+ "$schema": "http://json-schema.org/draft-07/schema#",
52
+ "additionalProperties": false,
53
+ "properties": {
54
+ "group1": {
55
+ "items": {
56
+ "additionalProperties": false,
57
+ "properties": {
58
+ "number": {
59
+ "type": "number",
60
+ },
61
+ "text": {
62
+ "type": "string",
63
+ },
64
+ },
65
+ "required": [
66
+ "text",
67
+ "number",
68
+ ],
69
+ "type": "object",
70
+ },
71
+ "type": "array",
72
+ },
73
+ "group2": {
74
+ "items": {
75
+ "additionalProperties": false,
76
+ "properties": {
77
+ "number": {
78
+ "type": "number",
79
+ },
80
+ "text": {
81
+ "type": "string",
82
+ },
83
+ },
84
+ "required": [
85
+ "text",
86
+ "number",
87
+ ],
88
+ "type": "object",
89
+ },
90
+ "type": "array",
91
+ },
92
+ },
93
+ "required": [
94
+ "group1",
95
+ "group2",
96
+ ],
97
+ "type": "object",
98
+ }
99
+ `;
100
+
101
+ exports[`zodSchema > zod/v4 > json schema conversion > should generate JSON schema for input when transform is used 1`] = `
102
+ {
103
+ "$schema": "http://json-schema.org/draft-07/schema#",
104
+ "additionalProperties": false,
105
+ "properties": {
106
+ "user": {
107
+ "additionalProperties": false,
108
+ "properties": {
109
+ "id": {
110
+ "type": "string",
111
+ },
112
+ "name": {
113
+ "type": "string",
114
+ },
115
+ },
116
+ "required": [
117
+ "id",
118
+ "name",
119
+ ],
120
+ "type": "object",
121
+ },
122
+ },
123
+ "required": [
124
+ "user",
125
+ ],
126
+ "type": "object",
127
+ }
128
+ `;
129
+
130
+ exports[`zodSchema > zod/v4 > json schema conversion > should support arrays 1`] = `
131
+ {
132
+ "$schema": "http://json-schema.org/draft-07/schema#",
133
+ "additionalProperties": false,
134
+ "properties": {
135
+ "items": {
136
+ "items": {
137
+ "type": "string",
138
+ },
139
+ "type": "array",
140
+ },
141
+ },
142
+ "required": [
143
+ "items",
144
+ ],
145
+ "type": "object",
146
+ }
147
+ `;
148
+
149
+ exports[`zodSchema > zod/v4 > json schema conversion > should support nullable 1`] = `
150
+ {
151
+ "$schema": "http://json-schema.org/draft-07/schema#",
152
+ "additionalProperties": false,
153
+ "properties": {
154
+ "location": {
155
+ "anyOf": [
156
+ {
157
+ "type": "string",
158
+ },
159
+ {
160
+ "type": "null",
161
+ },
162
+ ],
163
+ },
164
+ },
165
+ "required": [
166
+ "location",
167
+ ],
168
+ "type": "object",
169
+ }
170
+ `;
171
+
172
+ exports[`zodSchema > zod/v4 > json schema conversion > should support optional arrays 1`] = `
173
+ {
174
+ "$schema": "http://json-schema.org/draft-07/schema#",
175
+ "additionalProperties": false,
176
+ "properties": {
177
+ "items": {
178
+ "items": {
179
+ "type": "string",
180
+ },
181
+ "type": "array",
182
+ },
183
+ },
184
+ "type": "object",
185
+ }
186
+ `;
187
+
188
+ exports[`zodSchema > zod/v4 > json schema conversion > should support optional enums 1`] = `
189
+ {
190
+ "$schema": "http://json-schema.org/draft-07/schema#",
191
+ "additionalProperties": false,
192
+ "properties": {
193
+ "type": {
194
+ "enum": [
195
+ "a",
196
+ "b",
197
+ "c",
198
+ ],
199
+ "type": "string",
200
+ },
201
+ },
202
+ "type": "object",
203
+ }
204
+ `;
205
+
206
+ exports[`zodSchema > zod/v4 > json schema conversion > should support optional fields in object 1`] = `
207
+ {
208
+ "$schema": "http://json-schema.org/draft-07/schema#",
209
+ "additionalProperties": false,
210
+ "properties": {
211
+ "optional": {
212
+ "type": "string",
213
+ },
214
+ "required": {
215
+ "type": "string",
216
+ },
217
+ },
218
+ "required": [
219
+ "required",
220
+ ],
221
+ "type": "object",
222
+ }
223
+ `;
224
+
225
+ exports[`zodSchema > zod/v4 > json schema conversion > should support optional fields with descriptions in object 1`] = `
226
+ {
227
+ "$schema": "http://json-schema.org/draft-07/schema#",
228
+ "additionalProperties": false,
229
+ "properties": {
230
+ "optional": {
231
+ "description": "Optional description",
232
+ "type": "string",
233
+ },
234
+ "required": {
235
+ "description": "Required description",
236
+ "type": "string",
237
+ },
238
+ },
239
+ "required": [
240
+ "required",
241
+ ],
242
+ "type": "object",
243
+ }
244
+ `;
245
+
246
+ exports[`zodSchema > zod/v4 > json schema conversion > should support required enums 1`] = `
247
+ {
248
+ "$schema": "http://json-schema.org/draft-07/schema#",
249
+ "additionalProperties": false,
250
+ "properties": {
251
+ "type": {
252
+ "enum": [
253
+ "a",
254
+ "b",
255
+ "c",
256
+ ],
257
+ "type": "string",
258
+ },
259
+ },
260
+ "required": [
261
+ "type",
262
+ ],
263
+ "type": "object",
264
+ }
265
+ `;
266
+
267
+ exports[`zodSchema > zod/v4 > json schema conversion > should use recursive references with z.lazy when useReferences is true 1`] = `
268
+ {
269
+ "$schema": "http://json-schema.org/draft-07/schema#",
270
+ "additionalProperties": false,
271
+ "definitions": {
272
+ "__schema0": {
273
+ "additionalProperties": false,
274
+ "properties": {
275
+ "name": {
276
+ "type": "string",
277
+ },
278
+ "subcategories": {
279
+ "items": {
280
+ "$ref": "#/definitions/__schema0",
281
+ },
282
+ "type": "array",
283
+ },
284
+ },
285
+ "required": [
286
+ "name",
287
+ "subcategories",
288
+ ],
289
+ "type": "object",
290
+ },
291
+ },
292
+ "properties": {
293
+ "category": {
294
+ "$ref": "#/definitions/__schema0",
295
+ },
296
+ },
297
+ "required": [
298
+ "category",
299
+ ],
300
+ "type": "object",
301
+ }
302
+ `;
303
+
304
+ exports[`zodSchema > zod/v4 > json schema conversion > should use references when useReferences is true 1`] = `
305
+ {
306
+ "$schema": "http://json-schema.org/draft-07/schema#",
307
+ "additionalProperties": false,
308
+ "definitions": {
309
+ "__schema0": {
310
+ "additionalProperties": false,
311
+ "properties": {
312
+ "number": {
313
+ "type": "number",
314
+ },
315
+ "text": {
316
+ "type": "string",
317
+ },
318
+ },
319
+ "required": [
320
+ "text",
321
+ "number",
322
+ ],
323
+ "type": "object",
324
+ },
325
+ },
326
+ "properties": {
327
+ "group1": {
328
+ "items": {
329
+ "$ref": "#/definitions/__schema0",
330
+ },
331
+ "type": "array",
332
+ },
333
+ "group2": {
334
+ "items": {
335
+ "$ref": "#/definitions/__schema0",
336
+ },
337
+ "type": "array",
338
+ },
339
+ },
340
+ "required": [
341
+ "group1",
342
+ "group2",
343
+ ],
344
+ "type": "object",
345
+ }
346
+ `;
@@ -0,0 +1,289 @@
1
+ import { JSONSchema7 } from '@ai-sdk/provider';
2
+ import { describe, expect, it } from 'vitest';
3
+ import { addAdditionalPropertiesToJsonSchema } from './add-additional-properties-to-json-schema';
4
+
5
+ describe('addAdditionalPropertiesToJsonSchema', () => {
6
+ it('adds additionalProperties: false to objects recursively', () => {
7
+ const schema: JSONSchema7 = {
8
+ type: 'object',
9
+ properties: {
10
+ user: {
11
+ type: 'object',
12
+ properties: {
13
+ name: { type: 'string' },
14
+ },
15
+ },
16
+ age: { type: 'number' },
17
+ },
18
+ };
19
+
20
+ const result = addAdditionalPropertiesToJsonSchema(schema);
21
+
22
+ expect(result).toEqual({
23
+ type: 'object',
24
+ additionalProperties: false,
25
+ properties: {
26
+ user: {
27
+ type: 'object',
28
+ additionalProperties: false,
29
+ properties: {
30
+ name: { type: 'string' },
31
+ },
32
+ },
33
+ age: { type: 'number' },
34
+ },
35
+ });
36
+ });
37
+
38
+ it('adds additionalProperties: false to objects inside arrays', () => {
39
+ const schema: JSONSchema7 = {
40
+ type: 'object',
41
+ properties: {
42
+ ingredients: {
43
+ type: 'array',
44
+ items: {
45
+ type: 'object',
46
+ properties: {
47
+ name: { type: 'string' },
48
+ amount: { type: 'string' },
49
+ },
50
+ required: ['name', 'amount'],
51
+ },
52
+ },
53
+ },
54
+ required: ['ingredients'],
55
+ };
56
+
57
+ const result = addAdditionalPropertiesToJsonSchema(schema);
58
+
59
+ expect(result).toEqual({
60
+ type: 'object',
61
+ additionalProperties: false,
62
+ properties: {
63
+ ingredients: {
64
+ type: 'array',
65
+ items: {
66
+ type: 'object',
67
+ additionalProperties: false,
68
+ properties: {
69
+ name: { type: 'string' },
70
+ amount: { type: 'string' },
71
+ },
72
+ required: ['name', 'amount'],
73
+ },
74
+ },
75
+ },
76
+ required: ['ingredients'],
77
+ });
78
+ });
79
+
80
+ it('adds additionalProperties: false when type is a union that includes "object"', () => {
81
+ const schema: JSONSchema7 = {
82
+ type: 'object',
83
+ properties: {
84
+ response: {
85
+ type: ['object', 'null'],
86
+ properties: {
87
+ name: { type: 'string' },
88
+ },
89
+ },
90
+ },
91
+ };
92
+
93
+ expect(addAdditionalPropertiesToJsonSchema(schema)).toEqual({
94
+ type: 'object',
95
+ additionalProperties: false,
96
+ properties: {
97
+ response: {
98
+ type: ['object', 'null'],
99
+ additionalProperties: false,
100
+ properties: {
101
+ name: { type: 'string' },
102
+ },
103
+ },
104
+ },
105
+ });
106
+ });
107
+
108
+ it('adds additionalProperties: false to objects inside anyOf', () => {
109
+ const schema: JSONSchema7 = {
110
+ type: 'object',
111
+ properties: {
112
+ response: {
113
+ anyOf: [
114
+ { type: 'object', properties: { name: { type: 'string' } } },
115
+ { type: 'object', properties: { amount: { type: 'string' } } },
116
+ ],
117
+ },
118
+ },
119
+ };
120
+
121
+ expect(addAdditionalPropertiesToJsonSchema(schema)).toEqual({
122
+ type: 'object',
123
+ additionalProperties: false,
124
+ properties: {
125
+ response: {
126
+ anyOf: [
127
+ {
128
+ type: 'object',
129
+ additionalProperties: false,
130
+ properties: { name: { type: 'string' } },
131
+ },
132
+ {
133
+ type: 'object',
134
+ additionalProperties: false,
135
+ properties: { amount: { type: 'string' } },
136
+ },
137
+ ],
138
+ },
139
+ },
140
+ });
141
+ });
142
+
143
+ it('adds additionalProperties: false to objects inside allOf', () => {
144
+ const schema: JSONSchema7 = {
145
+ type: 'object',
146
+ properties: {
147
+ response: {
148
+ allOf: [
149
+ { type: 'object', properties: { name: { type: 'string' } } },
150
+ { type: 'object', properties: { age: { type: 'number' } } },
151
+ ],
152
+ },
153
+ },
154
+ };
155
+
156
+ expect(addAdditionalPropertiesToJsonSchema(schema)).toEqual({
157
+ type: 'object',
158
+ additionalProperties: false,
159
+ properties: {
160
+ response: {
161
+ allOf: [
162
+ {
163
+ type: 'object',
164
+ additionalProperties: false,
165
+ properties: { name: { type: 'string' } },
166
+ },
167
+ {
168
+ type: 'object',
169
+ additionalProperties: false,
170
+ properties: { age: { type: 'number' } },
171
+ },
172
+ ],
173
+ },
174
+ },
175
+ });
176
+ });
177
+
178
+ it('adds additionalProperties: false to objects inside oneOf', () => {
179
+ const schema: JSONSchema7 = {
180
+ type: 'object',
181
+ properties: {
182
+ response: {
183
+ oneOf: [
184
+ { type: 'object', properties: { success: { type: 'boolean' } } },
185
+ { type: 'object', properties: { error: { type: 'string' } } },
186
+ ],
187
+ },
188
+ },
189
+ };
190
+
191
+ expect(addAdditionalPropertiesToJsonSchema(schema)).toEqual({
192
+ type: 'object',
193
+ additionalProperties: false,
194
+ properties: {
195
+ response: {
196
+ oneOf: [
197
+ {
198
+ type: 'object',
199
+ additionalProperties: false,
200
+ properties: { success: { type: 'boolean' } },
201
+ },
202
+ {
203
+ type: 'object',
204
+ additionalProperties: false,
205
+ properties: { error: { type: 'string' } },
206
+ },
207
+ ],
208
+ },
209
+ },
210
+ });
211
+ });
212
+
213
+ it('adds additionalProperties: false to object schemas inside definitions (refs)', () => {
214
+ const schema: JSONSchema7 = {
215
+ type: 'object',
216
+ properties: {
217
+ node: { $ref: '#/definitions/Node' },
218
+ },
219
+ definitions: {
220
+ Node: {
221
+ type: 'object',
222
+ properties: {
223
+ value: { type: 'string' },
224
+ next: { $ref: '#/definitions/Node' }, // recursive reference
225
+ },
226
+ },
227
+ },
228
+ };
229
+
230
+ expect(addAdditionalPropertiesToJsonSchema(schema)).toEqual({
231
+ type: 'object',
232
+ additionalProperties: false,
233
+ properties: {
234
+ node: { $ref: '#/definitions/Node' },
235
+ },
236
+ definitions: {
237
+ Node: {
238
+ type: 'object',
239
+ additionalProperties: false,
240
+ properties: {
241
+ value: { type: 'string' },
242
+ next: { $ref: '#/definitions/Node' },
243
+ },
244
+ },
245
+ },
246
+ });
247
+ });
248
+
249
+ it('overwrites existing additionalProperties flags', () => {
250
+ const schema: JSONSchema7 = {
251
+ type: 'object',
252
+ additionalProperties: true,
253
+ properties: {
254
+ meta: {
255
+ type: 'object',
256
+ additionalProperties: true,
257
+ properties: {
258
+ id: { type: 'string' },
259
+ },
260
+ },
261
+ },
262
+ };
263
+
264
+ const result = addAdditionalPropertiesToJsonSchema(schema);
265
+
266
+ expect(result).toEqual({
267
+ type: 'object',
268
+ additionalProperties: false,
269
+ properties: {
270
+ meta: {
271
+ type: 'object',
272
+ additionalProperties: false,
273
+ properties: {
274
+ id: { type: 'string' },
275
+ },
276
+ },
277
+ },
278
+ });
279
+ });
280
+
281
+ it('leaves non-object schemas unchanged', () => {
282
+ const schema: JSONSchema7 = { type: 'string' };
283
+
284
+ const result = addAdditionalPropertiesToJsonSchema(schema);
285
+
286
+ expect(result).toBe(schema);
287
+ expect(result).toEqual({ type: 'string' });
288
+ });
289
+ });