@amritk/adapters 0.2.4 → 0.2.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.
@@ -9,3 +9,4 @@
9
9
  * source of truth for what is actually implemented today.
10
10
  */
11
11
  export type SourceFormat = 'json' | 'typebox' | 'zod' | 'valibot' | 'effect';
12
+ //# sourceMappingURL=source-format.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"source-format.d.ts","sourceRoot":"","sources":["../src/source-format.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,SAAS,GAAG,KAAK,GAAG,SAAS,GAAG,QAAQ,CAAA"}
@@ -11,3 +11,4 @@ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
11
11
  * by the consumer's schema module, never by mjst itself.
12
12
  */
13
13
  export declare const typeboxToJsonSchema: (source: unknown) => JSONSchema;
14
+ //# sourceMappingURL=typebox-to-json-schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typebox-to-json-schema.d.ts","sourceRoot":"","sources":["../src/typebox-to-json-schema.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAwDjE;;;;;;;;;;GAUG;AACH,eAAO,MAAM,mBAAmB,WAAY,OAAO,KAAG,UAUrD,CAAA"}
@@ -1,65 +1,73 @@
1
- var __defProp = Object.defineProperty;
2
- var __returnValue = (v) => v;
3
- function __exportSetter(name, newValue) {
4
- this[name] = __returnValue.bind(null, newValue);
5
- }
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, {
9
- get: all[name],
10
- enumerable: true,
11
- configurable: true,
12
- set: __exportSetter.bind(all, name)
13
- });
1
+ import { MJST_EXTENSION_KEY } from '@amritk/helpers/mjst-extension';
2
+ // The seven core JSON Schema 2020-12 types. Anything else in a `type` slot is a
3
+ // TypeBox extended type with no native JSON Schema equivalent.
4
+ const JSON_SCHEMA_TYPES = new Set(['string', 'number', 'integer', 'boolean', 'object', 'array', 'null']);
5
+ // Maps a TypeBox extended `type` string to the runtime class an `x-mjst`
6
+ // instanceOf hint should reference. Add entries here as more extended types
7
+ // gain generator support.
8
+ const EXTENDED_TYPE_TO_INSTANCE = {
9
+ Date: 'Date',
14
10
  };
15
- var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
16
-
17
- // ../helpers/dist/mjst-extension.js
18
- var MJST_EXTENSION_KEY = "x-mjst";
19
- var SUPPORTED_PRIMITIVES = new Set(["bigint"]);
20
-
21
- // src/typebox-to-json-schema.ts
22
- var JSON_SCHEMA_TYPES = new Set(["string", "number", "integer", "boolean", "object", "array", "null"]);
23
- var EXTENDED_TYPE_TO_INSTANCE = {
24
- Date: "Date"
11
+ // Maps a TypeBox extended `type` string to a non-JSON `x-mjst` primitive hint
12
+ // (e.g. Type.BigInt() emits `{ type: 'bigint' }`).
13
+ const EXTENDED_TYPE_TO_PRIMITIVE = {
14
+ bigint: 'bigint',
25
15
  };
26
- var EXTENDED_TYPE_TO_PRIMITIVE = {
27
- bigint: "bigint"
28
- };
29
- var rewriteExtendedTypes = (value) => {
30
- if (Array.isArray(value))
31
- return value.map(rewriteExtendedTypes);
32
- if (typeof value !== "object" || value === null)
33
- return value;
34
- const source = value;
35
- const result = {};
36
- for (const [key, val] of Object.entries(source)) {
37
- result[key] = rewriteExtendedTypes(val);
38
- }
39
- const type = result["type"];
40
- if (typeof type === "string" && !JSON_SCHEMA_TYPES.has(type)) {
41
- const primitive = EXTENDED_TYPE_TO_PRIMITIVE[type];
42
- const instanceOf = EXTENDED_TYPE_TO_INSTANCE[type];
43
- if (primitive) {
44
- delete result["type"];
45
- result[MJST_EXTENSION_KEY] = { primitive };
46
- } else if (instanceOf) {
47
- delete result["type"];
48
- result[MJST_EXTENSION_KEY] = { instanceOf };
49
- } else {
50
- console.warn(`[mjst] TypeBox type '${type}' has no JSON Schema or x-mjst mapping; leaving it unchanged.`);
16
+ /**
17
+ * Recursively rewrites TypeBox extended types into an `x-mjst` instanceOf hint.
18
+ *
19
+ * TypeBox emits non-standard `type` strings for runtime classes (e.g.
20
+ * `Type.Date()` produces `{ type: 'Date' }`). We drop the bogus `type` and
21
+ * record the class under `x-mjst` so the generators can emit the right
22
+ * TypeScript type and `instanceof` checks. Extended types we do not yet map are
23
+ * left untouched with a warning, preserving a permissive best-effort fallback.
24
+ */
25
+ const 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);
51
34
  }
52
- }
53
- return result;
54
- };
55
- var typeboxToJsonSchema = (source) => {
56
- if (typeof source !== "object" || source === null) {
57
- const received = source === null ? "null" : typeof source;
58
- throw new Error(`TypeBox adapter expected a schema object but received ${received}.`);
59
- }
60
- const sanitized = JSON.parse(JSON.stringify(source));
61
- return rewriteExtendedTypes(sanitized);
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
+ }
43
+ else if (instanceOf) {
44
+ delete result['type'];
45
+ result[MJST_EXTENSION_KEY] = { instanceOf };
46
+ }
47
+ else {
48
+ console.warn(`[mjst] TypeBox type '${type}' has no JSON Schema or x-mjst mapping; leaving it unchanged.`);
49
+ }
50
+ }
51
+ return result;
62
52
  };
63
- export {
64
- typeboxToJsonSchema
53
+ /**
54
+ * TypeBox schemas are already JSON Schema objects at runtime, but they carry
55
+ * non-enumerable symbol keys (`Kind`, `Optional`, ...) that TypeBox uses for its
56
+ * own type machinery. A JSON round-trip drops those symbols (and any `undefined`
57
+ * values), leaving a clean, plain JSON Schema. We then rewrite TypeBox's
58
+ * extended types (Date, ...) into `x-mjst` hints the generators understand.
59
+ *
60
+ * We deliberately do not import TypeBox here: the conversion only touches the
61
+ * plain-object shape, so TypeBox stays an optional peer dependency used solely
62
+ * by the consumer's schema module, never by mjst itself.
63
+ */
64
+ export const typeboxToJsonSchema = (source) => {
65
+ if (typeof source !== 'object' || source === null) {
66
+ const received = source === null ? 'null' : typeof source;
67
+ throw new Error(`TypeBox adapter expected a schema object but received ${received}.`);
68
+ }
69
+ // Boundary cast: JSON.parse yields `unknown`, and the round-tripped, rewritten
70
+ // TypeBox schema is a valid JSON Schema by construction.
71
+ const sanitized = JSON.parse(JSON.stringify(source));
72
+ return rewriteExtendedTypes(sanitized);
65
73
  };
@@ -10,3 +10,4 @@ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
10
10
  * extension — the same handling TypeBox and Zod dates receive.
11
11
  */
12
12
  export declare const valibotToJsonSchema: (source: unknown) => Promise<JSONSchema>;
13
+ //# sourceMappingURL=valibot-to-json-schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"valibot-to-json-schema.d.ts","sourceRoot":"","sources":["../src/valibot-to-json-schema.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAuCjE;;;;;;;;;GASG;AACH,eAAO,MAAM,mBAAmB,WAAkB,OAAO,KAAG,OAAO,CAAC,UAAU,CA0B7E,CAAA"}