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.
Files changed (219) hide show
  1. package/__tests__/array.test.ts +270 -151
  2. package/__tests__/badDefinitionType.test.ts +10 -3
  3. package/__tests__/basis.test.ts +6 -2
  4. package/__tests__/bounds.test.ts +282 -352
  5. package/__tests__/cast.test.ts +7 -2
  6. package/__tests__/completions.test.ts +8 -3
  7. package/__tests__/config.test.ts +95 -167
  8. package/__tests__/dateLiteral.test.ts +8 -4
  9. package/__tests__/declared.test.ts +7 -6
  10. package/__tests__/discrimination.test.ts +1 -1
  11. package/__tests__/divisor.test.ts +46 -22
  12. package/__tests__/enclosed.test.ts +39 -25
  13. package/__tests__/examples.test.ts +56 -56
  14. package/__tests__/expressions.test.ts +184 -112
  15. package/__tests__/generic.test.ts +307 -288
  16. package/__tests__/group.test.ts +67 -54
  17. package/__tests__/imports.test.ts +62 -49
  18. package/__tests__/instanceof.test.ts +25 -23
  19. package/__tests__/intersection.test.ts +52 -48
  20. package/__tests__/keyTraversal.test.ts +77 -73
  21. package/__tests__/keyof.test.ts +68 -52
  22. package/__tests__/keywords.test.ts +164 -138
  23. package/__tests__/literal.test.ts +60 -21
  24. package/__tests__/match.bench.ts +31 -0
  25. package/__tests__/match.test.ts +222 -22
  26. package/__tests__/narrow.test.ts +52 -54
  27. package/__tests__/objectLiteral.test.ts +347 -205
  28. package/__tests__/operator.bench.ts +4 -4
  29. package/__tests__/pipe.test.ts +501 -0
  30. package/__tests__/realWorld.test.ts +63 -0
  31. package/__tests__/regex.test.ts +17 -7
  32. package/__tests__/scope.bench.ts +1 -1
  33. package/__tests__/scope.test.ts +257 -23
  34. package/__tests__/serialization.test.ts +19 -0
  35. package/__tests__/string.test.ts +11 -7
  36. package/__tests__/submodule.test.ts +26 -39
  37. package/__tests__/this.test.ts +83 -57
  38. package/__tests__/thunk.test.ts +13 -7
  39. package/__tests__/traverse.test.ts +76 -54
  40. package/__tests__/tsGenerics.test.ts +12 -12
  41. package/__tests__/type.test.ts +36 -0
  42. package/__tests__/typeReference.test.ts +7 -7
  43. package/__tests__/unenclosed.test.ts +116 -100
  44. package/__tests__/union.test.ts +171 -104
  45. package/ark.ts +21 -65
  46. package/generic.ts +69 -0
  47. package/main.ts +6 -18
  48. package/match.ts +195 -93
  49. package/module.ts +15 -0
  50. package/out/ark.d.ts +15 -39
  51. package/out/ark.js +9 -18
  52. package/out/ark.js.map +1 -1
  53. package/out/generic.d.ts +21 -0
  54. package/out/generic.js +24 -0
  55. package/out/generic.js.map +1 -0
  56. package/out/main.d.ts +6 -7
  57. package/out/main.js +4 -3
  58. package/out/main.js.map +1 -1
  59. package/out/match.d.ts +57 -34
  60. package/out/match.js +51 -17
  61. package/out/match.js.map +1 -1
  62. package/out/module.d.ts +10 -0
  63. package/out/module.js +3 -0
  64. package/out/module.js.map +1 -0
  65. package/out/parser/definition.d.ts +11 -13
  66. package/out/parser/definition.js +10 -13
  67. package/out/parser/definition.js.map +1 -1
  68. package/out/parser/generic.d.ts +5 -7
  69. package/out/parser/generic.js +7 -11
  70. package/out/parser/generic.js.map +1 -1
  71. package/out/parser/objectLiteral.d.ts +45 -14
  72. package/out/parser/objectLiteral.js +61 -52
  73. package/out/parser/objectLiteral.js.map +1 -1
  74. package/out/parser/semantic/bounds.d.ts +10 -8
  75. package/out/parser/semantic/bounds.js +0 -1
  76. package/out/parser/semantic/bounds.js.map +1 -1
  77. package/out/parser/semantic/divisor.d.ts +5 -7
  78. package/out/parser/semantic/divisor.js +0 -1
  79. package/out/parser/semantic/infer.d.ts +24 -0
  80. package/out/parser/semantic/infer.js +1 -0
  81. package/out/parser/semantic/infer.js.map +1 -0
  82. package/out/parser/semantic/utils.d.ts +3 -7
  83. package/out/parser/semantic/utils.js +0 -1
  84. package/out/parser/semantic/validate.d.ts +15 -14
  85. package/out/parser/semantic/validate.js +1 -1
  86. package/out/parser/semantic/validate.js.map +1 -1
  87. package/out/parser/string/reduce/dynamic.d.ts +13 -13
  88. package/out/parser/string/reduce/dynamic.js +31 -26
  89. package/out/parser/string/reduce/dynamic.js.map +1 -1
  90. package/out/parser/string/reduce/shared.d.ts +6 -20
  91. package/out/parser/string/reduce/shared.js +0 -1
  92. package/out/parser/string/reduce/shared.js.map +1 -1
  93. package/out/parser/string/reduce/static.d.ts +27 -24
  94. package/out/parser/string/reduce/static.js +0 -1
  95. package/out/parser/string/shift/operand/date.d.ts +3 -3
  96. package/out/parser/string/shift/operand/date.js +4 -7
  97. package/out/parser/string/shift/operand/date.js.map +1 -1
  98. package/out/parser/string/shift/operand/enclosed.d.ts +4 -19
  99. package/out/parser/string/shift/operand/enclosed.js +12 -13
  100. package/out/parser/string/shift/operand/enclosed.js.map +1 -1
  101. package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
  102. package/out/parser/string/shift/operand/genericArgs.js +6 -10
  103. package/out/parser/string/shift/operand/genericArgs.js.map +1 -1
  104. package/out/parser/string/shift/operand/operand.d.ts +3 -4
  105. package/out/parser/string/shift/operand/operand.js +8 -12
  106. package/out/parser/string/shift/operand/operand.js.map +1 -1
  107. package/out/parser/string/shift/operand/unenclosed.d.ts +15 -22
  108. package/out/parser/string/shift/operand/unenclosed.js +24 -36
  109. package/out/parser/string/shift/operand/unenclosed.js.map +1 -1
  110. package/out/parser/string/shift/operator/bounds.d.ts +9 -13
  111. package/out/parser/string/shift/operator/bounds.js +43 -54
  112. package/out/parser/string/shift/operator/bounds.js.map +1 -1
  113. package/out/parser/string/shift/operator/divisor.d.ts +2 -4
  114. package/out/parser/string/shift/operator/divisor.js +1 -3
  115. package/out/parser/string/shift/operator/divisor.js.map +1 -1
  116. package/out/parser/string/shift/operator/operator.d.ts +2 -3
  117. package/out/parser/string/shift/operator/operator.js +12 -19
  118. package/out/parser/string/shift/operator/operator.js.map +1 -1
  119. package/out/parser/string/shift/scanner.d.ts +2 -2
  120. package/out/parser/string/shift/scanner.js +14 -16
  121. package/out/parser/string/shift/scanner.js.map +1 -1
  122. package/out/parser/string/string.d.ts +8 -6
  123. package/out/parser/string/string.js +1 -3
  124. package/out/parser/string/string.js.map +1 -1
  125. package/out/parser/tuple.d.ts +102 -43
  126. package/out/parser/tuple.js +119 -87
  127. package/out/parser/tuple.js.map +1 -1
  128. package/out/scope.d.ts +43 -120
  129. package/out/scope.js +39 -209
  130. package/out/scope.js.map +1 -1
  131. package/out/type.d.ts +43 -64
  132. package/out/type.js +30 -131
  133. package/out/type.js.map +1 -1
  134. package/package.json +7 -3
  135. package/parser/definition.ts +77 -93
  136. package/parser/generic.ts +38 -40
  137. package/parser/objectLiteral.ts +172 -136
  138. package/parser/semantic/bounds.ts +33 -32
  139. package/parser/semantic/divisor.ts +8 -10
  140. package/parser/semantic/infer.ts +141 -0
  141. package/parser/semantic/utils.ts +12 -23
  142. package/parser/semantic/validate.ts +81 -73
  143. package/parser/string/reduce/dynamic.ts +67 -55
  144. package/parser/string/reduce/shared.ts +1 -3
  145. package/parser/string/reduce/static.ts +67 -69
  146. package/parser/string/shift/operand/date.ts +15 -14
  147. package/parser/string/shift/operand/enclosed.ts +30 -25
  148. package/parser/string/shift/operand/genericArgs.ts +46 -45
  149. package/parser/string/shift/operand/operand.ts +32 -38
  150. package/parser/string/shift/operand/unenclosed.ts +107 -158
  151. package/parser/string/shift/operator/bounds.ts +118 -112
  152. package/parser/string/shift/operator/divisor.ts +14 -17
  153. package/parser/string/shift/operator/operator.ts +35 -46
  154. package/parser/string/shift/scanner.ts +51 -50
  155. package/parser/string/string.ts +30 -35
  156. package/parser/tuple.ts +375 -307
  157. package/scope.ts +170 -453
  158. package/type.ts +193 -274
  159. package/__tests__/astUtils.test.ts +0 -17
  160. package/__tests__/cyclic.test.ts +0 -144
  161. package/__tests__/jsdoc.test.ts +0 -64
  162. package/__tests__/morph.test.ts +0 -409
  163. package/__tests__/nodeDefinitions.test.ts +0 -190
  164. package/__tests__/typeMethods.test.ts +0 -69
  165. package/out/ark.d.ts.map +0 -1
  166. package/out/arkConfig.d.ts +0 -2
  167. package/out/arkConfig.d.ts.map +0 -1
  168. package/out/arkConfig.js +0 -10
  169. package/out/arkConfig.js.map +0 -1
  170. package/out/main.d.ts.map +0 -1
  171. package/out/match.d.ts.map +0 -1
  172. package/out/parser/definition.d.ts.map +0 -1
  173. package/out/parser/generic.d.ts.map +0 -1
  174. package/out/parser/objectLiteral.d.ts.map +0 -1
  175. package/out/parser/semantic/bounds.d.ts.map +0 -1
  176. package/out/parser/semantic/divisor.d.ts.map +0 -1
  177. package/out/parser/semantic/intersections.d.ts +0 -14
  178. package/out/parser/semantic/intersections.d.ts.map +0 -1
  179. package/out/parser/semantic/intersections.js +0 -2
  180. package/out/parser/semantic/intersections.js.map +0 -1
  181. package/out/parser/semantic/semantic.d.ts +0 -33
  182. package/out/parser/semantic/semantic.d.ts.map +0 -1
  183. package/out/parser/semantic/semantic.js +0 -2
  184. package/out/parser/semantic/semantic.js.map +0 -1
  185. package/out/parser/semantic/utils.d.ts.map +0 -1
  186. package/out/parser/semantic/validate.d.ts.map +0 -1
  187. package/out/parser/shared.d.ts +0 -54
  188. package/out/parser/shared.d.ts.map +0 -1
  189. package/out/parser/shared.js +0 -47
  190. package/out/parser/shared.js.map +0 -1
  191. package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
  192. package/out/parser/string/reduce/shared.d.ts.map +0 -1
  193. package/out/parser/string/reduce/static.d.ts.map +0 -1
  194. package/out/parser/string/shift/operand/date.d.ts.map +0 -1
  195. package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
  196. package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
  197. package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
  198. package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  199. package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
  200. package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
  201. package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
  202. package/out/parser/string/shift/scanner.d.ts.map +0 -1
  203. package/out/parser/string/string.d.ts.map +0 -1
  204. package/out/parser/tuple.d.ts.map +0 -1
  205. package/out/scope.d.ts.map +0 -1
  206. package/out/test.d.ts +0 -2
  207. package/out/test.d.ts.map +0 -1
  208. package/out/test.js +0 -7
  209. package/out/test.js.map +0 -1
  210. package/out/tsconfig.build.tsbuildinfo +0 -1
  211. package/out/type.d.ts.map +0 -1
  212. package/out/util.d.ts +0 -19
  213. package/out/util.d.ts.map +0 -1
  214. package/out/util.js +0 -7
  215. package/out/util.js.map +0 -1
  216. package/parser/semantic/intersections.ts +0 -46
  217. package/parser/semantic/semantic.ts +0 -144
  218. package/parser/shared.ts +0 -152
  219. package/util.ts +0 -36
