arktype 2.0.0-dev.0 → 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,57 +1,83 @@
1
- import { attest } from "@arktype/attest"
2
- import { scope, type } from "arktype"
3
- import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
4
-
5
- describe("this reference", () => {
6
- it("resolves from type", () => {
7
- const disappointingGift = type({
8
- label: "string",
9
- "box?": "this"
10
- })
11
-
12
- type ExpectedDisappointingGift = {
13
- label: string
14
- box?: ExpectedDisappointingGift
15
- }
16
- attest<ExpectedDisappointingGift>(disappointingGift.infer)
17
- })
18
-
19
- it("doesn't change when rereferenced", () => {
20
- const initial = type({
21
- initial: "this"
22
- })
23
-
24
- const reference = type({
25
- reference: initial
26
- })
27
- type Initial = {
28
- initial: Initial
29
- }
30
- type Expected = {
31
- reference: Initial
32
- }
33
-
34
- attest<Expected>(reference.infer)
35
- const types = scope({
36
- initial: {
37
- initial: "initial"
38
- },
39
- reference: {
40
- reference: "initial"
41
- }
42
- }).export()
43
- attest(reference.json).equals(types.reference.json)
44
- })
45
-
46
- it("unresolvable in scope", () => {
47
- attest(() =>
48
- scope({
49
- disappointingGift: {
50
- label: "string",
51
- // @ts-expect-error
52
- "box?": "this"
53
- }
54
- }).export()
55
- ).throwsAndHasTypeError(writeUnresolvableMessage("this"))
56
- })
57
- })
1
+ // import { attest, contextualize } from "@arktype/attest"
2
+ // import { writeUnresolvableMessage } from "@arktype/schema"
3
+ // import { scope, type } from "arktype"
4
+
5
+ // contextualize(() => {
6
+ // it("resolves from type", () => {
7
+ // const disappointingGift = type({
8
+ // label: "string",
9
+ // "box?": "this"
10
+ // })
11
+
12
+ // type ExpectedDisappointingGift = {
13
+ // label: string
14
+ // box?: ExpectedDisappointingGift
15
+ // }
16
+ // attest<ExpectedDisappointingGift>(disappointingGift.infer)
17
+ // })
18
+
19
+ // it("doesn't change when rereferenced", () => {
20
+ // const initial = type({
21
+ // initial: "this"
22
+ // })
23
+
24
+ // const reference = type({
25
+ // reference: initial
26
+ // })
27
+ // type Initial = {
28
+ // initial: Initial
29
+ // }
30
+ // type Expected = {
31
+ // reference: Initial
32
+ // }
33
+
34
+ // attest<Expected>(reference.infer)
35
+ // const types = scope({
36
+ // initial: {
37
+ // initial: "initial"
38
+ // },
39
+ // reference: {
40
+ // reference: "initial"
41
+ // }
42
+ // }).export()
43
+ // attest(reference.json).equals(types.reference.json)
44
+ // })
45
+
46
+ // it("unresolvable in scope", () => {
47
+ // attest(() =>
48
+ // scope({
49
+ // disappointingGift: {
50
+ // label: "string",
51
+ // // @ts-expect-error
52
+ // "box?": "this"
53
+ // }
54
+ // }).export()
55
+ // ).throwsAndHasTypeError(writeUnresolvableMessage("this"))
56
+ // })
57
+
58
+ // it("root expression", () => {
59
+ // const t = type({ a: "string" }, "|", { b: "this" })
60
+ // attest(t.infer).type.toString.snap(
61
+ // "{ a: string; } | { b: { a: string; } | any; }"
62
+ // )
63
+ // attest(t.json).equals(type([{ a: "string" }, "|", { b: "this" }]).json)
64
+ // })
65
+
66
+ // // TODO: reenable
67
+ // it("tuple expression", () => {
68
+ // const t = type([{ a: "string" }, "|", { b: "this" }])
69
+ // attest(t.infer).type.toString.snap(
70
+ // "{ a: string; } | { b: { a: string; } | any; }"
71
+ // )
72
+ // const types = scope({
73
+ // a: {
74
+ // a: "string"
75
+ // },
76
+ // b: {
77
+ // b: "expected"
78
+ // },
79
+ // expected: "a|b"
80
+ // }).export()
81
+ // attest(t.json).equals(types.expected.json)
82
+ // })
83
+ // })
@@ -1,10 +1,10 @@
1
- import { attest } from "@arktype/attest"
1
+ import { attest, contextualize } from "@arktype/attest"
2
+ import { writeUnresolvableMessage } from "@arktype/schema"
2
3
  import { scope, type } from "arktype"
3
4
  import { writeBadDefinitionTypeMessage } from "../parser/definition.js"
4
- import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
5
5
 
