arktype 3.0.0-beta.1 → 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 +48 -144
- package/out/_internal/regex/charset.d.ts +24 -0
- package/out/_internal/regex/escape.d.ts +29 -0
- package/out/_internal/regex/execArray.d.ts +24 -0
- package/out/_internal/regex/group.d.ts +34 -0
- package/out/_internal/regex/index.d.ts +2 -0
- package/out/_internal/regex/internal.d.ts +9 -0
- package/out/_internal/regex/parse.d.ts +10 -0
- package/out/_internal/regex/quantify.d.ts +59 -0
- package/out/_internal/regex/regex.d.ts +38 -0
- package/out/_internal/regex/state.d.ts +383 -0
- package/out/_internal/schema/codecs.d.ts +2 -0
- package/out/_internal/schema/config.d.ts +103 -0
- package/out/_internal/schema/constraint.d.ts +78 -0
- package/out/_internal/schema/errors.d.ts +1 -0
- package/out/_internal/schema/generic.d.ts +85 -0
- package/out/_internal/schema/i18n.d.ts +3 -0
- package/out/_internal/schema/index.d.ts +54 -0
- package/out/_internal/schema/internal.d.ts +15 -0
- package/out/_internal/schema/intrinsic.d.ts +28 -0
- package/out/_internal/schema/kinds.d.ts +76 -0
- package/out/_internal/schema/module.d.ts +23 -0
- package/out/_internal/schema/mutatesInput.d.ts +2 -0
- package/out/_internal/schema/node.d.ts +349 -0
- package/out/_internal/schema/parse.d.ts +77 -0
- package/out/_internal/schema/parseSchema.d.ts +116 -0
- package/out/_internal/schema/predicate.d.ts +46 -0
- package/out/_internal/schema/refinements/after.d.ts +32 -0
- package/out/_internal/schema/refinements/before.d.ts +32 -0
- package/out/_internal/schema/refinements/divisor.d.ts +41 -0
- package/out/_internal/schema/refinements/exactLength.d.ts +43 -0
- package/out/_internal/schema/refinements/kinds.d.ts +40 -0
- package/out/_internal/schema/refinements/max.d.ts +33 -0
- package/out/_internal/schema/refinements/maxLength.d.ts +32 -0
- package/out/_internal/schema/refinements/min.d.ts +33 -0
- package/out/_internal/schema/refinements/minLength.d.ts +32 -0
- package/out/_internal/schema/refinements/notEqual.d.ts +38 -0
- package/out/_internal/schema/refinements/notEqualLength.d.ts +38 -0
- package/out/_internal/schema/refinements/pattern.d.ts +43 -0
- package/out/_internal/schema/refinements/range.d.ts +101 -0
- package/out/_internal/schema/roots/alias.d.ts +49 -0
- package/out/_internal/schema/roots/basis.d.ts +12 -0
- package/out/_internal/schema/roots/domain.d.ts +45 -0
- package/out/_internal/schema/roots/intersection.d.ts +72 -0
- package/out/_internal/schema/roots/morph.d.ts +59 -0
- package/out/_internal/schema/roots/proto.d.ts +42 -0
- package/out/_internal/schema/roots/root.d.ts +163 -0
- package/out/_internal/schema/roots/union.d.ts +146 -0
- package/out/_internal/schema/roots/unit.d.ts +36 -0
- package/out/_internal/schema/runtime.d.ts +29 -0
- package/out/_internal/schema/runtimeLean.d.ts +2 -0
- package/out/_internal/schema/runtimeNone.d.ts +2 -0
- package/out/_internal/schema/runtimeNoneLean.d.ts +2 -0
- package/out/_internal/schema/scope.d.ts +203 -0
- package/out/_internal/schema/shared/codecs.d.ts +5 -0
- package/out/_internal/schema/shared/compile.d.ts +144 -0
- package/out/_internal/schema/shared/configured.d.ts +14 -0
- package/out/_internal/schema/shared/declare.d.ts +100 -0
- package/out/_internal/schema/shared/disjoint.d.ts +45 -0
- package/out/_internal/schema/shared/enWriters.d.ts +30 -0
- package/out/_internal/schema/shared/errorModes.d.ts +25 -0
- package/out/_internal/schema/shared/errorWriters.d.ts +34 -0
- package/out/_internal/schema/shared/errors.d.ts +264 -0
- package/out/_internal/schema/shared/implement.d.ts +154 -0
- package/out/_internal/schema/shared/inPlace.d.ts +8 -0
- package/out/_internal/schema/shared/issueFeatures.d.ts +38 -0
- package/out/_internal/schema/shared/issues.d.ts +261 -0
- package/out/_internal/schema/shared/issuesLean.d.ts +80 -0
- package/out/_internal/schema/shared/issuesNone.d.ts +22 -0
- package/out/_internal/schema/shared/issuesNoneLean.d.ts +20 -0
- package/out/_internal/schema/shared/issuesUndetailed.d.ts +25 -0
- package/out/_internal/schema/shared/jsonSchema.d.ts +153 -0
- package/out/_internal/schema/shared/listed.d.ts +15 -0
- package/out/_internal/schema/shared/locale.d.ts +149 -0
- package/out/_internal/schema/shared/registry.d.ts +16 -0
- package/out/_internal/schema/shared/render.d.ts +3 -0
- package/out/_internal/schema/shared/rootApply.d.ts +19 -0
- package/out/_internal/schema/shared/sets.d.ts +26 -0
- package/out/_internal/schema/shared/standardFailure.d.ts +22 -0
- package/out/_internal/schema/shared/standardSchema.d.ts +124 -0
- package/out/_internal/schema/shared/toJsonSchema.d.ts +229 -0
- package/out/_internal/schema/shared/transform.d.ts +35 -0
- package/out/_internal/schema/shared/traversal.d.ts +288 -0
- package/out/_internal/schema/shared/utils.d.ts +59 -0
- package/out/_internal/schema/structure/index.d.ts +43 -0
- package/out/_internal/schema/structure/optional.d.ts +61 -0
- package/out/_internal/schema/structure/prop.d.ts +45 -0
- package/out/_internal/schema/structure/required.d.ts +49 -0
- package/out/_internal/schema/structure/sequence.d.ts +127 -0
- package/out/_internal/schema/structure/shared.d.ts +3 -0
- package/out/_internal/schema/structure/structure.d.ts +178 -0
- package/out/_internal/schema/units.d.ts +123 -0
- package/out/_internal/sets/algebra.d.ts +26 -0
- package/out/_internal/sets/constraint.d.ts +19 -0
- package/out/_internal/sets/engine.d.ts +2 -0
- package/out/_internal/sets/formats.d.ts +23 -0
- package/out/_internal/sets/fromJsonSchema.d.ts +15 -0
- package/out/_internal/sets/implement.d.ts +16 -0
- package/out/_internal/sets/index.d.ts +2 -0
- package/out/_internal/sets/internal.d.ts +20 -0
- package/out/_internal/sets/intersections.d.ts +5 -0
- package/out/_internal/sets/kinds.d.ts +3 -0
- package/out/_internal/sets/refinements.d.ts +14 -0
- package/out/_internal/sets/roots/alias.d.ts +3 -0
- package/out/_internal/sets/roots/domain.d.ts +3 -0
- package/out/_internal/sets/roots/intersection.d.ts +4 -0
- package/out/_internal/sets/roots/morph.d.ts +4 -0
- package/out/_internal/sets/roots/proto.d.ts +3 -0
- package/out/_internal/sets/roots/union.d.ts +16 -0
- package/out/_internal/sets/roots/unit.d.ts +3 -0
- package/out/_internal/sets/structure/index.d.ts +3 -0
- package/out/_internal/sets/structure/prop.d.ts +8 -0
- package/out/_internal/sets/structure/sequence.d.ts +3 -0
- package/out/_internal/sets/structure/structure.d.ts +3 -0
- package/out/_internal/sets/toJsonSchema.d.ts +25 -0
- package/out/_internal/util/arrays.d.ts +130 -0
- package/out/_internal/util/clone.d.ts +4 -0
- package/out/_internal/util/describe.d.ts +25 -0
- package/out/_internal/util/domain.d.ts +49 -0
- package/out/_internal/util/errors.d.ts +29 -0
- package/out/_internal/util/facts.d.ts +22 -0
- package/out/_internal/util/flatMorph.d.ts +41 -0
- package/out/_internal/util/formats.d.ts +8 -0
- package/out/_internal/util/functions.d.ts +33 -0
- package/out/_internal/util/generics.d.ts +65 -0
- package/out/_internal/util/get.d.ts +9 -0
- package/out/_internal/util/hkt.d.ts +27 -0
- package/out/_internal/util/i18n.d.ts +12 -0
- package/out/_internal/util/index.d.ts +28 -0
- package/out/_internal/util/internal.d.ts +2 -0
- package/out/_internal/util/intersections.d.ts +50 -0
- package/out/_internal/util/isomorphic.d.ts +8 -0
- package/out/_internal/util/keys.d.ts +18 -0
- package/out/_internal/util/lazily.d.ts +7 -0
- package/out/_internal/util/numbers.d.ts +82 -0
- package/out/_internal/util/objectKinds.d.ts +157 -0
- package/out/_internal/util/path.d.ts +16 -0
- package/out/_internal/util/primitive.d.ts +16 -0
- package/out/_internal/util/records.d.ts +173 -0
- package/out/_internal/util/registry.d.ts +13 -0
- package/out/_internal/util/scanner.d.ts +110 -0
- package/out/_internal/util/serialize.d.ts +40 -0
- package/out/_internal/util/strings.d.ts +35 -0
- package/out/_internal/util/traits.d.ts +89 -0
- package/out/_internal/util/unionToTuple.d.ts +14 -0
- package/out/_internal/util/weak.d.ts +8 -0
- package/out/attributes.d.ts +15 -11
- package/out/checkers.d.ts +1 -1
- package/out/chunk-22RQSVV3.js +0 -0
- package/out/chunk-3HZC67MT.js +965 -0
- package/out/chunk-3KBOPH3J.js +1564 -0
- package/out/chunk-3SH2BQ4T.js +15 -0
- package/out/chunk-46PLKNON.js +126 -0
- package/out/{chunk-G45P2CLE.js → chunk-4FTHWK63.js} +6 -1
- package/out/{chunk-UK5UGFOX.js → chunk-4HREHNIV.js} +23 -11
- package/out/chunk-4NZCLVBD.js +14 -0
- package/out/chunk-6FMBO47M.js +7 -0
- package/out/chunk-7TALAWYX.js +10 -0
- package/out/{chunk-DLWCO4RV.js → chunk-BCYKO54Z.js} +11 -2
- package/out/chunk-BDJVONP7.js +168 -0
- package/out/{chunk-E3DZXEZK.js → chunk-C2JOL5M2.js} +4 -2
- package/out/{chunk-2U2AWCI3.js → chunk-C2V6QPE3.js} +126 -97
- package/out/chunk-C3B4IFQL.js +6408 -0
- package/out/chunk-CWSF57VM.js +10 -0
- package/out/chunk-F5BACUJS.js +7 -0
- package/out/{chunk-EVCML554.js → chunk-FWRSDUJ2.js} +14 -5
- package/out/chunk-GID3FSQA.js +161 -0
- package/out/chunk-I2KLQ2HA.js +22 -0
- package/out/chunk-K7SLP5D2.js +84 -0
- package/out/chunk-KQPSKGUI.js +9 -0
- package/out/chunk-KXOC3JEX.js +8234 -0
- package/out/chunk-KXOSUYRA.js +405 -0
- package/out/chunk-LHNEY6QW.js +0 -0
- package/out/chunk-MTWKGAGO.js +323 -0
- package/out/chunk-NU73GNCX.js +170 -0
- package/out/{chunk-RAZ3TXOE.js → chunk-P5W67FQH.js} +34 -19
- package/out/chunk-PDLTEX6Z.js +1118 -0
- package/out/{chunk-VV6YDN2D.js → chunk-QUS4IAXJ.js} +304 -263
- package/out/chunk-R7WXIH5P.js +94 -0
- package/out/chunk-TACLEIDM.js +95 -0
- package/out/chunk-UJJGG4VQ.js +71 -0
- package/out/chunk-US6BWOGJ.js +109 -0
- package/out/chunk-VMMMOO4R.js +945 -0
- package/out/chunk-VYMXHSFR.js +439 -0
- package/out/chunk-W2XOI5DY.js +141 -0
- package/out/chunk-WC5L5YRP.js +25 -0
- package/out/{chunk-NRIGTMPM.js → chunk-WKOSL2G6.js} +4 -1
- package/out/chunk-XLG66ZDF.js +0 -0
- package/out/{chunk-5U2ZMDNI.js → chunk-XNUAQSJ4.js} +52 -30
- package/out/chunk-ZRYEHGSG.js +79 -0
- package/out/cli.d.ts +28 -0
- package/out/cli.js +636 -0
- package/out/codecs.d.ts +6 -5
- package/out/codecs.js +23 -7
- package/out/config.d.ts +5 -17
- package/out/config.js +11 -2
- package/out/declare.d.ts +40 -12
- package/out/deferred.d.ts +2 -2
- package/out/errors.d.ts +1 -1
- package/out/errors.js +10 -2
- package/out/fn.d.ts +20 -13
- package/out/fromJsonSchema.d.ts +18 -0
- package/out/generic.d.ts +9 -6
- package/out/i18n.d.ts +2 -2
- package/out/i18n.js +9 -2
- package/out/index.d.ts +16 -7
- package/out/index.js +25 -8
- package/out/internal.d.ts +4 -0
- package/out/internal.js +41 -17
- package/out/island.js +22 -6
- package/out/islandDependency.js +42 -18
- package/out/jsonSchema/infer.d.ts +805 -0
- package/out/jsonSchema/validate.d.ts +416 -0
- package/out/keywords/Array.d.ts +2 -2
- package/out/keywords/builtins.d.ts +3 -3
- package/out/keywords/constructors.d.ts +1 -1
- package/out/keywords/functions.d.ts +5 -192
- package/out/keywords/keywords.d.ts +3 -3
- package/out/keywords/number.d.ts +2 -2
- package/out/keywords/phrases.d.ts +4 -191
- package/out/keywords/string.d.ts +3 -3
- package/out/keywords/ts.d.ts +18 -18
- package/out/keywords/ts.js +19 -4
- package/out/locale.d.ts +6 -2
- package/out/locales/de.js +9 -2
- package/out/locales/en.js +21 -5
- package/out/match.d.ts +13 -10
- package/out/module.d.ts +2 -2
- package/out/mutatesInput.js +18 -3
- package/out/nary.d.ts +220 -194
- package/out/parser/ast/bounds.d.ts +2 -2
- package/out/parser/ast/default.d.ts +2 -2
- package/out/parser/ast/divisor.d.ts +2 -2
- package/out/parser/ast/generic.d.ts +2 -2
- package/out/parser/ast/infer.d.ts +1 -1
- package/out/parser/ast/keyof.d.ts +2 -2
- package/out/parser/ast/utils.d.ts +1 -1
- package/out/parser/ast/validate.d.ts +2 -2
- package/out/parser/definition.d.ts +4 -4
- package/out/parser/objectLiteral.d.ts +2 -2
- package/out/parser/property.d.ts +2 -2
- package/out/parser/reduce/dynamic.d.ts +2 -2
- package/out/parser/reduce/shared.d.ts +1 -1
- package/out/parser/reduce/static.d.ts +1 -1
- package/out/parser/shift/operand/enclosed.d.ts +2 -2
- package/out/parser/shift/operand/genericArgs.d.ts +2 -2
- package/out/parser/shift/operand/operand.d.ts +1 -1
- package/out/parser/shift/operand/unenclosed.d.ts +2 -2
- package/out/parser/shift/operand/unresolvable.d.ts +2 -2
- package/out/parser/shift/operator/bounds.d.ts +2 -2
- package/out/parser/shift/operator/brand.d.ts +2 -2
- package/out/parser/shift/operator/default.d.ts +4 -4
- package/out/parser/shift/operator/divisor.d.ts +1 -1
- package/out/parser/shift/operator/operator.d.ts +1 -1
- package/out/parser/shift/tokens.d.ts +1 -1
- package/out/parser/string.d.ts +2 -2
- package/out/parser/tupleExpressions.d.ts +6 -8
- package/out/parser/tupleLiteral.d.ts +2 -2
- package/out/precompiled.d.ts +1 -1
- package/out/precompiled.js +16 -2
- package/out/regex.d.ts +1 -0
- package/out/regex.js +2 -0
- package/out/runtime.d.ts +10 -8
- package/out/runtime.js +6 -4
- package/out/runtimeDevelopment.js +70 -52
- package/out/schema.d.ts +1 -0
- package/out/schema.js +15 -0
- package/out/schemaInternal.d.ts +1 -0
- package/out/schemaInternal.js +1401 -0
- package/out/schemaIssueFeatures.d.ts +1 -0
- package/out/schemaRuntime.d.ts +1 -0
- package/out/schemaRuntime.js +7 -0
- package/out/schemaRuntimeLean.d.ts +1 -0
- package/out/schemaRuntimeLean.js +1870 -0
- package/out/schemaRuntimeNone.d.ts +1 -0
- package/out/schemaRuntimeNone.js +1774 -0
- package/out/schemaRuntimeNoneLean.d.ts +1 -0
- package/out/schemaRuntimeNoneLean.js +1767 -0
- package/out/scope.d.ts +3 -4
- package/out/shared/issueFeatures.d.ts +38 -0
- package/out/shared/issueFeatures.js +40 -0
- package/out/standalone/artifact.d.ts +6 -6
- package/out/standalone/baking.d.ts +2 -2
- package/out/standalone/combinators.d.ts +1 -1
- package/out/standalone/compilers.d.ts +1 -1
- package/out/standalone/fallbacks.d.ts +1 -1
- package/out/standalone/keys.d.ts +1 -1
- package/out/standalone/sites.d.ts +3 -3
- package/out/standalone/units.d.ts +1 -1
- package/out/standalone/values.d.ts +1 -1
- package/out/type.d.ts +6 -6
- package/out/util.d.ts +1 -0
- package/out/util.js +137 -0
- package/out/variants/array.d.ts +6 -6
- package/out/variants/base.d.ts +61 -10
- package/out/variants/date.d.ts +5 -5
- package/out/variants/instantiate.d.ts +1 -1
- package/out/variants/number.d.ts +6 -6
- package/out/variants/object.d.ts +25 -12
- package/out/variants/string.d.ts +8 -8
- package/package.json +183 -8
- package/skills/arktype/SKILL.md +93 -93
- package/skills/arktype/references/migrating.md +144 -158
- package/skills/arktype/references/precompile.md +7 -7
- package/out/chunk-4CUA5XIP.js +0 -7
- package/out/chunk-GPNK5353.js +0 -2529
- package/out/chunk-MLKGABMK.js +0 -9
- package/out/chunk-PRTMQCHS.js +0 -3
- package/out/chunk-R5G57XC7.js +0 -7
- package/out/chunk-SWOOCAHN.js +0 -4
- package/out/chunk-TAHPKDNM.js +0 -3
- package/out/chunk-UETB6YE2.js +0 -14
package/README.md
CHANGED
|
@@ -1,13 +1,34 @@
|
|
|
1
|
-
<
|
|
2
|
-
<
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://arktype.io">
|
|
3
|
+
<img
|
|
4
|
+
src="https://arktype.io/3/image/og.png"
|
|
5
|
+
width="720"
|
|
6
|
+
alt="ArkType — TypeScript's smallest, fastest validator. One prompt away."
|
|
7
|
+
/>
|
|
8
|
+
</a>
|
|
9
|
+
</p>
|
|
3
10
|
|
|
4
11
|
<p align="center">
|
|
5
|
-
<a href="https://arktype.io
|
|
12
|
+
<a href="https://arktype.io/docs/intro/setup"><strong>Docs</strong></a>
|
|
13
|
+
·
|
|
14
|
+
<a href="https://arktype.io/#board">Benchmarks</a>
|
|
15
|
+
·
|
|
16
|
+
<a href="https://arktype.io/docs/precompile">Precompile</a>
|
|
17
|
+
·
|
|
18
|
+
<a href="https://arktype.io/docs/migrating">Migrate from Zod</a>
|
|
19
|
+
·
|
|
20
|
+
<a href="https://arktype.io/playground">Playground</a>
|
|
21
|
+
·
|
|
22
|
+
<a href="https://arktype.io/discord">Discord</a>
|
|
6
23
|
</p>
|
|
7
24
|
|
|
8
|
-
|
|
25
|
+
## Quick start
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
npm install arktype
|
|
29
|
+
```
|
|
9
30
|
|
|
10
|
-
|
|
31
|
+
Write definitions in TypeScript's own syntax — inferred 1:1 and validated at runtime. Works with anything that takes [Standard Schema](https://standardschema.dev) (tRPC, Hono, React Hook Form, Drizzle, AI SDK, MCP SDK, and more).
|
|
11
32
|
|
|
12
33
|
```ts
|
|
13
34
|
import { type } from "arktype"
|
|
@@ -33,158 +54,41 @@ if (out instanceof type.errors) {
|
|
|
33
54
|
}
|
|
34
55
|
```
|
|
35
56
|
|
|
36
|
-
|
|
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).
|
|
57
|
+
## Migrate with one prompt
|
|
43
58
|
|
|
44
|
-
|
|
59
|
+
1. Add the ArkType [agent skill](https://arktype.io/docs/agents):
|
|
45
60
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
```
|
|
49
|
-
npx skills add arktypeio/arktype
|
|
50
|
-
```
|
|
61
|
+
```sh
|
|
62
|
+
npx skills add arktypeio/arktype
|
|
63
|
+
```
|
|
51
64
|
|
|
52
|
-
|
|
53
|
-
Migrate this project from Zod to ArkType.
|
|
54
|
-
```
|
|
65
|
+
2. Ask your coding agent:
|
|
55
66
|
|
|
56
|
-
|
|
67
|
+
```text
|
|
68
|
+
Migrate this project from Zod to ArkType.
|
|
69
|
+
```
|
|
57
70
|
|
|
58
|
-
|
|
59
|
-
// before
|
|
60
|
-
import { z } from "zod"
|
|
71
|
+
See the [migration guide](https://arktype.io/docs/migrating) for Zod, Valibot, TypeBox, Yup, Joi, Effect Schema, and JSON Schema.
|
|
61
72
|
|
|
62
|
-
|
|
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` |
|
|
73
|
+
## Docs
|
|
139
74
|
|
|
140
|
-
|
|
75
|
+
- **[Get started](https://arktype.io/docs/intro/setup)** — installation, editor setup, and your first type
|
|
76
|
+
- **[Precompile](https://arktype.io/docs/precompile)** — compile Types at build time with `@ark/aot`
|
|
77
|
+
- **[Benchmarks](https://arktype.io/#board)** — speed, startup, and bundle size, with methods and rerun commands
|
|
78
|
+
- **[Migration guide](https://arktype.io/docs/migrating)** — from Zod, Valibot, TypeBox, Yup, Joi, Effect Schema, and JSON Schema
|
|
79
|
+
- **[With agents](https://arktype.io/docs/agents)** — the skill and Markdown docs at [arktype.io/llms.txt](https://arktype.io/llms.txt)
|
|
80
|
+
- **[What's new in 3.0](https://arktype.io/docs/blog/3.0)** — release notes and [upgrading from 2.x](https://arktype.io/docs/upgrading)
|
|
141
81
|
|
|
142
|
-
|
|
82
|
+
## CLI
|
|
143
83
|
|
|
144
|
-
|
|
84
|
+
`arktype` ships the `ark` binary (`init`, `migrate`, `skill`, and `check` / `emit` / `declare` via `@ark/aot`). Inside an installed project, run `pnpm exec ark` (or `npx arktype` everywhere — `ark` on npm is an unrelated package):
|
|
145
85
|
|
|
146
86
|
```sh
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
```ts
|
|
151
|
-
// vite.config.ts
|
|
152
|
-
import { precompile } from "@ark/aot/unplugin"
|
|
153
|
-
|
|
154
|
-
export default {
|
|
155
|
-
plugins: [precompile.vite()]
|
|
156
|
-
}
|
|
87
|
+
npx arktype init # create ark.json and install the bundled skill
|
|
88
|
+
npx arktype migrate # install the skill and print a migration prompt
|
|
89
|
+
npx arktype check # run @ark/aot --check
|
|
157
90
|
```
|
|
158
91
|
|
|
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.
|
|
183
|
-
|
|
184
|
-
## Docs
|
|
185
|
-
|
|
186
|
-
[arktype.io](https://arktype.io/docs/intro/setup)
|
|
187
|
-
|
|
188
92
|
## Contributions
|
|
189
93
|
|
|
190
94
|
We accept and encourage pull requests from outside ArkType. Planned work is tracked in [this GitHub project](https://github.com/orgs/arktypeio/projects/4).
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Backslash, contains, ErrorMessage, noSuggest, Scanner, writeUnclosedGroupMessage } from "../util/index.js";
|
|
2
|
+
import type { NonZeroDigit, RegexClassChar, StringDigit, writeUnnecessaryEscapeMessage } from "./escape.ts";
|
|
3
|
+
import type { UnicodeFlag } from "./regex.ts";
|
|
4
|
+
import type { Control, s, State } from "./state.ts";
|
|
5
|
+
export type parseCharset<s extends State, unscanned extends string> = Scanner.shiftUntilEscapable<unscanned, "]", Backslash> extends (Scanner.shiftResult<infer scanned, infer nextUnscanned>) ? nextUnscanned extends `]${infer remaining}` ? scanned extends Scanner.shift<"^", string> ? s.shiftQuantifiable<s, string, remaining> : parseNonNegatedCharset<scanned, never, null, contains<s["flags"], UnicodeFlag>> extends infer result extends string ? [
|
|
6
|
+
result
|
|
7
|
+
] extends [never] ? s.error<emptyCharacterSetMessage> : result extends ErrorMessage ? s.error<result> : s.shiftQuantifiable<s, result, remaining> : never : s.error<writeUnclosedGroupMessage<"]">> : never;
|
|
8
|
+
type parseNonNegatedCharset<chars extends string, set extends string, lastChar extends string | null, unicode extends boolean> = parseChar<chars, unicode> extends (Scanner.shiftResult<infer result, infer unscanned>) ? result extends UnescapedDashMarker ? parseDash<unscanned, set, lastChar, unicode> : result extends ErrorMessage ? result : parseNonNegatedCharset<unscanned, set | result, result, unicode> : set;
|
|
9
|
+
type parseDash<unscanned extends string, set extends string, lastChar extends string | null, unicode extends boolean> = lastChar extends string ? parseChar<unscanned, unicode> extends (Scanner.shiftResult<infer rangeEnd, infer next>) ? rangeEnd extends ErrorMessage ? rangeEnd : parseNonNegatedCharset<next, set | inferRange<lastChar, rangeEnd>, null, unicode> : // trailing -, treat as literal
|
|
10
|
+
set | "-" : parseNonNegatedCharset<unscanned, set | "-", "-", unicode>;
|
|
11
|
+
type inferRange<start extends string, end extends string> = start | end extends StringDigit ? `${number}` : string;
|
|
12
|
+
type UnescapedDashMarker = noSuggest<"dash">;
|
|
13
|
+
type parseChar<unscanned extends string, unicode extends boolean> = unscanned extends Scanner.shift<infer lookahead, infer next> ? lookahead extends Backslash ? next extends Scanner.shift<infer escaped, infer postEscaped> ? Scanner.shiftResult<parseEscapedSetChar<escaped, unicode>, postEscaped> : never : Scanner.shiftResult<lookahead extends "-" ? UnescapedDashMarker : lookahead, next> : null;
|
|
14
|
+
type parseEscapedSetChar<char extends string, unicode extends boolean> = char extends "b" ? "\b" : char extends "d" ? `${number}` : char extends keyof EscapedControlChars ? EscapedControlChars[char] : char extends "0" | "c" | "x" | "u" | "p" | "P" | "s" | RegexClassChar ? string : unicode extends true ? char extends Control | "/" ? char : ErrorMessage<writeUnnecessaryEscapeMessage<char>> : char extends NonZeroDigit ? string : char;
|
|
15
|
+
type EscapedControlChars = {
|
|
16
|
+
n: "\n";
|
|
17
|
+
t: "\t";
|
|
18
|
+
r: "\r";
|
|
19
|
+
f: "\f";
|
|
20
|
+
v: "\v";
|
|
21
|
+
};
|
|
22
|
+
export declare const emptyCharacterSetMessage = "Empty character set [] is unsatisfiable";
|
|
23
|
+
export type emptyCharacterSetMessage = typeof emptyCharacterSetMessage;
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ErrorMessage, Scanner } from "../util/index.js";
|
|
2
|
+
import type { Control, ReferenceNode, s, State } from "./state.ts";
|
|
3
|
+
export type parseEscape<s extends State, unscanned extends string> = unscanned extends Scanner.shift<infer char, infer nextUnscanned> ? char extends NonZeroDigit ? parseNumericBackreference<s, unscanned> : char extends "k" ? parseNamedBackreference<s, nextUnscanned> : char extends UnicodePropertyChar ? parseUnicodeProperty<s, char, nextUnscanned> : parseSingleEscapedCharacter<s, char, nextUnscanned> : s.error<trailingBackslashMessage>;
|
|
4
|
+
type parseNumericBackreference<s extends State, fullUnscanned extends string> = Scanner.shiftUntilNot<fullUnscanned, StringDigit> extends (Scanner.shiftResult<infer ref, infer remaining>) ? s.shiftQuantifiable<s, ReferenceNode<ref>, remaining> : never;
|
|
5
|
+
type parseNamedBackreference<s extends State, unscanned extends string> = unscanned extends `<${infer ref}>${infer following}` ? s.shiftQuantifiable<s, ReferenceNode<ref>, following> : s.error<missingBackreferenceNameMessage>;
|
|
6
|
+
type parseUnicodeProperty<s extends State, char extends UnicodePropertyChar, unscanned extends string> = unscanned extends `{${string}}${infer following}` ? s.shiftQuantifiable<s, string, following> : s.error<writeInvalidUnicodePropertyMessage<char>>;
|
|
7
|
+
type parseSingleEscapedCharacter<s extends State, char extends string, remaining extends string> = parseEscapedChar<char> extends infer result extends string ? result extends ErrorMessage ? s.error<result> : s.shiftQuantifiable<s, result, remaining> : never;
|
|
8
|
+
export type parseEscapedChar<char extends string> = char extends RegexClassChar | "s" ? string : char extends "d" ? `${number}` : char extends BoundaryChar ? "" : char extends Control ? char : char extends "c" ? ErrorMessage<caretNotationMessage> : char extends StringEscapableChar ? ErrorMessage<writeStringEscapableMessage<char>> : ErrorMessage<writeUnnecessaryEscapeMessage<char>>;
|
|
9
|
+
export declare const trailingBackslashMessage = "A regex cannot end with \\";
|
|
10
|
+
export type trailingBackslashMessage = typeof trailingBackslashMessage;
|
|
11
|
+
export declare const writeUnresolvableBackreferenceMessage: <ref extends string | number>(ref: ref) => writeUnresolvableBackreferenceMessage<ref>;
|
|
12
|
+
export type writeUnresolvableBackreferenceMessage<ref extends string | number> = `Group ${ref} does not exist`;
|
|
13
|
+
export declare const missingBackreferenceNameMessage = "\\k must be followed by a named reference like <name>";
|
|
14
|
+
export type missingBackreferenceNameMessage = typeof missingBackreferenceNameMessage;
|
|
15
|
+
export declare const writeInvalidUnicodePropertyMessage: <char extends UnicodePropertyChar>(char: char) => writeInvalidUnicodePropertyMessage<char>;
|
|
16
|
+
export type writeInvalidUnicodePropertyMessage<char extends UnicodePropertyChar> = `\\${char} must be followed by a property like \\${char}{Emoji_Presentation}`;
|
|
17
|
+
export declare const writeUnnecessaryEscapeMessage: <char extends string>(char: char) => writeUnnecessaryEscapeMessage<char>;
|
|
18
|
+
export type writeUnnecessaryEscapeMessage<char extends string> = `Escape preceding ${char} is unnecessary and should be removed.`;
|
|
19
|
+
export declare const writeStringEscapableMessage: (char: StringEscapableChar) => "\\0 should be specified with a single backslash like regex('\\n')" | "\\f should be specified with a single backslash like regex('\\n')" | "\\n should be specified with a single backslash like regex('\\n')" | "\\r should be specified with a single backslash like regex('\\n')" | "\\t should be specified with a single backslash like regex('\\n')" | "\\u should be specified with a single backslash like regex('\\n')" | "\\v should be specified with a single backslash like regex('\\n')" | "\\x should be specified with a single backslash like regex('\\n')";
|
|
20
|
+
export type writeStringEscapableMessage<char extends StringEscapableChar> = `\\${char} should be specified with a single backslash like regex('\n')`;
|
|
21
|
+
export declare const caretNotationMessage = "\\\\cX notation is not supported. Use hex (\\\\x) or unicode (\\\\u) instead.";
|
|
22
|
+
export type caretNotationMessage = "\\cX notation is not supported. Use hex (\\x) or unicode (\\u) instead.";
|
|
23
|
+
export type StringEscapableChar = "t" | "n" | "r" | "f" | "v" | "0" | "x" | "u";
|
|
24
|
+
export type RegexClassChar = "w" | "W" | "D" | "S";
|
|
25
|
+
export type BoundaryChar = "b" | "B";
|
|
26
|
+
export type UnicodePropertyChar = "p" | "P";
|
|
27
|
+
export type NonZeroDigit = "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
|
|
28
|
+
export type StringDigit = "0" | NonZeroDigit;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { DynamicBase } from "../util/index.js";
|
|
2
|
+
import type { Flags, IndexedCaptures, NamedCaptures } from "./regex.ts";
|
|
3
|
+
export interface RegexExecArray<patternAndCaptures extends IndexedCaptures, namedCaptures extends NamedCaptures, flags extends Flags> extends DynamicBase<patternAndCaptures> {
|
|
4
|
+
/**
|
|
5
|
+
* The index of the search at which the result was found.
|
|
6
|
+
*/
|
|
7
|
+
index: number;
|
|
8
|
+
/**
|
|
9
|
+
* A copy of the search string.
|
|
10
|
+
*/
|
|
11
|
+
input: patternAndCaptures[0];
|
|
12
|
+
indices: flags extends `${string}d${string}` ? RegexIndicesArray<patternAndCaptures, namedCaptures> : undefined;
|
|
13
|
+
groups: keyof namedCaptures extends never ? undefined : namedCaptures;
|
|
14
|
+
}
|
|
15
|
+
export type RegexIndexRange = [start: number, end: number];
|
|
16
|
+
interface RegexIndicesArray<patternAndCaptures extends IndexedCaptures, namedCaptures extends NamedCaptures> extends DynamicBase<{
|
|
17
|
+
[i in keyof patternAndCaptures]: indexRangeOf<patternAndCaptures[i]>;
|
|
18
|
+
}> {
|
|
19
|
+
groups: keyof namedCaptures extends never ? undefined : {
|
|
20
|
+
[k in keyof namedCaptures]: indexRangeOf<namedCaptures[k]>;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
type indexRangeOf<capture> = capture extends string ? RegexIndexRange : undefined;
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { ErrorMessage, Scanner, writeUnclosedGroupMessage } from "../util/index.js";
|
|
2
|
+
import type { State, s } from "./state.ts";
|
|
3
|
+
type LookaroundChar = "=" | "!";
|
|
4
|
+
type lookaroundKindOf<char extends LookaroundChar> = char extends "=" ? State.UnnamedCaptureKind.lookaround : State.UnnamedCaptureKind.negativeLookaround;
|
|
5
|
+
export type ModifiableFlag = "i" | "m" | "s";
|
|
6
|
+
export type parseGroup<s extends State, unscanned extends string> = unscanned extends Scanner.shift<infer lookahead, infer next> ? lookahead extends "?" ? parseNonCapturingGroup<s, next> : s.pushGroup<s, State.UnnamedCaptureKind.indexed, unscanned, undefined> : s.error<writeUnclosedGroupMessage<")">>;
|
|
7
|
+
type parseNonCapturingGroup<s extends State, unscanned extends string> = unscanned extends Scanner.shift<infer lookahead, infer next> ? lookahead extends ":" ? s.pushGroup<s, State.UnnamedCaptureKind.noncapturing, next, undefined> : lookahead extends LookaroundChar ? s.pushGroup<s, lookaroundKindOf<lookahead>, next, undefined> : lookahead extends "<" ? parseNamedGroupOrLookbehind<s, next> : shiftModifiers<unscanned> extends (ShiftedModifiers<infer flags, infer negated, infer following>) ? following extends ErrorMessage<infer message> ? s.error<message> : s.pushGroup<s, State.UnnamedCaptureKind.noncapturing, following, "i" extends flags ? true : "i" extends negated ? false : undefined> : never : s.error<writeUnclosedGroupMessage<")">>;
|
|
8
|
+
type ShiftedModifiers<flags extends ModifiableFlag = ModifiableFlag, negated extends ModifiableFlag = ModifiableFlag, unscanned extends string = string> = [ParsedModifiers<flags, negated>, unscanned];
|
|
9
|
+
type ParsedModifiers<flags extends ModifiableFlag = ModifiableFlag, negated extends ModifiableFlag = ModifiableFlag> = {
|
|
10
|
+
flags: flags;
|
|
11
|
+
negated: negated;
|
|
12
|
+
};
|
|
13
|
+
type shiftModifiers<unscanned extends string> = Scanner.shiftUntil<unscanned, ":" | ")"> extends (Scanner.shiftResult<infer scanned, infer next>) ? next extends Scanner.shift<infer terminator, infer following> ? terminator extends ":" ? parseModifiers<scanned> extends (ParsedModifiers<infer flags, infer negated>) ? ShiftedModifiers<flags, negated, following> : ShiftedModifiers<never, never, ErrorMessage<parseModifiers<scanned> & string>> : ShiftedModifiers<never, never, ErrorMessage<unescapedLiteralQuestionMarkMessage>> : ShiftedModifiers<never, never, ErrorMessage<writeUnclosedGroupMessage<")">>> : never;
|
|
14
|
+
type parseModifiers<unscanned extends string> = _parseModifiers<unscanned, never, never>;
|
|
15
|
+
type _parseModifiers<unscanned extends string, flags extends ModifiableFlag, negated extends ModifiableFlag> = unscanned extends Scanner.shift<infer lookahead, infer next> ? lookahead extends "-" ? [
|
|
16
|
+
negated
|
|
17
|
+
] extends [never] ? next extends Scanner.shift<infer modifier, infer next> ? modifier extends ModifiableFlag ? modifier extends flags | negated ? writeDuplicateModifierMessage<modifier> : _parseModifiers<next, flags, negated | modifier> : writeInvalidModifierMessage<modifier> : missingNegatedModifierMessage : multipleModifierDashesMessage : lookahead extends ModifiableFlag ? lookahead extends flags | negated ? writeDuplicateModifierMessage<lookahead> : [
|
|
18
|
+
negated
|
|
19
|
+
] extends [never] ? _parseModifiers<next, flags | lookahead, negated> : _parseModifiers<next, flags, negated | lookahead> : writeInvalidModifierMessage<lookahead> : ParsedModifiers<flags, negated>;
|
|
20
|
+
export declare const writeDuplicateModifierMessage: <modifier extends ModifiableFlag>(modifier: modifier) => writeDuplicateModifierMessage<modifier>;
|
|
21
|
+
type writeDuplicateModifierMessage<modifier extends ModifiableFlag> = `Modifier ${modifier} cannot appear multiple times in a single group`;
|
|
22
|
+
export declare const multipleModifierDashesMessage = "Modifiers can include at most one '-' to negate subsequent flags";
|
|
23
|
+
type multipleModifierDashesMessage = typeof multipleModifierDashesMessage;
|
|
24
|
+
export declare const missingNegatedModifierMessage = "- must be followed by the modifier flag to negate ('i', 'm' or 's')";
|
|
25
|
+
type missingNegatedModifierMessage = typeof missingNegatedModifierMessage;
|
|
26
|
+
export declare const writeInvalidModifierMessage: <char extends string>(char: char) => writeInvalidModifierMessage<char>;
|
|
27
|
+
type writeInvalidModifierMessage<char extends string> = `Modifier flag ${char} must be 'i', 'm' or 's'`;
|
|
28
|
+
type parseNamedGroupOrLookbehind<s extends State, unscanned extends string> = unscanned extends (Scanner.shift<infer char extends LookaroundChar, infer next>) ? s.pushGroup<s, lookaroundKindOf<char>, next, undefined> : shiftNamedGroup<unscanned> extends (Scanner.shiftResult<infer name, infer following>) ? s.pushGroup<s, name, following, undefined> : s.error<writeUnclosedGroupMessage<")">>;
|
|
29
|
+
type shiftNamedGroup<unscanned extends string> = unscanned extends `${infer name}>${infer next}` ? name extends "" ? Scanner.shiftResult<"", ErrorMessage<unnamedCaptureGroupMessage>> : Scanner.shiftResult<name, next> : Scanner.shiftResult<"", ErrorMessage<writeUnclosedGroupMessage<">">>>;
|
|
30
|
+
export declare const unnamedCaptureGroupMessage = "Capture group <> requires a name";
|
|
31
|
+
export type unnamedCaptureGroupMessage = typeof unnamedCaptureGroupMessage;
|
|
32
|
+
export declare const unescapedLiteralQuestionMarkMessage = "literal ? must be escaped at the start of a group";
|
|
33
|
+
export type unescapedLiteralQuestionMarkMessage = typeof unescapedLiteralQuestionMarkMessage;
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./index.ts";
|
|
2
|
+
export * from "./charset.ts";
|
|
3
|
+
export * from "./escape.ts";
|
|
4
|
+
export * from "./execArray.ts";
|
|
5
|
+
export * from "./group.ts";
|
|
6
|
+
export * from "./parse.ts";
|
|
7
|
+
export * from "./quantify.ts";
|
|
8
|
+
export * from "./regex.ts";
|
|
9
|
+
export * from "./state.ts";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Backslash, contains, ErrorMessage, Scanner } from "../util/index.js";
|
|
2
|
+
import type { parseCharset } from "./charset.ts";
|
|
3
|
+
import type { parseEscape } from "./escape.ts";
|
|
4
|
+
import type { parseGroup } from "./group.ts";
|
|
5
|
+
import type { parseBuiltinQuantifier, parsePossibleRange, QuantifyingChar } from "./quantify.ts";
|
|
6
|
+
import type { Anchor, AnchorMarker, s, State, UnionTree } from "./state.ts";
|
|
7
|
+
export type parseState<s extends State> = s["unscanned"] extends ErrorMessage ? s["unscanned"] : s["unscanned"] extends "" ? s.finalize<s> : parseState<next<s>>;
|
|
8
|
+
export type next<s extends State> = s["unscanned"] extends Scanner.shift<infer lookahead, infer unscanned> ? lookahead extends "." ? s.shiftQuantifiable<s, string, unscanned> : lookahead extends Backslash ? parseEscape<s, unscanned> : lookahead extends "|" ? s.finalizeBranch<s, unscanned> : lookahead extends Anchor ? contains<s["flags"], "m"> extends true ? s.shiftQuantifiable<s, "", unscanned> : s.anchor<s, AnchorMarker<lookahead>, unscanned> : lookahead extends "(" ? parseGroup<s, unscanned> : lookahead extends ")" ? s.popGroup<s, unscanned> : lookahead extends QuantifyingChar ? parseBuiltinQuantifier<s, lookahead, unscanned> : lookahead extends "{" ? parsePossibleRange<s, unscanned> : lookahead extends "[" ? parseCharset<s, unscanned> : s.shiftQuantifiable<s, maybeSplitCasing<s["caseInsensitive"], lookahead>, unscanned> : never;
|
|
9
|
+
type maybeSplitCasing<caseInsensitive extends boolean, char extends string> = caseInsensitive extends false ? char : Lowercase<char> extends Uppercase<char> ? char : UnionTree<[Lowercase<char>, Capitalize<char>]>;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { parseNonNegativeInteger, Scanner } from "../util/index.js";
|
|
2
|
+
import type { s, State } from "./state.ts";
|
|
3
|
+
export type parseBuiltinQuantifier<s extends State, quantifier extends QuantifyingChar, unscanned extends string> = s["root"] extends "" ? s.error<writeUnmatchedQuantifierError<quantifier>> : quantifyBuiltin<s, quantifier, unscanned extends Scanner.shift<"?", infer lazyUnscanned> ? lazyUnscanned : unscanned>;
|
|
4
|
+
type quantifyBuiltin<s extends State, quantifier extends QuantifyingChar, unscanned extends string> = quantifier extends "?" ? s.pushQuantifier<s, 0, 1, unscanned> : quantifier extends "+" ? s.pushQuantifier<s, 1, null, unscanned> : quantifier extends "*" ? s.pushQuantifier<s, 0, null, unscanned> : never;
|
|
5
|
+
type ParsedRange = {
|
|
6
|
+
min: number;
|
|
7
|
+
max: number | null;
|
|
8
|
+
unscanned: string;
|
|
9
|
+
};
|
|
10
|
+
declare namespace ParsedRange {
|
|
11
|
+
type from<r extends ParsedRange> = r;
|
|
12
|
+
}
|
|
13
|
+
type skipPossibleQuestionMark<unscanned extends string> = unscanned extends `?${infer next}` ? next : unscanned;
|
|
14
|
+
type parsePossibleRangeString<unscanned extends string> = unscanned extends (`${infer l extends `${number}`},${infer r extends `${number}`}}${infer next}`) ? ParsedRange.from<{
|
|
15
|
+
min: parseNonNegativeInteger<l>;
|
|
16
|
+
max: parseNonNegativeInteger<r>;
|
|
17
|
+
unscanned: skipPossibleQuestionMark<next>;
|
|
18
|
+
}> : unscanned extends `${infer l extends `${number}`},}${infer next}` ? ParsedRange.from<{
|
|
19
|
+
min: parseNonNegativeInteger<l>;
|
|
20
|
+
max: null;
|
|
21
|
+
unscanned: skipPossibleQuestionMark<next>;
|
|
22
|
+
}> : unscanned extends `${infer l extends `${number}`}}${infer next}` ? ParsedRange.from<{
|
|
23
|
+
min: parseNonNegativeInteger<l>;
|
|
24
|
+
max: parseNonNegativeInteger<l>;
|
|
25
|
+
unscanned: skipPossibleQuestionMark<next>;
|
|
26
|
+
}> : null;
|
|
27
|
+
type GreaterDigits = {
|
|
28
|
+
"0": never;
|
|
29
|
+
"1": "0";
|
|
30
|
+
"2": "0" | "1";
|
|
31
|
+
"3": "0" | "1" | "2";
|
|
32
|
+
"4": "0" | "1" | "2" | "3";
|
|
33
|
+
"5": "0" | "1" | "2" | "3" | "4";
|
|
34
|
+
"6": "0" | "1" | "2" | "3" | "4" | "5";
|
|
35
|
+
"7": "0" | "1" | "2" | "3" | "4" | "5" | "6";
|
|
36
|
+
"8": "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7";
|
|
37
|
+
"9": "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8";
|
|
38
|
+
};
|
|
39
|
+
type isGreaterNaturalString<l extends string, r extends string, decided extends boolean | null = null> = l extends `${infer ld extends keyof GreaterDigits}${infer lRest}` ? r extends `${infer rd extends keyof GreaterDigits}${infer rRest}` ? isGreaterNaturalString<lRest, rRest, decided extends boolean ? decided : ld extends rd ? null : rd extends GreaterDigits[ld] ? true : false> : true : r extends "" ? decided extends true ? true : false : false;
|
|
40
|
+
type exceedsMax<min extends number, max extends number | null> = max extends number ? min extends 0 | max ? false : isGreaterNaturalString<`${min}`, `${max}`> : false;
|
|
41
|
+
type parseQuantifier<unscanned extends string, parsed extends ParsedRange> = unscanned extends `${infer range}${parsed["unscanned"]}` ? `{${range}` : never;
|
|
42
|
+
export type parsePossibleRange<s extends State, unscanned extends string, parsed extends ParsedRange | null = parsePossibleRangeString<unscanned>> = parsed extends ParsedRange ? s["root"] extends "" ? s.error<writeUnmatchedQuantifierError<parseQuantifier<unscanned, parsed>>> : [parsed["min"], parsed["max"]] extends ([
|
|
43
|
+
never,
|
|
44
|
+
unknown
|
|
45
|
+
] | [unknown, never]) ? s.error<writeUnnaturalNumberQuantifierError<parseQuantifier<unscanned, parsed>>> : exceedsMax<parsed["min"], parsed["max"]> extends true ? s.error<writeMinExceedsMaxQuantifierError<parseQuantifier<unscanned, parsed>>> : s.pushQuantifier<s, parsed["min"], parsed["max"], parsed["unscanned"]> : s.shiftQuantifiable<s, "{", unscanned>;
|
|
46
|
+
export type quantify<pattern extends string, min extends number, max extends number | null> = tryFastPath<pattern, min, max>;
|
|
47
|
+
type tryFastPath<pattern extends string, min extends number, max extends number | null> = max extends 0 ? "" : string extends pattern ? string : [pattern, `${number}`] extends [`${number}`, pattern] ? max extends null ? string : min extends 0 ? "" | `${number}` : `${number}` : min extends 0 ? max extends 1 ? "" | pattern : max extends number ? loopFromZero<pattern, max, "", []> : // max is null, all we can do is append ${string}
|
|
48
|
+
"" | `${pattern}${string}` : loopUntilMin<pattern, min, max, "", []>;
|
|
49
|
+
type loopFromZero<base extends string, max extends number, acc extends string, repetitions extends 1[]> = repetitions["length"] extends max ? acc : loopFromZero<base, max, acc | `${acc}${base}`, [...repetitions, 1]>;
|
|
50
|
+
type loopUntilMin<base extends string, min extends number, max extends number | null, acc extends string, repetitions extends 1[]> = repetitions["length"] extends min ? max extends number ? loopUntilMax<base, min, max, acc, repetitions> : repetitions["length"] extends 0 ? acc | `${acc}${base}${string}` : `${acc}${string}` : loopUntilMin<base, min, max, `${acc}${base}`, [...repetitions, 1]>;
|
|
51
|
+
type loopUntilMax<base extends string, min extends number, max extends number, acc extends string, repetitions extends 1[]> = repetitions["length"] extends max ? acc : loopUntilMax<base, min, max, acc | `${acc}${base}`, [...repetitions, 1]>;
|
|
52
|
+
export type QuantifyingChar = "*" | "+" | "?";
|
|
53
|
+
export declare const writeUnmatchedQuantifierError: <quantifier extends string>(quantifier: quantifier) => writeUnmatchedQuantifierError<quantifier>;
|
|
54
|
+
export type writeUnmatchedQuantifierError<quantifier extends string> = `Quantifier ${quantifier} requires a preceding token`;
|
|
55
|
+
export declare const writeUnnaturalNumberQuantifierError: <quantifier extends string>(quantifier: quantifier) => writeUnnaturalNumberQuantifierError<quantifier>;
|
|
56
|
+
export type writeUnnaturalNumberQuantifierError<quantifier extends string> = `Quantifier ${quantifier} must use natural numbers`;
|
|
57
|
+
export declare const writeMinExceedsMaxQuantifierError: <quantifier extends string>(quantifier: quantifier) => writeMinExceedsMaxQuantifierError<quantifier>;
|
|
58
|
+
export type writeMinExceedsMaxQuantifierError<quantifier extends string> = `Quantifier ${quantifier} min cannot exceed max`;
|
|
59
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ErrorMessage, inferred } from "../util/index.js";
|
|
2
|
+
import type { RegexExecArray } from "./execArray.ts";
|
|
3
|
+
import type { parseState } from "./parse.ts";
|
|
4
|
+
import type { State } from "./state.ts";
|
|
5
|
+
export type IndexedCaptures = Array<string | undefined>;
|
|
6
|
+
export type NamedCaptures = Record<string, string | undefined>;
|
|
7
|
+
export type UnicodeFlag = "v" | "u";
|
|
8
|
+
export type Flags = `${"d" | ""}${"g" | ""}${"i" | ""}${"m" | ""}${"s" | ""}${UnicodeFlag | ""}${"y" | ""}`;
|
|
9
|
+
export type RegexContext = {
|
|
10
|
+
flags?: Flags;
|
|
11
|
+
captures?: IndexedCaptures;
|
|
12
|
+
names?: NamedCaptures;
|
|
13
|
+
};
|
|
14
|
+
export interface Regex<out pattern extends string = string, out ctx extends RegexContext = RegexContext> extends RegExp {
|
|
15
|
+
[inferred]: pattern;
|
|
16
|
+
infer: pattern;
|
|
17
|
+
inferCaptures: ctx["captures"] extends IndexedCaptures ? ctx["captures"] : [];
|
|
18
|
+
inferNamedCaptures: ctx["names"] extends NamedCaptures ? ctx["names"] : {};
|
|
19
|
+
inferExecArray: RegexExecArray<[
|
|
20
|
+
pattern,
|
|
21
|
+
...this["inferCaptures"]
|
|
22
|
+
], this["inferNamedCaptures"], this["flags"]>;
|
|
23
|
+
flags: ctx["flags"] extends Flags ? ctx["flags"] : "";
|
|
24
|
+
test(s: string): s is pattern;
|
|
25
|
+
exec(s: string): this["inferExecArray"] | null;
|
|
26
|
+
exec(s: string): never;
|
|
27
|
+
}
|
|
28
|
+
export interface RegexParser {
|
|
29
|
+
<src extends string, flags extends Flags = "">(src: regex.validate<src, flags>, flags?: flags): regex.parse<src, flags>;
|
|
30
|
+
as: <pattern extends string = string, ctx extends RegexContext = {}>(src: string, flags?: Flags) => Regex<pattern, ctx>;
|
|
31
|
+
}
|
|
32
|
+
export declare const regex: RegexParser;
|
|
33
|
+
export type regex<pattern extends string = string, ctx extends RegexContext = RegexContext> = Regex<pattern, ctx>;
|
|
34
|
+
export declare namespace regex {
|
|
35
|
+
type infer<src extends string, flags extends Flags = ""> = parse<src, flags> extends Regex<infer pattern> ? pattern : never;
|
|
36
|
+
type validate<src extends string, flags extends Flags = ""> = parse<src, flags> extends infer e extends ErrorMessage ? e : src;
|
|
37
|
+
type parse<src extends string, flags extends Flags = ""> = parseState<State.initialize<src, flags>>;
|
|
38
|
+
}
|