arktype 2.2.10 → 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 (164) hide show
  1. package/README.md +181 -15
  2. package/out/attributes.d.ts +50 -14
  3. package/out/checkers.d.ts +172 -0
  4. package/out/chunk-2U2AWCI3.js +626 -0
  5. package/out/chunk-4CUA5XIP.js +7 -0
  6. package/out/chunk-5U2ZMDNI.js +1566 -0
  7. package/out/chunk-DLWCO4RV.js +18 -0
  8. package/out/chunk-E3DZXEZK.js +72 -0
  9. package/out/chunk-EVCML554.js +47 -0
  10. package/out/chunk-G45P2CLE.js +63 -0
  11. package/out/chunk-GPNK5353.js +2529 -0
  12. package/out/chunk-MLKGABMK.js +9 -0
  13. package/out/chunk-NRIGTMPM.js +7 -0
  14. package/out/chunk-PRTMQCHS.js +3 -0
  15. package/out/chunk-R5G57XC7.js +7 -0
  16. package/out/chunk-RAZ3TXOE.js +313 -0
  17. package/out/chunk-SWOOCAHN.js +4 -0
  18. package/out/chunk-TAHPKDNM.js +3 -0
  19. package/out/chunk-UETB6YE2.js +14 -0
  20. package/out/chunk-UK5UGFOX.js +254 -0
  21. package/out/chunk-VV6YDN2D.js +6118 -0
  22. package/out/codecs.d.ts +21 -0
  23. package/out/codecs.js +7 -0
  24. package/out/config.d.ts +8 -3
  25. package/out/config.js +2 -3
  26. package/out/declare.d.ts +17 -3
  27. package/out/deferred.d.ts +10 -0
  28. package/out/errors.d.ts +1 -0
  29. package/out/errors.js +2 -0
  30. package/out/fn.d.ts +1 -1
  31. package/out/generic.d.ts +3 -3
  32. package/out/i18n.d.ts +2 -0
  33. package/out/i18n.js +2 -0
  34. package/out/index.d.ts +5 -4
  35. package/out/index.js +8 -9
  36. package/out/internal.d.ts +70 -0
  37. package/out/internal.js +17 -0
  38. package/out/island.d.ts +2 -0
  39. package/out/island.js +6 -0
  40. package/out/islandDependency.d.ts +2 -0
  41. package/out/islandDependency.js +18 -0
  42. package/out/keywords/Array.d.ts +2 -2
  43. package/out/keywords/builtins.d.ts +5 -3
  44. package/out/keywords/constructors.d.ts +9 -1
  45. package/out/keywords/functions.d.ts +254 -0
  46. package/out/keywords/keywords.d.ts +8 -9
  47. package/out/keywords/number.d.ts +2 -8
  48. package/out/keywords/phrases.d.ts +245 -0
  49. package/out/keywords/string.d.ts +14 -113
  50. package/out/keywords/ts.d.ts +35 -33
  51. package/out/keywords/ts.js +4 -82
  52. package/out/locale.d.ts +19 -0
  53. package/out/locales/de.d.ts +2 -0
  54. package/out/locales/de.js +2 -0
  55. package/out/locales/en.d.ts +4 -0
  56. package/out/locales/en.js +5 -0
  57. package/out/match.d.ts +6 -6
  58. package/out/module.d.ts +1 -1
  59. package/out/mutatesInput.js +3 -0
  60. package/out/parser/ast/generic.d.ts +6 -3
  61. package/out/parser/ast/infer.d.ts +1 -1
  62. package/out/parser/ast/validate.d.ts +11 -6
  63. package/out/parser/definition.d.ts +68 -7
  64. package/out/parser/objectLiteral.d.ts +40 -12
  65. package/out/parser/property.d.ts +3 -5
  66. package/out/parser/reduce/dynamic.d.ts +16 -7
  67. package/out/parser/reduce/shared.d.ts +4 -0
  68. package/out/parser/reduce/static.d.ts +13 -13
  69. package/out/parser/shift/operand/date.d.ts +0 -5
  70. package/out/parser/shift/operand/operand.d.ts +2 -1
  71. package/out/parser/shift/operand/unenclosed.d.ts +9 -6
  72. package/out/parser/shift/operand/unresolvable.d.ts +68 -0
  73. package/out/parser/shift/operator/bounds.d.ts +9 -4
  74. package/out/parser/shift/operator/brand.d.ts +2 -2
  75. package/out/parser/shift/operator/default.d.ts +2 -2
  76. package/out/parser/shift/operator/divisor.d.ts +2 -2
  77. package/out/parser/shift/operator/operator.d.ts +1 -1
  78. package/out/parser/shift/tokens.d.ts +9 -2
  79. package/out/parser/string.d.ts +61 -7
  80. package/out/parser/tupleExpressions.d.ts +7 -3
  81. package/out/precompiled.d.ts +1 -0
  82. package/out/precompiled.js +2 -0
  83. package/out/runtime.d.ts +181 -0
  84. package/out/runtime.js +4 -0
  85. package/out/runtimeDevelopment.d.ts +1 -0
  86. package/out/runtimeDevelopment.js +133 -0
  87. package/out/runtimeExports.d.ts +1 -0
  88. package/out/scope.d.ts +40 -11
  89. package/out/standalone/artifact.d.ts +265 -0
  90. package/out/standalone/baking.d.ts +201 -0
  91. package/out/standalone/combinators.d.ts +13 -0
  92. package/out/standalone/compilers.d.ts +23 -0
  93. package/out/standalone/creators.d.ts +41 -0
  94. package/out/standalone/fallbacks.d.ts +48 -0
  95. package/out/standalone/keys.d.ts +31 -0
  96. package/out/standalone/modules.d.ts +26 -0
  97. package/out/standalone/sites.d.ts +52 -0
  98. package/out/standalone/units.d.ts +31 -0
  99. package/out/standalone/values.d.ts +60 -0
  100. package/out/standalone.d.ts +16 -0
  101. package/out/type.d.ts +45 -6
  102. package/out/variants/array.d.ts +1 -1
  103. package/out/variants/base.d.ts +47 -27
  104. package/out/variants/date.d.ts +1 -1
  105. package/out/variants/instantiate.d.ts +4 -2
  106. package/out/variants/number.d.ts +1 -1
  107. package/out/variants/object.d.ts +17 -12
  108. package/out/variants/string.d.ts +1 -1
  109. package/package.json +169 -8
  110. package/skills/arktype/SKILL.md +278 -43
  111. package/skills/arktype/references/migrating.md +1296 -0
  112. package/skills/arktype/references/precompile.md +191 -0
  113. package/out/attributes.js +0 -3
  114. package/out/fn.js +0 -58
  115. package/out/generic.js +0 -37
  116. package/out/keywords/Array.js +0 -21
  117. package/out/keywords/FormData.js +0 -40
  118. package/out/keywords/TypedArray.js +0 -16
  119. package/out/keywords/builtins.js +0 -11
  120. package/out/keywords/constructors.js +0 -16
  121. package/out/keywords/keywords.js +0 -48
  122. package/out/keywords/number.js +0 -49
  123. package/out/keywords/string.js +0 -467
  124. package/out/match.js +0 -95
  125. package/out/module.js +0 -3
  126. package/out/nary.js +0 -1
  127. package/out/parser/ast/bounds.js +0 -1
  128. package/out/parser/ast/default.js +0 -1
  129. package/out/parser/ast/divisor.js +0 -1
  130. package/out/parser/ast/generic.js +0 -1
  131. package/out/parser/ast/infer.js +0 -1
  132. package/out/parser/ast/keyof.js +0 -1
  133. package/out/parser/ast/utils.js +0 -1
  134. package/out/parser/ast/validate.js +0 -3
  135. package/out/parser/definition.js +0 -78
  136. package/out/parser/objectLiteral.js +0 -113
  137. package/out/parser/property.js +0 -16
  138. package/out/parser/reduce/dynamic.js +0 -153
  139. package/out/parser/reduce/shared.js +0 -25
  140. package/out/parser/reduce/static.js +0 -1
  141. package/out/parser/shift/operand/date.js +0 -23
  142. package/out/parser/shift/operand/enclosed.js +0 -82
  143. package/out/parser/shift/operand/genericArgs.js +0 -16
  144. package/out/parser/shift/operand/operand.js +0 -16
  145. package/out/parser/shift/operand/unenclosed.js +0 -59
  146. package/out/parser/shift/operator/bounds.js +0 -91
  147. package/out/parser/shift/operator/brand.js +0 -6
  148. package/out/parser/shift/operator/default.js +0 -29
  149. package/out/parser/shift/operator/divisor.js +0 -13
  150. package/out/parser/shift/operator/operator.js +0 -28
  151. package/out/parser/shift/tokens.js +0 -37
  152. package/out/parser/string.js +0 -42
  153. package/out/parser/tupleExpressions.js +0 -75
  154. package/out/parser/tupleLiteral.js +0 -128
  155. package/out/scope.js +0 -106
  156. package/out/type.js +0 -55
  157. package/out/variants/array.js +0 -1
  158. package/out/variants/base.js +0 -1
  159. package/out/variants/date.js +0 -1
  160. package/out/variants/instantiate.js +0 -1
  161. package/out/variants/number.js +0 -1
  162. package/out/variants/object.js +0 -1
  163. package/out/variants/string.js +0 -1
  164. /package/out/{declare.js → mutatesInput.d.ts} +0 -0
