arktype 2.0.0-dev.0 → 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.
Files changed (47) hide show
  1. package/__tests__/array.test.ts +42 -4
  2. package/__tests__/config.test.ts +75 -166
  3. package/__tests__/declared.test.ts +5 -4
  4. package/__tests__/keyTraversal.test.ts +71 -70
  5. package/__tests__/match.bench.ts +31 -0
  6. package/__tests__/match.test.ts +216 -26
  7. package/__tests__/narrow.test.ts +3 -3
  8. package/__tests__/objectLiteral.test.ts +19 -18
  9. package/match.ts +206 -79
  10. package/out/match.d.ts +60 -31
  11. package/out/match.d.ts.map +1 -1
  12. package/out/match.js +52 -10
  13. package/out/match.js.map +1 -1
  14. package/out/parser/definition.d.ts +6 -4
  15. package/out/parser/definition.d.ts.map +1 -1
  16. package/out/parser/definition.js.map +1 -1
  17. package/out/parser/objectLiteral.js +2 -2
  18. package/out/parser/objectLiteral.js.map +1 -1
  19. package/out/parser/semantic/semantic.d.ts +3 -3
  20. package/out/parser/semantic/semantic.d.ts.map +1 -1
  21. package/out/parser/shared.d.ts +4 -2
  22. package/out/parser/shared.d.ts.map +1 -1
  23. package/out/parser/shared.js +8 -0
  24. package/out/parser/shared.js.map +1 -1
  25. package/out/parser/string/reduce/static.d.ts +1 -1
  26. package/out/parser/string/reduce/static.d.ts.map +1 -1
  27. package/out/parser/tuple.d.ts +2 -2
  28. package/out/parser/tuple.d.ts.map +1 -1
  29. package/out/parser/tuple.js +14 -15
  30. package/out/parser/tuple.js.map +1 -1
  31. package/out/scope.d.ts +6 -11
  32. package/out/scope.d.ts.map +1 -1
  33. package/out/scope.js +0 -21
  34. package/out/scope.js.map +1 -1
  35. package/out/type.d.ts +5 -8
  36. package/out/type.d.ts.map +1 -1
  37. package/out/type.js +12 -8
  38. package/out/type.js.map +1 -1
  39. package/package.json +3 -3
  40. package/parser/definition.ts +7 -7
  41. package/parser/objectLiteral.ts +6 -6
  42. package/parser/semantic/semantic.ts +3 -4
  43. package/parser/shared.ts +19 -4
  44. package/parser/string/reduce/static.ts +2 -2
  45. package/parser/tuple.ts +18 -17
  46. package/scope.ts +5 -31
  47. package/type.ts +16 -14
@@ -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
- prematureRestMessage,
6
+ multipleVariadicMesage,
7
7
  writeNonArrayRestMessage
8
8
  } from "../parser/tuple.js"
9
9
 
10
10
  describe("array", () => {
11
11
  describe("base", () => {
12
- it("base", () => {
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(prematureRestMessage)
212
+ ).throwsAndHasTypeError(multipleVariadicMesage)
175
213
  attest(() =>
176
214
  // @ts-expect-error
177
215
  type([["...", "string[]"], "number"])
178
- ).throwsAndHasTypeError(prematureRestMessage)
216
+ ).throwsAndHasTypeError(multipleVariadicMesage)
179
217
  })
180
218
  })
181
219
  })
