@amritk/adapters 0.5.2 → 0.6.1

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/README.md CHANGED
@@ -25,7 +25,7 @@ The library adapters lean on their source library's own JSON Schema exporter (Zo
25
25
 
26
26
  - strips the dialect marker (`$schema`) the generators don't need,
27
27
  - rewrites constructs JSON Schema can't express — runtime `Date`, `bigint` — into the shared [`x-mjst`](#the-x-mjst-extension) hint the generators read, and
28
- - **warns and continues** on anything genuinely unrepresentable rather than throwing, so one unsupported field never blocks generation.
28
+ - **warns and continues** on anything genuinely unrepresentable rather than throwing, so one unsupported field never blocks generation (pass `{ strict: true }` to make a lossy construct throw instead).
29
29
 
30
30
  ---
31
31
 
@@ -43,12 +43,12 @@ The source libraries are **optional peer dependencies** — each adapter dynamic
43
43
  | Input format | Install |
44
44
  |:---|:---|
45
45
  | `typebox` | *(nothing — see below)* |
46
- | `zod` | `zod@>=4` |
46
+ | `zod` | `zod@>=4`, **or** `zod@3` plus `zod-to-json-schema@>=3` |
47
47
  | `valibot` | `valibot@>=1` **and** `@valibot/to-json-schema@>=1` |
48
48
  | `effect` | `effect@>=3` |
49
49
  | `avro` | *(nothing — an Avro schema is plain JSON)* |
50
50
 
51
- If the required package is missing (or too old), the adapter throws a clear, actionable error naming what to install — e.g. *"The Zod adapter requires 'zod' (v4 or later) to be installed in your project."*
51
+ If the required package is missing, the adapter throws a clear, actionable error naming what to install — e.g. *"The Zod adapter requires either 'zod' v4+ (for its native toJSONSchema) or the 'zod-to-json-schema' package (a fallback for Zod 3). Neither was found — install one in your project."*
52
52
 
53
53
  > [!NOTE]
54
54
  > The TypeBox adapter deliberately does **not** import TypeBox. It works purely on the plain-object shape of the schema (via a JSON round-trip), so TypeBox stays a dependency of your schema module alone, never of mjst.
@@ -57,7 +57,7 @@ If the required package is missing (or too old), the adapter throws a clear, act
57
57
 
58
58
  ## Usage
59
59
 
60
- Each adapter is a function `(source: unknown) => Promise<JSONSchema>` (the TypeBox and Avro adapters are synchronous, but are exposed through `getAdapter` with the same signature for uniformity — so always `await` when you go through it). Import them by subpath:
60
+ Each adapter is a function `(source: unknown, options?: { strict?: boolean }) => Promise<JSONSchema>` (the TypeBox and Avro adapters are synchronous, but are exposed through `getAdapter` with the same signature for uniformity — so always `await` when you go through it). `strict: true` makes a lossy construct throw instead of widening (Zod, Valibot, Avro). Import them by subpath:
61
61
 
62
62
  ```ts
63
63
  import { avroToJsonSchema } from '@amritk/adapters/avro-to-json-schema'
@@ -98,7 +98,7 @@ A TypeBox schema is already a JSON Schema object at runtime — it just carries
98
98
  ### Zod
99
99
 
100
100
  > [!IMPORTANT]
101
- > **Zod 4 or later only.** The adapter relies on Zod's native `toJSONSchema`, which does not exist before Zod 4 (see [`src/zod-to-json-schema.ts`](./src/zod-to-json-schema.ts)). On Zod 3 you'll get a clear error rather than a silent miss.
101
+ > **Zod 4 preferred.** The adapter relies on Zod's native `toJSONSchema`, which does not exist before Zod 4 (see [`src/zod-to-json-schema.ts`](./src/zod-to-json-schema.ts)). On Zod 3 it falls back to the optional [`zod-to-json-schema`](https://github.com/StefanTerdell/zod-to-json-schema) package — install it alongside Zod 3 — and with neither available you'll get a clear error rather than a silent miss.
102
102
 
103
103
  ```ts
104
104
  import { z } from 'zod'
