@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,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The schema authoring formats mjst can ingest.
|
|
3
|
+
*
|
|
4
|
+
* `'json'` is the built-in default: a plain JSON Schema file read from disk and
|
|
5
|
+
* handed straight to the generators. The others name external libraries whose
|
|
6
|
+
* schemas are first converted to JSON Schema by a matching adapter.
|
|
7
|
+
*
|
|
8
|
+
* A format may appear here before its adapter exists — `getAdapter` is the
|
|
9
|
+
* source of truth for what is actually implemented today.
|
|
10
|
+
*/
|
|
11
|
+
export type SourceFormat = 'json' | 'typebox' | 'zod' | 'valibot' | 'effect';
|
|
File without changes
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
|
|
2
|
+
/**
|
|
3
|
+
* TypeBox schemas are already JSON Schema objects at runtime, but they carry
|
|
4
|
+
* non-enumerable symbol keys (`Kind`, `Optional`, ...) that TypeBox uses for its
|
|
5
|
+
* own type machinery. A JSON round-trip drops those symbols (and any `undefined`
|
|
6
|
+
* values), leaving a clean, plain JSON Schema. We then rewrite TypeBox's
|
|
7
|
+
* extended types (Date, ...) into `x-mjst` hints the generators understand.
|
|
8
|
+
*
|
|
9
|
+
* We deliberately do not import TypeBox here: the conversion only touches the
|
|
10
|
+
* plain-object shape, so TypeBox stays an optional peer dependency used solely
|
|
11
|
+
* by the consumer's schema module, never by mjst itself.
|
|
12
|
+
*/
|
|
13
|
+
export declare const typeboxToJsonSchema: (source: unknown) => JSONSchema;
|
|
@@ -0,0 +1,61 @@
|
|
|
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
|
+
// ../helpers/dist/mjst-extension.js
|
|
14
|
+
var MJST_EXTENSION_KEY = "x-mjst";
|
|
15
|
+
var SUPPORTED_PRIMITIVES = new Set(["bigint"]);
|
|
16
|
+
|
|
17
|
+
// src/typebox-to-json-schema.ts
|
|
18
|
+
var JSON_SCHEMA_TYPES = new Set(["string", "number", "integer", "boolean", "object", "array", "null"]);
|
|
19
|
+
var EXTENDED_TYPE_TO_INSTANCE = {
|
|
20
|
+
Date: "Date"
|
|
21
|
+
};
|
|
22
|
+
var EXTENDED_TYPE_TO_PRIMITIVE = {
|
|
23
|
+
bigint: "bigint"
|
|
24
|
+
};
|
|
25
|
+
var rewriteExtendedTypes = (value) => {
|
|
26
|
+
if (Array.isArray(value))
|
|
27
|
+
return value.map(rewriteExtendedTypes);
|
|
28
|
+
if (typeof value !== "object" || value === null)
|
|
29
|
+
return value;
|
|
30
|
+
const source = value;
|
|
31
|
+
const result = {};
|
|
32
|
+
for (const [key, val] of Object.entries(source)) {
|
|
33
|
+
result[key] = rewriteExtendedTypes(val);
|
|
34
|
+
}
|
|
35
|
+
const type = result["type"];
|
|
36
|
+
if (typeof type === "string" && !JSON_SCHEMA_TYPES.has(type)) {
|
|
37
|
+
const primitive = EXTENDED_TYPE_TO_PRIMITIVE[type];
|
|
38
|
+
const instanceOf = EXTENDED_TYPE_TO_INSTANCE[type];
|
|
39
|
+
if (primitive) {
|
|
40
|
+
delete result["type"];
|
|
41
|
+
result[MJST_EXTENSION_KEY] = { primitive };
|
|
42
|
+
} else if (instanceOf) {
|
|
43
|
+
delete result["type"];
|
|
44
|
+
result[MJST_EXTENSION_KEY] = { instanceOf };
|
|
45
|
+
} else {
|
|
46
|
+
console.warn(`[mjst] TypeBox type '${type}' has no JSON Schema or x-mjst mapping; leaving it unchanged.`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return result;
|
|
50
|
+
};
|
|
51
|
+
var typeboxToJsonSchema = (source) => {
|
|
52
|
+
if (typeof source !== "object" || source === null) {
|
|
53
|
+
const received = source === null ? "null" : typeof source;
|
|
54
|
+
throw new Error(`TypeBox adapter expected a schema object but received ${received}.`);
|
|
55
|
+
}
|
|
56
|
+
const sanitized = JSON.parse(JSON.stringify(source));
|
|
57
|
+
return rewriteExtendedTypes(sanitized);
|
|
58
|
+
};
|
|
59
|
+
export {
|
|
60
|
+
typeboxToJsonSchema
|
|
61
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
|
|
2
|
+
/**
|
|
3
|
+
* Converts a Valibot schema into a Draft 2020-12 JSON Schema via
|
|
4
|
+
* `@valibot/to-json-schema`.
|
|
5
|
+
*
|
|
6
|
+
* Valibot's `date` schema has no JSON Schema representation and would otherwise
|
|
7
|
+
* throw, so we run with `errorMode: 'ignore'` (other unsupported constructs
|
|
8
|
+
* degrade to an open schema rather than failing the whole conversion) and use
|
|
9
|
+
* the `overrideSchema` hook to rewrite dates into the shared `x-mjst` instanceOf
|
|
10
|
+
* extension — the same handling TypeBox and Zod dates receive.
|
|
11
|
+
*/
|
|
12
|
+
export declare const valibotToJsonSchema: (source: unknown) => Promise<JSONSchema>;
|