arktype 3.0.0-beta.2 → 3.0.0-rc.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/README.md +55 -20
- package/out/_internal/regex/quantify.d.ts +17 -1
- package/out/_internal/schema/refinements/range.d.ts +2 -1
- package/out/_internal/schema/roots/domain.d.ts +1 -0
- package/out/_internal/schema/roots/root.d.ts +3 -1
- package/out/_internal/schema/roots/union.d.ts +8 -3
- package/out/_internal/schema/shared/compile.d.ts +2 -0
- package/out/_internal/schema/shared/declare.d.ts +9 -6
- package/out/_internal/schema/shared/errors.d.ts +3 -0
- package/out/_internal/schema/shared/issues.d.ts +5 -1
- package/out/_internal/schema/shared/issuesLean.d.ts +4 -0
- package/out/_internal/schema/shared/issuesNone.d.ts +1 -0
- package/out/_internal/schema/shared/issuesNoneLean.d.ts +1 -0
- package/out/_internal/schema/shared/standardFailure.d.ts +1 -0
- package/out/_internal/schema/shared/toJsonSchema.d.ts +16 -3
- package/out/_internal/schema/shared/transform.d.ts +1 -0
- package/out/_internal/sets/constraint.d.ts +2 -0
- package/out/_internal/sets/fromJsonSchema.d.ts +3 -1
- package/out/_internal/util/registry.d.ts +1 -1
- package/out/attributes.d.ts +12 -8
- package/out/{chunk-QBX4MEQR.js → chunk-3HZC67MT.js} +15 -15
- package/out/{chunk-5TN5JRP7.js → chunk-3KBOPH3J.js} +63 -41
- package/out/{chunk-NIZDOWFU.js → chunk-46PLKNON.js} +2 -2
- package/out/{chunk-7JXN4WJX.js → chunk-4FTHWK63.js} +1 -1
- package/out/{chunk-EA2DNEDI.js → chunk-4HREHNIV.js} +12 -12
- package/out/{chunk-J7CKAEDX.js → chunk-4NZCLVBD.js} +1 -1
- package/out/{chunk-7N3JEHA4.js → chunk-6FMBO47M.js} +1 -1
- package/out/{chunk-W77ROLOT.js → chunk-7TALAWYX.js} +1 -1
- package/out/{chunk-YMYPW3L5.js → chunk-BCYKO54Z.js} +3 -3
- package/out/{chunk-7SVIUP6X.js → chunk-C2JOL5M2.js} +2 -2
- package/out/{chunk-GPNWBJL3.js → chunk-C2V6QPE3.js} +17 -11
- package/out/{chunk-XMEK4I7J.js → chunk-C3B4IFQL.js} +887 -266
- package/out/{chunk-FY4M7GE7.js → chunk-FWRSDUJ2.js} +4 -4
- package/out/chunk-GID3FSQA.js +161 -0
- package/out/{chunk-CGWRW4JZ.js → chunk-K7SLP5D2.js} +4 -4
- package/out/{chunk-KDMHK4KP.js → chunk-KQPSKGUI.js} +2 -2
- package/out/{chunk-KVQCLBJR.js → chunk-KXOC3JEX.js} +241 -66
- package/out/{chunk-CQU7WIIR.js → chunk-KXOSUYRA.js} +6 -6
- package/out/{chunk-5WRZ25AB.js → chunk-MTWKGAGO.js} +4 -4
- package/out/{chunk-OA5Q2JLI.js → chunk-NU73GNCX.js} +8 -4
- package/out/{chunk-ZFB6DQWY.js → chunk-P5W67FQH.js} +6 -11
- package/out/{chunk-A7A6FDBS.js → chunk-PDLTEX6Z.js} +50 -44
- package/out/{chunk-RAM6G3F6.js → chunk-QUS4IAXJ.js} +26 -25
- package/out/{chunk-HA3POWOU.js → chunk-R7WXIH5P.js} +6 -5
- package/out/{chunk-YN3XRJ7H.js → chunk-UJJGG4VQ.js} +1 -1
- package/out/{chunk-NIYAI7AF.js → chunk-US6BWOGJ.js} +3 -3
- package/out/{chunk-JZ4KBCMM.js → chunk-VMMMOO4R.js} +21 -13
- package/out/{chunk-PIFOURNM.js → chunk-VYMXHSFR.js} +11 -4
- package/out/{chunk-57LF5Y5P.js → chunk-W2XOI5DY.js} +1 -1
- package/out/{chunk-SGFGLENC.js → chunk-WC5L5YRP.js} +3 -3
- package/out/{chunk-V4TZHFNK.js → chunk-WKOSL2G6.js} +1 -1
- package/out/{chunk-UH6XKS3C.js → chunk-XNUAQSJ4.js} +30 -9
- package/out/{chunk-C5JGK6GT.js → chunk-ZRYEHGSG.js} +20 -3
- package/out/cli.d.ts +5 -1
- package/out/cli.js +301 -49
- package/out/codecs.d.ts +4 -3
- package/out/codecs.js +18 -18
- package/out/config.js +8 -8
- package/out/declare.d.ts +2 -4
- package/out/errors.js +8 -8
- package/out/fn.d.ts +2 -2
- package/out/fromJsonSchema.d.ts +1 -1
- package/out/generic.d.ts +7 -4
- package/out/i18n.js +5 -5
- package/out/index.js +19 -19
- package/out/internal.js +32 -32
- package/out/island.js +18 -18
- package/out/islandDependency.js +33 -33
- package/out/jsonSchema/infer.d.ts +16 -6
- package/out/jsonSchema/validate.d.ts +16 -4
- package/out/keywords/functions.d.ts +4 -169
- package/out/keywords/keywords.d.ts +1 -1
- package/out/keywords/phrases.d.ts +3 -168
- package/out/keywords/ts.js +14 -14
- package/out/locales/de.js +6 -6
- package/out/locales/en.js +15 -15
- package/out/match.d.ts +12 -9
- package/out/mutatesInput.js +14 -14
- package/out/nary.d.ts +219 -193
- package/out/parser/definition.d.ts +2 -2
- package/out/parser/shift/operator/default.d.ts +2 -2
- package/out/parser/tupleExpressions.d.ts +2 -0
- package/out/precompiled.js +11 -11
- package/out/runtime.d.ts +1 -1
- package/out/runtime.js +5 -5
- package/out/schema.js +11 -11
- package/out/schemaInternal.js +29 -17
- package/out/schemaRuntime.js +4 -4
- package/out/schemaRuntimeLean.js +60 -36
- package/out/schemaRuntimeNone.js +52 -31
- package/out/schemaRuntimeNoneLean.js +52 -31
- package/out/type.d.ts +3 -3
- package/out/util.js +14 -11
- package/out/variants/base.d.ts +9 -8
- package/out/variants/object.d.ts +14 -6
- package/package.json +4 -4
- package/skills/arktype/SKILL.md +17 -17
- package/skills/arktype/references/migrating.md +13 -13
- package/skills/arktype/references/precompile.md +4 -4
- package/out/chunk-CSVAPWDE.js +0 -83
package/skills/arktype/SKILL.md
CHANGED
|
@@ -23,16 +23,16 @@ If asked to precompile too, set it up last, as [references/precompile.md](refere
|
|
|
23
23
|
|
|
24
24
|
Follow https://arktype.io/docs/upgrading.md, whose checklist links each breaking change. What most code hits:
|
|
25
25
|
|
|
26
|
-
| ArkType 2 | ArkType 3
|
|
27
|
-
| -------------------------------------------------------------- |
|
|
28
|
-
| `out[0]`, `out.length`, `out.map(...)`, `for (const e of out)` | the same on `out.issues`
|
|
29
|
-
| `error.message`, for text naming its path | `out.summary`, a line per error; `error.message` now leaves the path out
|
|
30
|
-
| `error.problem`, `{ problem: "..." }` | `error.message`, `{ message: "..." }`, reworded as a whole sentence
|
|
31
|
-
| `out.flatProblemsByPath`, `out.byPath`, `out.flatByPath` | `flatMessagesByPath(out)`, `byPath(out)`, `flatByPath(out)` from `arktype`
|
|
32
|
-
| `out.message`, `out.toTraversalError()` | `out.summary`, `new TraversalError(out)`
|
|
33
|
-
| `error.hasCode("min")`, `error.toString()` | `error.code === "min"`, `
|
|
34
|
-
| `ctx.errors.add(...)` | `ctx.error(...)` or `ctx.reject(...)`
|
|
35
|
-
| `configure()` anywhere, the `clone` option | a config file imported before any Type is created; remove `clone`
|
|
26
|
+
| ArkType 2 | ArkType 3 |
|
|
27
|
+
| -------------------------------------------------------------- | --------------------------------------------------------------------------------------------- |
|
|
28
|
+
| `out[0]`, `out.length`, `out.map(...)`, `for (const e of out)` | the same on `out.issues` |
|
|
29
|
+
| `error.message`, for text naming its path | `out.summary`, a line per error; `error.message` now leaves the path out |
|
|
30
|
+
| `error.problem`, `{ problem: "..." }` | `error.message`, `{ message: "..." }`, reworded as a whole sentence |
|
|
31
|
+
| `out.flatProblemsByPath`, `out.byPath`, `out.flatByPath` | `flatMessagesByPath(out)`, `byPath(out)`, `flatByPath(out)` from `arktype` |
|
|
32
|
+
| `out.message`, `out.toTraversalError()` | `out.summary`, `new TraversalError(out)` |
|
|
33
|
+
| `error.hasCode("min")`, `error.toString()` | `error.code === "min"`, `out.summary` (errors are plain objects; `error.message` has no path) |
|
|
34
|
+
| `ctx.errors.add(...)` | `ctx.error(...)` or `ctx.reject(...)` |
|
|
35
|
+
| `configure()` anywhere, the `clone` option | a config file imported before any Type is created; remove `clone` |
|
|
36
36
|
|
|
37
37
|
Then update snapshots of error text: a message no longer says what the value was, like `(was a number)`. Replace adapters written for ArkType 2 as [Integrations](#integrations) says: they pass valid data and fail on the first invalid input.
|
|
38
38
|
|
|
@@ -43,9 +43,9 @@ A project whose bundler config uses `@ark/aot` (e.g. `precompile.vite()` from `@
|
|
|
43
43
|
- Define Types from static values, and compose them inside definitions, like `const Admin = User.and({ role: "'admin'" })`. A definition reading a parameter, or a function calling `.or`, `.pick` or another composing method at runtime, fails the build. To reuse a pattern, define a Generic.
|
|
44
44
|
- Define Types in JavaScript or TypeScript modules. A Svelte component's script isn't precompiled, so one importing arktype fails the build: put the Types, and any code checking a result against `type.errors`, in a `.ts` module the component imports.
|
|
45
45
|
- If the project calls `configure()`, name its module as `config` under `precompile` in an `ark.json` beside `package.json`, which the plugin, the test drivers, the CLI and the ESLint rule all read.
|
|
46
|
-
- A build error names the file, line and column, then why, then the fix on its last line. Apply that fix. Don't set `allowUnsafe` or switch to `runtime: "arktype"` to get past one. `npx arktype-precompile --check src` lists every one without bundling.
|
|
46
|
+
- A build error names the file, line and column, then why, then the fix on its last line. Apply that fix. Don't set `allowUnsafe` or switch to `runtime: "arktype"` to get past one. `npx @ark/aot --check src` (or `pnpm exec arktype-precompile --check src`) lists every one without bundling.
|
|
47
47
|
- A validator's error messages say what failed but not what the value was, like `name must be a string`.
|
|
48
|
-
- The build works out which introspection members, like a JSON Schema or `expression`, each validator needs from the code reading it, its dependencies' included, so the MCP SDK, the AI SDK and OpenAPI generators get one with nothing to set. Setting one under `members` only overrides that. Code the build can't see, like code outside the bundle reading an entry's export,
|
|
48
|
+
- The build works out which introspection members, like a JSON Schema or `expression`, each validator needs from the code reading it, its dependencies' included, so the MCP SDK, the AI SDK and OpenAPI generators get one with nothing to set. Setting one under `members` only overrides that. Code the build can't see, like code outside the bundle reading an entry's export, reads `undefined`, and warns in development naming the member to set true under `members`.
|
|
49
49
|
|
|
50
50
|
Docs index: https://arktype.io/llms.txt, which links each page's Markdown. Any docs page is available as Markdown by adding `.md` to its URL, e.g. https://arktype.io/docs/objects.md. Every page in one file: https://arktype.io/llms-full.txt.
|
|
51
51
|
|
|
@@ -159,8 +159,8 @@ const CustomerDraft = Customer.partial()
|
|
|
159
159
|
Never interpolate a type into a string, as in ``type(`${Address}[]`)``. Use `Address.array()`, `Address.or(...)` and the other chained methods, or a tuple expression like `[Address, "[]"]`.
|
|
160
160
|
|
|
161
161
|
- `.merge()` adds keys and replaces existing ones. `.and()` intersects, and throws when a key's types conflict.
|
|
162
|
-
- `.merge()`, `"..."`, `.pick()`, `.omit()` and `.partial()` drop a `.narrow()` on the object they read, or throw if
|
|
163
|
-
- To share keys between types, merge a base Type, or `.pick()` them: `User.get("nick")` is the key's Type without its `?` or its default. To reuse a pattern across Types, define a Generic: `const Page = type("<t>", { items: "t[]", total: "number" })`, called as `Page(Item)`. In a Generic's body, `Omit`, `Pick`, `Partial`, `Required` and `Merge` do what the methods of those names do: `const Create = type("<t extends object>", "Omit<t, 'id'>")`.
|
|
162
|
+
- `.merge()`, `"..."`, `.pick()`, `.omit()` and `.partial()` drop a `.narrow()` on the object they read, or throw if the object also transforms its input: deletes undeclared keys, or has a default or a morph at a key. Keep the keys in a Type of their own, derive from that, and add each `.narrow()` last.
|
|
163
|
+
- To share keys between types, merge a base Type, or `.pick()` them: `User.get("nick")` is the key's Type without its `?` or its default, plus `| undefined` when the key is optional or defaulted. To reuse a pattern across Types, define a Generic: `const Page = type("<t>", { items: "t[]", total: "number" })`, called as `Page(Item)`. In a Generic's body, `Omit`, `Pick`, `Partial`, `Required` and `Merge` do what the methods of those names do: `const Create = type("<t extends object>", "Omit<t, 'id'>")`.
|
|
164
164
|
- `.as<T>()` is a cast. To infer a narrower type, use `.narrow()` with a type predicate, e.g. `(s): s is \`data-${string}\` => s.startsWith("data-")`.
|
|
165
165
|
- A definition built at runtime goes to `type.raw`, which infers nothing, then gets its type: `type.raw(Object.fromEntries(FLAGS.map(flag => [flag, "boolean"]))).as<Record<Flag, boolean>>()`.
|
|
166
166
|
|
|
@@ -302,9 +302,9 @@ const types = scope({
|
|
|
302
302
|
const post = types.Post({ title: "Hello", author: { name: "Ada" } })
|
|
303
303
|
```
|
|
304
304
|
|
|
305
|
-
Don't wrap definitions inside a scope with `type()`. They're resolved within the scope, and `type()` would resolve them globally, so an alias there is `unresolvable`. For a `.narrow()` or a `.pipe()` inside a scope, write a tuple: `["Tag[]", ":", (tags, ctx) => ...]` or `["string", "=>", s => s.trim()]`. For JSON data, use the built-in `"string | number | boolean | null | object.json"` rather than a recursive type of your own.
|
|
305
|
+
Don't wrap definitions inside a scope with `type()`. They're resolved within the scope, and `type()` would resolve them globally, so an alias there is `unresolvable`. For a `.narrow()` or a `.pipe()` inside a scope, write a tuple: `["Tag[]", ":", (tags, ctx) => ...]` or `["string", "=>", s => s.trim()]`. For JSON data, use the built-in `"string | number | boolean | null | object.json"` rather than a recursive type of your own. Define recursive Types once, at module scope, since each stays in memory for the life of the process.
|
|
306
306
|
|
|
307
|
-
TypeScript can't write an inferred cycle in a `.d.ts`: `tsc` declares it as `any`, or fails with `TS5088`. In a package or project whose declarations are emitted, write them with `npx
|
|
307
|
+
TypeScript can't write an inferred cycle in a `.d.ts`: `tsc` declares it as `any`, or fails with `TS5088`. In a package or project whose declarations are emitted, write them with `npx @ark/aot --declare` instead, which names each cycle for its alias and changes nothing at runtime. See https://arktype.io/docs/precompile/advanced.md.
|
|
308
308
|
|
|
309
309
|
## Extra keys
|
|
310
310
|
|
|
@@ -384,7 +384,7 @@ check(User, { name: "Ada" }, { name: "", age: -1 })
|
|
|
384
384
|
| `Only required keys may specify default values` | drop the `?`: a key with a default may be missing |
|
|
385
385
|
| `Argument of type 'never[]' is not assignable to parameter of type '() => ...'` at `.default([])` | an object or array default is a function, `.default(() => [])`, or `"string[] = []"` in a string |
|
|
386
386
|
| `An unordered union of a type including a morph and a type with overlapping input is indeterminate` | narrow the morph's input so no other branch accepts it, as in `"string.numeric.parse \| undefined"`; for objects that delete keys, see [Extra keys](#extra-keys) |
|
|
387
|
-
| `merge operand must be an object (was (In: { ... }) => To<...>)`, or `pick`, `omit`, `partial` | the object has a `.narrow()` and deletes undeclared keys: call the method on the object before its `.narrow()`, then add the narrow
|
|
387
|
+
| `merge operand must be an object (was (In: { ... }) => To<...>)`, or `pick`, `omit`, `partial` | the object has a `.narrow()` and deletes undeclared keys or has a default or morph at a key: call the method on the object before its `.narrow()`, then add the narrow |
|
|
388
388
|
| `Intersection at a of string and number results in an unsatisfiable type` | `.and()` found conflicting keys; use `.merge()` to replace them |
|
|
389
389
|
| `Index keys "a", "b" should be specified as named props` | write `Record<"a" \| "b", T>` as `{ a: T, b: T }` |
|
|
390
390
|
| `configure() must be called before any Type is created` | import the module calling `configure()` before any module that creates a Type |
|
|
@@ -114,7 +114,7 @@ const argsSchema = {
|
|
|
114
114
|
6. **Compare with the originals while Zod is still installed.** The project's tests check the schemas they use. Check the rest directly, and every schema if there's no `test` script: in a scratch file, import each original from a copy of its file (`git show HEAD:src/schemas.ts > scratch/schemas.zod.ts`) and its conversion, call both on the same inputs and compare which they accept and what they return. Use the project's fixtures or sample data where it has them, plus each case [Defaults that differ](#defaults-that-differ) lists: a missing key, a key present as `undefined`, an undeclared key, a wrong type, an array in place of an object, an empty string and a value at each bound. Fix each difference in the schema, then delete the scratch files. [How to convert](#how-to-convert) shows a check that their types match too.
|
|
115
115
|
7. **Swap dependencies.** Remove `zod` and the packages that existed only for it, and add their replacements, with the project's package manager, like `pnpm remove zod` or `npm uninstall zod`. Editing `package.json` by hand leaves the lockfile stale, and some package managers then refuse to run scripts. If `arktype` is already a dependency, keep that version. Don't upgrade another dependency to a new major version, like the MCP SDK to v2, so it takes ArkType; keep Zod for it as the table says. If a dependency requires `zod` as a peer dependency, keep it installed for that dependency and say so.
|
|
116
116
|
8. **Verify.** Run the `typecheck` and `test` scripts in `package.json` until both pass; `npx tsc` alone can miss a second tsconfig. Run the `build` script too, if there is one. If Jest fails with `Must use import to load ES Module`, naming arktype, [have it transform arktype's packages](https://arktype.io/docs/faq#why-does-jest-say-must-use-import-to-load-es-module), which ship ES modules only. If there's no `test` script, step 6's comparison is the behavior check, so say what it covered. Search again for imports of the old library, and check that every file importing `arktype`, `.js` files included, imports the config first if it creates a Type. Change a test only where it asserts the old library itself, like a Zod error message, an issue `code` or `instanceof ZodError`. Never delete a test, weaken an assertion or change an expected value to match new output. The exception is a snapshot of generated JSON Schema, which ArkType writes in a canonical order: required keys, then optional ones, each sorted, and enum and union members sorted. Reorder such a snapshot only when order is its only difference.
|
|
117
|
-
9. **Precompile, if asked.** Set it up as the skill's `references/precompile.md`, or [Precompile setup](https://arktype.io/docs/precompile/setup), says: add `@ark/aot` and the plugin for the bundler, name the config file from step 2 as `config` in `ark.json`, run `npx
|
|
117
|
+
9. **Precompile, if asked.** Set it up as the skill's `references/precompile.md`, or [Precompile setup](https://arktype.io/docs/precompile/setup), says: add `@ark/aot` and the plugin for the bundler, name the config file from step 2 as `config` in `ark.json`, run `npx @ark/aot --check src` until it accepts every definition, then run the `typecheck`, `test` and `build` scripts again. A library others install doesn't precompile itself: it publishes what `tsc` emits and [opts in](https://arktype.io/docs/precompile/advanced#libraries), so each app's build precompiles it. With `drizzle-arktype`, see [Precompile](#precompile) below.
|
|
118
118
|
10. **Report** what still behaves differently, if anything, such as error message text, any snapshot you reordered, and any dependency you kept Zod for. Where the code ships to a browser and wasn't precompiled, say that arktype's parser now ships with it, which [precompiling](#precompile) removes.
|
|
119
119
|
|
|
120
120
|
## Zod side by side
|
|
@@ -145,9 +145,9 @@ import "./arktype.config.ts"
|
|
|
145
145
|
import { type } from "arktype"
|
|
146
146
|
|
|
147
147
|
const User = type({
|
|
148
|
-
name:
|
|
148
|
+
name: type.string.atLeastLength(1, "Name is required"),
|
|
149
149
|
email: "string.email",
|
|
150
|
-
"age?": "number.integer
|
|
150
|
+
"age?": "0 <= number.integer <= 9007199254740991",
|
|
151
151
|
role: "'admin' | 'member' = 'member'"
|
|
152
152
|
})
|
|
153
153
|
|
|
@@ -172,7 +172,7 @@ configure({
|
|
|
172
172
|
})
|
|
173
173
|
```
|
|
174
174
|
|
|
175
|
-
Import it before anything creates a Type. To convert one schema without it, say both in the definition: `"+": "delete"` in each object,
|
|
175
|
+
Import it before anything creates a Type. To convert one schema without it, say both in the definition: `"+": "delete"` in each object, `"age?": "0 <= number.integer <= 9007199254740991 | undefined"`, and write `role`'s default as [Defaults that differ](#defaults-that-differ) does, so `undefined` gets it too. [Defaults that differ](#defaults-that-differ) has every default to check, for each library.
|
|
176
176
|
|
|
177
177
|
## Zod
|
|
178
178
|
|
|
@@ -240,11 +240,11 @@ Zod Mini spells most methods as functions, so `z.optional(z.string())` maps like
|
|
|
240
240
|
| `.extend({ ... })`, `.safeExtend({ ... })`, `.merge(B)` | `.merge({ ... })`, `.merge(B)` |
|
|
241
241
|
| `.pick({ a: true })`, `.omit({ a: true })` | `.pick("a")`, `.omit("a")` |
|
|
242
242
|
| `.partial()`, `.required()`, `.keyof()` | same names |
|
|
243
|
-
| `.required()` on
|
|
243
|
+
| `.required()` on a key with a default | `.required()`, which rejects `undefined` there where Zod's still fills in the default |
|
|
244
244
|
| `.deepPartial()` (Zod 3), `z.deepPartial(T)` | no equivalent: call `.partial()` on each nested object's Type |
|
|
245
245
|
| any of those on a schema with a `.refine()` | on the object before its `.narrow()`, see [Composing objects](#composing-objects) |
|
|
246
246
|
| `T.shape.a`, `Object.keys(T.shape)` | `T.get("a")`, `T.props.map(prop => prop.key)` |
|
|
247
|
-
| `z.object({ a: T.shape.a, b: B })`, to reuse a key | `T.pick("a").merge({ b: B })`: `T.get("a")`
|
|
247
|
+
| `z.object({ a: T.shape.a, b: B })`, to reuse a key | `T.pick("a").merge({ b: B })`: `T.get("a")` drops `?` and defaults, adding `\| undefined` |
|
|
248
248
|
| `.strict()`, `z.strictObject` | `"+": "reject"` in the object, or `T.onUndeclaredKey("reject")` |
|
|
249
249
|
| `.passthrough()`, `.loose()`, `z.looseObject` | `"+": "ignore"` in the object, or `T.onUndeclaredKey("ignore")` |
|
|
250
250
|
| the same, where code reads the extra keys | `"[string]": "unknown"` beside it, which infers the index signature Zod's type had |
|
|
@@ -536,7 +536,7 @@ const Flag = type("'true' | 'false'").pipe(s => s === "true")
|
|
|
536
536
|
```
|
|
537
537
|
|
|
538
538
|
- A default is an input, so a transform's default is what it parses, as Zod's `.prefault()` is: `z.stringbool().default(false)` is `Flag.default("false")`.
|
|
539
|
-
- `unknown` includes `undefined`, so a coercion at an optional or defaulted
|
|
539
|
+
- `unknown` includes `undefined`, so a coercion at an optional key, or at a defaulted one without `exactOptionalPropertyTypes: false`, is handed a key that's present as `undefined`, as `process.env`'s may be, where Zod's `.optional()` and `.default()` answer first. Return it or the default from the pipe, as `Server` does: `z.coerce.number().optional()` is `"limit?": type("unknown").pipe(v => (v === undefined ? v : Number(v))).to("number | undefined")`.
|
|
540
540
|
|
|
541
541
|
### Form data
|
|
542
542
|
|
|
@@ -769,13 +769,13 @@ const types = scope({
|
|
|
769
769
|
|
|
770
770
|
Inside a scope, `type()` can't see the scope's names, so write a refinement or a transform there as a tuple: `["Post[]", ":", (posts, ctx) => ...]` for a `.narrow()`, `["string", "=>", s => s.trim()]` for a `.pipe()`.
|
|
771
771
|
|
|
772
|
-
Delete the hand-written TypeScript type that `z.lazy` needed; `typeof Category.infer` and `typeof types.Post.infer` replace it. Where the module's declarations are emitted, as for a published package or a TypeScript project reference, `tsc` can't write the cycle in a `.d.ts`, so emit them with [`arktype-precompile --declare`](https://arktype.io/docs/precompile/advanced#cyclic-types), or keep the type and [declare the Type with it](https://arktype.io/docs/scopes#cyclic-types), `type.declare<Category>().type({ ... })`. For JSON data, use the `object.json` keyword rather than a recursive type of your own: `z.json()` is `"string | number | boolean | null | object.json"`.
|
|
772
|
+
Delete the hand-written TypeScript type that `z.lazy` needed; `typeof Category.infer` and `typeof types.Post.infer` replace it. Where the module's declarations are emitted, as for a published package or a TypeScript project reference, `tsc` can't write the cycle in a `.d.ts`, so emit them with [`arktype-precompile --declare`](https://arktype.io/docs/precompile/advanced#cyclic-types), or keep the type and [declare the Type with it](https://arktype.io/docs/scopes#cyclic-types), `type.declare<Category>().type({ ... })`. For JSON data, use the `object.json` keyword rather than a recursive type of your own: `z.json()` is `"string | number | boolean | null | object.json"`. Define recursive Types once, at module scope, since each stays in memory for the life of the process.
|
|
773
773
|
|
|
774
774
|
### Composing objects
|
|
775
775
|
|
|
776
776
|
`.merge()` replaces keys like Zod's `.extend()` or `v.object({ ...a.entries })`. `.and()` intersects, like `Type.Composite`, and throws when a key's types conflict. `.pick()` and `.omit()` take key names, as in `.pick("name", "email")`. Like Zod's, they and `.partial()` keep an object's `"+"`, so a `.strict()` object's are strict too, where `.merge()` takes `"+"` from the object merged in.
|
|
777
777
|
|
|
778
|
-
A `.narrow()` on an object doesn't survive them. `.merge()`, a `"..."` spread, `.pick()`, `.omit()` and `.partial()` take the object's keys and leave its narrow behind, so a refinement Zod's `.extend()` kept no longer runs. Where the object also deletes undeclared keys, they throw instead: `merge operand must be an object (was (In: { ... }) => To<...>)`. Either way, keep the keys in a Type of their own, derive from that, and add the narrow last:
|
|
778
|
+
A `.narrow()` on an object doesn't survive them. `.merge()`, a `"..."` spread, `.pick()`, `.omit()` and `.partial()` take the object's keys and leave its narrow behind, so a refinement Zod's `.extend()` kept no longer runs. Where the object also transforms its input — deletes undeclared keys, or has a default or morph at a key — they throw instead: `merge operand must be an object (was (In: { ... }) => To<...>)`. Either way, keep the keys in a Type of their own, derive from that, and add the narrow last:
|
|
779
779
|
|
|
780
780
|
```ts
|
|
781
781
|
import { type, type Traversal } from "arktype"
|
|
@@ -912,7 +912,7 @@ const parameters = (object: JsonSchema, where: "query" | "path") => {
|
|
|
912
912
|
}
|
|
913
913
|
```
|
|
914
914
|
|
|
915
|
-
With `onUndeclaredKey: "delete"`,
|
|
915
|
+
With `onUndeclaredKey: "delete"`, `toJsonSchema()` writes what an object accepts, so without `additionalProperties: false`, which `.out.toJsonSchema()` writes. `.in` and `.out` write a registered schema in full where `toJsonSchema()` writes its `$ref`.
|
|
916
916
|
|
|
917
917
|
Where you call `toJsonSchema` yourself, a scope can be the registry instead: with [`aliasDefs: true`](https://arktype.io/docs/json-schema/to-json-schema#named-aliases), each alias is written once, by name, and a `$ref` wherever it's used, and a [`defs` fallback](https://arktype.io/docs/json-schema/to-json-schema#openapi-30) collects them into `components`.
|
|
918
918
|
|
|
@@ -941,9 +941,9 @@ Zod 3, and Zod 4 before 4.5, also take `2025-01-01T10:00Z`, so write the seconds
|
|
|
941
941
|
|
|
942
942
|
Valibot's `v.isoDate()` is `Day` and `v.isoTimestamp()` is `Zoned`. Its `v.isoDateTime()` takes only minutes and no offset, like `2025-01-01T10:00`, so its pattern is `/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/`.
|
|
943
943
|
|
|
944
|
-
`string.date.iso` checks the
|
|
944
|
+
`string.date.iso` checks the calendar as Zod does, so `2020-02-30` fails and `2024-02-29` passes.
|
|
945
945
|
|
|
946
|
-
|
|
946
|
+
`toJsonSchema()` throws for these, since it writes one `pattern` for a string, and the schema a tool reads through Standard Schema keeps both, the second in an `allOf`. Where one is converted, say what to write, as Zod does: `Instant.configure({ jsonSchema: { type: "string", format: "date-time" } })`, or `format: "date"` for `Day`.
|
|
947
947
|
|
|
948
948
|
In JSON Schema, `format` is an annotation and validates nothing unless the validator, like ajv with `ajv-formats`, checks it; `string.email` always validates.
|
|
949
949
|
|
|
@@ -1027,7 +1027,7 @@ export default defineConfig({
|
|
|
1027
1027
|
})
|
|
1028
1028
|
```
|
|
1029
1029
|
|
|
1030
|
-
To see what's left, run `npx
|
|
1030
|
+
To see what's left, run `npx @ark/aot --check`, which lists each definition the default build would refuse. See [Dynamic vs Precompiled](https://arktype.io/docs/precompile/dynamic-vs-precompiled).
|
|
1031
1031
|
|
|
1032
1032
|
`drizzle-arktype`, from [Swap integrations](#migrating-a-project), creates Types from your tables as the program runs, so the build [runs it on arktype](https://arktype.io/docs/precompile/options#dependencies), shipping the parser. To precompile those Types too, write each one out, with a line that fails to compile if it drifts from the table, like `const matches: Equals<typeof selectTask.infer, typeof tasks.$inferSelect> = true`, `Equals` as in [How to convert](#how-to-convert).
|
|
1033
1033
|
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
3. In a monorepo, a workspace package, like a shared `schemas` package, that pnpm, npm, Yarn or Bun workspaces link into `node_modules` resolves to its own directory, so the build precompiles it with the program; nothing extra is needed. Pass each such package's directory to `--check` too, e.g. `src ../../packages/schemas/src`.
|
|
10
10
|
4. If the project calls `configure()`, name its module as `config` in an `ark.json` beside `package.json`, as below, or under an `"ark"` key of the same shape in `package.json` (the build fails if both are set). Put build options there, not in the plugin call: the plugin, the test drivers, `arktype-precompile` and the ESLint rule all read `ark.json`, but a test driver doesn't see the plugin's options, so the tests would fail without them.
|
|
11
11
|
5. Make the tests run precompiled too, as below.
|
|
12
|
-
6. Run `npx arktype-precompile --check src
|
|
12
|
+
6. Run `npx @ark/aot --check src` (or `pnpm exec arktype-precompile --check src`), which reads each JavaScript and TypeScript file in `src` and lists each definition the build would refuse, with the fix, until every site is accepted. Then run the build and the tests.
|
|
13
13
|
|
|
14
14
|
The ESLint rule reports the same errors as code is written: add `configs.recommended` from `@ark/aot/eslint` to `eslint.config.js`.
|
|
15
15
|
|
|
@@ -100,9 +100,9 @@ On the pages router's server, Next otherwise leaves dependencies external, where
|
|
|
100
100
|
|
|
101
101
|
A project that `tsc` compiles to run from its `outDir` precompiles what it emits: `"build": "tsc && arktype-precompile --emit"`. `--emit` writes each module reading arktype again from its source, so run anything that rewrites `tsc`'s output, like `tsc-alias`, after it: `"tsc && arktype-precompile --emit && tsc-alias"`.
|
|
102
102
|
|
|
103
|
-
A library that others install doesn't precompile itself, with `--emit` or a bundler: an app's build fails for a dependency that imports `arktype/runtime`. Precompiling a library means publishing what `tsc` emits with `"ark": { "aot": true }` in its `package.json` and `arktype` in `peerDependencies`, so each app's build precompiles it, and running `npx
|
|
103
|
+
A library that others install doesn't precompile itself, with `--emit` or a bundler: an app's build fails for a dependency that imports `arktype/runtime`. Precompiling a library means publishing what `tsc` emits with `"ark": { "aot": true }` in its `package.json` and `arktype` in `peerDependencies`, so each app's build precompiles it, and running `npx @ark/aot --check src` until it accepts every definition.
|
|
104
104
|
|
|
105
|
-
A package whose types others read writes its declarations with `npx
|
|
105
|
+
A package whose types others read writes its declarations with `npx @ark/aot --declare` in place of `tsc`'s: each Type, Scope and Module is declared by its shape, in a `~arktype.d.ts` beside the others, and nothing changes at runtime. A cyclic scope's types ship no other way, since `tsc` declares an inferred cycle as `any` or fails with `TS5088`. It reads the program with the TypeScript installed: 5.5 to 6's compiler API, or TypeScript 7.1 or later's native API through tsgo, or tsrs where its binary is installed; `--checker tsjs|tsgo|tsrs` chooses one.
|
|
106
106
|
|
|
107
107
|
### Tests
|
|
108
108
|
|
|
@@ -155,7 +155,7 @@ Composing inside a definition is fine anywhere, even in a function body: `const
|
|
|
155
155
|
|
|
156
156
|
## Errors and their fixes
|
|
157
157
|
|
|
158
|
-
Each build error names the file, line and column, then why, then the fix on its last line. Apply that fix. A bundler stops at the first error, so run `npx
|
|
158
|
+
Each build error names the file, line and column, then why, then the fix on its last line. Apply that fix. A bundler stops at the first error, so run `npx @ark/aot --check src` to see them all. Don't set `allowUnsafe` to get past one, and don't switch to `runtime: "arktype"` unless the program really composes or introspects Types at runtime, as that ships arktype's parser again.
|
|
159
159
|
|
|
160
160
|
| The build says | Fix |
|
|
161
161
|
| -------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
|
package/out/chunk-CSVAPWDE.js
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
withoutUndefined
|
|
3
|
-
} from "./chunk-KVQCLBJR.js";
|
|
4
|
-
import {
|
|
5
|
-
$ark
|
|
6
|
-
} from "./chunk-A7A6FDBS.js";
|
|
7
|
-
import {
|
|
8
|
-
hasArkKind
|
|
9
|
-
} from "./chunk-5TN5JRP7.js";
|
|
10
|
-
import {
|
|
11
|
-
omit,
|
|
12
|
-
throwParseError
|
|
13
|
-
} from "./chunk-QBX4MEQR.js";
|
|
14
|
-
|
|
15
|
-
// ../schema/out/shared/codecs.js
|
|
16
|
-
var inverses = /* @__PURE__ */ new WeakMap();
|
|
17
|
-
var encoders = /* @__PURE__ */ new WeakMap();
|
|
18
|
-
var writeMissingInverseMessage = (expression) => `A morph must have an inverse to be encoded: ${expression}`;
|
|
19
|
-
var encode = function (node) {
|
|
20
|
-
let encoder = encoders.get(node);
|
|
21
|
-
if (!encoder)
|
|
22
|
-
encoders.set(node, encoder = node.$.fin(encoderOf(node)));
|
|
23
|
-
return encoder;
|
|
24
|
-
};
|
|
25
|
-
var encoderOf = function (node) { return node.transform((kind, inner) => kind === "morph" ? invert(inner, node.$) : kind === "optional" && "default" in inner ? node.$.node("required", {
|
|
26
|
-
...omit(inner, { default: 1 }),
|
|
27
|
-
value: decodedValueOf(inner.value, node.$)
|
|
28
|
-
}) : kind === "structure" ? requireDefaulted(inner) : kind === "sequence" ? requireDefaultables(inner, node.$) : kind === "union" ? {
|
|
29
|
-
...inner,
|
|
30
|
-
// a branch without morphs encodes a value unchanged
|
|
31
|
-
branches: [...inner.branches].sort((l, r) => Number(l.tfs) - Number(r.tfs)),
|
|
32
|
-
ordered: true
|
|
33
|
-
} : inner, {
|
|
34
|
-
shouldTf: (n) => n.tfs || n.is("optional") && n.hasDef()
|
|
35
|
-
}) };
|
|
36
|
-
var invert = function (inner, $) {
|
|
37
|
-
let encoder = inner.declaredOut ?? $ark.kw.unknown.internal;
|
|
38
|
-
let inverse = inner.inverse;
|
|
39
|
-
const head = hasArkKind(inner.morphs[0], "root") ? inner.morphs[0] : void 0;
|
|
40
|
-
for (let i = inner.morphs.length - 1; i >= (head ? 1 : 0); i--) {
|
|
41
|
-
const morph = inner.morphs[i];
|
|
42
|
-
if (hasArkKind(morph, "root")) {
|
|
43
|
-
encoder = pipeEncoder(encoder, encoderOf(morph));
|
|
44
|
-
continue;
|
|
45
|
-
}
|
|
46
|
-
encoder = $.node("morph", {
|
|
47
|
-
morphs: [
|
|
48
|
-
encoder,
|
|
49
|
-
inverse ?? inverses.get(morph) ?? throwParseError(writeMissingInverseMessage($.node("morph", inner).expression))
|
|
50
|
-
],
|
|
51
|
-
inverse: morph
|
|
52
|
-
});
|
|
53
|
-
inverse = void 0;
|
|
54
|
-
}
|
|
55
|
-
const decodedIn = inner.declaredIn ?? head;
|
|
56
|
-
return decodedIn ? pipeEncoder(encoder, decodedIn) : encoder;
|
|
57
|
-
};
|
|
58
|
-
var pipeEncoder = (encoder, next) => encoder.isUnknown() ? next : next.isUnknown() ? encoder : encoder.rawPipeOnce(next);
|
|
59
|
-
var requireDefaulted = function (inner) {
|
|
60
|
-
if (!inner.optional)
|
|
61
|
-
return inner;
|
|
62
|
-
const props = [...inner.required ?? [], ...inner.optional];
|
|
63
|
-
return {
|
|
64
|
-
...inner,
|
|
65
|
-
required: props.filter((prop) => prop.is("required")),
|
|
66
|
-
optional: props.filter((prop) => prop.is("optional")),
|
|
67
|
-
order: inner.order ?? props.map((prop) => prop.key)
|
|
68
|
-
};
|
|
69
|
-
};
|
|
70
|
-
var requireDefaultables = function ({ defaultables = [], ...inner }, $) { return ({
|
|
71
|
-
...inner,
|
|
72
|
-
prefix: [
|
|
73
|
-
...inner.prefix ?? [],
|
|
74
|
-
...defaultables.map(([node]) => decodedValueOf(node, $))
|
|
75
|
-
]
|
|
76
|
-
}) };
|
|
77
|
-
var decodedValueOf = (value, $) => $.cfg.exactOptionalPropertyTypes === false ? withoutUndefined(value) : value;
|
|
78
|
-
|
|
79
|
-
export {
|
|
80
|
-
inverses,
|
|
81
|
-
writeMissingInverseMessage,
|
|
82
|
-
encode
|
|
83
|
-
};
|