arktype 3.0.0-beta.0 → 3.0.0-beta.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.
Files changed (51) hide show
  1. package/README.md +162 -10
  2. package/out/attributes.d.ts +4 -2
  3. package/out/checkers.d.ts +37 -2
  4. package/out/{chunk-Z4F45ZTE.js → chunk-2U2AWCI3.js} +3 -3
  5. package/out/{chunk-5NJWD7CO.js → chunk-5U2ZMDNI.js} +215 -140
  6. package/out/{chunk-KJ3C5GTL.js → chunk-DLWCO4RV.js} +1 -1
  7. package/out/{chunk-H4VRNKMB.js → chunk-E3DZXEZK.js} +1 -1
  8. package/out/{chunk-EJZJUYPI.js → chunk-EVCML554.js} +2 -2
  9. package/out/{chunk-KI2MOYNT.js → chunk-GPNK5353.js} +184 -32
  10. package/out/{chunk-LUXERINN.js → chunk-RAZ3TXOE.js} +40 -38
  11. package/out/chunk-SWOOCAHN.js +4 -0
  12. package/out/{chunk-ARTS74PX.js → chunk-UETB6YE2.js} +1 -1
  13. package/out/{chunk-JHDPSG5Y.js → chunk-UK5UGFOX.js} +6 -6
  14. package/out/{chunk-WEPQK3AY.js → chunk-VV6YDN2D.js} +441 -145
  15. package/out/codecs.js +5 -5
  16. package/out/config.d.ts +6 -3
  17. package/out/i18n.d.ts +1 -1
  18. package/out/i18n.js +1 -1
  19. package/out/index.d.ts +1 -1
  20. package/out/index.js +5 -5
  21. package/out/internal.d.ts +1 -0
  22. package/out/internal.js +11 -11
  23. package/out/island.js +2 -2
  24. package/out/islandDependency.js +11 -11
  25. package/out/keywords/functions.d.ts +24 -24
  26. package/out/keywords/keywords.d.ts +2 -2
  27. package/out/keywords/phrases.d.ts +23 -23
  28. package/out/keywords/ts.js +2 -2
  29. package/out/locale.d.ts +1 -1
  30. package/out/locales/de.js +1 -1
  31. package/out/locales/en.js +3 -3
  32. package/out/match.d.ts +5 -3
  33. package/out/mutatesInput.js +2 -2
  34. package/out/parser/definition.d.ts +54 -3
  35. package/out/parser/shift/operand/unenclosed.d.ts +3 -1
  36. package/out/parser/shift/operand/unresolvable.d.ts +68 -0
  37. package/out/parser/shift/operator/bounds.d.ts +4 -1
  38. package/out/runtime.d.ts +34 -25
  39. package/out/runtime.js +2 -2
  40. package/out/runtimeDevelopment.js +63 -4
  41. package/out/scope.d.ts +4 -4
  42. package/out/standalone/artifact.d.ts +4 -0
  43. package/out/standalone/baking.d.ts +8 -1
  44. package/out/standalone/sites.d.ts +6 -3
  45. package/out/standalone/values.d.ts +2 -0
  46. package/out/type.d.ts +42 -5
  47. package/package.json +49 -6
  48. package/skills/arktype/SKILL.md +211 -92
  49. package/skills/arktype/references/migrating.md +990 -255
  50. package/skills/arktype/references/precompile.md +8 -5
  51. package/out/chunk-EDEN5UWG.js +0 -4
package/README.md CHANGED
@@ -1,37 +1,189 @@
1
1
  <h1 align="center">ArkType</h1>
2
2
  <h3 align="center"><i>TypeScript's 1:1 validator, optimized from editor to runtime</i></h3>
3
3
 
4
- ArkType validates data at runtime with definitions written in TypeScript's own syntax.
4
+ <p align="center">
5
+ <a href="https://arktype.io/#board"><img src="https://arktype.io/image/og.png" width="640" alt="ArkType's places for validation speed, startup and bundle size, from the scoreboard at arktype.io" /></a>
6
+ </p>
5
7
 
