arktype 2.0.0-dev.1 → 2.0.0-dev.2
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 +42 -4
- package/__tests__/config.test.ts +75 -166
- package/__tests__/declared.test.ts +5 -4
- package/__tests__/keyTraversal.test.ts +71 -70
- package/__tests__/match.bench.ts +31 -0
- package/__tests__/match.test.ts +216 -26
- package/__tests__/narrow.test.ts +3 -3
- package/__tests__/objectLiteral.test.ts +19 -18
- package/match.ts +206 -79
- package/out/match.d.ts +60 -31
- package/out/match.d.ts.map +1 -1
- package/out/match.js +52 -10
- package/out/match.js.map +1 -1
- package/out/parser/definition.d.ts +6 -4
- package/out/parser/definition.d.ts.map +1 -1
- package/out/parser/definition.js.map +1 -1
- package/out/parser/objectLiteral.js +2 -2
- package/out/parser/objectLiteral.js.map +1 -1
- package/out/parser/semantic/semantic.d.ts +3 -3
- package/out/parser/semantic/semantic.d.ts.map +1 -1
- package/out/parser/shared.d.ts +4 -2
- package/out/parser/shared.d.ts.map +1 -1
- package/out/parser/shared.js +8 -0
- package/out/parser/shared.js.map +1 -1
- package/out/parser/string/reduce/static.d.ts +1 -1
- package/out/parser/string/reduce/static.d.ts.map +1 -1
- package/out/parser/tuple.d.ts +2 -2
- package/out/parser/tuple.d.ts.map +1 -1
- package/out/parser/tuple.js +14 -15
- package/out/parser/tuple.js.map +1 -1
- package/out/scope.d.ts +6 -11
- package/out/scope.d.ts.map +1 -1
- package/out/scope.js +0 -21
- package/out/scope.js.map +1 -1
- package/out/type.d.ts +5 -8
- package/out/type.d.ts.map +1 -1
- package/out/type.js +12 -8
- package/out/type.js.map +1 -1
- package/package.json +3 -3
- package/parser/definition.ts +7 -7
- package/parser/objectLiteral.ts +6 -6
- package/parser/semantic/semantic.ts +3 -4
- package/parser/shared.ts +19 -4
- package/parser/string/reduce/static.ts +2 -2
- package/parser/tuple.ts +18 -17
- package/scope.ts +5 -31
- package/type.ts +16 -14
package/__tests__/array.test.ts
CHANGED
|
@@ -3,27 +3,44 @@ import { scope, type } from "arktype"
|
|
|
3
3
|
import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
|
|
4
4
|
import { incompleteArrayTokenMessage } from "../parser/string/shift/operator/operator.js"
|
|
5
5
|
import {
|
|
6
|
-
|
|
6
|
+
multipleVariadicMesage,
|
|
7
7
|
writeNonArrayRestMessage
|
|
8
8
|
} from "../parser/tuple.js"
|
|
9
9
|
|
|
10
10
|
describe("array", () => {
|
|
11
11
|
describe("base", () => {
|
|
12
|
-
it("
|
|
12
|
+
it("allows and apply", () => {
|
|
13
13
|
const t = type("string[]")
|
|
14
14
|
attest<string[]>(t.infer)
|
|
15
15
|
attest(t.allows([])).equals(true)
|
|
16
|
+
attest(t([]).out).snap([])
|
|
16
17
|
attest(t.allows(["foo", "bar"])).equals(true)
|
|
18
|
+
attest(t(["foo", "bar"]).out).snap(["foo", "bar"])
|
|
17
19
|
attest(t.allows(["foo", "bar", 5])).equals(false)
|
|
20
|
+
attest(t(["foo", "bar", 5]).errors?.summary).snap(
|
|
21
|
+
"Value at [2] must be a string (was number)"
|
|
22
|
+
)
|
|
18
23
|
attest(t.allows([5, "foo", "bar"])).equals(false)
|
|
24
|
+
attest(t([5, "foo", "bar"]).errors?.summary).snap(
|
|
25
|
+
"Value at [0] must be a string (was number)"
|
|
26
|
+
)
|
|
19
27
|
})
|
|
28
|
+
|
|
20
29
|
it("nested", () => {
|
|
21
30
|
const t = type("string[][]")
|
|
22
31
|
attest<string[][]>(t.infer)
|
|
23
32
|
attest(t.allows([])).equals(true)
|
|
33
|
+
attest(t([]).out).snap([])
|
|
24
34
|
attest(t.allows([["foo"]])).equals(true)
|
|
35
|
+
attest(t([["foo"]]).out).snap([["foo"]])
|
|
25
36
|
attest(t.allows(["foo"])).equals(false)
|
|
37
|
+
attest(t(["foo"]).errors?.summary).snap(
|
|
38
|
+
"Value at [0] must be an array (was string)"
|
|
39
|
+
)
|
|
26
40
|
attest(t.allows([["foo", 5]])).equals(false)
|
|
41
|
+
attest(t([["foo", 5]]).errors?.summary).snap(
|
|
42
|
+
"Value at [0][1] must be a string (was number)"
|
|
43
|
+
)
|
|
27
44
|
})
|
|
28
45
|
|
|
29
46
|
it("tuple expression", () => {
|
|
@@ -31,6 +48,7 @@ describe("array", () => {
|
|
|
31
48
|
attest<string[]>(t.infer)
|
|
32
49
|
attest(t.json).equals(type("string[]").json)
|
|
33
50
|
})
|
|
51
|
+
|
|
34
52
|
describe("optional tuple literals", () => {
|
|
35
53
|
it("string optional", () => {
|
|
36
54
|
const t = type(["string?"])
|
|
@@ -91,12 +109,17 @@ describe("array", () => {
|
|
|
91
109
|
const t = type(["string", "number"])
|
|
92
110
|
attest<[string, number]>(t.infer)
|
|
93
111
|
attest(t.allows(["", 0])).equals(true)
|
|
112
|
+
attest(t(["", 0]).out).snap()
|
|
94
113
|
attest(t.allows([true, 0])).equals(false)
|
|
114
|
+
attest(t([true, 0]).errors?.summary).snap()
|
|
95
115
|
attest(t.allows([0, false])).equals(false)
|
|
116
|
+
attest(t([0, false]).errors?.summary).snap()
|
|
96
117
|
// too short
|
|
97
118
|
attest(t.allows([""])).equals(false)
|
|
119
|
+
attest(t([""]).errors?.summary).snap()
|
|
98
120
|
// too long
|
|
99
121
|
attest(t.allows(["", 0, 1])).equals(false)
|
|
122
|
+
attest(t(["", 0, 1]).errors?.summary).snap()
|
|
100
123
|
// non-array
|
|
101
124
|
attest(
|
|
102
125
|
t.allows({
|
|
@@ -105,7 +128,15 @@ describe("array", () => {
|
|
|
105
128
|
1: 0
|
|
106
129
|
})
|
|
107
130
|
).equals(false)
|
|
131
|
+
attest(
|
|
132
|
+
t({
|
|
133
|
+
length: 2,
|
|
134
|
+
0: "",
|
|
135
|
+
1: 0
|
|
136
|
+
}).errors?.summary
|
|
137
|
+
).snap()
|
|
108
138
|
})
|
|
139
|
+
|
|
109
140
|
it("nested", () => {
|
|
110
141
|
const t = type([["string", "number"], [{ a: "boolean", b: ["null"] }]])
|
|
111
142
|
attest<
|
|
@@ -121,7 +152,14 @@ describe("array", () => {
|
|
|
121
152
|
>(t.infer)
|
|
122
153
|
|
|
123
154
|
attest(t.allows([["", 0], [{ a: true, b: [null] }]])).equals(true)
|
|
155
|
+
attest(t([["foo", 1], [{ a: true, b: [null] }]]).out).snap([
|
|
156
|
+
["foo", 1],
|
|
157
|
+
[{ a: true, b: [null] }]
|
|
158
|
+
])
|
|
124
159
|
attest(t.allows([["", 0], [{ a: true, b: [undefined] }]])).equals(false)
|
|
160
|
+
attest(
|
|
161
|
+
t([["foo", 1], [{ a: true, b: [undefined] }]]).errors?.summary
|
|
162
|
+
).snap("[1][0].b[0] must be null (was undefined)")
|
|
125
163
|
})
|
|
126
164
|
})
|
|
127
165
|
describe("variadic tuple", () => {
|
|
@@ -171,11 +209,11 @@ describe("array", () => {
|
|
|
171
209
|
attest(() =>
|
|
172
210
|
// @ts-expect-error
|
|
173
211
|
type(["...number[]", "string"])
|
|
174
|
-
).throwsAndHasTypeError(
|
|
212
|
+
).throwsAndHasTypeError(multipleVariadicMesage)
|
|
175
213
|
attest(() =>
|
|
176
214
|
// @ts-expect-error
|
|
177
215
|
type([["...", "string[]"], "number"])
|
|
178
|
-
).throwsAndHasTypeError(
|
|
216
|
+
).throwsAndHasTypeError(multipleVariadicMesage)
|
|
179
217
|
})
|
|
180
218
|
})
|
|
181
219
|
})
|
package/__tests__/config.test.ts
CHANGED
|
@@ -1,168 +1,77 @@
|
|
|
1
|
+
import { attest } from "@arktype/attest"
|
|
2
|
+
import { scope, type } from "arktype"
|
|
3
|
+
|
|
1
4
|
describe("config traversal", () => {
|
|
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
|
-
// // "monster",
|
|
75
|
-
// // [
|
|
76
|
-
// // [
|
|
77
|
-
// // "config",
|
|
78
|
-
// // {
|
|
79
|
-
// // config: [
|
|
80
|
-
// // [
|
|
81
|
-
// // "mustBe",
|
|
82
|
-
// // "the number of dimensions in the monster group"
|
|
83
|
-
// // ]
|
|
84
|
-
// // ],
|
|
85
|
-
// // node: [["value", 196883]]
|
|
86
|
-
// // }
|
|
87
|
-
// // ]
|
|
88
|
-
// // ]
|
|
89
|
-
// // ]
|
|
90
|
-
// // ]
|
|
91
|
-
// // ])
|
|
92
|
-
// attest(t({ monster: 196882 }).errors?.summary).snap(
|
|
93
|
-
// "monster must be the number of dimensions in the monster group (was 196882)"
|
|
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
|
-
// })
|
|
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).errors?.summary).snap(
|
|
16
|
+
"Must be a series of characters (was number)"
|
|
17
|
+
)
|
|
18
|
+
attest<{ a: string }>(types.b.infer)
|
|
19
|
+
attest(types.b({ a: true }).errors?.summary).snap(
|
|
20
|
+
"a must be a series of characters (was boolean)"
|
|
21
|
+
)
|
|
22
|
+
})
|
|
23
|
+
it("tuple expression at path", () => {
|
|
24
|
+
const description = "the number of dimensions in the monster group"
|
|
25
|
+
const t = type({
|
|
26
|
+
monster: ["196883", "@", description]
|
|
27
|
+
})
|
|
28
|
+
attest<{ monster: 196883 }>(t.infer)
|
|
29
|
+
attest(t.description).equals(description)
|
|
30
|
+
attest(t({ monster: 196882 }).errors?.summary).snap(
|
|
31
|
+
"monster must be the number of dimensions in the monster group (was 196882)"
|
|
32
|
+
)
|
|
33
|
+
})
|
|
34
|
+
it("anonymous type config", () => {
|
|
35
|
+
const t = type(type("true", "@", { description: "unfalse" }))
|
|
36
|
+
attest<true>(t.infer)
|
|
37
|
+
attest(t(false).errors?.summary).snap("Must be unfalse (was false)")
|
|
38
|
+
})
|
|
39
|
+
it("anonymous type config at path", () => {
|
|
40
|
+
const unfalse = type("true", "@", { description: "unfalse" })
|
|
41
|
+
const t = type({ myKey: unfalse })
|
|
42
|
+
attest(t({ myKey: "500" }).errors?.summary).snap(
|
|
43
|
+
`myKey must be unfalse (was "500")`
|
|
44
|
+
)
|
|
45
|
+
})
|
|
46
|
+
it("anonymous type thunk", () => {
|
|
47
|
+
const t = type(() => type("false", "@", { description: "untrue" }))
|
|
48
|
+
attest<false>(t.infer)
|
|
49
|
+
attest(t.description).snap("untrue")
|
|
50
|
+
})
|
|
51
|
+
it("anonymous type thunk at path", () => {
|
|
52
|
+
const t = type({
|
|
53
|
+
myKey: () => type("false", "@", { description: "untrue" })
|
|
54
|
+
})
|
|
55
|
+
attest<{ myKey: false }>(t.infer)
|
|
56
|
+
attest(t({ myKey: true }).errors?.summary).snap(
|
|
57
|
+
"myKey must be untrue (was true)"
|
|
58
|
+
)
|
|
59
|
+
})
|
|
60
|
+
it("applies config to shallow descendants", () => {
|
|
61
|
+
const user = type({
|
|
62
|
+
name: "string",
|
|
63
|
+
age: "number"
|
|
64
|
+
}).describe("a valid user")
|
|
65
|
+
|
|
66
|
+
// should give the original error at a path
|
|
67
|
+
attest(
|
|
68
|
+
user({
|
|
69
|
+
name: "david",
|
|
70
|
+
age: true
|
|
71
|
+
}).errors?.summary
|
|
72
|
+
).snap("age must be a number (was boolean)")
|
|
73
|
+
|
|
74
|
+
// should give the shallow custom error
|
|
75
|
+
attest(user(null).errors?.summary).snap("Must be a valid user (was null)")
|
|
76
|
+
})
|
|
168
77
|
})
|
|
@@ -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'")
|
|
@@ -1,73 +1,74 @@
|
|
|
1
1
|
import { attest } from "@arktype/attest"
|
|
2
2
|
import { type } from "arktype"
|
|
3
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
|
-
})
|
|
4
|
+
// TODO: reenable
|
|
5
|
+
// describe("key traversal", () => {
|
|
6
|
+
// const getExtraneousB = () => ({ a: "ok", b: "why?" })
|
|
7
|
+
// it("loose by default", () => {
|
|
8
|
+
// const t = type({
|
|
9
|
+
// a: "string"
|
|
10
|
+
// })
|
|
11
|
+
// const dataWithExtraneousB = getExtraneousB()
|
|
12
|
+
// attest(t(dataWithExtraneousB).out).equals(dataWithExtraneousB)
|
|
13
|
+
// })
|
|
14
|
+
// it("invalid union", () => {
|
|
15
|
+
// const o = type([{ a: "string" }, "|", { b: "boolean" }]).configure({
|
|
16
|
+
// keys: "strict"
|
|
17
|
+
// })
|
|
18
|
+
// attest(o({ a: 2 }).errors?.summary).snap(
|
|
19
|
+
// 'a must be a string or removed (was {"a":2})'
|
|
20
|
+
// )
|
|
21
|
+
// })
|
|
22
|
+
// it("distilled type", () => {
|
|
23
|
+
// const t = type({
|
|
24
|
+
// a: "string"
|
|
25
|
+
// }).configure({ keys: "distilled" })
|
|
26
|
+
// attest(t({ a: "ok" }).out).equals({ a: "ok" })
|
|
27
|
+
// attest(t(getExtraneousB()).out).snap({ a: "ok" })
|
|
28
|
+
// })
|
|
29
|
+
// it("distilled array", () => {
|
|
30
|
+
// const o = type({ a: "email[]" }).configure({
|
|
31
|
+
// keys: "distilled"
|
|
32
|
+
// })
|
|
33
|
+
// attest(o({ a: ["shawn@arktype.io"] }).out).snap({
|
|
34
|
+
// a: ["shawn@arktype.io"]
|
|
35
|
+
// })
|
|
36
|
+
// attest(o({ a: ["notAnEmail"] }).errors?.summary).snap(
|
|
37
|
+
// "a/0 must be a valid email (was 'notAnEmail')"
|
|
38
|
+
// )
|
|
39
|
+
// // can handle missing keys
|
|
40
|
+
// attest(o({ b: ["shawn"] }).errors?.summary).snap("a must be defined")
|
|
41
|
+
// })
|
|
42
|
+
// it("distilled union", () => {
|
|
43
|
+
// const o = type([{ a: "string" }, "|", { b: "boolean" }]).configure({
|
|
44
|
+
// keys: "distilled"
|
|
45
|
+
// })
|
|
46
|
+
// // can distill to first branch
|
|
47
|
+
// attest(o({ a: "to", z: "bra" }).out).snap({ a: "to" })
|
|
48
|
+
// // can distill to second branch
|
|
49
|
+
// attest(o({ b: true, c: false }).out).snap({ b: true })
|
|
50
|
+
// // can handle missing keys
|
|
51
|
+
// attest(o({ a: 2 }).errors?.summary).snap(
|
|
52
|
+
// 'a must be a string or b must be defined (was {"a":2})'
|
|
53
|
+
// )
|
|
54
|
+
// })
|
|
55
|
+
// it("strict type", () => {
|
|
56
|
+
// const t = type({
|
|
57
|
+
// a: "string"
|
|
58
|
+
// }).configure({ keys: "strict" })
|
|
59
|
+
// attest(t({ a: "ok" }).out).equals({ a: "ok" })
|
|
60
|
+
// attest(t(getExtraneousB()).errors?.summary).snap("b must be removed")
|
|
61
|
+
// })
|
|
62
|
+
// it("strict array", () => {
|
|
63
|
+
// const o = type({ a: "string[]" }).configure({
|
|
64
|
+
// keys: "strict"
|
|
65
|
+
// })
|
|
66
|
+
// attest(o({ a: ["shawn"] }).out).snap({ a: ["shawn"] })
|
|
67
|
+
// attest(o({ a: [2] }).errors?.summary).snap(
|
|
68
|
+
// "a/0 must be a string (was number)"
|
|
69
|
+
// )
|
|
70
|
+
// attest(o({ b: ["shawn"] }).errors?.summary).snap(
|
|
71
|
+
// "b must be removed\na must be defined"
|
|
72
|
+
// )
|
|
73
|
+
// })
|
|
74
|
+
// })
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { bench } from "@arktype/attest"
|
|
2
|
+
import { match } from "arktype"
|
|
3
|
+
|
|
4
|
+
bench("general matchers", () => {
|
|
5
|
+
const matcher = match()
|
|
6
|
+
.when("string", (s) => s)
|
|
7
|
+
.when("number", (n) => n)
|
|
8
|
+
.when("boolean", (b) => b)
|
|
9
|
+
.orThrow()
|
|
10
|
+
|
|
11
|
+
const a = matcher("abc")
|
|
12
|
+
const b = matcher(4)
|
|
13
|
+
const c = matcher(true)
|
|
14
|
+
})
|
|
15
|
+
.median()
|
|
16
|
+
.types()
|
|
17
|
+
|
|
18
|
+
bench("match.only<T>", () => {
|
|
19
|
+
const matcher = match
|
|
20
|
+
.only<string | number | boolean>()
|
|
21
|
+
.when("string", (s) => s)
|
|
22
|
+
.when("number", (n) => n)
|
|
23
|
+
.when("boolean", (b) => b)
|
|
24
|
+
.finalize()
|
|
25
|
+
|
|
26
|
+
const a = matcher("abc")
|
|
27
|
+
const b = matcher(4)
|
|
28
|
+
const c = matcher(true)
|
|
29
|
+
})
|
|
30
|
+
.median()
|
|
31
|
+
.types()
|