@amritk/adapters 0.2.0
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/dist/adapter.d.ts +22 -0
- package/dist/adapter.js +0 -0
- package/dist/effect-to-json-schema.d.ts +11 -0
- package/dist/effect-to-json-schema.js +66264 -0
- package/dist/get-adapter.d.ts +11 -0
- package/dist/get-adapter.js +81915 -0
- package/dist/source-format.d.ts +11 -0
- package/dist/source-format.js +0 -0
- package/dist/typebox-to-json-schema.d.ts +13 -0
- package/dist/typebox-to-json-schema.js +61 -0
- package/dist/valibot-to-json-schema.d.ts +12 -0
- package/dist/valibot-to-json-schema.js +672 -0
- package/dist/zod-to-json-schema.d.ts +12 -0
- package/dist/zod-to-json-schema.js +14936 -0
- package/package.json +101 -0
- package/src/adapter.ts +24 -0
- package/src/effect-to-json-schema.test.ts +74 -0
- package/src/effect-to-json-schema.ts +57 -0
- package/src/get-adapter.test.ts +36 -0
- package/src/get-adapter.ts +52 -0
- package/src/source-format.ts +11 -0
- package/src/typebox-to-json-schema.test.ts +121 -0
- package/src/typebox-to-json-schema.ts +79 -0
- package/src/valibot-to-json-schema.test.ts +100 -0
- package/src/valibot-to-json-schema.ts +77 -0
- package/src/zod-to-json-schema.test.ts +114 -0
- package/src/zod-to-json-schema.ts +87 -0
|
@@ -0,0 +1,672 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __export = (target, all) => {
|
|
3
|
+
for (var name in all)
|
|
4
|
+
__defProp(target, name, {
|
|
5
|
+
get: all[name],
|
|
6
|
+
enumerable: true,
|
|
7
|
+
configurable: true,
|
|
8
|
+
set: (newValue) => all[name] = () => newValue
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
12
|
+
|
|
13
|
+
// ../../node_modules/.bun/@valibot+to-json-schema@1.7.0+731820d9428d8f81/node_modules/@valibot/to-json-schema/dist/index.mjs
|
|
14
|
+
var exports_dist = {};
|
|
15
|
+
__export(exports_dist, {
|
|
16
|
+
toStandardJsonSchema: () => toStandardJsonSchema,
|
|
17
|
+
toJsonSchemaDefs: () => toJsonSchemaDefs,
|
|
18
|
+
toJsonSchema: () => toJsonSchema,
|
|
19
|
+
getGlobalDefs: () => getGlobalDefs,
|
|
20
|
+
addGlobalDefs: () => addGlobalDefs
|
|
21
|
+
});
|
|
22
|
+
function addError(errors, message) {
|
|
23
|
+
if (errors) {
|
|
24
|
+
errors.push(message);
|
|
25
|
+
return errors;
|
|
26
|
+
}
|
|
27
|
+
return [message];
|
|
28
|
+
}
|
|
29
|
+
function escapeRegExp(string) {
|
|
30
|
+
return string.replace(ESCAPE_REGEX, "\\$&");
|
|
31
|
+
}
|
|
32
|
+
function handleError(message, config) {
|
|
33
|
+
switch (config?.errorMode) {
|
|
34
|
+
case "ignore":
|
|
35
|
+
break;
|
|
36
|
+
case "warn":
|
|
37
|
+
console.warn(message);
|
|
38
|
+
break;
|
|
39
|
+
default:
|
|
40
|
+
throw new Error(message);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function isJsonConstValue(value) {
|
|
44
|
+
return typeof value === "boolean" || typeof value === "number" && Number.isFinite(value) || typeof value === "string";
|
|
45
|
+
}
|
|
46
|
+
function isJsonEnumValues(values) {
|
|
47
|
+
return values.every(isJsonConstValue);
|
|
48
|
+
}
|
|
49
|
+
function convertAction(jsonSchema, valibotAction, config) {
|
|
50
|
+
if (config?.ignoreActions?.includes(valibotAction.type))
|
|
51
|
+
return jsonSchema;
|
|
52
|
+
let errors;
|
|
53
|
+
switch (valibotAction.type) {
|
|
54
|
+
case "base64":
|
|
55
|
+
jsonSchema.contentEncoding = "base64";
|
|
56
|
+
break;
|
|
57
|
+
case "bic":
|
|
58
|
+
case "cuid2":
|
|
59
|
+
case "decimal":
|
|
60
|
+
case "digits":
|
|
61
|
+
case "domain":
|
|
62
|
+
case "emoji":
|
|
63
|
+
case "hash":
|
|
64
|
+
case "hexadecimal":
|
|
65
|
+
case "hex_color":
|
|
66
|
+
case "isrc":
|
|
67
|
+
case "iso_time_second":
|
|
68
|
+
case "iso_week":
|
|
69
|
+
case "mac":
|
|
70
|
+
case "mac48":
|
|
71
|
+
case "mac64":
|
|
72
|
+
case "nanoid":
|
|
73
|
+
case "octal":
|
|
74
|
+
case "slug":
|
|
75
|
+
case "ulid":
|
|
76
|
+
if (jsonSchema.pattern)
|
|
77
|
+
errors = addError(errors, `The "${valibotAction.type}" action is not supported in combination with another regex action.`);
|
|
78
|
+
else
|
|
79
|
+
jsonSchema.pattern = valibotAction.requirement.source;
|
|
80
|
+
break;
|
|
81
|
+
case "description":
|
|
82
|
+
jsonSchema.description = valibotAction.description;
|
|
83
|
+
break;
|
|
84
|
+
case "email":
|
|
85
|
+
case "rfc_email":
|
|
86
|
+
jsonSchema.format = "email";
|
|
87
|
+
break;
|
|
88
|
+
case "ends_with":
|
|
89
|
+
if (jsonSchema.pattern)
|
|
90
|
+
errors = addError(errors, `The "${valibotAction.type}" action is not supported in combination with another regex action.`);
|
|
91
|
+
else
|
|
92
|
+
jsonSchema.pattern = `${escapeRegExp(valibotAction.requirement)}$`;
|
|
93
|
+
break;
|
|
94
|
+
case "empty":
|
|
95
|
+
if (jsonSchema.type === "array")
|
|
96
|
+
jsonSchema.maxItems = 0;
|
|
97
|
+
else {
|
|
98
|
+
if (jsonSchema.type !== "string")
|
|
99
|
+
errors = addError(errors, `The "${valibotAction.type}" action is not supported on type "${jsonSchema.type}".`);
|
|
100
|
+
jsonSchema.maxLength = 0;
|
|
101
|
+
}
|
|
102
|
+
break;
|
|
103
|
+
case "entries":
|
|
104
|
+
jsonSchema.minProperties = valibotAction.requirement;
|
|
105
|
+
jsonSchema.maxProperties = valibotAction.requirement;
|
|
106
|
+
break;
|
|
107
|
+
case "examples":
|
|
108
|
+
if (Array.isArray(jsonSchema.examples))
|
|
109
|
+
jsonSchema.examples = [...jsonSchema.examples, ...valibotAction.examples];
|
|
110
|
+
else
|
|
111
|
+
jsonSchema.examples = valibotAction.examples;
|
|
112
|
+
break;
|
|
113
|
+
case "gt_value":
|
|
114
|
+
if (jsonSchema.type !== "number" && jsonSchema.type !== "integer")
|
|
115
|
+
errors = addError(errors, `The "gt_value" action is not supported on type "${jsonSchema.type}".`);
|
|
116
|
+
if (config?.target === "openapi-3.0") {
|
|
117
|
+
errors = addError(errors, 'The "gt_value" action is not supported for OpenAPI 3.0.');
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
jsonSchema.exclusiveMinimum = valibotAction.requirement;
|
|
121
|
+
break;
|
|
122
|
+
case "includes":
|
|
123
|
+
if (jsonSchema.pattern)
|
|
124
|
+
errors = addError(errors, `The "${valibotAction.type}" action is not supported in combination with another regex action.`);
|
|
125
|
+
else
|
|
126
|
+
jsonSchema.pattern = escapeRegExp(valibotAction.requirement);
|
|
127
|
+
break;
|
|
128
|
+
case "integer":
|
|
129
|
+
jsonSchema.type = "integer";
|
|
130
|
+
break;
|
|
131
|
+
case "ipv4":
|
|
132
|
+
jsonSchema.format = "ipv4";
|
|
133
|
+
break;
|
|
134
|
+
case "ipv6":
|
|
135
|
+
jsonSchema.format = "ipv6";
|
|
136
|
+
break;
|
|
137
|
+
case "iso_date":
|
|
138
|
+
jsonSchema.format = "date";
|
|
139
|
+
break;
|
|
140
|
+
case "iso_date_time":
|
|
141
|
+
case "iso_timestamp":
|
|
142
|
+
jsonSchema.format = "date-time";
|
|
143
|
+
break;
|
|
144
|
+
case "iso_time":
|
|
145
|
+
jsonSchema.format = "time";
|
|
146
|
+
break;
|
|
147
|
+
case "jws_compact":
|
|
148
|
+
if (jsonSchema.pattern)
|
|
149
|
+
errors = addError(errors, `The "${valibotAction.type}" action is not supported in combination with another regex action.`);
|
|
150
|
+
else
|
|
151
|
+
jsonSchema.pattern = valibotAction.requirement.source;
|
|
152
|
+
break;
|
|
153
|
+
case "length":
|
|
154
|
+
if (jsonSchema.type === "array") {
|
|
155
|
+
jsonSchema.minItems = valibotAction.requirement;
|
|
156
|
+
jsonSchema.maxItems = valibotAction.requirement;
|
|
157
|
+
} else {
|
|
158
|
+
if (jsonSchema.type !== "string")
|
|
159
|
+
errors = addError(errors, `The "${valibotAction.type}" action is not supported on type "${jsonSchema.type}".`);
|
|
160
|
+
jsonSchema.minLength = valibotAction.requirement;
|
|
161
|
+
jsonSchema.maxLength = valibotAction.requirement;
|
|
162
|
+
}
|
|
163
|
+
break;
|
|
164
|
+
case "lt_value":
|
|
165
|
+
if (jsonSchema.type !== "number" && jsonSchema.type !== "integer")
|
|
166
|
+
errors = addError(errors, `The "lt_value" action is not supported on type "${jsonSchema.type}".`);
|
|
167
|
+
if (config?.target === "openapi-3.0") {
|
|
168
|
+
errors = addError(errors, 'The "lt_value" action is not supported for OpenAPI 3.0.');
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
jsonSchema.exclusiveMaximum = valibotAction.requirement;
|
|
172
|
+
break;
|
|
173
|
+
case "max_entries":
|
|
174
|
+
jsonSchema.maxProperties = valibotAction.requirement;
|
|
175
|
+
break;
|
|
176
|
+
case "max_length":
|
|
177
|
+
if (jsonSchema.type === "array")
|
|
178
|
+
jsonSchema.maxItems = valibotAction.requirement;
|
|
179
|
+
else {
|
|
180
|
+
if (jsonSchema.type !== "string")
|
|
181
|
+
errors = addError(errors, `The "${valibotAction.type}" action is not supported on type "${jsonSchema.type}".`);
|
|
182
|
+
jsonSchema.maxLength = valibotAction.requirement;
|
|
183
|
+
}
|
|
184
|
+
break;
|
|
185
|
+
case "max_value":
|
|
186
|
+
if (jsonSchema.type !== "number" && jsonSchema.type !== "integer")
|
|
187
|
+
errors = addError(errors, `The "max_value" action is not supported on type "${jsonSchema.type}".`);
|
|
188
|
+
jsonSchema.maximum = valibotAction.requirement;
|
|
189
|
+
break;
|
|
190
|
+
case "metadata":
|
|
191
|
+
if (typeof valibotAction.metadata.title === "string")
|
|
192
|
+
jsonSchema.title = valibotAction.metadata.title;
|
|
193
|
+
if (typeof valibotAction.metadata.description === "string")
|
|
194
|
+
jsonSchema.description = valibotAction.metadata.description;
|
|
195
|
+
if (Array.isArray(valibotAction.metadata.examples))
|
|
196
|
+
if (Array.isArray(jsonSchema.examples))
|
|
197
|
+
jsonSchema.examples = [...jsonSchema.examples, ...valibotAction.metadata.examples];
|
|
198
|
+
else
|
|
199
|
+
jsonSchema.examples = valibotAction.metadata.examples;
|
|
200
|
+
break;
|
|
201
|
+
case "min_entries":
|
|
202
|
+
jsonSchema.minProperties = valibotAction.requirement;
|
|
203
|
+
break;
|
|
204
|
+
case "min_length":
|
|
205
|
+
if (jsonSchema.type === "array")
|
|
206
|
+
jsonSchema.minItems = valibotAction.requirement;
|
|
207
|
+
else {
|
|
208
|
+
if (jsonSchema.type !== "string")
|
|
209
|
+
errors = addError(errors, `The "${valibotAction.type}" action is not supported on type "${jsonSchema.type}".`);
|
|
210
|
+
jsonSchema.minLength = valibotAction.requirement;
|
|
211
|
+
}
|
|
212
|
+
break;
|
|
213
|
+
case "min_value":
|
|
214
|
+
if (jsonSchema.type !== "number" && jsonSchema.type !== "integer")
|
|
215
|
+
errors = addError(errors, `The "min_value" action is not supported on type "${jsonSchema.type}".`);
|
|
216
|
+
jsonSchema.minimum = valibotAction.requirement;
|
|
217
|
+
break;
|
|
218
|
+
case "multiple_of":
|
|
219
|
+
jsonSchema.multipleOf = valibotAction.requirement;
|
|
220
|
+
break;
|
|
221
|
+
case "non_empty":
|
|
222
|
+
if (jsonSchema.type === "array")
|
|
223
|
+
jsonSchema.minItems = 1;
|
|
224
|
+
else {
|
|
225
|
+
if (jsonSchema.type !== "string")
|
|
226
|
+
errors = addError(errors, `The "${valibotAction.type}" action is not supported on type "${jsonSchema.type}".`);
|
|
227
|
+
jsonSchema.minLength = 1;
|
|
228
|
+
}
|
|
229
|
+
break;
|
|
230
|
+
case "not_value":
|
|
231
|
+
if (!isJsonConstValue(valibotAction.requirement)) {
|
|
232
|
+
errors = addError(errors, 'The requirement of the "not_value" action is not JSON compatible.');
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
if (config?.target === "openapi-3.0")
|
|
236
|
+
jsonSchema.not = { enum: [valibotAction.requirement] };
|
|
237
|
+
else
|
|
238
|
+
jsonSchema.not = { const: valibotAction.requirement };
|
|
239
|
+
break;
|
|
240
|
+
case "not_values":
|
|
241
|
+
if (!isJsonEnumValues(valibotAction.requirement)) {
|
|
242
|
+
errors = addError(errors, 'A requirement of the "not_values" action is not JSON compatible.');
|
|
243
|
+
break;
|
|
244
|
+
}
|
|
245
|
+
jsonSchema.not = { enum: valibotAction.requirement };
|
|
246
|
+
break;
|
|
247
|
+
case "regex":
|
|
248
|
+
if (valibotAction.requirement.flags)
|
|
249
|
+
errors = addError(errors, "RegExp flags are not supported by JSON Schema.");
|
|
250
|
+
if (jsonSchema.pattern)
|
|
251
|
+
errors = addError(errors, `The "${valibotAction.type}" action is not supported in combination with another regex action.`);
|
|
252
|
+
else
|
|
253
|
+
jsonSchema.pattern = valibotAction.requirement.source;
|
|
254
|
+
break;
|
|
255
|
+
case "safe_integer":
|
|
256
|
+
jsonSchema.type = "integer";
|
|
257
|
+
if (typeof jsonSchema.minimum !== "number" || jsonSchema.minimum < Number.MIN_SAFE_INTEGER)
|
|
258
|
+
jsonSchema.minimum = Number.MIN_SAFE_INTEGER;
|
|
259
|
+
if (typeof jsonSchema.maximum !== "number" || jsonSchema.maximum > Number.MAX_SAFE_INTEGER)
|
|
260
|
+
jsonSchema.maximum = Number.MAX_SAFE_INTEGER;
|
|
261
|
+
break;
|
|
262
|
+
case "starts_with":
|
|
263
|
+
if (jsonSchema.pattern)
|
|
264
|
+
errors = addError(errors, `The "${valibotAction.type}" action is not supported in combination with another regex action.`);
|
|
265
|
+
else
|
|
266
|
+
jsonSchema.pattern = `^${escapeRegExp(valibotAction.requirement)}`;
|
|
267
|
+
break;
|
|
268
|
+
case "title":
|
|
269
|
+
jsonSchema.title = valibotAction.title;
|
|
270
|
+
break;
|
|
271
|
+
case "url":
|
|
272
|
+
jsonSchema.format = "uri";
|
|
273
|
+
break;
|
|
274
|
+
case "uuid":
|
|
275
|
+
jsonSchema.format = "uuid";
|
|
276
|
+
break;
|
|
277
|
+
case "value":
|
|
278
|
+
if (!isJsonConstValue(valibotAction.requirement)) {
|
|
279
|
+
errors = addError(errors, 'The requirement of the "value" action is not JSON compatible.');
|
|
280
|
+
break;
|
|
281
|
+
}
|
|
282
|
+
if (config?.target === "openapi-3.0")
|
|
283
|
+
jsonSchema.enum = [valibotAction.requirement];
|
|
284
|
+
else
|
|
285
|
+
jsonSchema.const = valibotAction.requirement;
|
|
286
|
+
break;
|
|
287
|
+
case "values":
|
|
288
|
+
if (!isJsonEnumValues(valibotAction.requirement)) {
|
|
289
|
+
errors = addError(errors, 'A requirement of the "values" action is not JSON compatible.');
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
jsonSchema.enum = valibotAction.requirement;
|
|
293
|
+
break;
|
|
294
|
+
default:
|
|
295
|
+
errors = addError(errors, `The "${valibotAction.type}" action cannot be converted to JSON Schema.`);
|
|
296
|
+
}
|
|
297
|
+
if (config?.overrideAction) {
|
|
298
|
+
const actionOverride = config.overrideAction({
|
|
299
|
+
valibotAction,
|
|
300
|
+
jsonSchema,
|
|
301
|
+
errors
|
|
302
|
+
});
|
|
303
|
+
if (actionOverride)
|
|
304
|
+
return { ...actionOverride };
|
|
305
|
+
}
|
|
306
|
+
if (errors)
|
|
307
|
+
for (const message of errors)
|
|
308
|
+
handleError(message, config);
|
|
309
|
+
return jsonSchema;
|
|
310
|
+
}
|
|
311
|
+
function flattenPipe(pipe) {
|
|
312
|
+
return pipe.flatMap((item) => ("pipe" in item) ? flattenPipe(item.pipe) : item);
|
|
313
|
+
}
|
|
314
|
+
function convertSchema(jsonSchema, valibotSchema, config, context, skipRef = false) {
|
|
315
|
+
if (!skipRef) {
|
|
316
|
+
const referenceId = context.referenceMap.get(valibotSchema);
|
|
317
|
+
if (referenceId) {
|
|
318
|
+
jsonSchema.$ref = `#/$defs/${referenceId}`;
|
|
319
|
+
if (config?.overrideRef) {
|
|
320
|
+
const refOverride = config.overrideRef({
|
|
321
|
+
...context,
|
|
322
|
+
referenceId,
|
|
323
|
+
valibotSchema,
|
|
324
|
+
jsonSchema
|
|
325
|
+
});
|
|
326
|
+
if (refOverride)
|
|
327
|
+
jsonSchema.$ref = refOverride;
|
|
328
|
+
}
|
|
329
|
+
return jsonSchema;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
if ("pipe" in valibotSchema) {
|
|
333
|
+
const flatPipe = flattenPipe(valibotSchema.pipe);
|
|
334
|
+
let startIndex = 0;
|
|
335
|
+
let stopIndex = flatPipe.length - 1;
|
|
336
|
+
if (config?.typeMode === "input") {
|
|
337
|
+
const inputStopIndex = flatPipe.slice(1).findIndex((item) => item.kind === "schema" || item.kind === "transformation" && (item.type === "find_item" || item.type === "parse_json" || item.type === "raw_transform" || item.type === "reduce_items" || item.type === "stringify_json" || item.type === "to_bigint" || item.type === "to_boolean" || item.type === "to_date" || item.type === "to_number" || item.type === "to_string" || item.type === "transform"));
|
|
338
|
+
if (inputStopIndex !== -1)
|
|
339
|
+
stopIndex = inputStopIndex;
|
|
340
|
+
} else if (config?.typeMode === "output") {
|
|
341
|
+
const outputStartIndex = flatPipe.findLastIndex((item) => item.kind === "schema");
|
|
342
|
+
if (outputStartIndex !== -1)
|
|
343
|
+
startIndex = outputStartIndex;
|
|
344
|
+
}
|
|
345
|
+
for (let index = startIndex;index <= stopIndex; index++) {
|
|
346
|
+
const valibotPipeItem = flatPipe[index];
|
|
347
|
+
if (valibotPipeItem.kind === "schema") {
|
|
348
|
+
if (index > startIndex)
|
|
349
|
+
handleError('Set the "typeMode" config to "input" or "output" to convert pipelines with multiple schemas.', config);
|
|
350
|
+
jsonSchema = convertSchema(jsonSchema, valibotPipeItem, config, context, true);
|
|
351
|
+
} else
|
|
352
|
+
jsonSchema = convertAction(jsonSchema, valibotPipeItem, config);
|
|
353
|
+
}
|
|
354
|
+
return jsonSchema;
|
|
355
|
+
}
|
|
356
|
+
let errors;
|
|
357
|
+
switch (valibotSchema.type) {
|
|
358
|
+
case "boolean":
|
|
359
|
+
jsonSchema.type = "boolean";
|
|
360
|
+
break;
|
|
361
|
+
case "null":
|
|
362
|
+
if (config?.target === "openapi-3.0")
|
|
363
|
+
jsonSchema.enum = [null];
|
|
364
|
+
else
|
|
365
|
+
jsonSchema.type = "null";
|
|
366
|
+
break;
|
|
367
|
+
case "number":
|
|
368
|
+
jsonSchema.type = "number";
|
|
369
|
+
break;
|
|
370
|
+
case "string":
|
|
371
|
+
jsonSchema.type = "string";
|
|
372
|
+
break;
|
|
373
|
+
case "array":
|
|
374
|
+
jsonSchema.type = "array";
|
|
375
|
+
jsonSchema.items = convertSchema({}, valibotSchema.item, config, context);
|
|
376
|
+
break;
|
|
377
|
+
case "tuple":
|
|
378
|
+
case "tuple_with_rest":
|
|
379
|
+
case "loose_tuple":
|
|
380
|
+
case "strict_tuple":
|
|
381
|
+
jsonSchema.type = "array";
|
|
382
|
+
if (config?.target === "openapi-3.0") {
|
|
383
|
+
jsonSchema.items = { anyOf: [] };
|
|
384
|
+
jsonSchema.minItems = valibotSchema.items.length;
|
|
385
|
+
for (const item of valibotSchema.items)
|
|
386
|
+
jsonSchema.items.anyOf.push(convertSchema({}, item, config, context));
|
|
387
|
+
if (valibotSchema.type === "tuple_with_rest")
|
|
388
|
+
jsonSchema.items.anyOf.push(convertSchema({}, valibotSchema.rest, config, context));
|
|
389
|
+
else if (valibotSchema.type === "strict_tuple" || valibotSchema.type === "tuple")
|
|
390
|
+
jsonSchema.maxItems = valibotSchema.items.length;
|
|
391
|
+
} else if (config?.target === "draft-2020-12") {
|
|
392
|
+
jsonSchema.prefixItems = [];
|
|
393
|
+
jsonSchema.minItems = valibotSchema.items.length;
|
|
394
|
+
for (const item of valibotSchema.items)
|
|
395
|
+
jsonSchema.prefixItems.push(convertSchema({}, item, config, context));
|
|
396
|
+
if (valibotSchema.type === "tuple_with_rest")
|
|
397
|
+
jsonSchema.items = convertSchema({}, valibotSchema.rest, config, context);
|
|
398
|
+
else if (valibotSchema.type === "strict_tuple")
|
|
399
|
+
jsonSchema.items = false;
|
|
400
|
+
} else {
|
|
401
|
+
jsonSchema.items = [];
|
|
402
|
+
jsonSchema.minItems = valibotSchema.items.length;
|
|
403
|
+
for (const item of valibotSchema.items)
|
|
404
|
+
jsonSchema.items.push(convertSchema({}, item, config, context));
|
|
405
|
+
if (valibotSchema.type === "tuple_with_rest")
|
|
406
|
+
jsonSchema.additionalItems = convertSchema({}, valibotSchema.rest, config, context);
|
|
407
|
+
else if (valibotSchema.type === "strict_tuple")
|
|
408
|
+
jsonSchema.additionalItems = false;
|
|
409
|
+
}
|
|
410
|
+
break;
|
|
411
|
+
case "object":
|
|
412
|
+
case "object_with_rest":
|
|
413
|
+
case "loose_object":
|
|
414
|
+
case "strict_object":
|
|
415
|
+
jsonSchema.type = "object";
|
|
416
|
+
jsonSchema.properties = {};
|
|
417
|
+
jsonSchema.required = [];
|
|
418
|
+
for (const key in valibotSchema.entries) {
|
|
419
|
+
const entry = valibotSchema.entries[key];
|
|
420
|
+
jsonSchema.properties[key] = convertSchema({}, entry, config, context);
|
|
421
|
+
if (entry.type !== "exact_optional" && entry.type !== "nullish" && entry.type !== "optional")
|
|
422
|
+
jsonSchema.required.push(key);
|
|
423
|
+
}
|
|
424
|
+
if (valibotSchema.type === "object_with_rest")
|
|
425
|
+
jsonSchema.additionalProperties = convertSchema({}, valibotSchema.rest, config, context);
|
|
426
|
+
else if (valibotSchema.type === "strict_object")
|
|
427
|
+
jsonSchema.additionalProperties = false;
|
|
428
|
+
break;
|
|
429
|
+
case "record":
|
|
430
|
+
if (config?.target === "openapi-3.0" && "pipe" in valibotSchema.key)
|
|
431
|
+
errors = addError(errors, 'The "record" schema with a schema for the key that contains a "pipe" cannot be converted to JSON Schema.');
|
|
432
|
+
if (valibotSchema.key.type !== "string")
|
|
433
|
+
errors = addError(errors, `The "record" schema with the "${valibotSchema.key.type}" schema for the key cannot be converted to JSON Schema.`);
|
|
434
|
+
jsonSchema.type = "object";
|
|
435
|
+
if (config?.target !== "openapi-3.0")
|
|
436
|
+
jsonSchema.propertyNames = convertSchema({}, valibotSchema.key, config, context);
|
|
437
|
+
jsonSchema.additionalProperties = convertSchema({}, valibotSchema.value, config, context);
|
|
438
|
+
break;
|
|
439
|
+
case "any":
|
|
440
|
+
case "unknown":
|
|
441
|
+
break;
|
|
442
|
+
case "never":
|
|
443
|
+
jsonSchema.not = {};
|
|
444
|
+
break;
|
|
445
|
+
case "nullable":
|
|
446
|
+
case "nullish":
|
|
447
|
+
if (config?.target === "openapi-3.0") {
|
|
448
|
+
const innerSchema = convertSchema({}, valibotSchema.wrapped, config, context);
|
|
449
|
+
Object.assign(jsonSchema, innerSchema);
|
|
450
|
+
jsonSchema.nullable = true;
|
|
451
|
+
} else
|
|
452
|
+
jsonSchema.anyOf = [convertSchema({}, valibotSchema.wrapped, config, context), { type: "null" }];
|
|
453
|
+
if (valibotSchema.default !== undefined)
|
|
454
|
+
jsonSchema.default = typeof valibotSchema.default === "function" ? valibotSchema.default() : valibotSchema.default;
|
|
455
|
+
break;
|
|
456
|
+
case "exact_optional":
|
|
457
|
+
case "optional":
|
|
458
|
+
case "undefinedable":
|
|
459
|
+
jsonSchema = convertSchema(jsonSchema, valibotSchema.wrapped, config, context);
|
|
460
|
+
if (valibotSchema.default !== undefined)
|
|
461
|
+
jsonSchema.default = typeof valibotSchema.default === "function" ? valibotSchema.default() : valibotSchema.default;
|
|
462
|
+
break;
|
|
463
|
+
case "literal":
|
|
464
|
+
if (typeof valibotSchema.literal !== "boolean" && typeof valibotSchema.literal !== "number" && typeof valibotSchema.literal !== "string")
|
|
465
|
+
errors = addError(errors, 'The value of the "literal" schema is not JSON compatible.');
|
|
466
|
+
if (config?.target === "openapi-3.0")
|
|
467
|
+
jsonSchema.enum = [valibotSchema.literal];
|
|
468
|
+
else
|
|
469
|
+
jsonSchema.const = valibotSchema.literal;
|
|
470
|
+
break;
|
|
471
|
+
case "enum":
|
|
472
|
+
jsonSchema.enum = valibotSchema.options;
|
|
473
|
+
if (valibotSchema.options.every((option) => typeof option === "string"))
|
|
474
|
+
jsonSchema.type = "string";
|
|
475
|
+
else if (valibotSchema.options.every((option) => typeof option === "number"))
|
|
476
|
+
jsonSchema.type = "number";
|
|
477
|
+
else if (config?.target !== "openapi-3.0")
|
|
478
|
+
jsonSchema.type = ["string", "number"];
|
|
479
|
+
break;
|
|
480
|
+
case "picklist": {
|
|
481
|
+
const hasInvalidOption = valibotSchema.options.some((option) => typeof option !== "number" && typeof option !== "string");
|
|
482
|
+
if (hasInvalidOption)
|
|
483
|
+
errors = addError(errors, 'An option of the "picklist" schema is not JSON compatible.');
|
|
484
|
+
jsonSchema.enum = valibotSchema.options;
|
|
485
|
+
if (valibotSchema.options.every((option) => typeof option === "string"))
|
|
486
|
+
jsonSchema.type = "string";
|
|
487
|
+
else if (valibotSchema.options.every((option) => typeof option === "number"))
|
|
488
|
+
jsonSchema.type = "number";
|
|
489
|
+
else if (!hasInvalidOption && config?.target !== "openapi-3.0")
|
|
490
|
+
jsonSchema.type = ["string", "number"];
|
|
491
|
+
break;
|
|
492
|
+
}
|
|
493
|
+
case "union":
|
|
494
|
+
jsonSchema.anyOf = valibotSchema.options.map((option) => convertSchema({}, option, config, context));
|
|
495
|
+
break;
|
|
496
|
+
case "variant":
|
|
497
|
+
jsonSchema.oneOf = valibotSchema.options.map((option) => convertSchema({}, option, config, context));
|
|
498
|
+
break;
|
|
499
|
+
case "intersect":
|
|
500
|
+
jsonSchema.allOf = valibotSchema.options.map((option) => convertSchema({}, option, config, context));
|
|
501
|
+
break;
|
|
502
|
+
case "lazy": {
|
|
503
|
+
let wrappedValibotSchema = context.getterMap.get(valibotSchema.getter);
|
|
504
|
+
if (!wrappedValibotSchema) {
|
|
505
|
+
wrappedValibotSchema = valibotSchema.getter(undefined);
|
|
506
|
+
context.getterMap.set(valibotSchema.getter, wrappedValibotSchema);
|
|
507
|
+
}
|
|
508
|
+
let referenceId = context.referenceMap.get(wrappedValibotSchema);
|
|
509
|
+
if (!referenceId) {
|
|
510
|
+
referenceId = `${refCount++}`;
|
|
511
|
+
context.referenceMap.set(wrappedValibotSchema, referenceId);
|
|
512
|
+
context.definitions[referenceId] = convertSchema({}, wrappedValibotSchema, config, context, true);
|
|
513
|
+
}
|
|
514
|
+
jsonSchema.$ref = `#/$defs/${referenceId}`;
|
|
515
|
+
if (config?.overrideRef) {
|
|
516
|
+
const refOverride = config.overrideRef({
|
|
517
|
+
...context,
|
|
518
|
+
referenceId,
|
|
519
|
+
valibotSchema: wrappedValibotSchema,
|
|
520
|
+
jsonSchema
|
|
521
|
+
});
|
|
522
|
+
if (refOverride)
|
|
523
|
+
jsonSchema.$ref = refOverride;
|
|
524
|
+
}
|
|
525
|
+
break;
|
|
526
|
+
}
|
|
527
|
+
default:
|
|
528
|
+
errors = addError(errors, `The "${valibotSchema.type}" schema cannot be converted to JSON Schema.`);
|
|
529
|
+
}
|
|
530
|
+
if (config?.overrideSchema) {
|
|
531
|
+
const schemaOverride = config.overrideSchema({
|
|
532
|
+
...context,
|
|
533
|
+
referenceId: context.referenceMap.get(valibotSchema),
|
|
534
|
+
valibotSchema,
|
|
535
|
+
jsonSchema,
|
|
536
|
+
errors
|
|
537
|
+
});
|
|
538
|
+
if (schemaOverride)
|
|
539
|
+
return { ...schemaOverride };
|
|
540
|
+
}
|
|
541
|
+
if (errors)
|
|
542
|
+
for (const message of errors)
|
|
543
|
+
handleError(message, config);
|
|
544
|
+
return jsonSchema;
|
|
545
|
+
}
|
|
546
|
+
function addGlobalDefs(definitions) {
|
|
547
|
+
store = {
|
|
548
|
+
...store ?? {},
|
|
549
|
+
...definitions
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
function getGlobalDefs() {
|
|
553
|
+
return store;
|
|
554
|
+
}
|
|
555
|
+
function toJsonSchema(schema, config) {
|
|
556
|
+
const context = {
|
|
557
|
+
definitions: {},
|
|
558
|
+
referenceMap: /* @__PURE__ */ new Map,
|
|
559
|
+
getterMap: /* @__PURE__ */ new Map
|
|
560
|
+
};
|
|
561
|
+
const definitions = config?.definitions ?? getGlobalDefs();
|
|
562
|
+
if (definitions) {
|
|
563
|
+
for (const key in definitions)
|
|
564
|
+
context.referenceMap.set(definitions[key], key);
|
|
565
|
+
for (const key in definitions)
|
|
566
|
+
context.definitions[key] = convertSchema({}, definitions[key], config, context, true);
|
|
567
|
+
}
|
|
568
|
+
const jsonSchema = convertSchema({}, schema, config, context);
|
|
569
|
+
const target = config?.target ?? "draft-07";
|
|
570
|
+
if (target === "draft-2020-12")
|
|
571
|
+
jsonSchema.$schema = "https://json-schema.org/draft/2020-12/schema";
|
|
572
|
+
else if (target === "draft-07")
|
|
573
|
+
jsonSchema.$schema = "http://json-schema.org/draft-07/schema#";
|
|
574
|
+
if (context.referenceMap.size)
|
|
575
|
+
jsonSchema.$defs = context.definitions;
|
|
576
|
+
return jsonSchema;
|
|
577
|
+
}
|
|
578
|
+
function toJsonSchemaDefs(definitions, config) {
|
|
579
|
+
const context = {
|
|
580
|
+
definitions: {},
|
|
581
|
+
referenceMap: /* @__PURE__ */ new Map,
|
|
582
|
+
getterMap: /* @__PURE__ */ new Map
|
|
583
|
+
};
|
|
584
|
+
for (const key in definitions)
|
|
585
|
+
context.referenceMap.set(definitions[key], key);
|
|
586
|
+
for (const key in definitions)
|
|
587
|
+
context.definitions[key] = convertSchema({}, definitions[key], config, context, true);
|
|
588
|
+
return context.definitions;
|
|
589
|
+
}
|
|
590
|
+
function toStandardJsonSchema(schema) {
|
|
591
|
+
return { "~standard": {
|
|
592
|
+
...schema["~standard"],
|
|
593
|
+
jsonSchema: {
|
|
594
|
+
input(options) {
|
|
595
|
+
if (SUPPORTED_TARGETS.includes(options.target))
|
|
596
|
+
return toJsonSchema(schema, {
|
|
597
|
+
typeMode: "input",
|
|
598
|
+
target: options.target,
|
|
599
|
+
...options.libraryOptions
|
|
600
|
+
});
|
|
601
|
+
throw new Error(`Unsupported target: ${options.target}`);
|
|
602
|
+
},
|
|
603
|
+
output(options) {
|
|
604
|
+
if (SUPPORTED_TARGETS.includes(options.target))
|
|
605
|
+
return toJsonSchema(schema, {
|
|
606
|
+
typeMode: "output",
|
|
607
|
+
target: options.target,
|
|
608
|
+
...options.libraryOptions
|
|
609
|
+
});
|
|
610
|
+
throw new Error(`Unsupported target: ${options.target}`);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
} };
|
|
614
|
+
}
|
|
615
|
+
var ESCAPE_REGEX, refCount = 0, store, SUPPORTED_TARGETS;
|
|
616
|
+
var init_dist = __esm(() => {
|
|
617
|
+
ESCAPE_REGEX = /[.*+?^${}()|[\]\\]/g;
|
|
618
|
+
SUPPORTED_TARGETS = [
|
|
619
|
+
"draft-07",
|
|
620
|
+
"draft-2020-12",
|
|
621
|
+
"openapi-3.0"
|
|
622
|
+
];
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
// ../helpers/dist/mjst-extension.js
|
|
626
|
+
var MJST_EXTENSION_KEY = "x-mjst";
|
|
627
|
+
var SUPPORTED_PRIMITIVES = new Set(["bigint"]);
|
|
628
|
+
|
|
629
|
+
// src/valibot-to-json-schema.ts
|
|
630
|
+
var loadToJsonSchema = async () => {
|
|
631
|
+
let mod;
|
|
632
|
+
try {
|
|
633
|
+
mod = await Promise.resolve().then(() => (init_dist(), exports_dist));
|
|
634
|
+
} catch {
|
|
635
|
+
throw new Error("The Valibot adapter requires '@valibot/to-json-schema' (and 'valibot') to be installed in your project.");
|
|
636
|
+
}
|
|
637
|
+
const named = mod["toJsonSchema"];
|
|
638
|
+
const fromDefault = mod["default"]?.["toJsonSchema"];
|
|
639
|
+
const converter = typeof named === "function" ? named : typeof fromDefault === "function" ? fromDefault : undefined;
|
|
640
|
+
if (!converter) {
|
|
641
|
+
throw new Error("'@valibot/to-json-schema' did not export 'toJsonSchema'.");
|
|
642
|
+
}
|
|
643
|
+
return converter;
|
|
644
|
+
};
|
|
645
|
+
var valibotToJsonSchema = async (source) => {
|
|
646
|
+
if (typeof source !== "object" || source === null) {
|
|
647
|
+
const received = source === null ? "null" : typeof source;
|
|
648
|
+
throw new Error(`Valibot adapter expected a Valibot schema but received ${received}.`);
|
|
649
|
+
}
|
|
650
|
+
const toJsonSchema2 = await loadToJsonSchema();
|
|
651
|
+
let json;
|
|
652
|
+
try {
|
|
653
|
+
json = toJsonSchema2(source, {
|
|
654
|
+
errorMode: "ignore",
|
|
655
|
+
overrideSchema: (ctx) => {
|
|
656
|
+
if (ctx.valibotSchema?.type === "date")
|
|
657
|
+
return { [MJST_EXTENSION_KEY]: { instanceOf: "Date" } };
|
|
658
|
+
if (ctx.valibotSchema?.type === "bigint")
|
|
659
|
+
return { [MJST_EXTENSION_KEY]: { primitive: "bigint" } };
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
});
|
|
663
|
+
} catch (error) {
|
|
664
|
+
throw new Error(`Valibot adapter failed to convert the schema. Is it a valid Valibot schema?
|
|
665
|
+
${String(error)}`);
|
|
666
|
+
}
|
|
667
|
+
delete json["$schema"];
|
|
668
|
+
return json;
|
|
669
|
+
};
|
|
670
|
+
export {
|
|
671
|
+
valibotToJsonSchema
|
|
672
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
|
|
2
|
+
/**
|
|
3
|
+
* Converts a Zod schema into a Draft 2020-12 JSON Schema using Zod 4's native
|
|
4
|
+
* `toJSONSchema`.
|
|
5
|
+
*
|
|
6
|
+
* Zod's `z.date()` has no JSON Schema representation and would otherwise throw.
|
|
7
|
+
* We pass `unrepresentable: 'any'` so conversion never fails on it, then use the
|
|
8
|
+
* `override` hook to rewrite date schemas into an `x-mjst` instanceOf hint — the
|
|
9
|
+
* same extension TypeBox dates use — so generated types and runtime checks treat
|
|
10
|
+
* them as `Date`.
|
|
11
|
+
*/
|
|
12
|
+
export declare const zodToJsonSchema: (source: unknown) => Promise<JSONSchema>;
|