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

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,409 @@
1
+ import { attest } from "@arktype/attest"
2
+ import {
3
+ writeUndiscriminableMorphUnionMessage,
4
+ type ArkTypeError,
5
+ type Out
6
+ } from "@arktype/schema"
7
+ import { scope, type, type Ark, type Type } from "arktype"
8
+
9
+ describe("morph", () => {
10
+ it("base", () => {
11
+ const t = type("boolean").morph((data) => `${data}`)
12
+ attest<Type<(In: boolean) => Out<string>>>(t)
13
+ attest<string>(t.infer)
14
+ attest<boolean>(t.in.infer)
15
+ const result = t(true)
16
+ if (result.errors) {
17
+ return result.errors.throw()
18
+ }
19
+ attest<string>(result.out).equals("true")
20
+ attest(t("foo").errors?.summary).snap("Must be boolean (was string)")
21
+ attest(t.root).equals(type(["boolean", "=>", (data) => `${data}`]).root)
22
+ })
23
+ it("endomorph", () => {
24
+ const t = type(["boolean", "=>", (data) => !data])
25
+ attest<Type<(In: boolean) => Out<boolean>>>(t)
26
+ const result = t(true)
27
+ if (result.errors) {
28
+ return result.errors.throw()
29
+ }
30
+ attest<boolean>(result.out).equals(false)
31
+ })
32
+ // it("chained to type", () => {
33
+ // const t = type(["string>5", "=>", arktypes.parse.date])
34
+ // attest<Type<(In: string) => Out<Date>>>(t)
35
+ // attest(t("5/21/1993").out?.getDate()).equals(21)
36
+ // attest(t("foobar").errors?.summary).snap(
37
+ // "Must be a valid date (was 'foobar')"
38
+ // )
39
+ // })
40
+ it("validated output", () => {
41
+ const parsedUser = type("string").morph((s) => JSON.parse(s), {
42
+ name: "string",
43
+ age: "number"
44
+ })
45
+ attest<
46
+ Type<
47
+ (In: string) => Out<{
48
+ name: string
49
+ age: number
50
+ }>,
51
+ Ark
52
+ >
53
+ >(parsedUser)
54
+ })
55
+ it("any as out", () => {
56
+ const t = type("string", "=>", (s) => s as any)
57
+ attest<string>(t.in.infer)
58
+ attest<any>(t.infer)
59
+ })
60
+ it("never as out", () => {
61
+ const t = type("string", "=>", (s) => s as never)
62
+ attest<string>(t.in.infer)
63
+ attest<never>(t.infer)
64
+ })
65
+ // it("return problem", () => {
66
+ // const divide100By = type([
67
+ // "number",
68
+ // "=>",
69
+ // (n, errors) => (n === 0 ? errors.mustBe("non-zero", n, []) : 100 / n)
70
+ // ])
71
+ // attest<Type<(In: number) => Out<number>>>(divide100By)
72
+ // attest(divide100By(5).out).equals(20)
73
+ // attest(divide100By(0).errors?.summary).snap("Must be non-zero (was 0)")
74
+ // })
75
+ it("adds a problem if one is returned without being added", () => {
76
+ const divide100By = type([
77
+ "number",
78
+ "=>",
79
+ (n, errors) => {
80
+ if (n !== 0) {
81
+ return 100 / n
82
+ } else {
83
+ // errors.mustBe("non-zero")
84
+ // errors.byPath = {}
85
+ return (errors as unknown as ArkTypeError[]).pop()
86
+ }
87
+ }
88
+ ])
89
+ attest(divide100By(0).errors?.summary).snap("Must be non-zero (was 0)")
90
+ })
91
+ it("at path", () => {
92
+ const t = type({ a: ["string", "=>", (data) => data.length] })
93
+ attest<Type<{ a: (In: string) => Out<number> }>>(t)
94
+
95
+ const result = t({ a: "four" })
96
+ if (result.errors) {
97
+ return result.errors.throw()
98
+ }
99
+ attest<{ a: number }>(result.out).equals({ a: 4 })
100
+ })
101
+ it("in array", () => {
102
+ const types = scope({
103
+ lengthOfString: ["string", "=>", (data) => data.length],
104
+ mapToLengths: "lengthOfString[]"
105
+ }).export()
106
+ attest<Type<((In: string) => Out<number>)[]>>(types.mapToLengths)
107
+ const result = types.mapToLengths(["1", "22", "333"])
108
+ if (result.errors) {
109
+ return result.errors.throw()
110
+ }
111
+ attest<number[]>(result.out).equals([1, 2, 3])
112
+ })
113
+ it("object inference", () => {
114
+ const t = type([{ a: "string" }, "=>", (data) => `${data}`])
115
+ attest<Type<(In: { a: string }) => Out<string>>>(t)
116
+ })
117
+ it("intersection", () => {
118
+ const $ = scope({
119
+ b: "3.14",
120
+ a: () => $.type("number"), //.morph((data) => `${data}`),
121
+ aAndB: () => $.type("a&b"),
122
+ bAndA: () => $.type("b&a")
123
+ })
124
+ // const types = $.compile()
125
+ // attest<Type<(In: 3.14) => string>>(types.aAndB)
126
+ // attest(types.aAndB.node).snap({
127
+ // number: { rules: { value: 3.14 }, morph: "(function)" }
128
+ // })
129
+ // attest<typeof types.aAndB>(types.bAndA)
130
+ // attest(types.bAndA.node).equals(types.aAndB.node)
131
+ })
132
+ it("object intersection", () => {
133
+ // const $ = scope({
134
+ // a: () => $.type({ a: "1" }).morph((data) => `${data}`),
135
+ // b: { b: "2" },
136
+ // c: "a&b"
137
+ // })
138
+ // const types = $.compile()
139
+ // attest<Type<(In: { a: 1; b: 2 }) => string>>(types.c)
140
+ // attest(types.c.node).snap({
141
+ // object: {
142
+ // rules: {
143
+ // props: {
144
+ // a: { number: { value: 1 } },
145
+ // b: { number: { value: 2 } }
146
+ // }
147
+ // },
148
+ // morph: "(function)"
149
+ // }
150
+ // })
151
+ })
152
+ it("union", () => {
153
+ const types = scope({
154
+ a: ["number", "=>", (data) => `${data}`],
155
+ b: "boolean",
156
+ aOrB: "a|b",
157
+ bOrA: "b|a"
158
+ }).export()
159
+ attest<Type<boolean | ((In: number) => Out<string>)>>(types.aOrB)
160
+ // attest(types.aOrB.node).snap({
161
+ // number: { rules: {}, morph: "(function)" },
162
+ // boolean: true
163
+ // })
164
+ // attest<typeof types.aOrB>(types.bOrA)
165
+ // attest(types.bOrA.node).equals(types.aOrB.node)
166
+ })
167
+ it("union with output", () => {
168
+ const t = type("number|parse.number")
169
+ attest<number>(t.infer)
170
+ })
171
+ it("deep intersection", () => {
172
+ // TODO: Fix
173
+ // const types = scope({
174
+ // a: { a: ["number>0", "=>", (data) => data + 1] },
175
+ // b: { a: "1" },
176
+ // c: "a&b"
177
+ // }).export()
178
+ // attest<Type<{ a: (In: 1) => Out<number> }>>(types.c)
179
+ // attest(types.c.node).snap({
180
+ // object: {
181
+ // props: {
182
+ // a: { number: { rules: { value: 1 }, morph: "(function)" } }
183
+ // }
184
+ // }
185
+ // })
186
+ })
187
+ it("deep union", () => {
188
+ const types = scope({
189
+ a: { a: ["number>0", "=>", (data) => `${data}`] },
190
+ b: { a: "Function" },
191
+ c: "a|b"
192
+ }).export()
193
+ attest<
194
+ Type<
195
+ | {
196
+ a: (In: number) => Out<string>
197
+ }
198
+ | {
199
+ a: Function
200
+ }
201
+ >
202
+ >(types.c)
203
+
204
+ // attest(types.c.node).snap({
205
+ // object: [
206
+ // {
207
+ // props: {
208
+ // a: {
209
+ // number: {
210
+ // rules: {
211
+ // range: {
212
+ // min: { limit: 0, comparator: ">" }
213
+ // }
214
+ // },
215
+ // morph: "(function)"
216
+ // }
217
+ // }
218
+ // }
219
+ // },
220
+ // { props: { a: "Function" } }
221
+ // ]
222
+ // })
223
+ })
224
+ it("chained reference", () => {
225
+ const $ = scope({
226
+ a: type("string").morph((s) => s.length),
227
+ b: () => $.type("a").morph((n) => n === 0)
228
+ })
229
+ const types = $.export()
230
+ attest<Type<(In: string) => Out<boolean>>>(types.b)
231
+ // attest(types.b.node).snap({
232
+ // string: { rules: {}, morph: ["(function)", "(function)"] }
233
+ // })
234
+ })
235
+ it("chained nested", () => {
236
+ const $ = scope({
237
+ a: type("string").morph((s) => s.length),
238
+ b: () => $.type({ a: "a" }).morph(({ a }) => a === 0)
239
+ })
240
+
241
+ const types = $.export()
242
+ attest<Type<(In: { a: string }) => Out<boolean>>>(types.b)
243
+ // attest(types.b.node).snap({
244
+ // object: { rules: { props: { a: "a" } }, morph: "(function)" }
245
+ // })
246
+ })
247
+ it("directly nested", () => {
248
+ const t = type([
249
+ {
250
+ a: ["string", "=>", (s: string) => s.length]
251
+ },
252
+ "=>",
253
+ ({ a }) => a === 0
254
+ ])
255
+ attest<Type<(In: { a: string }) => Out<boolean>>>(t)
256
+ // attest(t.node).snap({
257
+ // object: {
258
+ // rules: {
259
+ // props: { a: { string: { rules: {}, morph: "(function)" } } }
260
+ // },
261
+ // morph: "(function)"
262
+ // }
263
+ // })
264
+ })
265
+ it("discriminable tuple union", () => {
266
+ const $ = scope({
267
+ a: () => $.type(["string"]).morph((s) => [...s, "!"]),
268
+ b: ["boolean"],
269
+ c: () => $.type("a|b")
270
+ })
271
+ const types = $.export()
272
+ attest<Type<[boolean] | ((In: [string]) => Out<string[]>)>>(types.c)
273
+ })
274
+ it("double intersection", () => {
275
+ // attest(() => {
276
+ // const z = scope({
277
+ // a: ["boolean", "=>", (data) => `${data}`],
278
+ // b: ["boolean", "=>", (data) => `${data}!!!`],
279
+ // c: "a&b"
280
+ // }).compile()
281
+ // }).throws("Intersection of morphs results in an unsatisfiable type")
282
+ })
283
+ it("undiscriminated union", () => {
284
+ // TODO: fix
285
+ // attest(() => {
286
+ // scope({
287
+ // a: ["/.*/", "=>", (s) => s.trim()],
288
+ // b: "string",
289
+ // c: "a|b"
290
+ // }).export()
291
+ // }).throws(writeUndiscriminableMorphUnionMessage("/"))
292
+ })
293
+ it("deep double intersection", () => {
294
+ attest(() => {
295
+ // const s = scope({
296
+ // a: { a: ["boolean", "=>", (data) => `${data}`] },
297
+ // b: { a: ["boolean", "=>", (data) => `${data}!!!`] },
298
+ // c: "a&b"
299
+ // }).compile()
300
+ }).throws("At a: Intersection of morphs results in an unsatisfiable type")
301
+ })
302
+ it("deep undiscriminated union", () => {
303
+ attest(() => {
304
+ scope({
305
+ a: { a: ["string", "=>", (s) => s.trim()] },
306
+ b: { a: "'foo'" },
307
+ c: "a|b"
308
+ }).export()
309
+ }).throws(writeUndiscriminableMorphUnionMessage("/"))
310
+ })
311
+ it("deep undiscriminated reference", () => {
312
+ const $ = scope({
313
+ a: { a: ["string", "=>", (s) => s.trim()] },
314
+ b: { a: "boolean" },
315
+ c: { b: "boolean" }
316
+ })
317
+ const t = $.type("a|b")
318
+ attest<
319
+ Type<
320
+ | {
321
+ a: (In: string) => Out<string>
322
+ }
323
+ | {
324
+ a: boolean
325
+ }
326
+ >
327
+ >(t)
328
+
329
+ attest(() => {
330
+ scope({
331
+ a: { a: ["string", "=>", (s) => s.trim()] },
332
+ b: { b: "boolean" },
333
+ c: "a|b"
334
+ }).export()
335
+ }).throws(writeUndiscriminableMorphUnionMessage("/"))
336
+ })
337
+ it("array double intersection", () => {
338
+ // attest(() => {
339
+ // scope({
340
+ // a: { a: ["number>0", "=>", (data) => data + 1] },
341
+ // b: { a: ["number>0", "=>", (data) => data + 2] },
342
+ // c: "a[]&b[]"
343
+ // }).export()
344
+ // }).throws(
345
+ // "At [index]/a: Intersection of morphs results in an unsatisfiable type"
346
+ // )
347
+ })
348
+ it("undiscriminated morph at path", () => {
349
+ attest(() => {
350
+ scope({
351
+ a: { a: ["string", "=>", (s) => s.trim()] },
352
+ b: { b: "boolean" },
353
+ c: { key: "a|b" }
354
+ }).export()
355
+ }).throws(writeUndiscriminableMorphUnionMessage("key"))
356
+ })
357
+ it("helper morph intersection", () => {
358
+ attest(() =>
359
+ type("string")
360
+ .morph((s) => s.length)
361
+ .and(type("string").morph((s) => s.length))
362
+ ).throws("Intersection of morphs results in an unsatisfiable type")
363
+ })
364
+ it("union helper undiscriminated", () => {
365
+ attest(() =>
366
+ type("string")
367
+ .morph((s) => s.length)
368
+ .or("'foo'")
369
+ ).throws(writeUndiscriminableMorphUnionMessage("/"))
370
+ })
371
+ // it("problem not included in return", () => {
372
+ // const parsedInt = type([
373
+ // "string",
374
+ // "=>",
375
+ // (s, errors) => {
376
+ // const result = parseInt(s)
377
+ // if (Number.isNaN(result)) {
378
+ // return errors.mustBe("an integer string", s, [])
379
+ // }
380
+ // return result
381
+ // }
382
+ // ])
383
+ // attest<Type<(In: string) => Out<number>>>(parsedInt)
384
+ // attest(parsedInt("5").out).snap(5)
385
+ // attest(parsedInt("five").errors?.summary).snap(
386
+ // "Must be an integer string (was 'five')"
387
+ // )
388
+ // })
389
+ it("nullable return", () => {
390
+ const toNullableNumber = type(["string", "=>", (s) => s.length || null])
391
+ attest<Type<(In: string) => Out<number | null>>>(toNullableNumber)
392
+ })
393
+ it("undefinable return", () => {
394
+ const toUndefinableNumber = type([
395
+ "string",
396
+ "=>",
397
+ (s) => s.length || undefined
398
+ ])
399
+ attest<Type<(In: string) => Out<number | undefined>>>(toUndefinableNumber)
400
+ })
401
+ it("null or undefined return", () => {
402
+ const toMaybeNumber = type([
403
+ "string",
404
+ "=>",
405
+ (s) => (s.length === 0 ? undefined : s.length === 1 ? null : s.length)
406
+ ])
407
+ attest<Type<(In: string) => Out<number | null | undefined>>>(toMaybeNumber)
408
+ })
409
+ })
@@ -0,0 +1,102 @@
1
+ import { attest } from "@arktype/attest"
2
+ import type { Out } from "@arktype/schema"
3
+ import type { equals } from "@arktype/util"
4
+ import { type, type Type } from "arktype"
5
+
6
+ describe("narrow", () => {
7
+ it("implicit problem", () => {
8
+ const isOdd = (n: number) => n % 2 === 1
9
+ const odd = type(["number", ":", isOdd])
10
+ attest<number>(odd.infer)
11
+ // attest(odd.node).equals({ number: { narrow: isOdd as any } })
12
+ attest(odd(1).out).equals(1)
13
+ attest(odd(2).errors?.summary).snap(
14
+ "Must be valid according to isOdd (was 2)"
15
+ )
16
+ })
17
+ it("implicit problem anonymous", () => {
18
+ const even = type(["number", ":", (n) => n % 2 === 0])
19
+ attest(even(1).errors?.summary).snap("Must be valid (was 1)")
20
+ })
21
+ it("explicit problem", () => {
22
+ const even = type([
23
+ "number",
24
+ ":",
25
+ (n, ctx) => n % 3 === 0 || ctx.invalid({ expected: "divisible by 3" })
26
+ ])
27
+ attest(even(1).errors?.summary).snap("Must be divisible by 3 (was 1)")
28
+ })
29
+ // it("problem at path", () => {
30
+ // type([{ s: "string" }])
31
+ // const abEqual = type([
32
+ // {
33
+ // a: "number",
34
+ // b: "number"
35
+ // },
36
+ // ":",
37
+ // ({ a, b }, ctx) => {
38
+ // if (a === b) {
39
+ // return true
40
+ // }
41
+ // // a, ["a"]
42
+ // ctx.error("equal to b")
43
+ // // b, ["b"]
44
+ // ctx.error("equal to a")
45
+ // return false
46
+ // }
47
+ // ])
48
+ // attest(abEqual({ a: 1, b: 1 }).out).equals({ a: 1, b: 1 })
49
+ // attest(abEqual({ a: 1, b: 2 }).errors?.summary).snap(
50
+ // 'a must be equal to b (was {"a":1,"b":2})\nb must be equal to a (was {"a":1,"b":2})'
51
+ // )
52
+ // })
53
+ it("functional predicate", () => {
54
+ const one = type(["number", ":", (n): n is 1 => n === 1])
55
+ attest<Type<1>>(one)
56
+ })
57
+ it("functional parameter inference", () => {
58
+ type Expected = number | boolean[]
59
+ const validateNumberOrBooleanList = <t>(
60
+ t: equals<t, Expected> extends true ? t : Expected
61
+ ) => true
62
+ attest<number | boolean[]>(
63
+ type([
64
+ "number|boolean[]",
65
+ ":",
66
+ (data) => validateNumberOrBooleanList(data)
67
+ ]).infer
68
+ )
69
+ attest(() => {
70
+ // @ts-expect-error
71
+ type(["number|boolean[]", ":", (data: number | string[]) => !!data])
72
+ }).type.errors("Type 'boolean' is not assignable to type 'string'.")
73
+ })
74
+ it("narrow problem", () => {
75
+ const palindrome = type([
76
+ "string",
77
+ ":",
78
+ (s, ctx) =>
79
+ s === [...s].reverse().join("") ? true : ctx.invalid("a palindrome")
80
+ ])
81
+ attest<Type<string>>(palindrome)
82
+ attest(palindrome("dad").out).snap("dad")
83
+ attest(palindrome("david").errors?.summary).snap(
84
+ "Must be a palindrome (was 'david')"
85
+ )
86
+ })
87
+ it("narrows the output type of a morph", () => {
88
+ const t = type("string")
89
+ .morph((s) => s.length)
90
+ .narrow((n): n is 5 => n === 5)
91
+ attest<Type<(In: string) => Out<5>>>(t)
92
+ attest(t.json).snap({ domain: "string" })
93
+ })
94
+ it("expression", () => {
95
+ const t = type("string", ":", (s): s is `f${string}` => s[0] === "f")
96
+ attest<`f${string}`>(t.infer)
97
+ attest(t.json).snap({
98
+ basis: "string",
99
+ predicate: ["$ark.anonymousFunction8"]
100
+ })
101
+ })
102
+ })
@@ -0,0 +1,190 @@
1
+ // describe("node definitions", () => {
2
+ // describe("basis", () => {
3
+ // it("domain", () => {
4
+ // const t = node({
5
+ // basis: "string"
6
+ // })
7
+ // attest<TypeNode<string>>(t)
8
+ // })
9
+ // it("class", () => {
10
+ // const t = node({
11
+ // basis: Date
12
+ // })
13
+ // attest<TypeNode<Date>>(t)
14
+ // })
15
+ // it("value", () => {
16
+ // const t = node({
17
+ // basis: ["===", 3.14159]
18
+ // })
19
+ // attest<TypeNode<3.14159>>(t)
20
+ // })
21
+ // })
22
+ // it("optional props", () => {
23
+ // const t = node({
24
+ // basis: "object",
25
+ // props: {
26
+ // a: {
27
+ // value: { basis: "string" }
28
+ // },
29
+ // b: {
30
+ // optional: true,
31
+ // value: { basis: "number" }
32
+ // }
33
+ // }
34
+ // })
35
+ // attest<TypeNode<{>(t)
36
+ // a: string
37
+ // b?: boolean
38
+ // }>
39
+ // })
40
+ // it("arrays", () => {
41
+ // const t = node({
42
+ // basis: Array,
43
+ // props: [
44
+ // {},
45
+ // {
46
+ // key: arrayIndexInput(),
47
+ // value: {
48
+ // basis: "object",
49
+ // props: {
50
+ // name: {
51
+ // value: { basis: "string" }
52
+ // }
53
+ // }
54
+ // }
55
+ // }
56
+ // ]
57
+ // })
58
+ // attest<TypeNode<{ name: string }[]>>(t)
59
+ // })
60
+ // it("variadic tuple", () => {
61
+ // const t = node({
62
+ // basis: Array,
63
+ // props: [
64
+ // {
65
+ // 0: {
66
+ // value: { basis: "string" }
67
+ // },
68
+ // // works for numeric or string keys
69
+ // "1": {
70
+ // value: { basis: "number" }
71
+ // }
72
+ // },
73
+ // {
74
+ // key: arrayIndexInput(2),
75
+ // value: {
76
+ // basis: "symbol"
77
+ // }
78
+ // }
79
+ // ]
80
+ // })
81
+ // attest<TypeNode<[string, number, ...symbol[]]>>(t)
82
+ // })
83
+ // it("non-variadic tuple", () => {
84
+ // const t = node({
85
+ // basis: Array,
86
+ // props: {
87
+ // 0: {
88
+ // value: {
89
+ // basis: "object",
90
+ // props: {
91
+ // a: { value: { basis: "string" } },
92
+ // b: { value: { basis: "number" } }
93
+ // }
94
+ // }
95
+ // },
96
+ // 1: {
97
+ // value: {
98
+ // basis: ["===", "arktype"]
99
+ // }
100
+ // },
101
+ // length: {
102
+ // prerequisite: true,
103
+ // value: { basis: ["===", 2] }
104
+ // }
105
+ // }
106
+ // })
107
+ // attest<TypeNode<>(t)
108
+ // [
109
+ // {
110
+ // a: string
111
+ // b: number
112
+ // },
113
+ // "arktype"
114
+ // ]
115
+ // >
116
+ // })
117
+ // it("branches", () => {
118
+ // const t = node(
119
+ // { basis: ["===", "foo"] },
120
+ // { basis: ["===", "bar"] },
121
+ // { basis: "number" },
122
+ // {
123
+ // basis: "object",
124
+ // props: { a: { value: { basis: "bigint" } } }
125
+ // }
126
+ // )
127
+ // attest<TypeNode<number | "foo" | "bar" | { a: bigint }>>(t)
128
+ // })
129
+ // it("narrow", () => {
130
+ // const t = node({
131
+ // basis: "string",
132
+ // narrow: (s): s is "foo" => s === "foo"
133
+ // })
134
+ // attest<TypeNode<"foo">>(t)
135
+ // })
136
+ // it("narrow array", () => {
137
+ // const t = node({
138
+ // basis: "object",
139
+ // narrow: [
140
+ // (o): o is { a: string } => typeof o.a === "string",
141
+ // (o): o is { b: boolean } => typeof o.b === "boolean"
142
+ // ] as const
143
+ // })
144
+ // attest<TypeNode<{>(t)
145
+ // a: string
146
+ // b: boolean
147
+ // }>
148
+ // })
149
+ // it("morph", () => {
150
+ // const t = node({
151
+ // basis: "string",
152
+ // morph: (s: string) => s.length
153
+ // })
154
+ // attest<TypeNode<(In: string) => Out<number>>>(t)
155
+ // })
156
+ // it("morph list", () => {
157
+ // const t = node({
158
+ // basis: "string",
159
+ // morph: [(s: string) => s.length, (n: number) => ({ n })] as const
160
+ // })
161
+ // attest<TypeNode<(In: string) => Out<{ n: number }>>>(t)
162
+ // })
163
+ // it("never", () => {
164
+ // const t = node()
165
+ // attest<TypeNode<never>>(t)
166
+ // })
167
+ // it("errors on rule in wrong domain", () => {
168
+ // attest(() =>
169
+ // node({
170
+ // basis: "number",
171
+ // divisor: 5,
172
+ // // @ts-expect-error
173
+ // regex: "/.*/"
174
+ // })
175
+ // ).throws.snap(
176
+ // "Error: regex constraint may only be applied to a string (was number)"
177
+ // )
178
+ // })
179
+ // it("errors on filter literal", () => {
180
+ // attest(() =>
181
+ // node({
182
+ // basis: ["===", true],
183
+ // // @ts-expect-error
184
+ // narrow: (b: boolean) => b === true
185
+ // })
186
+ // ).throws(
187
+ // "narrow constraint may only be applied to a non-literal type (was true)"
188
+ // )
189
+ // })
190
+ // })