arktype 1.1.0-dev-2 → 2.0.0-dev.1

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 (296) hide show
  1. package/__tests__/array.test.ts +272 -0
  2. package/__tests__/astUtils.test.ts +17 -0
  3. package/__tests__/badDefinitionType.test.ts +51 -0
  4. package/__tests__/basis.test.ts +46 -0
  5. package/__tests__/bounds.test.ts +388 -0
  6. package/__tests__/cast.test.ts +32 -0
  7. package/__tests__/completions.test.ts +46 -0
  8. package/__tests__/config.test.ts +168 -0
  9. package/__tests__/cyclic.test.ts +144 -0
  10. package/__tests__/dateLiteral.test.ts +32 -0
  11. package/__tests__/declared.test.ts +123 -0
  12. package/__tests__/discrimination.test.ts +74 -0
  13. package/__tests__/divisor.test.ts +90 -0
  14. package/__tests__/enclosed.test.ts +93 -0
  15. package/__tests__/examples.test.ts +56 -0
  16. package/__tests__/expressions.test.ts +138 -0
  17. package/__tests__/generateBenchData.ts +89 -0
  18. package/__tests__/generated/cyclic.ts +3058 -0
  19. package/__tests__/generic.test.ts +294 -0
  20. package/__tests__/group.test.ts +72 -0
  21. package/__tests__/hkt.test.ts +22 -0
  22. package/__tests__/imports.test.ts +113 -0
  23. package/__tests__/instanceof.test.ts +102 -0
  24. package/__tests__/intersection.test.ts +104 -0
  25. package/__tests__/jsdoc.test.ts +64 -0
  26. package/__tests__/keyTraversal.test.ts +73 -0
  27. package/__tests__/keyof.test.ts +134 -0
  28. package/__tests__/keywords.test.ts +188 -0
  29. package/__tests__/literal.test.ts +51 -0
  30. package/__tests__/match.test.ts +34 -0
  31. package/__tests__/methods.bench.ts +22 -0
  32. package/__tests__/morph.test.ts +409 -0
  33. package/__tests__/narrow.test.ts +102 -0
  34. package/__tests__/nodeDefinitions.test.ts +190 -0
  35. package/__tests__/object.bench.ts +28 -0
  36. package/__tests__/objectLiteral.test.ts +300 -0
  37. package/__tests__/operand.bench.ts +45 -0
  38. package/__tests__/operator.bench.ts +130 -0
  39. package/__tests__/regex.test.ts +58 -0
  40. package/__tests__/scope.bench.ts +39 -0
  41. package/__tests__/scope.test.ts +140 -0
  42. package/__tests__/string.test.ts +48 -0
  43. package/__tests__/submodule.test.ts +99 -0
  44. package/__tests__/this.test.ts +57 -0
  45. package/__tests__/thunk.test.ts +80 -0
  46. package/__tests__/traverse.test.ts +119 -0
  47. package/__tests__/tsGenerics.test.ts +12 -0
  48. package/__tests__/typeMethods.test.ts +69 -0
  49. package/__tests__/typeReference.test.ts +39 -0
  50. package/__tests__/unenclosed.test.ts +120 -0
  51. package/__tests__/union.test.ts +147 -0
  52. package/ark.ts +91 -0
  53. package/main.ts +7 -9
  54. package/match.ts +47 -68
  55. package/out/ark.d.ts +52 -0
  56. package/out/ark.d.ts.map +1 -0
  57. package/out/ark.js +19 -0
  58. package/out/ark.js.map +1 -0
  59. package/out/arkConfig.d.ts +2 -0
  60. package/out/arkConfig.d.ts.map +1 -0
  61. package/out/arkConfig.js +10 -0
  62. package/out/arkConfig.js.map +1 -0
  63. package/out/main.d.ts +7 -0
  64. package/out/main.d.ts.map +1 -0
  65. package/out/main.js +4 -0
  66. package/out/main.js.map +1 -0
  67. package/out/match.d.ts +48 -0
  68. package/out/match.d.ts.map +1 -0
  69. package/{dist → out}/match.js +0 -5
  70. package/out/match.js.map +1 -0
  71. package/{dist → out}/parser/definition.d.ts +3 -3
  72. package/out/parser/definition.d.ts.map +1 -0
  73. package/{dist → out}/parser/definition.js +8 -5
  74. package/out/parser/definition.js.map +1 -0
  75. package/out/parser/generic.d.ts +18 -0
  76. package/out/parser/generic.d.ts.map +1 -0
  77. package/out/parser/generic.js +19 -0
  78. package/out/parser/generic.js.map +1 -0
  79. package/{dist → out}/parser/objectLiteral.d.ts +14 -5
  80. package/out/parser/objectLiteral.d.ts.map +1 -0
  81. package/out/parser/objectLiteral.js +66 -0
  82. package/out/parser/objectLiteral.js.map +1 -0
  83. package/out/parser/semantic/bounds.d.ts +13 -0
  84. package/out/parser/semantic/bounds.d.ts.map +1 -0
  85. package/{dist → out}/parser/semantic/bounds.js.map +1 -1
  86. package/{dist → out}/parser/semantic/divisor.d.ts +2 -2
  87. package/{dist → out}/parser/semantic/divisor.d.ts.map +1 -1
  88. package/{dist → out}/parser/semantic/intersections.d.ts +1 -2
  89. package/out/parser/semantic/intersections.d.ts.map +1 -0
  90. package/out/parser/semantic/semantic.d.ts +33 -0
  91. package/out/parser/semantic/semantic.d.ts.map +1 -0
  92. package/{dist → out}/parser/semantic/utils.d.ts +1 -1
  93. package/{dist → out}/parser/semantic/utils.d.ts.map +1 -1
  94. package/{dist → out}/parser/semantic/validate.d.ts +3 -3
  95. package/out/parser/semantic/validate.d.ts.map +1 -0
  96. package/{dist → out}/parser/semantic/validate.js.map +1 -1
  97. package/{dist → out}/parser/shared.d.ts +7 -1
  98. package/out/parser/shared.d.ts.map +1 -0
  99. package/{dist → out}/parser/shared.js +7 -1
  100. package/out/parser/shared.js.map +1 -0
  101. package/{dist → out}/parser/string/reduce/dynamic.d.ts +11 -9
  102. package/out/parser/string/reduce/dynamic.d.ts.map +1 -0
  103. package/{dist → out}/parser/string/reduce/dynamic.js +13 -2
  104. package/out/parser/string/reduce/dynamic.js.map +1 -0
  105. package/{dist → out}/parser/string/reduce/shared.d.ts +29 -2
  106. package/{dist → out}/parser/string/reduce/shared.d.ts.map +1 -1
  107. package/{dist → out}/parser/string/reduce/shared.js +20 -1
  108. package/out/parser/string/reduce/shared.js.map +1 -0
  109. package/{dist → out}/parser/string/reduce/static.d.ts +1 -2
  110. package/out/parser/string/reduce/static.d.ts.map +1 -0
  111. package/{dist → out}/parser/string/shift/operand/date.d.ts +3 -3
  112. package/out/parser/string/shift/operand/date.d.ts.map +1 -0
  113. package/{dist → out}/parser/string/shift/operand/date.js.map +1 -1
  114. package/{dist → out}/parser/string/shift/operand/enclosed.d.ts +2 -2
  115. package/{dist → out}/parser/string/shift/operand/enclosed.d.ts.map +1 -1
  116. package/{dist → out}/parser/string/shift/operand/enclosed.js +4 -4
  117. package/out/parser/string/shift/operand/enclosed.js.map +1 -0
  118. package/out/parser/string/shift/operand/genericArgs.d.ts +29 -0
  119. package/out/parser/string/shift/operand/genericArgs.d.ts.map +1 -0
  120. package/out/parser/string/shift/operand/genericArgs.js +25 -0
  121. package/out/parser/string/shift/operand/genericArgs.js.map +1 -0
  122. package/{dist → out}/parser/string/shift/operand/operand.d.ts.map +1 -1
  123. package/{dist → out}/parser/string/shift/operand/operand.js.map +1 -1
  124. package/{dist → out}/parser/string/shift/operand/unenclosed.d.ts +7 -9
  125. package/out/parser/string/shift/operand/unenclosed.d.ts.map +1 -0
  126. package/{dist → out}/parser/string/shift/operand/unenclosed.js +10 -10
  127. package/out/parser/string/shift/operand/unenclosed.js.map +1 -0
  128. package/{dist → out}/parser/string/shift/operator/bounds.d.ts +11 -33
  129. package/out/parser/string/shift/operator/bounds.d.ts.map +1 -0
  130. package/out/parser/string/shift/operator/bounds.js +106 -0
  131. package/out/parser/string/shift/operator/bounds.js.map +1 -0
  132. package/{dist → out}/parser/string/shift/operator/divisor.js.map +1 -1
  133. package/out/parser/string/shift/operator/operator.d.ts.map +1 -0
  134. package/{dist → out}/parser/string/shift/operator/operator.js.map +1 -1
  135. package/{dist → out}/parser/string/shift/scanner.d.ts +1 -1
  136. package/{dist → out}/parser/string/shift/scanner.d.ts.map +1 -1
  137. package/out/parser/string/shift/scanner.js.map +1 -0
  138. package/{dist → out}/parser/string/string.d.ts +9 -11
  139. package/out/parser/string/string.d.ts.map +1 -0
  140. package/out/parser/string/string.js +23 -0
  141. package/out/parser/string/string.js.map +1 -0
  142. package/{dist → out}/parser/tuple.d.ts +9 -11
  143. package/out/parser/tuple.d.ts.map +1 -0
  144. package/{dist → out}/parser/tuple.js +17 -14
  145. package/out/parser/tuple.js.map +1 -0
  146. package/{dist → out}/scope.d.ts +16 -16
  147. package/out/scope.d.ts.map +1 -0
  148. package/{dist → out}/scope.js +27 -22
  149. package/out/scope.js.map +1 -0
  150. package/out/test.d.ts +2 -0
  151. package/out/test.d.ts.map +1 -0
  152. package/out/test.js +7 -0
  153. package/out/test.js.map +1 -0
  154. package/out/tsconfig.build.tsbuildinfo +1 -0
  155. package/{dist → out}/type.d.ts +23 -47
  156. package/out/type.d.ts.map +1 -0
  157. package/{dist → out}/type.js +27 -31
  158. package/out/type.js.map +1 -0
  159. package/out/util.d.ts +19 -0
  160. package/out/util.d.ts.map +1 -0
  161. package/out/util.js +7 -0
  162. package/out/util.js.map +1 -0
  163. package/package.json +13 -15
  164. package/parser/definition.ts +58 -55
  165. package/parser/generic.ts +17 -157
  166. package/parser/objectLiteral.ts +107 -30
  167. package/parser/semantic/bounds.ts +22 -24
  168. package/parser/semantic/divisor.ts +2 -2
  169. package/parser/semantic/intersections.ts +17 -18
  170. package/parser/semantic/semantic.ts +80 -43
  171. package/parser/semantic/utils.ts +7 -7
  172. package/parser/semantic/validate.ts +40 -36
  173. package/parser/shared.ts +28 -17
  174. package/parser/string/reduce/dynamic.ts +26 -11
  175. package/parser/string/reduce/shared.ts +35 -7
  176. package/parser/string/reduce/static.ts +20 -22
  177. package/parser/string/shift/operand/date.ts +1 -4
  178. package/parser/string/shift/operand/enclosed.ts +5 -6
  179. package/parser/string/shift/operand/genericArgs.ts +127 -0
  180. package/parser/string/shift/operand/operand.ts +21 -21
  181. package/parser/string/shift/operand/unenclosed.ts +54 -68
  182. package/parser/string/shift/operator/bounds.ts +99 -64
  183. package/parser/string/shift/operator/operator.ts +31 -31
  184. package/parser/string/shift/scanner.ts +7 -7
  185. package/parser/string/string.ts +29 -32
  186. package/parser/tuple.ts +87 -88
  187. package/scope.ts +81 -80
  188. package/type.ts +76 -132
  189. package/util.ts +36 -0
  190. package/dist/main.d.ts +0 -11
  191. package/dist/main.d.ts.map +0 -1
  192. package/dist/main.js +0 -8
  193. package/dist/main.js.map +0 -1
  194. package/dist/match.d.ts +0 -46
  195. package/dist/match.d.ts.map +0 -1
  196. package/dist/match.js.map +0 -1
  197. package/dist/parser/definition.d.ts.map +0 -1
  198. package/dist/parser/definition.js.map +0 -1
  199. package/dist/parser/generic.d.ts +0 -44
  200. package/dist/parser/generic.d.ts.map +0 -1
  201. package/dist/parser/generic.js +0 -46
  202. package/dist/parser/generic.js.map +0 -1
  203. package/dist/parser/objectLiteral.d.ts.map +0 -1
  204. package/dist/parser/objectLiteral.js +0 -38
  205. package/dist/parser/objectLiteral.js.map +0 -1
  206. package/dist/parser/semantic/bounds.d.ts +0 -13
  207. package/dist/parser/semantic/bounds.d.ts.map +0 -1
  208. package/dist/parser/semantic/intersections.d.ts.map +0 -1
  209. package/dist/parser/semantic/semantic.d.ts +0 -22
  210. package/dist/parser/semantic/semantic.d.ts.map +0 -1
  211. package/dist/parser/semantic/validate.d.ts.map +0 -1
  212. package/dist/parser/shared.d.ts.map +0 -1
  213. package/dist/parser/shared.js.map +0 -1
  214. package/dist/parser/string/reduce/dynamic.d.ts.map +0 -1
  215. package/dist/parser/string/reduce/dynamic.js.map +0 -1
  216. package/dist/parser/string/reduce/shared.js.map +0 -1
  217. package/dist/parser/string/reduce/static.d.ts.map +0 -1
  218. package/dist/parser/string/shift/operand/date.d.ts.map +0 -1
  219. package/dist/parser/string/shift/operand/enclosed.js.map +0 -1
  220. package/dist/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  221. package/dist/parser/string/shift/operand/unenclosed.js.map +0 -1
  222. package/dist/parser/string/shift/operator/bounds.d.ts.map +0 -1
  223. package/dist/parser/string/shift/operator/bounds.js +0 -95
  224. package/dist/parser/string/shift/operator/bounds.js.map +0 -1
  225. package/dist/parser/string/shift/operator/operator.d.ts.map +0 -1
  226. package/dist/parser/string/shift/scanner.js.map +0 -1
  227. package/dist/parser/string/string.d.ts.map +0 -1
  228. package/dist/parser/string/string.js +0 -31
  229. package/dist/parser/string/string.js.map +0 -1
  230. package/dist/parser/tuple.d.ts.map +0 -1
  231. package/dist/parser/tuple.js.map +0 -1
  232. package/dist/scope.d.ts.map +0 -1
  233. package/dist/scope.js.map +0 -1
  234. package/dist/scopes/ark.d.ts +0 -38
  235. package/dist/scopes/ark.d.ts.map +0 -1
  236. package/dist/scopes/ark.js +0 -24
  237. package/dist/scopes/ark.js.map +0 -1
  238. package/dist/scopes/jsObjects.d.ts +0 -19
  239. package/dist/scopes/jsObjects.d.ts.map +0 -1
  240. package/dist/scopes/jsObjects.js +0 -15
  241. package/dist/scopes/jsObjects.js.map +0 -1
  242. package/dist/scopes/parsing.d.ts +0 -18
  243. package/dist/scopes/parsing.d.ts.map +0 -1
  244. package/dist/scopes/parsing.js +0 -61
  245. package/dist/scopes/parsing.js.map +0 -1
  246. package/dist/scopes/tsGenerics.d.ts +0 -17
  247. package/dist/scopes/tsGenerics.d.ts.map +0 -1
  248. package/dist/scopes/tsGenerics.js +0 -13
  249. package/dist/scopes/tsGenerics.js.map +0 -1
  250. package/dist/scopes/tsKeywords.d.ts +0 -24
  251. package/dist/scopes/tsKeywords.d.ts.map +0 -1
  252. package/dist/scopes/tsKeywords.js +0 -20
  253. package/dist/scopes/tsKeywords.js.map +0 -1
  254. package/dist/scopes/utils/creditCard.d.ts +0 -3
  255. package/dist/scopes/utils/creditCard.d.ts.map +0 -1
  256. package/dist/scopes/utils/creditCard.js +0 -36
  257. package/dist/scopes/utils/creditCard.js.map +0 -1
  258. package/dist/scopes/utils/date.d.ts +0 -24
  259. package/dist/scopes/utils/date.d.ts.map +0 -1
  260. package/dist/scopes/utils/date.js +0 -53
  261. package/dist/scopes/utils/date.js.map +0 -1
  262. package/dist/scopes/validation.d.ts +0 -20
  263. package/dist/scopes/validation.d.ts.map +0 -1
  264. package/dist/scopes/validation.js +0 -57
  265. package/dist/scopes/validation.js.map +0 -1
  266. package/dist/type.d.ts.map +0 -1
  267. package/dist/type.js.map +0 -1
  268. package/scopes/ark.ts +0 -64
  269. package/scopes/jsObjects.ts +0 -29
  270. package/scopes/parsing.ts +0 -81
  271. package/scopes/tsGenerics.ts +0 -28
  272. package/scopes/tsKeywords.ts +0 -39
  273. package/scopes/utils/creditCard.ts +0 -37
  274. package/scopes/utils/date.ts +0 -108
  275. package/scopes/validation.ts +0 -81
  276. /package/{dist → out}/parser/semantic/bounds.js +0 -0
  277. /package/{dist → out}/parser/semantic/divisor.js +0 -0
  278. /package/{dist → out}/parser/semantic/divisor.js.map +0 -0
  279. /package/{dist → out}/parser/semantic/intersections.js +0 -0
  280. /package/{dist → out}/parser/semantic/intersections.js.map +0 -0
  281. /package/{dist → out}/parser/semantic/semantic.js +0 -0
  282. /package/{dist → out}/parser/semantic/semantic.js.map +0 -0
  283. /package/{dist → out}/parser/semantic/utils.js +0 -0
  284. /package/{dist → out}/parser/semantic/utils.js.map +0 -0
  285. /package/{dist → out}/parser/semantic/validate.js +0 -0
  286. /package/{dist → out}/parser/string/reduce/static.js +0 -0
  287. /package/{dist → out}/parser/string/reduce/static.js.map +0 -0
  288. /package/{dist → out}/parser/string/shift/operand/date.js +0 -0
  289. /package/{dist → out}/parser/string/shift/operand/operand.d.ts +0 -0
  290. /package/{dist → out}/parser/string/shift/operand/operand.js +0 -0
  291. /package/{dist → out}/parser/string/shift/operator/divisor.d.ts +0 -0
  292. /package/{dist → out}/parser/string/shift/operator/divisor.d.ts.map +0 -0
  293. /package/{dist → out}/parser/string/shift/operator/divisor.js +0 -0
  294. /package/{dist → out}/parser/string/shift/operator/operator.d.ts +0 -0
  295. /package/{dist → out}/parser/string/shift/operator/operator.js +0 -0
  296. /package/{dist → out}/parser/string/shift/scanner.js +0 -0