@@ -130,7 +130,7 @@ const User = v.object({
130
130
  const jsonSchema = await valibotToJsonSchema(User)
131
131
  ```
132
132
 
133
- Requires both `valibot` and `@valibot/to-json-schema`. As with Zod, `v.date()` / `v.bigint()` are rescued into `x-mjst` hints via the converter's `overrideSchema` hook. The converter runs in `errorMode: 'warn'`, so any other unsupported construct degrades to an open schema and is logged rather than throwing.
133
+ Requires both `valibot` and `@valibot/to-json-schema`. As with Zod, `v.date()` / `v.bigint()` are rescued into `x-mjst` hints via the converter's `overrideSchema` hook. The converter runs in `errorMode: 'ignore'` (targeting draft 2020-12), so any other unsupported construct degrades to an open schema; mjst collects those and reports them in one batched `[mjst] Valibot adapter: …` warning rather than throwing.
134
134
 
135
135
  ### Effect
136
136
 
@@ -254,10 +254,10 @@ Both schema→type paths honour the brand identically:
254
254
 
255
255
  Some source types have no faithful JSON Schema representation and are **not** rescued into an `x-mjst` hint. Rather than fail the whole conversion, the adapters widen those to "accept anything" (`{}`) — which means **the generated type is wider than the source schema** — and emit a `[mjst]` warning to stderr so the widening is visible, never silent. Behaviour per library:
256
256
 
257
- - **Zod.** These Zod types become "accept anything": `symbol`, `nan`, `void`, `undefined`, `never`, `map`, `set`, `promise`, `function`. When any appear, the adapter logs, e.g.: *"[mjst] Zod adapter: function, symbol have no JSON Schema representation and became 'accept anything'. The generated type will be wider than the Zod schema."*
258
- - **Valibot.** The converter runs in `errorMode: 'warn'`: an unsupported construct degrades to an open schema and `@valibot/to-json-schema` logs which one, so the widening is reported by the converter itself.
257
+ - **Zod.** These Zod types become "accept anything": `symbol`, `nan`, `void`, `undefined`, `never`, `map`, `set`, `promise`, `function`. When any appear, the adapter logs, e.g.: *"[mjst] Zod adapter: function, symbol have no full JSON Schema representation and were widened. The generated type will be wider than the Zod schema."*
258
+ - **Valibot.** The converter runs in `errorMode: 'ignore'`: an unsupported construct degrades to an open schema, and the adapter collects every such construct from the converter's override hooks and reports them in one `[mjst] Valibot adapter: …` warning of the same shape as Zod's.
259
259
  - **TypeBox.** An extended `type` string with no mapping (see below) is left unchanged with a warning: *"[mjst] TypeBox type '…' has no JSON Schema or x-mjst mapping; leaving it unchanged."*
260
- - **Avro.** Only the `decimal` and `duration` logical types widen, each degrading to its base type: *"[mjst] Avro adapter: the decimal logical type (precision/scale) has no full JSON Schema representation and was widened."* Everything else in Avro maps exactly, or is rejected outright — a duplicate name, a reference to an undefined name, or a malformed `record`/`enum`/`fixed` throws rather than converting to something wrong.
260
+ - **Avro.** Only the `decimal` and `duration` logical types widen, each degrading to its base type: *"[mjst] Avro adapter: the decimal logical type (precision/scale) has no full JSON Schema representation and was widened. The generated type will be wider than the Avro schema."* Everything else in Avro maps exactly, or is rejected outright — a duplicate name, a reference to an undefined name, or a malformed `record`/`enum`/`fixed` throws rather than converting to something wrong.
261
261
  - **Effect.** Effect does not widen — it is stricter. `JSONSchema.make` throws on any unrepresentable type, wherever it sits. The adapter catches that and descends structurally, rebuilding the container (struct, array, union, refinement, …) and rescuing every `BigIntFromSelf` / `DateFromSelf` leaf it reaches into an `x-mjst` hint. Only a leaf it has no rescue for (a raw `symbol`, say) is fatal, and then it throws an actionable message rather than Effect's opaque one: replace the type with a JSON-representable one, or add a `jsonSchema` annotation to that field.
262
262
 
263
263
  If any of these matter to your schema, prefer a representable alternative (e.g. model a set as an array) or add the library's own JSON Schema annotation.
@@ -2,8 +2,11 @@
2
2
  * The schema authoring formats mjst can ingest.
3
3
  *
4
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.
5
+ * handed straight to the generators. `'asyncapi'` is also read from disk — an
6
+ * AsyncAPI 2.x/3.0 document (JSON or YAML) whose message schemas
7
+ * `@amritk/asyncapi` extracts, so it never reaches an adapter either. The
8
+ * others name external libraries whose schemas are first converted to JSON
9
+ * Schema by a matching adapter.
7
10
  *
8
11
  * `'avro'` is the odd one out in how it is *loaded*, not converted: an Avro
9
12
  * schema is a `.avsc` JSON document rather than a module exporting a value, so
@@ -13,4 +16,4 @@
13
16
  * A format may appear here before its adapter exists — `getAdapter` is the
14
17
  * source of truth for what is actually implemented today.
15
18
  */
16
- export type SourceFormat = 'json' | 'typebox' | 'zod' | 'valibot' | 'effect' | 'avro';
19
+ export type SourceFormat = 'json' | 'typebox' | 'zod' | 'valibot' | 'effect' | 'avro' | 'asyncapi';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/adapters",
3
- "version": "0.5.2",
3
+ "version": "0.6.1",
4
4
  "description": "Convert schemas from external libraries (TypeBox, Zod, Avro, ...) into JSON Schema for mjst.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -44,10 +44,10 @@
44
44
  },
45
45
  "dependencies": {
46
46
  "json-schema-typed": "^8.0.1",
47
- "@amritk/helpers": "^0.18.0"
47
+ "@amritk/helpers": "^0.20.0"
48
48
  },
49
49
  "devDependencies": {
50
- "@amritk/runtime-validators": "^0.13.0"
50
+ "@amritk/runtime-validators": "^0.13.1"
51
51
  },
52
52
  "exports": {
53
53
  "./package.json": "./package.json",