@@ -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
- describe("cast", () => {
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
- describe("completions", () => {
4
+ contextualize(() => {
5
5
  it("completes standalone keyword", () => {
6
6
  // @ts-expect-error
7
- attest(() => type("s")).completions({ s: ["string", "symbol", "semver"] })
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",
@@ -1,168 +1,96 @@
1
- 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
- // })
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
- describe("date literal", () => {
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/05/05T09:00:00.00Z"))).equals(false)
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
- describe("declared", () => {
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
- 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'")
@@ -52,7 +52,7 @@ describe("discrimination", () => {
52
52
  // duration: "'s' | 'min' | 'h'",
53
53
  // operator: "'to'"
54
54
  // })
55
- // attest(t.root.hasKind("union") && t.root.discriminant).equals(null)
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
- describe("divisibility", () => {
7
- describe("parse", () => {
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({ basis: "number", divisor: 2 })
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({ basis: "number", divisor: 5 })
25
+ attest(t.json).snap({ domain: "number", divisor: 5 })
17
26
  })
18
- it("with bound", () => {
19
- const t = type("number%8<3")
20
- attest(t.json).equals(type("number%8").and("number<3").json)
21
- attest(t.root.description).snap(
22
- "a number and a multiple of 8 and less than 3"
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("unknown")
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("string")
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
- .throws("Divisibility operand string must be a number")
60
- .type.errors("Divisibility operand number | string must be a number")
76
+ attest(() => type("(number|string)%10")).throwsAndHasTypeError(
77
+ writeIndivisibleMessage(keywordNodes.number.or(keywordNodes.string))
78
+ )
61
79
  })
62
- })
63
- describe("intersection", () => {
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 (a multiple of 3) and 8 results in an unsatisfiable type"
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
- describe("parse enclosed", () => {
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([{ unit: "foo" }, { basis: "Array" }])
15
+ attest(t.json).snap([
16
+ { proto: "Array", sequence: { domain: "string", regex: [".*"] } },
17
+ { unit: "foo" }
18
+ ])
15
19
  })
16
- describe("errors", () => {
17
- describe("unterminated", () => {
18
- it("regex", () => {
19
- // @ts-expect-error
20
- attest(() => type("/.*")).throwsAndHasTypeError(
21
- writeUnterminatedEnclosedMessage(".*", "/")
22
- )
23
- })
24
- it("single-quote", () => {
25
- // @ts-expect-error
26
- attest(() => type("'.*")).throwsAndHasTypeError(
27
- writeUnterminatedEnclosedMessage(".*", "'")
28
- )
29
- })
30
- it("double-quote", () => {
31
- // @ts-expect-error
32
- attest(() => type('".*')).throwsAndHasTypeError(
33
- writeUnterminatedEnclosedMessage(".*", '"')
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
- attest<"hello">(type("'hello'").infer)
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