@@ -0,0 +1,144 @@
1
+ import { attest } from "@arktype/attest"
2
+ import { scope } from "arktype"
3
+
4
+ const getCyclicScope = () =>
5
+ scope({
6
+ package: {
7
+ name: "string",
8
+ "dependencies?": "package[]",
9
+ "contributors?": "contributor[]"
10
+ },
11
+ contributor: {
12
+ email: "email",
13
+ "packages?": "package[]"
14
+ }
15
+ })
16
+
17
+ type Package = ReturnType<typeof getCyclicScope>["infer"]["package"]
18
+
19
+ const getCyclicData = () => {
20
+ const packageData = {
21
+ name: "arktype",
22
+ dependencies: [{ name: "typescript" }],
23
+ contributors: [{ email: "david@arktype.io" }]
24
+ } satisfies Package
25
+ packageData.dependencies.push(packageData)
26
+ return packageData
27
+ }
28
+
29
+ describe("cyclic", () => {
30
+ it("cyclic union", () => {
31
+ const $ = scope({
32
+ a: { b: "b|false" },
33
+ b: { a: "a|true" }
34
+ })
35
+ attest($.infer).type.toString.snap(
36
+ "{ a: { b: false | { a: true | any; }; }; b: { a: true | { b: false | any; }; }; }"
37
+ )
38
+ })
39
+ it("cyclic intersection", () => {
40
+ const $ = scope({
41
+ a: { b: "b&a" },
42
+ b: { a: "a&b" }
43
+ })
44
+ attest($.infer).type.toString.snap(
45
+ "{ a: { b: { a: { b: any; a: any; }; b: any; }; }; b: { a: { b: { a: any; b: any; }; a: any; }; }; }"
46
+ )
47
+ })
48
+ // TODO: reenable
49
+ it("cyclic", () => {
50
+ const types = scope({ a: { b: "b" }, b: { a: "a" } }).export()
51
+ // attest(types.a.node).snap({
52
+ // object: { props: { b: "b" } }
53
+ // })
54
+ // Type hint displays as "..." on hitting cycle (or any if "noErrorTruncation" is true)
55
+ attest<{
56
+ b: {
57
+ a: {
58
+ b: {
59
+ a: any
60
+ }
61
+ }
62
+ }
63
+ }>(types.a.infer)
64
+ attest<{
65
+ b: {
66
+ a: any
67
+ }
68
+ }>(types.b.infer.a.b.a.b.a.b.a)
69
+
70
+ // @ts-expect-error
71
+ attest(types.a.infer.b.a.b.c).type.errors.snap(
72
+ `Property 'c' does not exist on type '{ a: { b: ...; }; }'.`
73
+ )
74
+ })
75
+ it("allows valid", () => {
76
+ const types = getCyclicScope().export()
77
+ const data = getCyclicData()
78
+ attest(types.package(data).out).snap({
79
+ name: "arktype",
80
+ dependencies: [{ name: "typescript" }, "(cycle)" as any as Package],
81
+ contributors: [{ email: "david@arktype.io" }]
82
+ })
83
+ })
84
+ it("adds errors on invalid", () => {
85
+ const types = getCyclicScope().export()
86
+ const data = getCyclicData()
87
+ data.contributors[0].email = "ssalbdivad"
88
+ attest(types.package(data).errors?.summary).snap(
89
+ "dependencies/1/contributors/0/email must be a valid email (was 'ssalbdivad')\ncontributors/0/email must be a valid email (was 'ssalbdivad')"
90
+ )
91
+ })
92
+ it("can include cyclic data in message", () => {
93
+ const data = getCyclicData()
94
+ const nonSelfDependent = getCyclicScope().type([
95
+ "package",
96
+ ":",
97
+ (p) => !p.dependencies?.some((d) => d.name === p.name)
98
+ ])
99
+ attest(nonSelfDependent(data).errors?.summary).snap(
100
+ 'Must be valid (was {"name":"arktype","dependencies":[{"name":"typescript"},"(cycle)"],"contributors":[{"email":"david@arktype.io"}]})'
101
+ )
102
+ })
103
+
104
+ it("equivalent cycles reduce", () => {
105
+ // // TODO: reduce this case or create an issue
106
+ // const $ = scope({
107
+ // user: {
108
+ // friends: "user[]",
109
+ // name: "string"
110
+ // },
111
+ // admin: {
112
+ // friends: "user[]",
113
+ // name: "string"
114
+ // }
115
+ // })
116
+ })
117
+
118
+ it("union cyclic reference", () => {
119
+ const types = scope({
120
+ a: {
121
+ b: "b"
122
+ },
123
+ b: {
124
+ a: "a|3"
125
+ }
126
+ })
127
+ attest(types.infer).type.toString.snap(
128
+ "{ a: { b: { a: 3 | any; }; }; b: { a: 3 | { b: any; }; }; }"
129
+ )
130
+ })
131
+ it("intersect cyclic reference", () => {
132
+ const types = scope({
133
+ a: {
134
+ b: "b"
135
+ },
136
+ b: {
137
+ c: "a&b"
138
+ }
139
+ })
140
+ attest(types.infer).type.toString.snap(
141
+ "{ a: { b: { c: { b: any; c: any; }; }; }; b: { c: { b: any; c: any; }; }; }"
142
+ )
143
+ })
144
+ })
@@ -0,0 +1,32 @@
1
+ import { attest } from "@arktype/attest"
2
+ import { type } from "arktype"
3
+ import { writeInvalidDateMessage } from "../parser/string/shift/operand/date.js"
4
+
5
+ describe("date literal", () => {
6
+ it("base", () => {
7
+ const t = type("d'2000/05/05'")
8
+ attest<Date>(t.infer)
9
+ attest(t.allows(new Date("2000/05/05"))).equals(true)
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)
12
+ })
13
+ it("with punctuation", () => {
14
+ const ISO = type("d'2000-05-05T04:00:00.000Z'")
15
+ attest<Date>(ISO.infer)
16
+ attest(ISO.allows(new Date("2000/05/05"))).equals(true)
17
+ attest(ISO.allows(new Date("2000/07/05"))).equals(false)
18
+ })
19
+ it("allows spaces", () => {
20
+ const t = type("d' 2021 / 05 / 01 '")
21
+ attest(t.allows(new Date("2021/05/01"))).equals(true)
22
+ })
23
+ it("epoch", () => {
24
+ const now = new Date()
25
+ const t = type(`d'${now.valueOf()}'`)
26
+ attest(t.allows(now)).equals(true)
27
+ attest(t.allows(new Date(now.valueOf() + 1)))
28
+ })
29
+ it("invalid date", () => {
30
+ attest(() => type("d'tuesday'")).throws(writeInvalidDateMessage("tuesday"))
31
+ })
32
+ })
@@ -0,0 +1,123 @@
1
+ import { attest } from "@arktype/attest"
2
+ import { declare, type } from "arktype"
3
+
4
+ describe("declared", () => {
5
+ it("shallow", () => {
6
+ const shallow = declare<number>().type("number")
7
+ attest<number>(shallow.infer)
8
+ attest(shallow.json).equals(type("number").json)
9
+ })
10
+
11
+ it("obj", () => {
12
+ type Expected = { a: string; b?: number }
13
+ const t = declare<Expected>().type({
14
+ a: "string",
15
+ "b?": "number"
16
+ })
17
+ attest<Expected>(t.infer)
18
+ })
19
+
20
+ it("tuple", () => {
21
+ type Expected = [string, number]
22
+ const t = declare<Expected>().type(["string", "number"])
23
+ attest<Expected>(t.infer)
24
+ })
25
+
26
+ it("bad element", () => {
27
+ attest(
28
+ // @ts-expect-error
29
+ declare<[string, number]>().type(["string", "boolean"])
30
+ ).type.errors(
31
+ `Type 'string' is not assignable to type '{ declared: number; inferred: boolean; }'`
32
+ )
33
+ })
34
+
35
+ it("too short", () => {
36
+ // @ts-expect-error
37
+ attest(declare<[string, number]>().type(["string"])).type.errors(
38
+ `Source has 1 element(s) but target requires 2`
39
+ )
40
+ })
41
+
42
+ it("too long", () => {
43
+ attest(
44
+ // @ts-expect-error
45
+ declare<[string, number]>().type(["string", "number", "number"])
46
+ ).type.errors(`Source has 3 element(s) but target requires 2`)
47
+ })
48
+
49
+ it("tuple expression", () => {
50
+ const t = declare<0 | 1>().type(["0", "|", "1"])
51
+ attest<0 | 1>(t.infer)
52
+ })
53
+
54
+ it("regexp", () => {
55
+ const t = declare<string>().type(/.*/)
56
+ attest<string>(t.infer)
57
+ })
58
+
59
+ it("Inferred<t>", () => {
60
+ const foo = type("'foo'")
61
+ const t = declare<"foo">().type(foo)
62
+ attest<"foo">(t.infer)
63
+ })
64
+
65
+ it("bad tuple expression", () => {
66
+ attest(
67
+ // @ts-expect-error
68
+ declare<"foo" | "bar">().type(["'foo'", "|", "'baz'"])
69
+ ).type.errors(`{ declared: "foo" | "bar"; inferred: "foo" | "baz"; }`)
70
+ })
71
+
72
+ it("narrower", () => {
73
+ // @ts-expect-error
74
+ attest(() => declare<string>().type("'foo'")).type.errors(
75
+ `Argument of type 'string' is not assignable to parameter of type '{ declared: string; inferred: "foo"; }'`
76
+ )
77
+ })
78
+
79
+ it("wider", () => {
80
+ attest(() =>
81
+ declare<{ a: string }>().type({
82
+ // @ts-expect-error
83
+ a: "unknown"
84
+ })
85
+ ).type.errors(
86
+ `Type 'string' is not assignable to type '{ declared: string; inferred: unknown; }'`
87
+ )
88
+ })
89
+
90
+ it("missing key", () => {
91
+ attest(() =>
92
+ // @ts-expect-error
93
+ declare<{ a: string; b: number }>().type({
94
+ a: "string"
95
+ })
96
+ ).type.errors(
97
+ `Property 'b' is missing in type '{ a: "string"; }' but required in type '{ a: "string"; "b?": unknown; }'.`
98
+ )
99
+ })
100
+
101
+ it("missing optional key", () => {
102
+ attest(() =>
103
+ // @ts-expect-error
104
+ declare<{ a: string; b?: number }>().type({
105
+ a: "string"
106
+ })
107
+ ).type.errors(
108
+ `Property '"b?"' is missing in type '{ a: "string"; }' but required in type '{ a: "string"; "b?": unknown; }'.`
109
+ )
110
+ })
111
+
112
+ it("extraneous key", () => {
113
+ attest(() =>
114
+ declare<{ a: string }>().type({
115
+ a: "string",
116
+ // @ts-expect-error
117
+ b: "boolean"
118
+ })
119
+ ).type.errors(
120
+ `Object literal may only specify known properties, and 'b' does not exist in type '{ a: "string"; }'.`
121
+ )
122
+ })
123
+ })
@@ -0,0 +1,74 @@
1
+ // import { attest } from "@arktype/attest"
2
+ // import { scope, type } from "arktype"
3
+
4
+ describe("discrimination", () => {
5
+ // it("2 literal branches", () => {
6
+ // // should not use a switch with <=2 branches to avoid visual clutter
7
+ // const t = type("'a'|'b'")
8
+ // attest(t.json).snap({ unit: "a" })
9
+ // attest(t.allows("a")).equals(true)
10
+ // attest(t.allows("b")).equals(true)
11
+ // attest(t.allows("c")).equals(false)
12
+ // })
13
+ // it(">2 literal branches", () => {
14
+ // const t = type("'a'|'b'|'c'")
15
+ // attest(t.json).snap({ unit: "a" })
16
+ // attest(t.allows("a")).equals(true)
17
+ // attest(t.allows("b")).equals(true)
18
+ // attest(t.allows("c")).equals(true)
19
+ // attest(t.allows("d")).equals(false)
20
+ // })
21
+ // const getPlaces = () =>
22
+ // scope({
23
+ // rainForest: {
24
+ // climate: "'wet'",
25
+ // color: "'green'",
26
+ // isRainForest: "true"
27
+ // },
28
+ // desert: { climate: "'dry'", color: "'brown'", isDesert: "true" },
29
+ // sky: { climate: "'dry'", color: "'blue'", isSky: "true" },
30
+ // ocean: { climate: "'wet'", color: "'blue'", isOcean: "true" }
31
+ // })
32
+ // it("nested", () => {
33
+ // const t = getPlaces().type("ocean|sky|rainForest|desert")
34
+ // attest(t.json).snap()
35
+ // })
36
+ // it("undiscriminable", () => {
37
+ // const t = getPlaces().type([
38
+ // "ocean",
39
+ // "|",
40
+ // {
41
+ // climate: "'wet'",
42
+ // color: "'blue'",
43
+ // indistinguishableFrom: "ocean"
44
+ // }
45
+ // ])
46
+ // })
47
+ // it("doesn't discriminate optional key", () => {
48
+ // const t = type({
49
+ // direction: "'forward' | 'backward'",
50
+ // "operator?": "'by'"
51
+ // }).or({
52
+ // duration: "'s' | 'min' | 'h'",
53
+ // operator: "'to'"
54
+ // })
55
+ // attest(t.root.hasKind("union") && t.root.discriminant).equals(null)
56
+ // })
57
+ // it("default case", () => {
58
+ // const t = getPlaces().type([
59
+ // "ocean|rainForest",
60
+ // "|",
61
+ // { temperature: "'hot'" }
62
+ // ])
63
+ // })
64
+ // it("discriminable default", () => {
65
+ // const t = getPlaces().type([
66
+ // { temperature: "'cold'" },
67
+ // "|",
68
+ // ["ocean|rainForest", "|", { temperature: "'hot'" }]
69
+ // ])
70
+ // })
71
+ // it("won't discriminate between possibly empty arrays", () => {
72
+ // const t = type("string[]|boolean[]")
73
+ // })
74
+ })
@@ -0,0 +1,90 @@
1
+ import { attest } from "@arktype/attest"
2
+ import { writeIndivisibleMessage } from "@arktype/schema"
3
+ import { type } from "arktype"
4
+ import { writeInvalidDivisorMessage } from "../parser/string/shift/operator/divisor.js"
5
+
6
+ describe("divisibility", () => {
7
+ describe("parse", () => {
8
+ it("integer literal", () => {
9
+ const divisibleByTwo = type("number%2")
10
+ attest<number>(divisibleByTwo.infer)
11
+ attest(divisibleByTwo.json).snap({ basis: "number", divisor: 2 })
12
+ })
13
+ it("whitespace after %", () => {
14
+ const t = type("number % 5")
15
+ attest<number>(t.infer)
16
+ attest(t.json).snap({ basis: "number", divisor: 5 })
17
+ })
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"
23
+ )
24
+ })
25
+ it("allows non-narrowed divisor", () => {
26
+ const d = 5 as number
27
+ attest<number>(type(`number%${d}`).infer)
28
+ })
29
+ it("fails at runtime on non-integer divisor", () => {
30
+ attest(() => type("number%2.3")).throws(writeInvalidDivisorMessage("2.3"))
31
+ })
32
+ it("non-numeric divisor", () => {
33
+ // @ts-expect-error
34
+ attest(() => type("number%foobar")).throwsAndHasTypeError(
35
+ writeInvalidDivisorMessage("foobar")
36
+ )
37
+ })
38
+ it("zero divisor", () => {
39
+ // @ts-expect-error
40
+ attest(() => type("number%0")).throwsAndHasTypeError(
41
+ writeInvalidDivisorMessage(0)
42
+ )
43
+ })
44
+ it("unknown", () => {
45
+ // @ts-expect-error
46
+ attest(() => type("unknown%2")).throwsAndHasTypeError(
47
+ writeIndivisibleMessage("unknown")
48
+ )
49
+ })
50
+ it("indivisible", () => {
51
+ // @ts-expect-error
52
+ attest(() => type("string%1")).throwsAndHasTypeError(
53
+ writeIndivisibleMessage("string")
54
+ )
55
+ })
56
+ it("overlapping", () => {
57
+ // @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")
61
+ })
62
+ })
63
+ describe("intersection", () => {
64
+ it("identical", () => {
65
+ const t = type("number%2&number%2")
66
+ attest(t.json).equals(type("number%2").json)
67
+ })
68
+ it("purely divisible", () => {
69
+ const t = type("number%4&number%2")
70
+ attest(t.json).equals(type("number%4").json)
71
+ })
72
+ it("common divisor", () => {
73
+ const t = type("number%6&number%4")
74
+ attest(t.json).equals(type("number%12").json)
75
+ })
76
+ it("relatively prime", () => {
77
+ const t = type("number%2&number%3")
78
+ attest(t.json).equals(type("number%6").json)
79
+ })
80
+ it("valid literal", () => {
81
+ const t = type("number%5&0")
82
+ attest(t.json).equals(type("0").json)
83
+ })
84
+ it("invalid literal", () => {
85
+ attest(() => type("number%3&8")).throws(
86
+ "Intersection of (a multiple of 3) and 8 results in an unsatisfiable type"
87
+ )
88
+ })
89
+ })
90
+ })
@@ -0,0 +1,93 @@
1
+ import { attest } from "@arktype/attest"
2
+ import { type } from "arktype"
3
+ import { writeUnterminatedEnclosedMessage } from "../parser/string/shift/operand/enclosed.js"
4
+
5
+ describe("parse enclosed", () => {
6
+ it("with spaces", () => {
7
+ const t = type("'this has spaces'")
8
+ attest<"this has spaces">(t.infer)
9
+ attest(t.json).snap({ unit: "this has spaces" })
10
+ })
11
+ it("with neighbors", () => {
12
+ const t = type("'foo'|/.*/[]")
13
+ attest<"foo" | string[]>(t.infer)
14
+ attest(t.json).snap([{ unit: "foo" }, { basis: "Array" }])
15
+ })
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
+ })
37
+ })
38
+ it("single-quoted", () => {
39
+ attest<"hello">(type("'hello'").infer)
40
+ })
41
+ it("double-quoted", () => {
42
+ attest<"goodbye">(type('"goodbye"').infer)
43
+ })
44
+ it("regex literal", () => {
45
+ attest<string>(type("/.*/").infer)
46
+ })
47
+ it("invalid regex", () => {
48
+ attest(() => type("/[/")).throws(
49
+ "Invalid regular expression: /[/: Unterminated character class"
50
+ )
51
+ })
52
+ it("mixed quote types", () => {
53
+ attest<"'single-quoted'">(type(`"'single-quoted'"`).infer)
54
+ attest<'"double-quoted"'>(type(`'"double-quoted"'`).infer)
55
+ })
56
+ it("ignores enclosed operators", () => {
57
+ attest<"yes|no|maybe">(type("'yes|no|maybe'").infer)
58
+ })
59
+ it("mix of enclosed and unenclosed operators", () => {
60
+ attest<"yes|no" | "true|false">(type("'yes|no'|'true|false'").infer)
61
+ })
62
+ it("escaped enclosing", () => {
63
+ const t = type("'don\\'t'")
64
+ attest<"don't">(t.infer)
65
+ })
66
+ it("string literal stress", () => {
67
+ const s = `"3.
68
+ 14159265358979323846264338327950288419716939937510
69
+ 58209749445923078164062862089986280348253421170679
70
+ 82148086513282306647093844609550582231725359408128
71
+ 48111745028410270193852110555964462294895493038196
72
+ 44288109756659334461284756482337867831652712019091
73
+ 45648566923460348610454326648213393607260249141273
74
+ 72458700660631558817488152092096282925409171536436
75
+ 78925903600113305305488204665213841469519415116094
76
+ 33057270365759591953092186117381932611793105118548
77
+ 07446237996274956735188575272489122793818301194912
78
+ 98336733624406566430860213949463952247371907021798
79
+ 60943702770539217176293176752384674818467669405132
80
+ 00056812714526356082778577134275778960917363717872
81
+ 14684409012249534301465495853710507922796892589235
82
+ 42019956112129021960864034418159813629774771309960
83
+ 51870721134999999837297804995105973173281609631859
84
+ 50244594553469083026425223082533446850352619311881
85
+ 71010003137838752886587533208381420617177669147303
86
+ 59825349042875546873115956286388235378759375195778
87
+ 185778053217122680661300192"`
88
+ // parses exactly 1001 characters before hitting a recursion limit
89
+ const t = type(s)
90
+ type Expected = typeof s extends `"${infer enclosed}"` ? enclosed : never
91
+ attest<Expected>(t.infer)
92
+ })
93
+ })
@@ -0,0 +1,56 @@
1
+ describe("snippets", () => {
2
+ // it("demo", async () => {
3
+ // const typeSnippet = await import("../../docs/examples/demo.js")
4
+ // attest<{>(typeSnippet.pkg.infer)
5
+ // name: string
6
+ // version: string
7
+ // contributors?: string[]
8
+ // }
9
+ // attest(typeSnippet.errors?.summary).snap(
10
+ // "contributors must be more than 1 items long (was 1)"
11
+ // )
12
+ // })
13
+ // it("type", async () => {
14
+ // const typeSnippet = await import("./benches/type.js")
15
+ // attest<{>(typeSnippet.user.infer)
16
+ // name: string
17
+ // device: {
18
+ // platform: "android" | "ios"
19
+ // version?: number
20
+ // }
21
+ // }
22
+ // attest(typeSnippet.errors?.summary).snap(
23
+ // "device/platform must be 'android' or 'ios' (was 'enigma')"
24
+ // )
25
+ // })
26
+ // // it("scope", async () => {
27
+ // // const scopeSnippet = await import("../examples/scope.js")
28
+ // // attest<{>(scopeSnippet.types.package.infer)
29
+ // // name: string
30
+ // // dependencies?: any[]
31
+ // // devDependencies?: any[]
32
+ // // contributors?: {
33
+ // // email: string
34
+ // // packages?: any[]
35
+ // // }[]
36
+ // // }
37
+ // // attest(scopeSnippet.errors?.summary).snap(
38
+ // // "dependencies/0/dependencies/0/contributors/0/email must be a valid email (was 'david@sharktypeio')\ncontributors/0/email must be a valid email (was 'david@sharktypeio')"
39
+ // // )
40
+ // // })
41
+ // it("optimized", async () => {
42
+ // const example = await import("../../docs/examples/optimized.js")
43
+ // attest(example.deepLeftOrRight.infer).typed as
44
+ // | {
45
+ // auto: {
46
+ // discriminated: "left"
47
+ // }
48
+ // }
49
+ // | {
50
+ // auto: {
51
+ // discriminated: "right"
52
+ // }
53
+ // }
54
+ // attest<number>(example.numericIntersection.infer)
55
+ // })
56
+ })