@amritk/adapters 0.3.4 → 0.3.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 (2) hide show
  1. package/README.md +46 -9
  2. package/package.json +6 -2
package/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  **Convert schemas authored in TypeBox, Zod, Valibot, or Effect into Draft 2020-12 JSON Schema — the single input shape the mjst generators understand.**
6
6
 
7
7
  ![status](https://img.shields.io/badge/status-pre--alpha-ef4444?style=flat-square) 
8
- ![version](https://img.shields.io/badge/version-v0.2.16-6366f1?style=flat-square&logo=npm&logoColor=white) 
8
+ ![version](https://img.shields.io/npm/v/@amritk/adapters?style=flat-square&logo=npm&logoColor=white&label=version&color=6366f1) 
9
9
  ![license](https://img.shields.io/badge/license-MIT-22c55e?style=flat-square) 
10
10
  ![JSON Schema](https://img.shields.io/badge/JSON%20Schema-2020--12-f97316?style=flat-square) 
11
11
  ![node](https://img.shields.io/badge/node-%E2%89%A520-339933?style=flat-square&logo=node.js&logoColor=white) 
@@ -145,18 +145,21 @@ const User = Schema.Struct({
145
145
  const jsonSchema = await effectToJsonSchema(User)
146
146
  ```
147
147
 
148
- Requires `effect@>=3`. A top-level `Schema.BigIntFromSelf` / `Schema.DateFromSelf` is rescued into an `x-mjst` hint; everything else goes through `JSONSchema.make`. **Read the [encoded-representation caveat](#effect-encodes-the-wire-representation) before choosing between `Schema.Date` and `Schema.DateFromSelf`** — it changes whether you get a `string` or a runtime `Date`.
148
+ Requires `effect@>=3`. A `Schema.BigIntFromSelf` / `Schema.DateFromSelf` **anywhere in the tree** — top level or nested inside a struct, array, or union — is rescued into an `x-mjst` hint; every subtree `JSONSchema.make` accepts is taken from it verbatim. **Read the [encoded-representation caveat](#effect-encodes-the-wire-representation) before choosing between `Schema.Date` and `Schema.DateFromSelf`** — it changes whether you get a `string` or a runtime `Date`.
149
149
 
150
150
  ---
151
151
 
152
152
  ## The `x-mjst` extension
153
153
 
154
- JSON Schema's core vocabulary has no keyword for a runtime `Date` or a `bigint`. mjst carries those as a vendor extension, `x-mjst`, that the generators read to emit the right TypeScript type and runtime check. Every adapter maps to the **same** two hints, so a `Date` authored in any of the four libraries generates identically:
154
+ JSON Schema's core vocabulary has no keyword for a runtime `Date`, a `bigint`, or a nominal brand. mjst carries those as a vendor extension, `x-mjst`, that the generators read to emit the right TypeScript type and runtime check:
155
155
 
156
- | Source construct | `x-mjst` hint | Generated handling |
157
- |:---|:---|:---|
158
- | a runtime `Date` | `{ instanceOf: 'Date' }` | typed as `Date`, checked with `instanceof` |
159
- | a `bigint` | `{ primitive: 'bigint' }` | typed as `bigint`, checked with `typeof` |
156
+ | `x-mjst` hint | Generated handling |
157
+ |:---|:---|
158
+ | `{ instanceOf: 'Date' }` | typed as `Date`, checked with `instanceof` |
159
+ | `{ primitive: 'bigint' }` | typed as `bigint`, checked with `typeof` |
160
+ | `{ brand: 'UserId' }` | typed as `Base & { readonly __brand: 'UserId' }`; validates as `Base` at runtime |
161
+
162
+ `instanceOf` / `primitive` are emitted by the adapters (an adapter maps to the **same** hint whichever library authored it, so a `Date` generates identically everywhere). `brand` is **hand-authored** — no adapter emits it today — and is purely type-level: the runtime value still validates as its underlying JSON Schema type, but the generated TypeScript type is intersected with a unique brand so a `UserId` is not interchangeable with a plain `string`. See [Nominal brands](#nominal-brands) below.
160
163
 
161
164
  Per library, the `Date` / `bigint` sources are:
162
165
 
@@ -167,6 +170,40 @@ Per library, the `Date` / `bigint` sources are:
167
170
  | **Valibot** | `v.date()` | `v.bigint()` |
168
171
  | **Effect** | `Schema.DateFromSelf` | `Schema.BigIntFromSelf` |
169
172
 
173
+ ### Nominal brands
174
+
175
+ `{ brand: 'Name' }` gives a schema a **nominal** (branded) type without changing
176
+ what it accepts at runtime. Attach it to any schema and the generated TypeScript
177
+ intersects the base type with a unique marker:
178
+
179
+ ```jsonc
180
+ { "type": "string", "format": "uuid", "x-mjst": { "brand": "UserId" } }
181
+ // generated type: (string & { readonly __brand: 'UserId' })
182
+ // runtime check: still just "a string" (plus the uuid format check)
183
+ ```
184
+
185
+ Because the brand lives only in the type, two ids with different brands are not
186
+ assignable to each other, so you can't pass an `OrderId` where a `UserId` is
187
+ expected — the same protection Drizzle's `.$type<UserId>()` gives a column, now
188
+ carried by the schema. The name must match `^[\w$ -]+$` (it is embedded in a
189
+ string literal in generated output); an unsafe name is ignored.
190
+
191
+ Both schema→type paths honour the brand identically:
192
+
193
+ - the **code generators** (`@amritk/generate-parsers` etc.) emit it into `.d.ts`;
194
+ - the type-level [`FromSchema`](../runtime-validators) reads it too, so anything
195
+ typed from a live schema literal — most notably `@amritk/api` route
196
+ `params` / `query` / `body` — carries the brand into your handler. Declaring a
197
+ route param `{ type: 'string', 'x-mjst': { brand: 'UserId' } }` makes
198
+ `params.id` a `UserId`, not a plain `string`, end-to-end (handler **and** the
199
+ derived typed client). Write the schema inline or `as const` so the literal
200
+ survives inference.
201
+
202
+ > The brand shape is `{ readonly __brand: 'UserId' }`. If you want it to be the
203
+ > *same* type as a Drizzle-branded id, define that id to this shape (or brand the
204
+ > Drizzle column to match); mjst aligns a convention, it doesn't reuse Drizzle's
205
+ > own brand symbol.
206
+
170
207
  ---
171
208
 
172
209
  ## Lossy constructs & widening warnings
@@ -176,7 +213,7 @@ Some source types have no faithful JSON Schema representation and are **not** re
176
213
  - **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."*
177
214
  - **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.
178
215
  - **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."*
179
- - **Effect.** Effect does not widen — it is stricter. `JSONSchema.make` **throws** on a *nested* unrepresentable type (a `BigIntFromSelf` / `DateFromSelf` below the top level with no `jsonSchema` annotation). The adapter catches that and re-throws an actionable message pointing you at the fix: use the string-encoded `Schema.Date` / `Schema.BigInt`, or add a `jsonSchema` annotation to that field.
216
+ - **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.
180
217
 
181
218
  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.
182
219
 
@@ -200,7 +237,7 @@ Any other extended type (`Uint8Array`, `Symbol`, `Undefined`, …) is left untou
200
237
  Effect models a value as a **decode/encode pair**, and `JSONSchema.make` describes the **encoded** (wire) representation — not the runtime type. This is the caveat most likely to surprise you:
201
238
 
202
239
  - `Schema.Date` decodes a `Date` **from a string**, so it converts to a **`string`** schema — not a runtime `Date`. The adapter passes this through unchanged, because it accurately reflects what Effect expects on the wire.
203
- - Only the `*FromSelf` variants — `Schema.DateFromSelf`, `Schema.BigIntFromSelf` — describe the runtime value itself, and those are the ones rescued into `x-mjst` runtime-type hints (see [`src/effect-to-json-schema.ts:63`](./src/effect-to-json-schema.ts#L63)).
240
+ - Only the `*FromSelf` variants — `Schema.DateFromSelf`, `Schema.BigIntFromSelf` — describe the runtime value itself, and those are the ones rescued into `x-mjst` runtime-type hints (see [`src/effect-to-json-schema.ts:121-122`](./src/effect-to-json-schema.ts#L121-L122)).
204
241
 
205
242
  So: want a generated `Date`? Author `Schema.DateFromSelf`. Want a string that Effect parses into a `Date`? Author `Schema.Date` and expect a `string` in the generated output. The same distinction applies to `Schema.BigInt` (→ `string`) vs `Schema.BigIntFromSelf` (→ `bigint`).
206
243
 
package/package.json CHANGED
@@ -1,8 +1,12 @@
1
1
  {
2
2
  "name": "@amritk/adapters",
3
- "version": "0.3.4",
3
+ "version": "0.3.6",
4
4
  "description": "Convert schemas from external libraries (TypeBox, Zod, ...) into JSON Schema for mjst.",
5
5
  "type": "module",
6
+ "sideEffects": false,
7
+ "engines": {
8
+ "node": ">=20"
9
+ },
6
10
  "license": "MIT",
7
11
  "author": "amritk",
8
12
  "keywords": [
@@ -38,7 +42,7 @@
38
42
  },
39
43
  "dependencies": {
40
44
  "json-schema-typed": "^8.0.1",
41
- "@amritk/helpers": "0.13.4"
45
+ "@amritk/helpers": "0.14.0"
42
46
  },
43
47
  "exports": {
44
48
  "./package.json": "./package.json",