@@ -1,168 +1,77 @@
1
+ import { attest } from "@arktype/attest"
2
+ import { scope, type } from "arktype"
3
+
1
4
  describe("config traversal", () => {
2
- // it("tuple expression", () => {
3
- // const mustBe = "a series of characters"
4
- // const types = scope({
5
- // a: ["string", ":", { mustBe }],
6
- // b: {
7
- // a: "a"
8
- // }
9
- // }).compile()
10
- // attest<string>(types.a.infer)
11
- // // attest(types.a.flat).snap([
12
- // // [
13
- // // "config",
14
- // // {
15
- // // config: [["mustBe", "a series of characters"]],
16
- // // node: "string"
17
- // // }
18
- // // ]
19
- // // ])
20
- // attest(types.a(1).errors?.summary).snap(
21
- // "Must be a series of characters (was number)"
22
- // )
23
- // attest<{ a: string }>(types.b.infer)
24
- // // attest(types.b.flat).equals([
25
- // // ["domain", "object"],
26
- // // [
27
- // // "requiredProp",
28
- // // [
29
- // // "a",
30
- // // [
31
- // // [
32
- // // "config",
33
- // // {
34
- // // config: [["mustBe", mustBe]],
35
- // // node: "string"
36
- // // }
37
- // // ]
38
- // // ]
39
- // // ]
40
- // // ]
41
- // // ])
42
- // attest(types.b({ a: true }).errors?.summary).snap(
43
- // "a must be a series of characters (was boolean)"
44
- // )
45
- // })
46
- // it("tuple expression at path", () => {
47
- // const t = type({
48
- // monster: [
49
- // "196883",
50
- // ":",
51
- // {
52
- // mustBe: "the number of dimensions in the monster group"
53
- // }
54
- // ]
55
- // })
56
- // attest<{ monster: 196883 }>(t.infer)
57
- // // attest(t.node).snap({
58
- // // object: {
59
- // // props: {
60
- // // monster: {
61
- // // node: { number: { value: 196883 } },
62
- // // config: {
63
- // // mustBe: "the number of dimensions in the monster group"
64
- // // }
65
- // // }
66
- // // }
67
- // // }
68
- // // })
69
- // // attest(t.flat).snap([
70
- // // ["domain", "object"],
71
- // // [
72
- // // "requiredProp",
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
- it("regexp", () => {
55
- const t = declare<string>().type(/.*/)
56
- attest<string>(t.infer)
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
- describe("key traversal", () => {
5
- const getExtraneousB = () => ({ a: "ok", b: "why?" })
6
- it("loose by default", () => {
7
- const t = type({
8
- a: "string"
9
- })
10
- const dataWithExtraneousB = getExtraneousB()
11
- attest(t(dataWithExtraneousB).out).equals(dataWithExtraneousB)
12
- })
13
- it("invalid union", () => {
14
- const o = type([{ a: "string" }, "|", { b: "boolean" }]).configure({
15
- keys: "strict"
16
- })
17
- attest(o({ a: 2 }).errors?.summary).snap(
18
- 'a must be a string or removed (was {"a":2})'
19
- )
20
- })
21
- it("distilled type", () => {
22
- const t = type({
23
- a: "string"
24
- }).configure({ keys: "distilled" })
25
- attest(t({ a: "ok" }).out).equals({ a: "ok" })
26
- attest(t(getExtraneousB()).out).snap({ a: "ok" })
27
- })
28
- it("distilled array", () => {
29
- const o = type({ a: "email[]" }).configure({
30
- keys: "distilled"
31
- })
32
- attest(o({ a: ["shawn@arktype.io"] }).out).snap({
33
- a: ["shawn@arktype.io"]
34
- })
35
- attest(o({ a: ["notAnEmail"] }).errors?.summary).snap(
36
- "a/0 must be a valid email (was 'notAnEmail')"
37
- )
38
- // can handle missing keys
39
- attest(o({ b: ["shawn"] }).errors?.summary).snap("a must be defined")
40
- })
41
- it("distilled union", () => {
42
- const o = type([{ a: "string" }, "|", { b: "boolean" }]).configure({
43
- keys: "distilled"
44
- })
45
- // can distill to first branch
46
- attest(o({ a: "to", z: "bra" }).out).snap({ a: "to" })
47
- // can distill to second branch
48
- attest(o({ b: true, c: false }).out).snap({ b: true })
49
- // can handle missing keys
50
- attest(o({ a: 2 }).errors?.summary).snap(
51
- 'a must be a string or b must be defined (was {"a":2})'
52
- )
53
- })
54
- it("strict type", () => {
55
- const t = type({
56
- a: "string"
57
- }).configure({ keys: "strict" })
58
- attest(t({ a: "ok" }).out).equals({ a: "ok" })
59
- attest(t(getExtraneousB()).errors?.summary).snap("b must be removed")
60
- })
61
- it("strict array", () => {
62
- const o = type({ a: "string[]" }).configure({
63
- keys: "strict"
64
- })
65
- attest(o({ a: ["shawn"] }).out).snap({ a: ["shawn"] })
66
- attest(o({ a: [2] }).errors?.summary).snap(
67
- "a/0 must be a string (was number)"
68
- )
69
- attest(o({ b: ["shawn"] }).errors?.summary).snap(
70
- "b must be removed\na must be defined"
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()