package/README.md CHANGED
@@ -1,23 +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 is a runtime validation library that parses optimized validators from familiar, type-safe 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
- It can be used to check external data like JSON payloads or forms at the boundaries of your code (similar to Zod).
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.
7
9
 
8
- <video
9
- autoPlay
10
- loop
11
- controls
12
- playsInline
13
- muted
14
- disablePictureInPicture
15
- src="https://github.com/user-attachments/assets/69fdded6-50a9-402d-a28c-afa58db63c90"
16
- />
10
+ **Using Zod?** [Switch with one prompt](#switching-from-zod). Your schemas keep their names, their messages and the libraries that read them.
11
+
12
+ ```ts
13
+ import { type } from "arktype"
14
+
15
+ const User = type({
16
+ name: "string > 0",
17
+ email: "string.email",
18
+ "age?": "number.integer >= 0"
19
+ })
20
+
21
+ // { name: string; email: string; age?: number }
22
+ type User = typeof User.infer
23
+
24
+ const out = User({ name: "Ada", email: "ada@", age: -1 })
25
+
26
+ if (out instanceof type.errors) {
27
+ // email must be an email address
28
+ // age must be non-negative
29
+ console.error(out.summary)
30
+ } else {
31
+ // out is a User
32
+ console.log(out.name)
33
+ }
34
+ ```
35
+
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.
17
183
 
18
184
  ## Docs
19
185
 
20
- See our [docs site](https://arktype.io)
186
+ [arktype.io](https://arktype.io/docs/intro/setup)
21
187
 
22
188
  ## Contributions
23
189
 
@@ -27,16 +193,16 @@ Depending on your level of familiarity with type systems and TS generics, some p
27
193
 
28
194
  If you're planning on submitting a non-trivial fix or a new feature, please [create an issue first](https://github.com/arktypeio/arktype/issues/new) so everyone's on the same page. The last thing we want is for you to spend time on a submission we're unable to merge.
29
195
 
30
- When you're ready, check out our [guide](./.github/CONTRIBUTING.md) to get started!
196
+ When you're ready, check out our [guide](https://github.com/arktypeio/arktype/blob/main/.github/CONTRIBUTING.md) to get started!
31
197
 
32
198
  ## License
33
199
 
34
200
  This project is licensed under the terms of the
35
- [MIT license](./LICENSE).
201
+ [MIT license](https://github.com/arktypeio/arktype/blob/main/LICENSE).
36
202
 
37
203
  ## Code of Conduct
38
204
 
39
- We will not tolerate any form of disrespect toward members of our community. Please refer to our [Code of Conduct](./.github/CODE_OF_CONDUCT.md) and reach out to david@arktype.io immediately if you've seen or experienced an interaction that may violate these standards.
205
+ We will not tolerate any form of disrespect toward members of our community. Please refer to our [Code of Conduct](https://github.com/arktypeio/arktype/blob/main/.github/CODE_OF_CONDUCT.md) and reach out to david@arktype.io immediately if you've seen or experienced an interaction that may violate these standards.
40
206
 
41
207
  ## Sponsorship
42
208
 
@@ -1,25 +1,42 @@
1
- import type { ArkError, ArkErrors, Morph } from "@ark/schema";
2
- import { type anyOrNever, type array, type Brand, type equals, type Hkt, type intersectArrays, type isSafelyMappable, type merge, type optionalKeyOf, type Primitive, type show, type unionKeyOf, type unionToTuple } from "@ark/util";
1
+ import type { ArkError, ArkErrors, arkKind, Morph } from "@ark/schema";
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";
6
6
  export type { arkPrototypes as object } from "./keywords/constructors.ts";
7
- export type Comparator = "<" | "<=" | ">" | ">=" | "==";
7
+ export type Comparator = "<" | "<=" | ">" | ">=" | "==" | "!=";
8
8
  export type RegexLiteral<source extends string = string> = `/${source}/`;
9
9
  export type DateLiteral<source extends string = string> = `d"${source}"` | `d'${source}'`;
10
10
  export type LimitLiteral = number | DateLiteral;
11
11
  export type normalizeLimit<limit> = limit extends DateLiteral<infer source> ? source : limit extends number | string ? limit : never;
12
- export type distill<t, side extends distill.Side> = finalizeDistillation<t, _distill<t, side>>;
12
+ export type distill<t, side extends distill.Side> = [
13
+ distilledSidesOf<t>
14
+ ] extends [undistilledOn<side>] ? t : _distill<t, side>;
13
15
  export declare namespace distill {
14
- type Side = "in" | "out" | "introspectableOut";
16
+ type Side = "in" | "out" | "knownOut";
15
17
  type In<t> = distill<t, "in">;
16
18
  type Out<t> = distill<t, "out">;
17
19
  namespace introspectable {
18
- type Out<t> = distill<t, "introspectableOut">;
20
+ type Out<t> = distill<t, "knownOut">;
19
21
  }
20
22
  }
21
- type finalizeDistillation<t, distilled> = equals<t, distilled> extends true ? t : distilled;
22
- type _distill<t, side extends distill.Side> = t extends undefined ? t : [t] extends [anyOrNever] ? t : unknown extends t ? unknown : t extends Brand<infer base> ? side extends "in" ? base : t : t extends TerminallyInferredObject | Primitive ? t : t extends Function ? t extends (...args: never) => anyOrNever ? t : t extends InferredMorph<infer i, infer o> ? distillIo<i, o, side> : t : t extends Default<infer constraint> ? _distill<constraint, side> : t extends array ? distillArray<t, side> : isSafelyMappable<t> extends true ? distillMappable<t, side> : t;
23
+ type distilledSidesOf<t> = t extends undefined ? never : [t] extends [anyOrNever] ? never : unknown extends t ? never : t extends Brand ? "in" : t extends TerminallyInferredObject | Primitive ? never : t extends Function ? t extends (...args: never) => anyOrNever ? never : t extends InferredMorph ? distill.Side : never : t extends Default ? distill.Side : t extends array ? keyof t extends ArrayKey ? {
24
+ [i: number]: distilledSidesOf<t[number]>;
25
+ } : {
26
+ [k in number | Exclude<keyof t, ArrayKey>]: distilledSidesOf<t[k]>;
27
+ } : isSafelyMappable<t> extends true ? string extends keyof t ? indexedSidesOf<t> : symbol extends keyof t ? indexedSidesOf<t> : {
28
+ [k in keyof t]-?: distilledSidesOf<t[k]>;
29
+ } : never;
30
+ type indexedSidesOf<t> = {
31
+ [k in keyof t as string extends k ? "" : symbol extends k ? "" : k]-?: distilledSidesOf<t[k]>;
32
+ };
33
+ type ArrayKey = keyof unknown[] | `${number}`;
34
+ type undistilledOn<side extends distill.Side> = Exclude<distill.Side, side> | {
35
+ readonly [k: PropertyKey]: undistilledOn<side>;
36
+ };
37
+ type _distill<t, side extends distill.Side> = t extends undefined ? t : [t] extends [anyOrNever] ? t : unknown extends t ? unknown : t extends {
38
+ readonly [brand]: [infer base, unknown];
39
+ } ? side extends "in" ? base : t : t extends TerminallyInferredObject | Primitive ? t : t extends Function ? t extends (...args: never) => anyOrNever ? t : t extends InferredMorph<infer i, infer o> ? distillIo<i, o, side> : t : t extends Default<infer constraint> ? _distill<constraint, side> : t extends array ? distillArray<t, side> : isSafelyMappable<t> extends true ? distillMappable<t, side> : t;
23
40
  type distillMappable<o, side extends distill.Side> = side extends "in" ? show<{
24
41
  [k in keyof o as k extends inferredDefaultKeyOf<o> ? never : k]: _distill<o[k], side>;
25
42
  } & {
@@ -29,9 +46,13 @@ type distillMappable<o, side extends distill.Side> = side extends "in" ? show<{
29
46
  };
30
47
  type distillIo<i, o extends Out, side extends distill.Side> = side extends "out" ? _distill<o["t"], side> : side extends "in" ? _distill<i, side> : o extends To<infer validatedOut> ? _distill<validatedOut, side> : unknown;
31
48
  type unwrapInput<t> = t extends InferredMorph<infer i> ? t extends anyOrNever ? t : i : t;
32
- type inferredDefaultKeyOf<o> = keyof o extends infer k ? k extends keyof o ? unwrapInput<o[k]> extends Default<infer t> ? [
49
+ type inferredDefaultKeyOf<o> = string extends keyof o ? keyof indexedDefaults<o> : symbol extends keyof o ? keyof indexedDefaults<o> : keyof o extends infer k ? k extends keyof o ? isInferredDefault<o[k]> extends true ? k : never : never : never;
50
+ type indexedDefaults<o> = {
51
+ [k in keyof o as isInferredDefault<o[k]> extends true ? k : never]: 0;
52
+ };
53
+ type isInferredDefault<v> = unwrapInput<v> extends Default<infer t> ? [
33
54
  t
34
- ] extends [anyOrNever] ? never : k : never : never : never;
55
+ ] extends [anyOrNever] ? false : true : false;
35
56
  type distillArray<t extends array, side extends distill.Side> = t[number][] extends t ? alignReadonly<_distill<t[number], side>[], t> : distillNonArraykeys<t, alignReadonly<distillArrayFromPrefix<[...t], side, []>, t>, side>;
36
57
  type alignReadonly<result extends unknown[], original extends array> = original extends unknown[] ? result : Readonly<result>;
37
58
  type distillNonArraykeys<originalArray extends array, distilledArray, side extends distill.Side> = keyof originalArray extends keyof distilledArray ? distilledArray : distilledArray & _distill<{
@@ -44,9 +65,11 @@ type distillArrayFromPrefix<t extends array, side extends distill.Side, prefix e
44
65
  type distillArrayFromPostfix<t extends array, side extends distill.Side, postfix extends array> = t extends readonly [...infer init, infer last] ? distillArrayFromPostfix<init, side, [_distill<last, side>, ...postfix]> : [...{
45
66
  [i in keyof t]: _distill<t[i], side>;
46
67
  }, ...postfix];
47
- type BuiltinTerminalObjectKind = Exclude<arkPrototypes.NonDegenerateName, "Array" | "Function">;
48
68
  /** Objects we don't want to expand during inference like Date or Promise */
49
- type TerminallyInferredObject = arkPrototypes.instanceOf<BuiltinTerminalObjectKind> | ArkEnv.prototypes;
69
+ export type TerminallyInferredObject = BuiltinTerminalObject | ArkEnv.prototypes;
70
+ type BuiltinTerminalObject = {
71
+ [k in keyof arkPrototypes.instances]: k extends "Array" | "Function" ? never : {} extends arkPrototypes.instances[k] ? never : arkPrototypes.instances[k];
72
+ }[keyof arkPrototypes.instances];
50
73
  export type inferPredicate<t, predicate> = predicate extends (data: any, ...args: any[]) => data is infer narrowed ? narrowed : t;
51
74
  export type inferNaryPipe<morphs extends readonly Morph[]> = _inferNaryPipe<morphs, unknown>;
52
75
  type _inferNaryPipe<remaining extends readonly unknown[], result> = remaining extends (readonly [infer head extends Morph, ...infer tail extends Morph[]]) ? _inferNaryPipe<tail, inferMorph<result, head>> : result;
@@ -61,7 +84,12 @@ type _inferUnorderedMerge<types extends readonly unknown[], optionalKey extends
61
84
  /** Coalesce keys that exist and are optional on one or more branches of a union */
62
85
  type optionalAtLeastOnceUnionKeyOf<t> = t extends unknown ? optionalKeyOf<t> : never;
63
86
  type _inferNaryMerge<remaining extends readonly unknown[], result> = remaining extends (readonly [infer head, ...infer tail extends readonly unknown[]]) ? _inferNaryMerge<tail, merge<result, head>> : result;
64
- export type inferMorphOut<morph extends Morph> = Exclude<ReturnType<morph>, ArkError | ArkErrors>;
87
+ export type inferMorphOut<morph extends Morph> = excludeErrors<ReturnType<morph>>;
88
+ type excludeErrors<out> = out extends {
89
+ path: unknown;
90
+ } | {
91
+ [arkKind]: "errors";
92
+ } ? out extends ArkError | ArkErrors ? never : out : out;
65
93
  declare const isMorphOutKey: " isMorphOut";
66
94
  export interface Out<o = any> {
67
95
  [isMorphOutKey]: true;
@@ -77,6 +105,12 @@ export type Default<t = unknown, v = unknown> = {
77
105
  [defaultsToKey]: [t, v];
78
106
  };
79
107
  export type withDefault<t, v, undistributed = t> = t extends InferredMorph ? addDefaultToMorph<t, v> : Default<Exclude<undistributed, InferredMorph>, v>;
108
+ export type required<t> = {
109
+ [k in keyof t]-?: withoutDefault<t[k]>;
110
+ };
111
+ type withoutDefault<t> = t extends Default<infer constraint> ? constraint : t extends InferredMorph<infer i, infer o> ? [
112
+ i
113
+ ] extends [anyOrNever] ? t : [i] extends [Default<infer constraint>] ? (In: constraint) => o : t : t;
80
114
  type addDefaultToMorph<t extends InferredMorph, v> = [
81
115
  normalizeMorphDistribution<t>
82
116
  ] extends [InferredMorph<infer i, infer o>] ? (In: Default<i, v>) => o : never;
@@ -88,7 +122,9 @@ type normalizeMorphDistribution<t, undistributedIn = t extends InferredMorph<inf
88
122
  export type defaultFor<t = unknown> = (Primitive extends t ? Primitive : t extends Primitive ? t : never) | (() => t);
89
123
  export type termOrType<t> = t | Type<t, any>;
90
124
  export type inferIntersection<l, r> = normalizeMorphDistribution<_inferIntersection<l, r, false>>;
91
- 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;
92
128
  export type inferPipe<l, r> = normalizeMorphDistribution<_inferIntersection<l, r, true>>;
93
129
  type _inferIntersection<l, r, piped extends boolean> = [
94
130
  l & r
@@ -0,0 +1,172 @@
1
+ import { type ArkErrorCode, type FailureContext, type FailureSite, type SiteContext } from "@ark/schema/runtime";
2
+ import { failsBranches } from "./runtime.ts";
3
+ /** a node a size build composes, reporting to a ctx it's passed */
4
+ export interface Checker {
5
+ (data: unknown, ctx?: FailureContext | number): boolean;
6
+ /** what a key it's the value of expects where the key is missing */
7
+ description?: string | undefined;
8
+ /** where it transforms, e.g. by writing a default, data it allows */
9
+ t?: Transform | undefined;
10
+ /** where it checks a key that may be absent */
11
+ o?: 1;
12
+ /** the default of a key that may be absent, if it has one */
13
+ d?: [value?: unknown];
14
+ /** where its default replaces undefined too */
15
+ u?: 1 | undefined;
16
+ /** where a morph writes its default, as a function's may fail */
17
+ w?: DefaultWrite;
18
+ /** where it holds a callback reading its ctx, its Allows at the key ctx is at, or at k under it */
19
+ a?: ((data: unknown, ctx: any, k?: PropertyKey) => boolean) | undefined;
20
+ /** where it holds an alias, how a key applies it */
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"];
26
+ }
27
+ type DefaultWrite = (data: any, ctx?: any) => unknown;
28
+ interface Transform {
29
+ (data: any, ctx?: any): unknown;
30
+ /** where it holds a morph, how a key applies what it's the transform of */
31
+ e?: ApplyAt | undefined;
32
+ /** where it reports to a ctx, rather than returning what fails */
33
+ c?: 1 | undefined;
34
+ }
35
+ type ApplyAt = (ctx: any, checker: Checker, value: unknown, k: PropertyKey) => boolean;
36
+ /** a site, and the checker of the test data passes to avoid it */
37
+ export interface Check extends SiteContext, Checker {
38
+ test: (data: any) => boolean;
39
+ }
40
+ export declare const check: (code: ArkErrorCode, description: string | undefined, test: (data: any) => boolean) => Check;
41
+ export declare const narrowedBy: (read: () => (data: any) => boolean, description?: string) => Checker;
42
+ export declare const narrowedWithCtx: (read: () => (data: any, ctx: any) => boolean, description?: string) => Checker;
43
+ export declare const readingCtx: (checker: Checker) => Checker;
44
+ /** Allows of a node reading its ctx, or passed a depth by an alias it holds */
45
+ export declare const allowsReadingCtx: (checker: Checker) => (data: unknown, ctx: any) => boolean;
46
+ /** what a checker reads of a node compiled since it's in a cycle, or holding one */
47
+ interface AliasedNode {
48
+ id: string;
49
+ tfs?: true;
50
+ tfsById?: true;
51
+ tAllows: (data: unknown, ctx: any) => boolean;
52
+ tApply: Checker;
53
+ tTransform?: Transform;
54
+ }
55
+ export declare const compiled: (node: AliasedNode, transforms?: 1) => Checker;
56
+ export declare const aliasedValue: (checker: Checker, node: AliasedNode, flags?: 1 | 2 | 3) => Checker;
57
+ export declare const aliasedMember: (checker: Checker, id: string) => Checker;
58
+ export declare const holdingAlias: (checker: Checker) => Checker;
59
+ export declare const narrowedInCtx: (read: () => (data: any, ctx: any) => boolean, description?: string) => Checker;
60
+ export declare const narrowedAfter: (structure: Checker, narrowed: Checker) => Checker;
61
+ export declare const transformingNarrowedAfter: (structure: Checker, narrowed: Checker) => Checker;
62
+ export declare const narrowedByEach: (checkers: readonly Checker[]) => Checker;
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;
87
+ /** the checker a validator is created from, as a call a bundler may drop */
88
+ export declare const allowsOf: (validator: {
89
+ allows: Checker;
90
+ }) => Checker;
91
+ export declare const stringCheck: Check;
92
+ export declare const numberCheck: Check;
93
+ export declare const objectCheck: Check;
94
+ export declare const arrayCheck: Check;
95
+ export declare const min: (rule: number, description?: any) => Check;
96
+ export declare const moreThan: (rule: number, description?: any) => Check;
97
+ export declare const max: (rule: number, description?: any) => Check;
98
+ export declare const lessThan: (rule: number, description?: any) => Check;
99
+ export declare const minLength: (rule: number, description?: any) => Check;
100
+ export declare const maxLength: (rule: number, description?: any) => Check;
101
+ export declare const exactLength: (rule: number, description?: any) => Check;
102
+ export declare const divisor: (rule: number, description?: any) => Check;
103
+ export declare const integerCheck: Check;
104
+ export declare const pattern: (instance: RegExp, description?: any) => Check;
105
+ export declare const stringRange: (max?: number, min?: number) => Checker;
106
+ export declare const integerRange: (least?: number, most?: number) => Checker;
107
+ export declare const arrayOf: (element: Checker, max?: number, min?: number) => Checker;
108
+ export declare const describeValues: (values: readonly unknown[]) => string;
109
+ export declare const unit: (value: unknown, description?: any) => Checker;
110
+ export declare const booleanChecker: Checker;
111
+ export declare const intersection: (basis: Check | 0, refinements: readonly Check[], within?: Checker, brief?: any) => Checker;
112
+ export declare const optional: (checker: Checker, value_0?: unknown) => Checker;
113
+ export declare const writtenBy: (checker: Checker, write: DefaultWrite, replacesUndefined?: 1) => Checker;
114
+ export declare const replacingUndefined: (checker: Checker, value: unknown) => Checker;
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;
118
+ type Props = Record<string, Checker>;
119
+ export declare const props: (declared: Props) => Checker;
120
+ export declare const closedProps: (declared: Props, description?: any) => Checker;
121
+ export declare const recordOf: (value: Checker) => Checker;
122
+ export declare const indexed: (signatures: readonly [key: Checker, value: Checker][], declared?: Props, removed?: string) => Checker;
123
+ interface WrittenValues {
124
+ transformed(t: Transform, value: unknown, k: PropertyKey): unknown;
125
+ failed(): unknown;
126
+ defaulted(write: DefaultWrite, out: any): void;
127
+ }
128
+ export declare const transformingProps: (declared: Props, checker?: Checker) => Checker;
129
+ export declare const transformingClosedProps: (declared: Props, description?: string) => Checker;
130
+ export declare const failingProps: (declared: Props, withCtx?: 1) => Checker;
131
+ export declare const failingClosedProps: (declared: Props, description?: string, withCtx?: 1) => Checker;
132
+ export declare const deletingProps: (declared: Props) => Checker;
133
+ export declare const failingDeletingProps: (declared: Props, withCtx?: 1) => Checker;
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;
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;
137
+ export declare const elementsOf: (element: Checker) => Checker;
138
+ export declare const transformingElementsOf: (element: Checker) => Checker;
139
+ export declare const failingElementsOf: (element: Checker, withCtx?: 1) => Checker;
140
+ export declare const failingArrayOf: (element: Checker, max?: number, min?: number, withCtx?: 1) => Checker;
141
+ /** a step of a morph: what it returns of what the step before returned */
142
+ interface MorphStep {
143
+ (data: any, ctx?: any): unknown;
144
+ /** where it pipes to a node, so reports to a ctx */
145
+ p?: 1;
146
+ }
147
+ export declare const piped: (checker: Checker, reports?: 1) => MorphStep;
148
+ export declare const morphing: (head: Checker | 0, steps: readonly MorphStep[]) => Checker;
149
+ export declare const transformOf: (checker: Checker) => Transform;
150
+ export declare const checkerNode: (checker: Checker, reportsPred?: true) => Readonly<{
151
+ tfs: true;
152
+ reportsPred: true | undefined;
153
+ tApply: Checker;
154
+ tTransform: Transform | undefined;
155
+ allows: Checker;
156
+ }>;
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;
159
+ export declare const failingBranches: (branchOf: (data: any) => Checker | 1, branches: readonly (Checker | 1)[]) => Transform;
160
+ type FailingBranches = typeof failingBranches;
161
+ export declare const units: (values: readonly unknown[], code?: ArkErrorCode, description?: any) => 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;
166
+ export declare const nullableDescriptionOf: (domain: string) => string;
167
+ export declare const nullable: (checker: Checker | 1, domain?: any) => Checker;
168
+ export declare const nullableObject: (checker: Checker) => Checker;
169
+ export declare const discriminate: (key: string, cases: Record<string, Checker | 1>, description?: any, missed?: FailureSite, isOwn?: 1, areBooleans?: 1, fails?: FailingBranches) => Checker;
170
+ export declare const discriminateOr: (otherwise: Checker, key: string, cases: Record<string, Checker | 1>, description: string, missed?: FailureSite, fails?: FailingBranches) => Checker;
171
+ export declare const union: (branches: readonly Checker[], description: string, issueOf: Parameters<typeof failsBranches>[3], fails?: FailingBranches) => Checker;
172
+ export {};