6
- ```sh
7
- npm install arktype
8
- ```
8
+ Write a type the way TypeScript does. ArkType checks the definition as you type, infers the type and validates data against it at runtime.
9
+
10
+ **Using Zod?** [Switch with one prompt](#switching-from-zod). Your schemas keep their names, their messages and the libraries that read them.
9
11
 
10
12
  ```ts
11
13
  import { type } from "arktype"
12
14
 
13
15
  const User = type({
14
- name: "string",
16
+ name: "string > 0",
17
+ email: "string.email",
15
18
  "age?": "number.integer >= 0"
16
19
  })
17
20
 
18
- const out = User({ name: "Alan", age: -1 })
21
+ // { name: string; email: string; age?: number }
22
+ type User = typeof User.infer
23
+
24
+ const out = User({ name: "Ada", email: "ada@", age: -1 })
19
25
 
20
26
  if (out instanceof type.errors) {
27
+ // email must be an email address
21
28
  // age must be non-negative
22
29
  console.error(out.summary)
23
30
  } else {
24
- // { name: string; age?: number }
31
+ // out is a User
25
32
  console.log(out.name)
26
33
  }
27
34
  ```
28
35
 
29
- To ship less, [precompile](https://arktype.io/docs/precompile) your Types when you build: each becomes a validator with only the checks it needs, so arktype's parser never ships.
36
+ ```sh
37
+ npm install arktype
38
+ ```
39
+
40
+ - **Checked as you type.** A definition is parsed by TypeScript's own type system, so a typo is a type error where you wrote it.
41
+ - **Precompiled when you build.** [`@ark/aot`](#precompile) replaces each Type with a standalone validator, so no parser ships and nothing compiles at startup. [arktype.io](https://arktype.io/#board) has the benchmarks against Zod, Valibot, Typia and ajv, each with its method.
42
+ - **Accepted where Zod is.** Every Type is a [Standard Schema](https://standardschema.dev) that writes its own JSON Schema, so tRPC, oRPC, Hono, react-hook-form, the AI SDK and the MCP SDK [accept it](https://arktype.io/docs/integrations).
43
+
44
+ ## Switching from Zod
45
+
46
+ One command, then one prompt to your coding agent:
47
+
48
+ ```sh
49
+ npx skills add arktypeio/arktype
50
+ ```
51
+
52
+ ```
53
+ Migrate this project from Zod to ArkType.
54
+ ```
55
+
56
+ The [skill](https://arktype.io/docs/agents) teaches your agent ArkType and includes the [migration guide](https://arktype.io/docs/migrating) it follows. It converts each schema where it is, under the same name, keeping what the schema accepts, rejects and returns. Before removing `zod`, it runs each schema your tests don't cover against its Zod original on the same inputs, then runs your typecheck and tests. Where a dependency accepts only Zod, it keeps Zod for that call and tells you.
57
+
58
+ ```ts
59
+ // before
60
+ import { z } from "zod"
61
+
62
+ const Signup = z.object({
63
+ email: z.email("Enter a valid email"),
64
+ password: z.string().min(8, "Use 8 or more characters"),
65
+ role: z.enum(["admin", "member"])
66
+ })
67
+
68
+ type Signup = z.infer<typeof Signup>
69
+ ```
70
+
71
+ ```ts
72
+ // after
73
+ import { type } from "arktype"
74
+
75
+ const Signup = type({
76
+ email: ["string.email", "@", { message: "Enter a valid email" }],
77
+ password: ["string >= 8", "@", { message: "Use 8 or more characters" }],
78
+ role: "'admin' | 'member'"
79
+ })
80
+
81
+ type Signup = typeof Signup.infer
82
+ ```
83
+
84
+ ### What you keep
85
+
86
+ **Your messages.** A `message` is shown exactly as you wrote it and never includes the path, as Zod's `issue.message` doesn't. A form library reading either `Signup` above through Standard Schema gets the same path and message for each issue:
87
+
88
+ ```ts
89
+ Signup["~standard"].validate({ email: "x", password: "short", role: "admin" })
90
+ // {
91
+ // issues: [
92
+ // { path: ["email"], message: "Enter a valid email", ... },
93
+ // { path: ["password"], message: "Use 8 or more characters", ... }
94
+ // ]
95
+ // }
96
+ ```
97
+
98
+ A missing key says its value's message too, so it covers `required_error`. A refinement's message becomes `ctx.reject({ message, path })`, and `out.summary` names each path for a log: `email: Enter a valid email`.
99
+
100
+ **Your integrations.** A library that accepts Zod through Standard Schema accepts the Type in the same call: tRPC's `.input()`, TanStack Form, `@t3-oss/env-*`, the AI SDK's `tool()` from v6 and v2 of the MCP SDK. `zodResolver` becomes `standardSchemaResolver`, and `zod-to-json-schema` becomes `Signup.toJsonSchema()`. The guide [lists each swap](https://arktype.io/docs/migrating#migrating-a-project).
101
+
102
+ **Your pace.** A Zod schema works inside an ArkType definition, so you can convert one file at a time:
103
+
104
+ ```ts
105
+ // not converted yet
106
+ const Address = z.object({ street: z.string(), city: z.string() })
107
+
108
+ const User = type({ name: "string", address: Address })
109
+ ```
110
+
111
+ Not switching yet? [ArkSets](https://arktype.io/docs/sets) runs ArkType's set theory on the Zod schemas you have: `isSubset(v1, v2)` tells you whether a schema change still accepts everything the old one did.
112
+
113
+ ### What changes
114
+
115
+ These are the differences a migration has to handle. The guide covers each, and your agent reports any it couldn't keep identical.
116
+
117
+ - **Defaults differ.** `z.object()` removes undeclared keys and `.optional()` accepts `undefined`. ArkType keeps undeclared keys and treats an optional key as TypeScript's `exactOptionalPropertyTypes` does, so the migration sets `onUndeclaredKey: "delete"` once and writes `"key?": "T | undefined"`. [Defaults that differ](https://arktype.io/docs/migrating#defaults-that-differ) has the full table.
118
+ - **Validation is synchronous.** An async `.refine()` moves out of the schema, to run after validation.
119
+ - **A check across keys runs last.** A `.narrow()` on an object runs once every key is valid, where Zod 4 also runs a `.refine()` when a key fails a check like `.min()`.
120
+ - **Default error text is ArkType's.** `must be an email address` where Zod says `Invalid email address`. Messages you wrote are unchanged.
121
+
122
+ <details>
123
+ <summary><b>Zod to ArkType, the common cases</b></summary>
124
+
125
+ | Zod | ArkType |
126
+ | ----------------------------------------- | ----------------------------------------------------------- |
127
+ | `z.object({ ... })` | `type({ ... })` |
128
+ | `z.string().min(1).max(100)` | `"1 <= string <= 100"` |
129
+ | `z.string().email()`, `.url()`, `.uuid()` | `"string.email"`, `"string.url"`, `"string.uuid"` |
130
+ | `z.number().int().positive()` | `"number.integer > 0"` |
131
+ | `z.enum(["a", "b"])` | `"'a' \| 'b'"` |
132
+ | `z.array(z.string()).min(1)` | `"string[] >= 1"` |
133
+ | `z.union([A, B])`, `z.discriminatedUnion` | `A.or(B)`, which finds the discriminant itself |
134
+ | `.extend({ ... })` | `.merge({ ... })` |
135
+ | `.refine(fn, message)` | `.narrow((v, ctx) => fn(v) \|\| ctx.reject({ message }))` |
136
+ | `.transform(fn)` | `.pipe(fn)` |
137
+ | `z.infer<typeof T>`, `z.input` | `typeof T.infer`, `typeof T.inferIn` |
138
+ | `T.parse(x)`, `T.safeParse(x)` | `T.assert(x)`, `T(x)` checked with `instanceof type.errors` |
139
+
140
+ </details>
141
+
142
+ The guide maps [the whole Zod API](https://arktype.io/docs/migrating#zod), and has tables for Valibot, TypeBox, Yup, Joi, Effect Schema and JSON Schema.
143
+
144
+ ## Precompile
145
+
146
+ ```sh
147
+ npm install -D @ark/aot
148
+ ```
149
+
150
+ ```ts
151
+ // vite.config.ts
152
+ import { precompile } from "@ark/aot/unplugin"
153
+
154
+ export default {
155
+ plugins: [precompile.vite()]
156
+ }
157
+ ```
158
+
159
+ When you build, each Type your program defines becomes a validator with only the checks it needs, on `arktype/runtime`. The code that calls it doesn't change, and neither do your definitions or the types they infer.
160
+
161
+ - **Plugins** for Vite, Rollup, Rolldown, webpack, Rspack, esbuild, Bun and Farm, with drivers for Node, Bun's runtime and Next's Turbopack. Without a bundler, run `tsc && npx arktype-precompile --emit`. [Setup](https://arktype.io/docs/precompile/setup) has a config for each.
162
+ - **Errors that name the fix.** A definition the build can't precompile, like one built from a value known only at runtime, fails the build with its file, line, reason and what to write instead. `npx arktype-precompile --check "src/**/*.ts"` lists them before you switch.
163
+ - **Dynamic when you need it.** `type()` still parses definitions at runtime with no build step, and a precompiled build can leave any definition to it.
164
+
165
+ [Precompile](https://arktype.io/docs/precompile) has the benchmarks, the build times and how a precompiled validator differs.
166
+
167
+ ## With agents
168
+
169
+ - `npx skills add arktypeio/arktype` adds the [skill](https://arktype.io/docs/agents): current syntax, how it maps to TypeScript, the mistakes to avoid and how to work in a precompiled project.
170
+ - Every docs page is Markdown at its URL plus `.md`, and [arktype.io/llms.txt](https://arktype.io/llms.txt) indexes them.
171
+ - Every error names its fix, from `tsc`, from the build and from validation, so an agent can act on each without a docs lookup.
172
+
173
+ ## New in 3.0
174
+
175
+ - [Precompilation](https://arktype.io/docs/precompile) with `@ark/aot`
176
+ - [Codecs](https://arktype.io/docs/codecs): `codec` pairs a morph with its inverse, and `.encode` inverts a Type's morphs
177
+ - [Errors as plain objects](https://arktype.io/docs/errors), each with a `path`, a `code` and a `message`
178
+ - [Error messages in other languages](https://arktype.io/docs/i18n)
179
+ - [JSON Schema](https://arktype.io/docs/json-schema/to-json-schema) for draft 2020-12, draft-07 and OpenAPI 3.0, from every Type
180
+ - [ArkSets](https://arktype.io/docs/sets): `isSubset(v1, v2)` and the rest of ArkType's set algebra, for Types, JSON Schemas and Standard JSON Schemas like Zod's
181
+
182
+ The [announcement](https://arktype.io/docs/blog/3.0) has the rest, and [Upgrading](https://arktype.io/docs/upgrading) lists each breaking change since 2.2 with what to write instead.
30
183
 
31
184
  ## Docs
32
185
 
33
- - [arktype.io](https://arktype.io/docs/intro/setup)
34
- - [Using ArkType with coding agents](https://arktype.io/docs/agents): the skill (`npx skills add arktypeio/arktype`) and Markdown docs at [arktype.io/llms.txt](https://arktype.io/llms.txt)
186
+ [arktype.io](https://arktype.io/docs/intro/setup)
35
187
 
36
188
  ## Contributions
37
189
 
@@ -1,5 +1,5 @@
1
1
  import type { ArkError, ArkErrors, arkKind, Morph } from "@ark/schema";
2
- import { type anyOrNever, type array, type Brand, type brand, type Hkt, type intersectArrays, type isSafelyMappable, type merge, type optionalKeyOf, type Primitive, type show, type unionKeyOf, type unionToTuple } from "@ark/util";
2
+ import { type anyOrNever, type array, type Brand, type brand, type Hkt, type inferred, type intersectArrays, type isSafelyMappable, type merge, type optionalKeyOf, type Primitive, type show, type unionKeyOf, type unionToTuple } from "@ark/util";
3
3
  import type { arkPrototypes } from "./keywords/constructors.ts";
4
4
  import type { type } from "./keywords/keywords.ts";
5
5
  import type { Type } from "./type.ts";
@@ -122,7 +122,9 @@ type normalizeMorphDistribution<t, undistributedIn = t extends InferredMorph<inf
122
122
  export type defaultFor<t = unknown> = (Primitive extends t ? Primitive : t extends Primitive ? t : never) | (() => t);
123
123
  export type termOrType<t> = t | Type<t, any>;
124
124
  export type inferIntersection<l, r> = normalizeMorphDistribution<_inferIntersection<l, r, false>>;
125
- export type inferMorph<t, morph extends Morph> = morph extends type.cast<infer tMorph> ? inferPipe<t, tMorph> : inferMorphOut<morph> extends infer out ? (In: distill.In<t>) => Out<out> : never;
125
+ export type inferMorph<t, morph extends Morph> = morph extends {
126
+ [inferred]: infer tMorph;
127
+ } ? inferPipe<t, tMorph> : morph extends type.cast<infer tMorph> ? inferPipe<t, tMorph> : inferMorphOut<morph> extends infer out ? (In: distill.In<t>) => Out<out> : never;
126
128
  export type inferPipe<l, r> = normalizeMorphDistribution<_inferIntersection<l, r, true>>;
127
129
  type _inferIntersection<l, r, piped extends boolean> = [
128
130
  l & r
package/out/checkers.d.ts CHANGED
@@ -19,6 +19,10 @@ export interface Checker {
19
19
  a?: ((data: unknown, ctx: any, k?: PropertyKey) => boolean) | undefined;
20
20
  /** where it holds an alias, how a key applies it */
21
21
  e?: ApplyAt;
22
+ /** the message a key it's the value of says where the key is missing, if its own */
23
+ x?: string | undefined;
24
+ /** where x is configured for required's code, what notes that on the missing key's issue */
25
+ text?: SiteContext["text"];
22
26
  }
23
27
  type DefaultWrite = (data: any, ctx?: any) => unknown;
24
28
  interface Transform {
@@ -54,8 +58,32 @@ export declare const aliasedMember: (checker: Checker, id: string) => Checker;
54
58
  export declare const holdingAlias: (checker: Checker) => Checker;
55
59
  export declare const narrowedInCtx: (read: () => (data: any, ctx: any) => boolean, description?: string) => Checker;
56
60
  export declare const narrowedAfter: (structure: Checker, narrowed: Checker) => Checker;
61
+ export declare const transformingNarrowedAfter: (structure: Checker, narrowed: Checker) => Checker;
57
62
  export declare const narrowedByEach: (checkers: readonly Checker[]) => Checker;
58
63
  export declare const narrowed: (narrow: (data: any) => boolean, description?: any) => Checker;
64
+ /**
65
+ * a callback a build wrote where a unit reads it, as nothing names it: one a
66
+ * declaration held itself would be named for its variable, which what
67
+ * describes it would then read, and no minifier writes a function reading
68
+ * `arguments` into its call
69
+ */
70
+ export declare function unnamed(): unknown;
71
+ /**
72
+ * a narrow a build read as `(data, ctx) => passes(data) || ctx.reject({ message })`,
73
+ * which is what it returns: the callback of nearly every field saying why it failed
74
+ */
75
+ export declare const rejecting: (passes: (data: any) => unknown, message: string) => (data: unknown, ctx: any) => any;
76
+ /**
77
+ * the checker of that narrow, written where a unit reads it as its test and
78
+ * its message: data failing the test fails at the site its ctx would reject
79
+ * at, so it reads no ctx, and its root no Traversal
80
+ */
81
+ export declare const rejectsWith: (passes: (data: any) => unknown, message: string, description?: string) => Checker;
82
+ /**
83
+ * the checker of one a build proved of that shape but holds as a function:
84
+ * the only ctx it's passed is one whose `reject` fails at that site
85
+ */
86
+ export declare const narrowedRejecting: (read: () => (data: any, ctx: any) => unknown, description?: string) => Checker;
59
87
  /** the checker a validator is created from, as a call a bundler may drop */
60
88
  export declare const allowsOf: (validator: {
61
89
  allows: Checker;
@@ -85,6 +113,8 @@ export declare const optional: (checker: Checker, value_0?: unknown) => Checker;
85
113
  export declare const writtenBy: (checker: Checker, write: DefaultWrite, replacesUndefined?: 1) => Checker;
86
114
  export declare const replacingUndefined: (checker: Checker, value: unknown) => Checker;
87
115
  export declare const required: (checker: Checker, description: string) => Checker;
116
+ export declare const sayingMissing: (checker: Checker, message: string, text?: SiteContext["text"], description?: any) => Checker;
117
+ export declare const saying: (checker: Checker, message: string) => Checker;
88
118
  type Props = Record<string, Checker>;
89
119
  export declare const props: (declared: Props) => Checker;
90
120
  export declare const closedProps: (declared: Props, description?: any) => Checker;
@@ -102,6 +132,7 @@ export declare const failingClosedProps: (declared: Props, description?: string,
102
132
  export declare const deletingProps: (declared: Props) => Checker;
103
133
  export declare const failingDeletingProps: (declared: Props, withCtx?: 1) => Checker;
104
134
  type SitedWrite = (data: any, checkers: readonly Checker[], values: WrittenValues | undefined, copy: (data: any) => any) => any;
135
+ export declare const sitedProps: (allows: (data: any, checkers: readonly Checker[]) => boolean, declared: Props, write?: SitedWrite | 0, walks?: Checker) => Checker;
105
136
  export declare const sited: (allows: (data: any, checkers: readonly Checker[]) => boolean, declared: Props, write: SitedWrite | 0, writes: 0 | 1 | 2 | 3 | 4, removed?: string, withCtx?: 1) => Checker;
106
137
  export declare const elementsOf: (element: Checker) => Checker;
107
138
  export declare const transformingElementsOf: (element: Checker) => Checker;
@@ -123,11 +154,15 @@ export declare const checkerNode: (checker: Checker, reportsPred?: true) => Read
123
154
  tTransform: Transform | undefined;
124
155
  allows: Checker;
125
156
  }>;
126
- export declare const ctxNode: (flags: object, tAllows: Checker, tApply: Checker, tTransform?: Transform) => any;
157
+ export declare const ctxNode: (flags: object, tAllows: Checker, tApply: Checker, tTransform?: Transform, declared?: object) => any;
158
+ export declare const readingCtxNode: (tApply: Checker, reportsPred?: true) => any;
127
159
  export declare const failingBranches: (branchOf: (data: any) => Checker | 1, branches: readonly (Checker | 1)[]) => Transform;
128
160
  type FailingBranches = typeof failingBranches;
129
161
  export declare const units: (values: readonly unknown[], code?: ArkErrorCode, description?: any) => Checker;
130
- export declare const domains: (cases: Record<string, Checker | 1>, description: string, fails?: FailingBranches) => Checker;
162
+ export declare const describeDomains: (cases: Record<string, {
163
+ description?: string | undefined;
164
+ } | 1>) => string;
165
+ export declare const domains: (cases: Record<string, Checker | 1>, description?: any, fails?: FailingBranches) => Checker;
131
166
  export declare const nullableDescriptionOf: (domain: string) => string;
132
167
  export declare const nullable: (checker: Checker | 1, domain?: any) => Checker;
133
168
  export declare const nullableObject: (checker: Checker) => Checker;
@@ -8,10 +8,10 @@ import {
8
8
  tsGenericDefinitions,
9
9
  tsKeywordDefinitions,
10
10
  unknown
11
- } from "./chunk-KI2MOYNT.js";
11
+ } from "./chunk-GPNK5353.js";
12
12
  import {
13
13
  keywordMeta
14
- } from "./chunk-H4VRNKMB.js";
14
+ } from "./chunk-E3DZXEZK.js";
15
15
  import {
16
16
  capitalizeString,
17
17
  epochDescriptions,
@@ -35,7 +35,7 @@ import {
35
35
  parsesFinite,
36
36
  trimString,
37
37
  uppercaseString
38
- } from "./chunk-JHDPSG5Y.js";
38
+ } from "./chunk-UK5UGFOX.js";
39
39
 
40
40
  // out/keywords/builtins.js
41
41
  import { genericNode, intrinsic } from "@ark/schema";