@amritk/adapters 0.3.4 → 0.3.5

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 +43 -6
  2. package/package.json +2 -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) 
@@ -151,12 +151,15 @@ Requires `effect@>=3`. A top-level `Schema.BigIntFromSelf` / `Schema.DateFromSel
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/adapters",
3
- "version": "0.3.4",
3
+ "version": "0.3.5",
4
4
  "description": "Convert schemas from external libraries (TypeBox, Zod, ...) into JSON Schema for mjst.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -38,7 +38,7 @@
38
38
  },
39
39
  "dependencies": {
40
40
  "json-schema-typed": "^8.0.1",
41
- "@amritk/helpers": "0.13.4"
41
+ "@amritk/helpers": "0.13.5"
42
42
  },
43
43
  "exports": {
44
44
  "./package.json": "./package.json",