6
- describe("thunk", () => {
7
- it("thunk", () => {
6
+ contextualize(() => {
7
+ it("in type", () => {
8
8
  const t = type(() => type("boolean"))
9
9
  attest<boolean>(t.infer)
10
10
  attest(() => {
@@ -12,7 +12,8 @@ describe("thunk", () => {
12
12
  type(() => type("moolean"))
13
13
  }).throwsAndHasTypeError(writeUnresolvableMessage("moolean"))
14
14
  })
15
- it("thunks in scope", () => {
15
+
16
+ it("in scope", () => {
16
17
  const $ = scope({
17
18
  a: () => $.type({ b: "b" }),
18
19
  b: () => $.type({ a: "string" })
@@ -42,6 +43,7 @@ describe("thunk", () => {
42
43
  // object: { props: { a: { object: { props: { a: "string" } } } } }
43
44
  // })
44
45
  })
46
+
45
47
  it("expression from thunk", () => {
46
48
  const $ = scope({
47
49
  a: () => $.type({ a: "string" }),
@@ -54,22 +56,26 @@ describe("thunk", () => {
54
56
  // object: { props: { a: "string", b: "boolean" } }
55
57
  // })
56
58
  })
57
- it("shallow thunk in type", () => {
59
+
60
+ it("shallow in type", () => {
58
61
  const t = type(() => type("string"))
59
62
  attest(t.json).equals(type("string").json)
60
63
  attest<string>(t.infer)
61
64
  })
62
- it("deep thunk in type", () => {
65
+
66
+ it("deep in type", () => {
63
67
  const t = type({ a: () => type("string") })
64
68
  attest(t.json).equals(type({ a: "string" }).json)
65
69
  attest<{ a: string }>(t.infer)
66
70
  })
71
+
67
72
  it("non-type thunk in scope", () => {
68
73
  const $ = scope({
69
74
  a: () => 42
70
75
  })
71
76
  attest(() => $.export()).throws(writeBadDefinitionTypeMessage("number"))
72
77
  })
78
+
73
79
  it("parse error in thunk in scope", () => {
74
80
  const $ = scope({
75
81
  // @ts-expect-error
@@ -1,119 +1,141 @@
1
- import { attest } from "@arktype/attest"
1
+ import { attest, contextualize } from "@arktype/attest"
2
2
  import { scope, type } from "arktype"
3
3
 
4
- describe("traverse", () => {
4
+ contextualize(() => {
5
5
  it("divisible", () => {
6
6
  const t = type("number%2")
7
- attest(t(4).out).snap(4)
8
- attest(t(5).errors?.summary).snap("Must be a multiple of 2 (was 5)")
7
+ attest(t(4)).snap(4)
8
+ attest(t(5).toString()).snap("must be a multiple of 2 (was 5)")
9
9
  })
10
+
10
11
  it("range", () => {
11
12
  const t = type("number>2")
12
- attest(t(3).out).snap(3)
13
- attest(t(2).errors?.summary).snap("Must be more than 2 (was 2)")
13
+ attest(t(3)).snap(3)
14
+ attest(t(2).toString()).snap("must be more than 2 (was 2)")
14
15
  })
16
+
15
17
  it("domain", () => {
16
18
  const t = type("number")
17
- attest(t(5).out).snap(5)
18
- attest(t("foo").errors?.summary).snap("Must be a number (was string)")
19
+ attest(t(5)).snap(5)
20
+ attest(t("foo").toString()).snap("must be a number (was string)")
19
21
  })
22
+
20
23
  it("regex", () => {
21
24
  const t = type("/.*@arktype.io/")
22
- attest(t("shawn@arktype.io").out).snap("shawn@arktype.io")
23
- attest(t("shawn@hotmail.com").errors?.summary).snap(
24
- "Must be a string matching /.*@arktype.io/ (was 'shawn@hotmail.com')"
25
+ attest(t("shawn@arktype.io")).snap("shawn@arktype.io")
26
+ attest(t("shawn@hotmail.com").toString()).snap(
27
+ 'must be matched by .*@arktype.io (was "shawn@hotmail.com")'
25
28
  )
26
29
  })
30
+
27
31
  it("required keys", () => {
28
32
  const t = type({
29
33
  name: "string",
30
34
  age: "number",
31
35
  "title?": "string"
32
36
  })
33
- attest(t({ name: "Shawn", age: 99 }).out).snap({
37
+ attest(t({ name: "Shawn", age: 99 })).snap({
34
38
  name: "Shawn",
35
39
  age: 99
36
40
  })
37
- attest(t({ name: "Shawn" }).errors?.summary).snap("age must be defined")
41
+ attest(t({ name: "Shawn" }).toString()).snap(
42
+ "age must be a number (was missing)"
43
+ )
38
44
  })
45
+
39
46
  it("customized builtin problem", () => {
40
47
  const types = scope(
41
- { isEven: "number%2" }
42
- //{
43
- // TODO: Fix
44
- // codes: {
45
- // divisor: {
46
- // mustBe: (divisor) => `a multiple of ${divisor}`,
47
- // writeReason: (mustBe, was) => `${was} is not ${mustBe}!`
48
- // }
49
- // }
50
- //}
48
+ { isEven: "number%2" },
49
+ {
50
+ divisor: {
51
+ expected: ctx => `% ${ctx.rule} !== 0`,
52
+ problem: ctx => `${ctx.actual} ${ctx.expected}`
53
+ }
54
+ }
51
55
  ).export()
52
- attest(types.isEven(3).errors?.summary).snap("3 is not a multiple of 2!")
56
+ attest(types.isEven(3).toString()).snap("3 % 2 !== 0")
53
57
  })
58
+
54
59
  it("domains", () => {
55
60
  const t = type("string|number[]")
56
- attest(t([1]).out).snap([1])
57
- attest(t("hello").out).snap("hello")
58
- attest(t(2).errors?.summary).snap("Must be a string or an object (was 2)")
61
+ attest(t([1])).snap([1])
62
+ attest(t("hello")).snap("hello")
63
+ attest(t(2).toString()).snap("must be a string or an array (was number)")
59
64
  })
65
+
60
66
  it("tuple length", () => {
61
67
  const t = type(["string", "number", "string", "string[]"])
62
68
  const data: typeof t.infer = ["foo", 5, "boo", []]
63
- attest(t(data).out).equals(data)
64
- attest(t(["hello"]).errors?.summary).snap("length must be 4 (was 1)")
69
+ attest(t(data)).equals(data)
70
+ attest(t(["hello"]).toString()).snap(
71
+ 'must be exactly length 4 (was ["hello"])'
72
+ )
65
73
  })
74
+
66
75
  it("branches", () => {
67
- const t = type([{ a: "string" }, "|", { b: "boolean" }])
68
- attest(t({ a: "ok" }).out).snap({ a: "ok" })
69
- attest(t({ b: true }).out).snap({ b: true })
70
- attest(t({}).errors?.summary).snap(
71
- "a must be defined or b must be defined (was {})"
76
+ const t = type({ bar: "boolean" }, "|", { foo: "string" })
77
+ attest(t({ foo: "ok" })).snap({ foo: "ok" })
78
+ attest(t({ bar: true })).snap({ bar: true })
79
+ attest(t({}).toString()).snap(
80
+ "bar must be boolean (was missing) or foo must be a string (was missing)"
81
+ )
82
+ attest(t({ bar: "swapped", foo: true }).toString()).snap(
83
+ 'bar must be boolean (was "swapped") or foo must be a string (was true)'
72
84
  )
73
85
  })
86
+
74
87
  it("branches at path", () => {
75
88
  const t = type({ key: [{ a: "string" }, "|", { b: "boolean" }] })
76
- attest(t({ key: { a: "ok" } }).out).snap({ key: { a: "ok" } })
77
- attest(t({ key: { b: true } }).out).snap({ key: { b: true } })
78
- attest(t({ key: {} }).errors?.summary).snap(
79
- "At key, a must be defined or b must be defined (was {})"
89
+ attest(t({ key: { a: "ok" } })).snap({ key: { a: "ok" } })
90
+ attest(t({ key: { b: true } })).snap({ key: { b: true } })
91
+ attest(t({ key: {} }).toString()).snap(
92
+ "key.a must be a string (was missing) or key.b must be boolean (was missing)"
80
93
  )
81
94
  })
95
+
82
96
  it("switch", () => {
83
- const t = type([{ a: "string" }, "|", { a: "boolean" }])
84
- attest(t({ a: "ok" }).out).snap({ a: "ok" })
85
- attest(t({ a: true }).out).snap({ a: true })
97
+ const t = type({ a: "string" }).or({ a: "null" }).or({ a: "number" })
98
+ attest(t({ a: "ok" })).snap({ a: "ok" })
99
+ attest(t({ a: 5 })).snap({ a: 5 })
86
100
  // value isn't present
87
- attest(t({}).errors?.summary).snap(
88
- "a must be a string or boolean (was undefined)"
101
+ attest(t({}).toString()).snap(
102
+ "a must be a number, a string or null (was missing)"
89
103
  )
90
104
  // unsatisfying value
91
- attest(t({ a: 5 }).errors?.summary).snap(
92
- "a must be a string or boolean (was 5)"
105
+ attest(t({ a: false }).toString()).snap(
106
+ "a must be a number, a string or null (was false)"
93
107
  )
94
108
  })
109
+
95
110
  it("multiple switch", () => {
96
111
  const types = scope({
97
- a: { a: "string" },
98
- b: { a: "number" },
99
- c: { a: "Function" },
112
+ a: { foo: "string" },
113
+ b: { foo: "number" },
114
+ c: { foo: "Function" },
100
115
  d: "a|b|c"
101
116
  }).export()
102
- attest(types.d({}).errors?.summary).snap(
103
- "a must be a string, a number or an object (was undefined)"
117
+ attest(types.d({}).toString()).snap(
118
+ "foo must be a function, a number or a string (was missing)"
119
+ )
120
+ attest(types.d({ foo: null }).toString()).snap(
121
+ "foo must be a function, a number or a string (was null)"
104
122
  )
105
123
  })
106
124
 
107
125
  it("multi", () => {
108
126
  const naturalNumber = type("integer>0")
109
- attest(naturalNumber(-1.2).errors?.summary).snap(
110
- "-1.2 must be...\n• an integer\n• more than 0"
127
+ attest(naturalNumber(-1.2).toString()).snap(
128
+ `must be...
129
+ • an integer
130
+ • more than 0`
111
131
  )
112
132
  const naturalAtPath = type({
113
133
  natural: naturalNumber
114
134
  })
115
- attest(naturalAtPath({ natural: -0.1 }).errors?.summary).snap(
116
- "At natural, -0.1 must be...\n• an integer\n• more than 0"
135
+ attest(naturalAtPath({ natural: -0.1 }).toString()).snap(
136
+ `natural must be...
137
+ • an integer
138
+ • more than 0`
117
139
  )
118
140
  })
119
141
  })
@@ -1,12 +1,12 @@
1
- describe("tsGenerics", () => {
2
- // it("in scope", () => {
3
- // const types = scope({
4
- // t: "Record<string, number>"
5
- // }).export()
6
- // attest<Record<string, number>>(types.t.infer)
7
- // })
8
- // it("in module", () => {
9
- // const t = arktypes.Record("string", "number")
10
- // attest<Record<string, number>>(t.infer)
11
- // })
12
- })
1
+ // describe("tsGenerics", () => {
2
+ // it("in scope", () => {
3
+ // const types = scope({
4
+ // t: "Record<string, number>"
5
+ // }).export()
6
+ // attest<Record<string, number>>(types.t.infer)
7
+ // })
8
+ // it("in module", () => {
9
+ // const t = arktypes.Record("string", "number")
10
+ // attest<Record<string, number>>(t.infer)
11
+ // })
12
+ // })
@@ -0,0 +1,36 @@
1
+ import { attest, contextualize } from "@arktype/attest"
2
+ import { ArkError, type } from "arktype"
3
+ import { AssertionError } from "node:assert"
4
+
5
+ contextualize(() => {
6
+ it("root discriminates", () => {
7
+ const t = type("string")
8
+ const out = t("")
9
+ if (out instanceof type.errors) out.throw()
10
+ else attest<string>(out)
11
+ })
12
+
13
+ it("allows", () => {
14
+ const t = type("number%2")
15
+ const data: unknown = 4
16
+ if (t.allows(data)) {
17
+ // narrows correctly
18
+ attest<number>(data)
19
+ } else throw new Error()
20
+
21
+ attest(t.allows(5)).equals(false)
22
+ })
23
+
24
+ // TODO: ?
25
+ it("errors can be thrown", () => {
26
+ const t = type("number")
27
+ try {
28
+ const result = t("invalid")
29
+ attest(result instanceof type.errors && result.throw())
30
+ } catch (e) {
31
+ attest(e instanceof ArkError).equals(true)
32
+ return
33
+ }
34
+ throw new AssertionError({ message: "Expected to throw" })
35
+ })
36
+ })
@@ -1,8 +1,8 @@
1
- import { attest } from "@arktype/attest"
2
- import { ark, scope, type } from "arktype"
3
- import { writeUnresolvableMessage } from "../parser/string/shift/operand/unenclosed.js"
1
+ import { attest, contextualize } from "@arktype/attest"
2
+ import { writeUnresolvableMessage } from "@arktype/schema"
3
+ import { ambient, scope, type } from "arktype"
4
4
 
5
- describe("type references", () => {
5
+ contextualize(() => {
6
6
  it("shallow type reference", () => {
7
7
  const t = type(type("boolean"))
8
8
  attest<boolean>(t.infer)
@@ -24,9 +24,9 @@ describe("type references", () => {
24
24
  const a = type({ a: "string" })
25
25
  const $ = scope({ a })
26
26
  const types = $.export()
27
- attest(types.a.json).equals(type({ a: "string" }).json)
28
- attest(a.scope).is(ark)
29
- attest(types.a.scope).is($)
27
+ attest(types.a.json).equals(a.json)
28
+ attest(a.$.json).equals(ambient.json)
29
+ attest(types.a.$.json).equals($.json)
30
30
  attest<{ a: string }>(types.a.infer)
31
31
  })
32
32