arktype 2.0.0-dev.1 → 2.0.0-dev.10
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/__tests__/array.test.ts +270 -151
- package/__tests__/badDefinitionType.test.ts +10 -3
- package/__tests__/basis.test.ts +6 -2
- package/__tests__/bounds.test.ts +282 -352
- package/__tests__/cast.test.ts +7 -2
- package/__tests__/completions.test.ts +8 -3
- package/__tests__/config.test.ts +95 -167
- package/__tests__/dateLiteral.test.ts +8 -4
- package/__tests__/declared.test.ts +7 -6
- package/__tests__/discrimination.test.ts +1 -1
- package/__tests__/divisor.test.ts +46 -22
- package/__tests__/enclosed.test.ts +39 -25
- package/__tests__/examples.test.ts +56 -56
- package/__tests__/expressions.test.ts +184 -112
- package/__tests__/generic.test.ts +307 -288
- package/__tests__/group.test.ts +67 -54
- package/__tests__/imports.test.ts +62 -49
- package/__tests__/instanceof.test.ts +25 -23
- package/__tests__/intersection.test.ts +52 -48
- package/__tests__/keyTraversal.test.ts +77 -73
- package/__tests__/keyof.test.ts +68 -52
- package/__tests__/keywords.test.ts +164 -138
- package/__tests__/literal.test.ts +60 -21
- package/__tests__/match.bench.ts +31 -0
- package/__tests__/match.test.ts +222 -22
- package/__tests__/narrow.test.ts +52 -54
- package/__tests__/objectLiteral.test.ts +347 -205
- package/__tests__/operator.bench.ts +4 -4
- package/__tests__/pipe.test.ts +501 -0
- package/__tests__/realWorld.test.ts +63 -0
- package/__tests__/regex.test.ts +17 -7
- package/__tests__/scope.bench.ts +1 -1
- package/__tests__/scope.test.ts +257 -23
- package/__tests__/serialization.test.ts +19 -0
- package/__tests__/string.test.ts +11 -7
- package/__tests__/submodule.test.ts +26 -39
- package/__tests__/this.test.ts +83 -57
- package/__tests__/thunk.test.ts +13 -7
- package/__tests__/traverse.test.ts +76 -54
- package/__tests__/tsGenerics.test.ts +12 -12
- package/__tests__/type.test.ts +36 -0
- package/__tests__/typeReference.test.ts +7 -7
- package/__tests__/unenclosed.test.ts +116 -100
- package/__tests__/union.test.ts +171 -104
- package/ark.ts +21 -65
- package/generic.ts +69 -0
- package/main.ts +6 -18
- package/match.ts +195 -93
- package/module.ts +15 -0
- package/out/ark.d.ts +15 -39
- package/out/ark.js +9 -18
- package/out/ark.js.map +1 -1
- package/out/generic.d.ts +21 -0
- package/out/generic.js +24 -0
- package/out/generic.js.map +1 -0
- package/out/main.d.ts +6 -7
- package/out/main.js +4 -3
- package/out/main.js.map +1 -1
- package/out/match.d.ts +57 -34
- package/out/match.js +51 -17
- package/out/match.js.map +1 -1
- package/out/module.d.ts +10 -0
- package/out/module.js +3 -0
- package/out/module.js.map +1 -0
- package/out/parser/definition.d.ts +11 -13
- package/out/parser/definition.js +10 -13
- package/out/parser/definition.js.map +1 -1
- package/out/parser/generic.d.ts +5 -7
- package/out/parser/generic.js +7 -11
- package/out/parser/generic.js.map +1 -1
- package/out/parser/objectLiteral.d.ts +45 -14
- package/out/parser/objectLiteral.js +61 -52
- package/out/parser/objectLiteral.js.map +1 -1
- package/out/parser/semantic/bounds.d.ts +10 -8
- package/out/parser/semantic/bounds.js +0 -1
- package/out/parser/semantic/bounds.js.map +1 -1
- package/out/parser/semantic/divisor.d.ts +5 -7
- package/out/parser/semantic/divisor.js +0 -1
- package/out/parser/semantic/infer.d.ts +24 -0
- package/out/parser/semantic/infer.js +1 -0
- package/out/parser/semantic/infer.js.map +1 -0
- package/out/parser/semantic/utils.d.ts +3 -7
- package/out/parser/semantic/utils.js +0 -1
- package/out/parser/semantic/validate.d.ts +15 -14
- package/out/parser/semantic/validate.js +1 -1
- package/out/parser/semantic/validate.js.map +1 -1
- package/out/parser/string/reduce/dynamic.d.ts +13 -13
- package/out/parser/string/reduce/dynamic.js +31 -26
- package/out/parser/string/reduce/dynamic.js.map +1 -1
- package/out/parser/string/reduce/shared.d.ts +6 -20
- package/out/parser/string/reduce/shared.js +0 -1
- package/out/parser/string/reduce/shared.js.map +1 -1
- package/out/parser/string/reduce/static.d.ts +27 -24
- package/out/parser/string/reduce/static.js +0 -1
- package/out/parser/string/shift/operand/date.d.ts +3 -3
- package/out/parser/string/shift/operand/date.js +4 -7
- package/out/parser/string/shift/operand/date.js.map +1 -1
- package/out/parser/string/shift/operand/enclosed.d.ts +4 -19
- package/out/parser/string/shift/operand/enclosed.js +12 -13
- package/out/parser/string/shift/operand/enclosed.js.map +1 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
- package/out/parser/string/shift/operand/genericArgs.js +6 -10
- package/out/parser/string/shift/operand/genericArgs.js.map +1 -1
- package/out/parser/string/shift/operand/operand.d.ts +3 -4
- package/out/parser/string/shift/operand/operand.js +8 -12
- package/out/parser/string/shift/operand/operand.js.map +1 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts +15 -22
- package/out/parser/string/shift/operand/unenclosed.js +24 -36
- package/out/parser/string/shift/operand/unenclosed.js.map +1 -1
- package/out/parser/string/shift/operator/bounds.d.ts +9 -13
- package/out/parser/string/shift/operator/bounds.js +43 -54
- package/out/parser/string/shift/operator/bounds.js.map +1 -1
- package/out/parser/string/shift/operator/divisor.d.ts +2 -4
- package/out/parser/string/shift/operator/divisor.js +1 -3
- package/out/parser/string/shift/operator/divisor.js.map +1 -1
- package/out/parser/string/shift/operator/operator.d.ts +2 -3
- package/out/parser/string/shift/operator/operator.js +12 -19
- package/out/parser/string/shift/operator/operator.js.map +1 -1
- package/out/parser/string/shift/scanner.d.ts +2 -2
- package/out/parser/string/shift/scanner.js +14 -16
- package/out/parser/string/shift/scanner.js.map +1 -1
- package/out/parser/string/string.d.ts +8 -6
- package/out/parser/string/string.js +1 -3
- package/out/parser/string/string.js.map +1 -1
- package/out/parser/tuple.d.ts +102 -43
- package/out/parser/tuple.js +119 -87
- package/out/parser/tuple.js.map +1 -1
- package/out/scope.d.ts +43 -120
- package/out/scope.js +39 -209
- package/out/scope.js.map +1 -1
- package/out/type.d.ts +43 -64
- package/out/type.js +30 -131
- package/out/type.js.map +1 -1
- package/package.json +7 -3
- package/parser/definition.ts +77 -93
- package/parser/generic.ts +38 -40
- package/parser/objectLiteral.ts +172 -136
- package/parser/semantic/bounds.ts +33 -32
- package/parser/semantic/divisor.ts +8 -10
- package/parser/semantic/infer.ts +141 -0
- package/parser/semantic/utils.ts +12 -23
- package/parser/semantic/validate.ts +81 -73
- package/parser/string/reduce/dynamic.ts +67 -55
- package/parser/string/reduce/shared.ts +1 -3
- package/parser/string/reduce/static.ts +67 -69
- package/parser/string/shift/operand/date.ts +15 -14
- package/parser/string/shift/operand/enclosed.ts +30 -25
- package/parser/string/shift/operand/genericArgs.ts +46 -45
- package/parser/string/shift/operand/operand.ts +32 -38
- package/parser/string/shift/operand/unenclosed.ts +107 -158
- package/parser/string/shift/operator/bounds.ts +118 -112
- package/parser/string/shift/operator/divisor.ts +14 -17
- package/parser/string/shift/operator/operator.ts +35 -46
- package/parser/string/shift/scanner.ts +51 -50
- package/parser/string/string.ts +30 -35
- package/parser/tuple.ts +375 -307
- package/scope.ts +170 -453
- package/type.ts +193 -274
- package/__tests__/astUtils.test.ts +0 -17
- package/__tests__/cyclic.test.ts +0 -144
- package/__tests__/jsdoc.test.ts +0 -64
- package/__tests__/morph.test.ts +0 -409
- package/__tests__/nodeDefinitions.test.ts +0 -190
- package/__tests__/typeMethods.test.ts +0 -69
- package/out/ark.d.ts.map +0 -1
- package/out/arkConfig.d.ts +0 -2
- package/out/arkConfig.d.ts.map +0 -1
- package/out/arkConfig.js +0 -10
- package/out/arkConfig.js.map +0 -1
- package/out/main.d.ts.map +0 -1
- package/out/match.d.ts.map +0 -1
- package/out/parser/definition.d.ts.map +0 -1
- package/out/parser/generic.d.ts.map +0 -1
- package/out/parser/objectLiteral.d.ts.map +0 -1
- package/out/parser/semantic/bounds.d.ts.map +0 -1
- package/out/parser/semantic/divisor.d.ts.map +0 -1
- package/out/parser/semantic/intersections.d.ts +0 -14
- package/out/parser/semantic/intersections.d.ts.map +0 -1
- package/out/parser/semantic/intersections.js +0 -2
- package/out/parser/semantic/intersections.js.map +0 -1
- package/out/parser/semantic/semantic.d.ts +0 -33
- package/out/parser/semantic/semantic.d.ts.map +0 -1
- package/out/parser/semantic/semantic.js +0 -2
- package/out/parser/semantic/semantic.js.map +0 -1
- package/out/parser/semantic/utils.d.ts.map +0 -1
- package/out/parser/semantic/validate.d.ts.map +0 -1
- package/out/parser/shared.d.ts +0 -54
- package/out/parser/shared.d.ts.map +0 -1
- package/out/parser/shared.js +0 -47
- package/out/parser/shared.js.map +0 -1
- package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
- package/out/parser/string/reduce/shared.d.ts.map +0 -1
- package/out/parser/string/reduce/static.d.ts.map +0 -1
- package/out/parser/string/shift/operand/date.d.ts.map +0 -1
- package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
- package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
- package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
- package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
- package/out/parser/string/shift/scanner.d.ts.map +0 -1
- package/out/parser/string/string.d.ts.map +0 -1
- package/out/parser/tuple.d.ts.map +0 -1
- package/out/scope.d.ts.map +0 -1
- package/out/test.d.ts +0 -2
- package/out/test.d.ts.map +0 -1
- package/out/test.js +0 -7
- package/out/test.js.map +0 -1
- package/out/tsconfig.build.tsbuildinfo +0 -1
- package/out/type.d.ts.map +0 -1
- package/out/util.d.ts +0 -19
- package/out/util.d.ts.map +0 -1
- package/out/util.js +0 -7
- package/out/util.js.map +0 -1
- package/parser/semantic/intersections.ts +0 -46
- package/parser/semantic/semantic.ts +0 -144
- package/parser/shared.ts +0 -152
- package/util.ts +0 -36
package/__tests__/cast.test.ts
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
2
2
|
import type { Constructor } from "@arktype/util"
|
|
3
3
|
import { type, type Type } from "arktype"
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
contextualize(() => {
|
|
6
6
|
it("primitive", () => {
|
|
7
7
|
attest<Type<"foo">>(type("string" as type.cast<"foo">))
|
|
8
8
|
})
|
|
9
|
+
|
|
9
10
|
it("object", () => {
|
|
10
11
|
// definitions that are cast can't be validated
|
|
11
12
|
attest<Type<{ a: "foo" }>>(type({ a: "string" } as type.cast<{ a: "foo" }>))
|
|
12
13
|
})
|
|
14
|
+
|
|
13
15
|
it("primitive to object", () => {
|
|
14
16
|
attest<Type<{ a: "foo" }>>(type("string" as type.cast<{ a: "foo" }>))
|
|
15
17
|
})
|
|
18
|
+
|
|
16
19
|
it("object to primitive", () => {
|
|
17
20
|
attest<Type<"foo">>(type({ a: "string" } as type.cast<"foo">))
|
|
18
21
|
})
|
|
22
|
+
|
|
19
23
|
it("infer function", () => {
|
|
20
24
|
type F = () => boolean
|
|
21
25
|
const constructable = type({} as type.cast<F>)
|
|
@@ -23,6 +27,7 @@ describe("cast", () => {
|
|
|
23
27
|
attest<F>(constructable.infer)
|
|
24
28
|
attest<F>(constructable.in.infer)
|
|
25
29
|
})
|
|
30
|
+
|
|
26
31
|
it("infer constructable", () => {
|
|
27
32
|
const constructable = type({} as type.cast<Constructor>)
|
|
28
33
|
attest<Type<Constructor>>(constructable)
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
2
2
|
import { scope, type } from "arktype"
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
contextualize(() => {
|
|
5
5
|
it("completes standalone keyword", () => {
|
|
6
6
|
// @ts-expect-error
|
|
7
|
-
attest(() => type("s")).completions({
|
|
7
|
+
attest(() => type("s")).completions({
|
|
8
|
+
s: ["string", "symbol", "semver"]
|
|
9
|
+
})
|
|
8
10
|
})
|
|
11
|
+
|
|
9
12
|
it("completes within objects", () => {
|
|
10
13
|
// @ts-expect-error
|
|
11
14
|
attest(() => type({ a: "a", b: "b" })).completions({
|
|
@@ -13,12 +16,14 @@ describe("completions", () => {
|
|
|
13
16
|
b: ["bigint", "boolean"]
|
|
14
17
|
})
|
|
15
18
|
})
|
|
19
|
+
|
|
16
20
|
it("completes within expressions", () => {
|
|
17
21
|
// @ts-expect-error
|
|
18
22
|
attest(() => type("string|n")).completions({
|
|
19
23
|
"string|n": ["string|number", "string|null", "string|never"]
|
|
20
24
|
})
|
|
21
25
|
})
|
|
26
|
+
|
|
22
27
|
it("completes user-defined aliases", () => {
|
|
23
28
|
const $ = scope({
|
|
24
29
|
over9000: "number>9000",
|
package/__tests__/config.test.ts
CHANGED
|
@@ -1,168 +1,96 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
// it("anonymous type config", () => {
|
|
97
|
-
// const t = type(type("true", { mustBe: "unfalse" }))
|
|
98
|
-
// attest<true>(t.infer)
|
|
99
|
-
// // attest(t.flat).snap([
|
|
100
|
-
// // [
|
|
101
|
-
// // "config",
|
|
102
|
-
// // { config: [["mustBe", "unfalse"]], node: [["value", true]] }
|
|
103
|
-
// // ]
|
|
104
|
-
// // ])
|
|
105
|
-
// attest(t(false).errors?.summary).snap("Must be unfalse (was false)")
|
|
106
|
-
// })
|
|
107
|
-
// it("anonymous type config at path", () => {
|
|
108
|
-
// const unfalse = type("true", { mustBe: "unfalse" })
|
|
109
|
-
// const t = type({ myKey: unfalse })
|
|
110
|
-
// // attest(t.flat).snap([
|
|
111
|
-
// // ["domain", "object"],
|
|
112
|
-
// // [
|
|
113
|
-
// // "requiredProp",
|
|
114
|
-
// // [
|
|
115
|
-
// // "myKey",
|
|
116
|
-
// // [
|
|
117
|
-
// // [
|
|
118
|
-
// // "config",
|
|
119
|
-
// // {
|
|
120
|
-
// // config: [["mustBe", "unfalse"]],
|
|
121
|
-
// // node: [["value", true]]
|
|
122
|
-
// // }
|
|
123
|
-
// // ]
|
|
124
|
-
// // ]
|
|
125
|
-
// // ]
|
|
126
|
-
// // ]
|
|
127
|
-
// // ])
|
|
128
|
-
// attest(t({ myKey: "500" }).errors?.summary).snap(
|
|
129
|
-
// "myKey must be unfalse (was '500')"
|
|
130
|
-
// )
|
|
131
|
-
// // config only applies within myKey
|
|
132
|
-
// attest(t({ yourKey: "500" }).errors?.summary).snap(
|
|
133
|
-
// "myKey must be defined"
|
|
134
|
-
// )
|
|
135
|
-
// })
|
|
136
|
-
// it("anonymous type thunk", () => {
|
|
137
|
-
// const t = type(() => type("false", { mustBe: "untrue" }))
|
|
138
|
-
// attest<false>(t.infer)
|
|
139
|
-
// // attest(t.flat).snap([
|
|
140
|
-
// // [
|
|
141
|
-
// // "config",
|
|
142
|
-
// // { config: [["mustBe", "untrue"]], node: [["value", false]] }
|
|
143
|
-
// // ]
|
|
144
|
-
// // ])
|
|
145
|
-
// })
|
|
146
|
-
// it("anonymous type thunk at path", () => {
|
|
147
|
-
// const t = type({ myKey: () => type("false", { mustBe: "untrue" }) })
|
|
148
|
-
// attest<{ myKey: false }>(t.infer)
|
|
149
|
-
// // attest(t.flat).snap([
|
|
150
|
-
// // ["domain", "object"],
|
|
151
|
-
// // [
|
|
152
|
-
// // "requiredProp",
|
|
153
|
-
// // [
|
|
154
|
-
// // "myKey",
|
|
155
|
-
// // [
|
|
156
|
-
// // [
|
|
157
|
-
// // "config",
|
|
158
|
-
// // {
|
|
159
|
-
// // config: [["mustBe", "untrue"]],
|
|
160
|
-
// // node: [["value", false]]
|
|
161
|
-
// // }
|
|
162
|
-
// // ]
|
|
163
|
-
// // ]
|
|
164
|
-
// // ]
|
|
165
|
-
// // ]
|
|
166
|
-
// // ])
|
|
167
|
-
// })
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
2
|
+
import { scope, type } from "arktype"
|
|
3
|
+
|
|
4
|
+
contextualize(() => {
|
|
5
|
+
it("tuple expression", () => {
|
|
6
|
+
const description = "a series of characters"
|
|
7
|
+
const types = scope({
|
|
8
|
+
a: ["string", "@", description],
|
|
9
|
+
b: {
|
|
10
|
+
a: "a"
|
|
11
|
+
}
|
|
12
|
+
}).export()
|
|
13
|
+
attest<string>(types.a.infer)
|
|
14
|
+
attest(types.a.description).equals(description)
|
|
15
|
+
attest(types.a(1).toString()).snap(
|
|
16
|
+
"must be a series of characters (was number)"
|
|
17
|
+
)
|
|
18
|
+
attest<{ a: string }>(types.b.infer)
|
|
19
|
+
attest(types.b({ a: true }).toString()).snap(
|
|
20
|
+
"a must be a series of characters (was true)"
|
|
21
|
+
)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it("tuple expression at path", () => {
|
|
25
|
+
const description = "the number of dimensions in the monster group"
|
|
26
|
+
const t = type({
|
|
27
|
+
monster: ["196883", "@", description]
|
|
28
|
+
})
|
|
29
|
+
attest<{ monster: 196883 }>(t.infer)
|
|
30
|
+
attest(t.description).snap(
|
|
31
|
+
"{ monster: the number of dimensions in the monster group }"
|
|
32
|
+
)
|
|
33
|
+
attest(t({ monster: 196882 }).toString()).snap(
|
|
34
|
+
"monster must be the number of dimensions in the monster group (was 196882)"
|
|
35
|
+
)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it("anonymous type config", () => {
|
|
39
|
+
const t = type(type("true", "@", { description: "unfalse" }))
|
|
40
|
+
attest<true>(t.infer)
|
|
41
|
+
attest(t(false).toString()).snap("must be unfalse (was false)")
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it("anonymous type config at path", () => {
|
|
45
|
+
const unfalse = type("true", "@", { description: "unfalse" })
|
|
46
|
+
const t = type({ myKey: unfalse })
|
|
47
|
+
attest(t({ myKey: "500" }).toString()).snap(
|
|
48
|
+
`myKey must be unfalse (was "500")`
|
|
49
|
+
)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it("anonymous type thunk", () => {
|
|
53
|
+
const t = type(() => type("false", "@", { description: "untrue" }))
|
|
54
|
+
attest<false>(t.infer)
|
|
55
|
+
attest(t.description).snap("untrue")
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it("anonymous type thunk at path", () => {
|
|
59
|
+
const t = type({
|
|
60
|
+
myKey: () => type("false", "@", { description: "untrue" })
|
|
61
|
+
})
|
|
62
|
+
attest<{ myKey: false }>(t.infer)
|
|
63
|
+
attest(t({ myKey: true }).toString()).snap(
|
|
64
|
+
"myKey must be untrue (was true)"
|
|
65
|
+
)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it("applies config to shallow descendants", () => {
|
|
69
|
+
const user = type({
|
|
70
|
+
name: "string",
|
|
71
|
+
age: "number"
|
|
72
|
+
}).describe("a valid user")
|
|
73
|
+
|
|
74
|
+
// should give the original error at a path
|
|
75
|
+
attest(
|
|
76
|
+
user({
|
|
77
|
+
name: "david",
|
|
78
|
+
age: true
|
|
79
|
+
}).toString()
|
|
80
|
+
).snap("age must be a number (was true)")
|
|
81
|
+
|
|
82
|
+
// should give the shallow custom error
|
|
83
|
+
attest(user(null).toString()).snap("must be a valid user (was null)")
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it("jitless", () => {
|
|
87
|
+
const types = scope(
|
|
88
|
+
{
|
|
89
|
+
fast: "false"
|
|
90
|
+
},
|
|
91
|
+
{ jitless: true }
|
|
92
|
+
).export()
|
|
93
|
+
|
|
94
|
+
attest(types.fast.raw.jit).equals(false)
|
|
95
|
+
})
|
|
168
96
|
})
|
|
@@ -1,31 +1,35 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
2
2
|
import { type } from "arktype"
|
|
3
3
|
import { writeInvalidDateMessage } from "../parser/string/shift/operand/date.js"
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
contextualize(() => {
|
|
6
6
|
it("base", () => {
|
|
7
7
|
const t = type("d'2000/05/05'")
|
|
8
8
|
attest<Date>(t.infer)
|
|
9
9
|
attest(t.allows(new Date("2000/05/05"))).equals(true)
|
|
10
10
|
attest(t.allows(new Date("2000/06/05"))).equals(false)
|
|
11
|
-
attest(t.allows(new Date("2000
|
|
11
|
+
attest(t.allows(new Date("2000-05-05T09:00:00.000Z"))).equals(false)
|
|
12
12
|
})
|
|
13
|
+
|
|
13
14
|
it("with punctuation", () => {
|
|
14
15
|
const ISO = type("d'2000-05-05T04:00:00.000Z'")
|
|
15
16
|
attest<Date>(ISO.infer)
|
|
16
17
|
attest(ISO.allows(new Date("2000/05/05"))).equals(true)
|
|
17
18
|
attest(ISO.allows(new Date("2000/07/05"))).equals(false)
|
|
18
19
|
})
|
|
20
|
+
|
|
19
21
|
it("allows spaces", () => {
|
|
20
22
|
const t = type("d' 2021 / 05 / 01 '")
|
|
21
23
|
attest(t.allows(new Date("2021/05/01"))).equals(true)
|
|
22
24
|
})
|
|
25
|
+
|
|
23
26
|
it("epoch", () => {
|
|
24
27
|
const now = new Date()
|
|
25
28
|
const t = type(`d'${now.valueOf()}'`)
|
|
26
29
|
attest(t.allows(now)).equals(true)
|
|
27
|
-
attest(t.allows(new Date(now.valueOf() + 1)))
|
|
30
|
+
attest(t.allows(new Date(now.valueOf() + 1))).equals(false)
|
|
28
31
|
})
|
|
32
|
+
|
|
29
33
|
it("invalid date", () => {
|
|
30
34
|
attest(() => type("d'tuesday'")).throws(writeInvalidDateMessage("tuesday"))
|
|
31
35
|
})
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
2
2
|
import { declare, type } from "arktype"
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
contextualize(() => {
|
|
5
5
|
it("shallow", () => {
|
|
6
6
|
const shallow = declare<number>().type("number")
|
|
7
7
|
attest<number>(shallow.infer)
|
|
@@ -51,10 +51,11 @@ describe("declared", () => {
|
|
|
51
51
|
attest<0 | 1>(t.infer)
|
|
52
52
|
})
|
|
53
53
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
// TODO: figure out narrowing
|
|
55
|
+
// it("regexp", () => {
|
|
56
|
+
// const t = declare<string>().type(/.*/)
|
|
57
|
+
// attest<string>(t.infer)
|
|
58
|
+
// })
|
|
58
59
|
|
|
59
60
|
it("Inferred<t>", () => {
|
|
60
61
|
const foo = type("'foo'")
|
|
@@ -52,7 +52,7 @@ describe("discrimination", () => {
|
|
|
52
52
|
// duration: "'s' | 'min' | 'h'",
|
|
53
53
|
// operator: "'to'"
|
|
54
54
|
// })
|
|
55
|
-
// attest(t.
|
|
55
|
+
// attest(t.hasKind("union") && t.discriminant).equals(null)
|
|
56
56
|
// })
|
|
57
57
|
// it("default case", () => {
|
|
58
58
|
// const t = getPlaces().type([
|
|
@@ -1,90 +1,114 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
2
|
-
import { writeIndivisibleMessage } from "@arktype/schema"
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
2
|
+
import { keywordNodes, writeIndivisibleMessage } from "@arktype/schema"
|
|
3
3
|
import { type } from "arktype"
|
|
4
4
|
import { writeInvalidDivisorMessage } from "../parser/string/shift/operator/divisor.js"
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
contextualize(
|
|
7
|
+
"parse",
|
|
8
|
+
() => {
|
|
8
9
|
it("integer literal", () => {
|
|
9
10
|
const divisibleByTwo = type("number%2")
|
|
10
11
|
attest<number>(divisibleByTwo.infer)
|
|
11
|
-
attest(divisibleByTwo.json).snap({
|
|
12
|
+
attest(divisibleByTwo.json).snap({ domain: "number", divisor: 2 })
|
|
12
13
|
})
|
|
14
|
+
|
|
15
|
+
it("chained", () => {
|
|
16
|
+
const t = type("number").constrain("divisor", 2)
|
|
17
|
+
const expected = type("number%2")
|
|
18
|
+
attest<typeof expected>(t)
|
|
19
|
+
attest(t.json).equals(expected.json)
|
|
20
|
+
})
|
|
21
|
+
|
|
13
22
|
it("whitespace after %", () => {
|
|
14
23
|
const t = type("number % 5")
|
|
15
24
|
attest<number>(t.infer)
|
|
16
|
-
attest(t.json).snap({
|
|
25
|
+
attest(t.json).snap({ domain: "number", divisor: 5 })
|
|
17
26
|
})
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
27
|
+
|
|
28
|
+
it("with bounds", () => {
|
|
29
|
+
const t = type("7<number%8<222")
|
|
30
|
+
const expected = type("number%8").and("7<number<222")
|
|
31
|
+
attest(t.json).equals(expected.json)
|
|
32
|
+
attest(t.description).snap(
|
|
33
|
+
"a number and a multiple of 8 and less than 222 and more than 7"
|
|
23
34
|
)
|
|
24
35
|
})
|
|
36
|
+
|
|
25
37
|
it("allows non-narrowed divisor", () => {
|
|
26
38
|
const d = 5 as number
|
|
27
39
|
attest<number>(type(`number%${d}`).infer)
|
|
28
40
|
})
|
|
41
|
+
|
|
29
42
|
it("fails at runtime on non-integer divisor", () => {
|
|
30
43
|
attest(() => type("number%2.3")).throws(writeInvalidDivisorMessage("2.3"))
|
|
31
44
|
})
|
|
45
|
+
|
|
32
46
|
it("non-numeric divisor", () => {
|
|
33
47
|
// @ts-expect-error
|
|
34
48
|
attest(() => type("number%foobar")).throwsAndHasTypeError(
|
|
35
49
|
writeInvalidDivisorMessage("foobar")
|
|
36
50
|
)
|
|
37
51
|
})
|
|
52
|
+
|
|
38
53
|
it("zero divisor", () => {
|
|
39
54
|
// @ts-expect-error
|
|
40
55
|
attest(() => type("number%0")).throwsAndHasTypeError(
|
|
41
56
|
writeInvalidDivisorMessage(0)
|
|
42
57
|
)
|
|
43
58
|
})
|
|
59
|
+
|
|
44
60
|
it("unknown", () => {
|
|
45
61
|
// @ts-expect-error
|
|
46
62
|
attest(() => type("unknown%2")).throwsAndHasTypeError(
|
|
47
|
-
writeIndivisibleMessage(
|
|
63
|
+
writeIndivisibleMessage(keywordNodes.unknown)
|
|
48
64
|
)
|
|
49
65
|
})
|
|
66
|
+
|
|
50
67
|
it("indivisible", () => {
|
|
51
68
|
// @ts-expect-error
|
|
52
69
|
attest(() => type("string%1")).throwsAndHasTypeError(
|
|
53
|
-
writeIndivisibleMessage(
|
|
70
|
+
writeIndivisibleMessage(keywordNodes.string)
|
|
54
71
|
)
|
|
55
72
|
})
|
|
73
|
+
|
|
56
74
|
it("overlapping", () => {
|
|
57
75
|
// @ts-expect-error
|
|
58
|
-
attest(() => type("(number|string)%10"))
|
|
59
|
-
.
|
|
60
|
-
|
|
76
|
+
attest(() => type("(number|string)%10")).throwsAndHasTypeError(
|
|
77
|
+
writeIndivisibleMessage(keywordNodes.number.or(keywordNodes.string))
|
|
78
|
+
)
|
|
61
79
|
})
|
|
62
|
-
}
|
|
63
|
-
|
|
80
|
+
},
|
|
81
|
+
"intersection",
|
|
82
|
+
() => {
|
|
64
83
|
it("identical", () => {
|
|
65
84
|
const t = type("number%2&number%2")
|
|
66
85
|
attest(t.json).equals(type("number%2").json)
|
|
67
86
|
})
|
|
87
|
+
|
|
68
88
|
it("purely divisible", () => {
|
|
69
89
|
const t = type("number%4&number%2")
|
|
70
90
|
attest(t.json).equals(type("number%4").json)
|
|
71
91
|
})
|
|
92
|
+
|
|
72
93
|
it("common divisor", () => {
|
|
73
94
|
const t = type("number%6&number%4")
|
|
74
95
|
attest(t.json).equals(type("number%12").json)
|
|
75
96
|
})
|
|
97
|
+
|
|
76
98
|
it("relatively prime", () => {
|
|
77
99
|
const t = type("number%2&number%3")
|
|
78
100
|
attest(t.json).equals(type("number%6").json)
|
|
79
101
|
})
|
|
102
|
+
|
|
80
103
|
it("valid literal", () => {
|
|
81
104
|
const t = type("number%5&0")
|
|
82
105
|
attest(t.json).equals(type("0").json)
|
|
83
106
|
})
|
|
107
|
+
|
|
84
108
|
it("invalid literal", () => {
|
|
85
|
-
attest(() => type("number%3&8")).throws(
|
|
86
|
-
"Intersection of
|
|
109
|
+
attest(() => type("number%3&8")).throws.snap(
|
|
110
|
+
"ParseError: Intersection of 8 and number & % 3 results in an unsatisfiable type"
|
|
87
111
|
)
|
|
88
112
|
})
|
|
89
|
-
}
|
|
90
|
-
|
|
113
|
+
}
|
|
114
|
+
)
|
|
@@ -1,68 +1,82 @@
|
|
|
1
|
-
import { attest } from "@arktype/attest"
|
|
1
|
+
import { attest, contextualize } from "@arktype/attest"
|
|
2
2
|
import { type } from "arktype"
|
|
3
3
|
import { writeUnterminatedEnclosedMessage } from "../parser/string/shift/operand/enclosed.js"
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
contextualize(() => {
|
|
6
6
|
it("with spaces", () => {
|
|
7
7
|
const t = type("'this has spaces'")
|
|
8
8
|
attest<"this has spaces">(t.infer)
|
|
9
9
|
attest(t.json).snap({ unit: "this has spaces" })
|
|
10
10
|
})
|
|
11
|
+
|
|
11
12
|
it("with neighbors", () => {
|
|
12
13
|
const t = type("'foo'|/.*/[]")
|
|
13
14
|
attest<"foo" | string[]>(t.infer)
|
|
14
|
-
attest(t.json).snap([
|
|
15
|
+
attest(t.json).snap([
|
|
16
|
+
{ proto: "Array", sequence: { domain: "string", regex: [".*"] } },
|
|
17
|
+
{ unit: "foo" }
|
|
18
|
+
])
|
|
15
19
|
})
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
})
|
|
20
|
+
|
|
21
|
+
it("unterminated regex", () => {
|
|
22
|
+
// @ts-expect-error
|
|
23
|
+
attest(() => type("/.*")).throwsAndHasTypeError(
|
|
24
|
+
writeUnterminatedEnclosedMessage(".*", "/")
|
|
25
|
+
)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it("unterminated single-quote", () => {
|
|
29
|
+
// @ts-expect-error
|
|
30
|
+
attest(() => type("'.*")).throwsAndHasTypeError(
|
|
31
|
+
writeUnterminatedEnclosedMessage(".*", "'")
|
|
32
|
+
)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it("unterminated double-quote", () => {
|
|
36
|
+
// @ts-expect-error
|
|
37
|
+
attest(() => type('".*')).throwsAndHasTypeError(
|
|
38
|
+
writeUnterminatedEnclosedMessage(".*", '"')
|
|
39
|
+
)
|
|
37
40
|
})
|
|
41
|
+
|
|
38
42
|
it("single-quoted", () => {
|
|
39
|
-
|
|
43
|
+
const t = type("'hello'")
|
|
44
|
+
attest<"hello">(t.infer)
|
|
45
|
+
attest(t.json).snap({ unit: "hello" })
|
|
40
46
|
})
|
|
47
|
+
|
|
41
48
|
it("double-quoted", () => {
|
|
42
49
|
attest<"goodbye">(type('"goodbye"').infer)
|
|
43
50
|
})
|
|
51
|
+
|
|
44
52
|
it("regex literal", () => {
|
|
45
53
|
attest<string>(type("/.*/").infer)
|
|
46
54
|
})
|
|
55
|
+
|
|
47
56
|
it("invalid regex", () => {
|
|
48
57
|
attest(() => type("/[/")).throws(
|
|
49
58
|
"Invalid regular expression: /[/: Unterminated character class"
|
|
50
59
|
)
|
|
51
60
|
})
|
|
61
|
+
|
|
52
62
|
it("mixed quote types", () => {
|
|
53
63
|
attest<"'single-quoted'">(type(`"'single-quoted'"`).infer)
|
|
54
64
|
attest<'"double-quoted"'>(type(`'"double-quoted"'`).infer)
|
|
55
65
|
})
|
|
66
|
+
|
|
56
67
|
it("ignores enclosed operators", () => {
|
|
57
68
|
attest<"yes|no|maybe">(type("'yes|no|maybe'").infer)
|
|
58
69
|
})
|
|
70
|
+
|
|
59
71
|
it("mix of enclosed and unenclosed operators", () => {
|
|
60
72
|
attest<"yes|no" | "true|false">(type("'yes|no'|'true|false'").infer)
|
|
61
73
|
})
|
|
74
|
+
|
|
62
75
|
it("escaped enclosing", () => {
|
|
63
76
|
const t = type("'don\\'t'")
|
|
64
77
|
attest<"don't">(t.infer)
|
|
65
78
|
})
|
|
79
|
+
|
|
66
80
|
it("string literal stress", () => {
|
|
67
81
|
const s = `"3.
|
|
68
82
|
14159265358979323846264338327950288419716939937510
|