arktype 2.0.0-dev.1 → 2.0.0-dev.11

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 (221) hide show
  1. package/out/api.d.ts +6 -0
  2. package/out/api.js +5 -0
  3. package/out/ark.d.ts +15 -39
  4. package/out/ark.js +9 -18
  5. package/out/config.d.ts +1 -0
  6. package/out/config.js +2 -0
  7. package/out/generic.d.ts +21 -0
  8. package/out/generic.js +24 -0
  9. package/out/main.d.ts +6 -7
  10. package/out/main.js +4 -3
  11. package/out/match.d.ts +57 -34
  12. package/out/match.js +51 -17
  13. package/out/module.d.ts +12 -0
  14. package/out/module.js +3 -0
  15. package/out/parser/definition.d.ts +14 -14
  16. package/out/parser/definition.js +10 -13
  17. package/out/parser/generic.d.ts +5 -7
  18. package/out/parser/generic.js +7 -11
  19. package/out/parser/objectLiteral.d.ts +44 -18
  20. package/out/parser/objectLiteral.js +57 -51
  21. package/out/parser/semantic/bounds.d.ts +10 -8
  22. package/out/parser/semantic/bounds.js +0 -1
  23. package/out/parser/semantic/divisor.d.ts +5 -7
  24. package/out/parser/semantic/divisor.js +0 -1
  25. package/out/parser/semantic/infer.d.ts +24 -0
  26. package/out/parser/semantic/infer.js +1 -0
  27. package/out/parser/semantic/utils.d.ts +3 -7
  28. package/out/parser/semantic/utils.js +0 -1
  29. package/out/parser/semantic/validate.d.ts +16 -13
  30. package/out/parser/semantic/validate.js +1 -1
  31. package/out/parser/string/reduce/dynamic.d.ts +13 -13
  32. package/out/parser/string/reduce/dynamic.js +31 -26
  33. package/out/parser/string/reduce/shared.d.ts +14 -28
  34. package/out/parser/string/reduce/shared.js +4 -3
  35. package/out/parser/string/reduce/static.d.ts +27 -24
  36. package/out/parser/string/reduce/static.js +0 -1
  37. package/out/parser/string/shift/operand/date.d.ts +2 -2
  38. package/out/parser/string/shift/operand/date.js +4 -7
  39. package/out/parser/string/shift/operand/enclosed.d.ts +3 -4
  40. package/out/parser/string/shift/operand/enclosed.js +14 -14
  41. package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
  42. package/out/parser/string/shift/operand/genericArgs.js +6 -10
  43. package/out/parser/string/shift/operand/operand.d.ts +3 -4
  44. package/out/parser/string/shift/operand/operand.js +8 -12
  45. package/out/parser/string/shift/operand/unenclosed.d.ts +16 -21
  46. package/out/parser/string/shift/operand/unenclosed.js +24 -36
  47. package/out/parser/string/shift/operator/bounds.d.ts +8 -12
  48. package/out/parser/string/shift/operator/bounds.js +45 -56
  49. package/out/parser/string/shift/operator/divisor.d.ts +1 -3
  50. package/out/parser/string/shift/operator/divisor.js +1 -3
  51. package/out/parser/string/shift/operator/operator.d.ts +1 -2
  52. package/out/parser/string/shift/operator/operator.js +12 -19
  53. package/out/parser/string/shift/scanner.d.ts +2 -2
  54. package/out/parser/string/shift/scanner.js +14 -16
  55. package/out/parser/string/string.d.ts +8 -6
  56. package/out/parser/string/string.js +1 -3
  57. package/out/parser/tuple.d.ts +102 -43
  58. package/out/parser/tuple.js +119 -87
  59. package/out/scope.d.ts +44 -119
  60. package/out/scope.js +39 -209
  61. package/out/type.d.ts +43 -64
  62. package/out/type.js +30 -131
  63. package/package.json +12 -10
  64. package/__tests__/array.test.ts +0 -272
  65. package/__tests__/astUtils.test.ts +0 -17
  66. package/__tests__/badDefinitionType.test.ts +0 -51
  67. package/__tests__/basis.test.ts +0 -46
  68. package/__tests__/bounds.test.ts +0 -388
  69. package/__tests__/cast.test.ts +0 -32
  70. package/__tests__/completions.test.ts +0 -46
  71. package/__tests__/config.test.ts +0 -168
  72. package/__tests__/cyclic.test.ts +0 -144
  73. package/__tests__/dateLiteral.test.ts +0 -32
  74. package/__tests__/declared.test.ts +0 -123
  75. package/__tests__/discrimination.test.ts +0 -74
  76. package/__tests__/divisor.test.ts +0 -90
  77. package/__tests__/enclosed.test.ts +0 -93
  78. package/__tests__/examples.test.ts +0 -56
  79. package/__tests__/expressions.test.ts +0 -138
  80. package/__tests__/generateBenchData.ts +0 -89
  81. package/__tests__/generated/cyclic.ts +0 -3058
  82. package/__tests__/generic.test.ts +0 -294
  83. package/__tests__/group.test.ts +0 -72
  84. package/__tests__/hkt.test.ts +0 -22
  85. package/__tests__/imports.test.ts +0 -113
  86. package/__tests__/instanceof.test.ts +0 -102
  87. package/__tests__/intersection.test.ts +0 -104
  88. package/__tests__/jsdoc.test.ts +0 -64
  89. package/__tests__/keyTraversal.test.ts +0 -73
  90. package/__tests__/keyof.test.ts +0 -134
  91. package/__tests__/keywords.test.ts +0 -188
  92. package/__tests__/literal.test.ts +0 -51
  93. package/__tests__/match.test.ts +0 -34
  94. package/__tests__/methods.bench.ts +0 -22
  95. package/__tests__/morph.test.ts +0 -409
  96. package/__tests__/narrow.test.ts +0 -102
  97. package/__tests__/nodeDefinitions.test.ts +0 -190
  98. package/__tests__/object.bench.ts +0 -28
  99. package/__tests__/objectLiteral.test.ts +0 -300
  100. package/__tests__/operand.bench.ts +0 -45
  101. package/__tests__/operator.bench.ts +0 -130
  102. package/__tests__/regex.test.ts +0 -58
  103. package/__tests__/scope.bench.ts +0 -39
  104. package/__tests__/scope.test.ts +0 -140
  105. package/__tests__/string.test.ts +0 -48
  106. package/__tests__/submodule.test.ts +0 -99
  107. package/__tests__/this.test.ts +0 -57
  108. package/__tests__/thunk.test.ts +0 -80
  109. package/__tests__/traverse.test.ts +0 -119
  110. package/__tests__/tsGenerics.test.ts +0 -12
  111. package/__tests__/typeMethods.test.ts +0 -69
  112. package/__tests__/typeReference.test.ts +0 -39
  113. package/__tests__/unenclosed.test.ts +0 -120
  114. package/__tests__/union.test.ts +0 -147
  115. package/ark.ts +0 -91
  116. package/main.ts +0 -18
  117. package/match.ts +0 -116
  118. package/out/ark.d.ts.map +0 -1
  119. package/out/ark.js.map +0 -1
  120. package/out/arkConfig.d.ts +0 -2
  121. package/out/arkConfig.d.ts.map +0 -1
  122. package/out/arkConfig.js +0 -10
  123. package/out/arkConfig.js.map +0 -1
  124. package/out/main.d.ts.map +0 -1
  125. package/out/main.js.map +0 -1
  126. package/out/match.d.ts.map +0 -1
  127. package/out/match.js.map +0 -1
  128. package/out/parser/definition.d.ts.map +0 -1
  129. package/out/parser/definition.js.map +0 -1
  130. package/out/parser/generic.d.ts.map +0 -1
  131. package/out/parser/generic.js.map +0 -1
  132. package/out/parser/objectLiteral.d.ts.map +0 -1
  133. package/out/parser/objectLiteral.js.map +0 -1
  134. package/out/parser/semantic/bounds.d.ts.map +0 -1
  135. package/out/parser/semantic/bounds.js.map +0 -1
  136. package/out/parser/semantic/divisor.d.ts.map +0 -1
  137. package/out/parser/semantic/divisor.js.map +0 -1
  138. package/out/parser/semantic/intersections.d.ts +0 -14
  139. package/out/parser/semantic/intersections.d.ts.map +0 -1
  140. package/out/parser/semantic/intersections.js +0 -2
  141. package/out/parser/semantic/intersections.js.map +0 -1
  142. package/out/parser/semantic/semantic.d.ts +0 -33
  143. package/out/parser/semantic/semantic.d.ts.map +0 -1
  144. package/out/parser/semantic/semantic.js +0 -2
  145. package/out/parser/semantic/semantic.js.map +0 -1
  146. package/out/parser/semantic/utils.d.ts.map +0 -1
  147. package/out/parser/semantic/utils.js.map +0 -1
  148. package/out/parser/semantic/validate.d.ts.map +0 -1
  149. package/out/parser/semantic/validate.js.map +0 -1
  150. package/out/parser/shared.d.ts +0 -54
  151. package/out/parser/shared.d.ts.map +0 -1
  152. package/out/parser/shared.js +0 -47
  153. package/out/parser/shared.js.map +0 -1
  154. package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
  155. package/out/parser/string/reduce/dynamic.js.map +0 -1
  156. package/out/parser/string/reduce/shared.d.ts.map +0 -1
  157. package/out/parser/string/reduce/shared.js.map +0 -1
  158. package/out/parser/string/reduce/static.d.ts.map +0 -1
  159. package/out/parser/string/reduce/static.js.map +0 -1
  160. package/out/parser/string/shift/operand/date.d.ts.map +0 -1
  161. package/out/parser/string/shift/operand/date.js.map +0 -1
  162. package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
  163. package/out/parser/string/shift/operand/enclosed.js.map +0 -1
  164. package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
  165. package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
  166. package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
  167. package/out/parser/string/shift/operand/operand.js.map +0 -1
  168. package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  169. package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
  170. package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
  171. package/out/parser/string/shift/operator/bounds.js.map +0 -1
  172. package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
  173. package/out/parser/string/shift/operator/divisor.js.map +0 -1
  174. package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
  175. package/out/parser/string/shift/operator/operator.js.map +0 -1
  176. package/out/parser/string/shift/scanner.d.ts.map +0 -1
  177. package/out/parser/string/shift/scanner.js.map +0 -1
  178. package/out/parser/string/string.d.ts.map +0 -1
  179. package/out/parser/string/string.js.map +0 -1
  180. package/out/parser/tuple.d.ts.map +0 -1
  181. package/out/parser/tuple.js.map +0 -1
  182. package/out/scope.d.ts.map +0 -1
  183. package/out/scope.js.map +0 -1
  184. package/out/test.d.ts +0 -2
  185. package/out/test.d.ts.map +0 -1
  186. package/out/test.js +0 -7
  187. package/out/test.js.map +0 -1
  188. package/out/tsconfig.build.tsbuildinfo +0 -1
  189. package/out/type.d.ts.map +0 -1
  190. package/out/type.js.map +0 -1
  191. package/out/util.d.ts +0 -19
  192. package/out/util.d.ts.map +0 -1
  193. package/out/util.js +0 -7
  194. package/out/util.js.map +0 -1
  195. package/parser/definition.ts +0 -159
  196. package/parser/generic.ts +0 -68
  197. package/parser/objectLiteral.ts +0 -197
  198. package/parser/semantic/bounds.ts +0 -61
  199. package/parser/semantic/divisor.ts +0 -13
  200. package/parser/semantic/intersections.ts +0 -46
  201. package/parser/semantic/semantic.ts +0 -144
  202. package/parser/semantic/utils.ts +0 -26
  203. package/parser/semantic/validate.ts +0 -109
  204. package/parser/shared.ts +0 -152
  205. package/parser/string/reduce/dynamic.ts +0 -196
  206. package/parser/string/reduce/shared.ts +0 -101
  207. package/parser/string/reduce/static.ts +0 -298
  208. package/parser/string/shift/operand/date.ts +0 -54
  209. package/parser/string/shift/operand/enclosed.ts +0 -118
  210. package/parser/string/shift/operand/genericArgs.ts +0 -127
  211. package/parser/string/shift/operand/operand.ts +0 -48
  212. package/parser/string/shift/operand/unenclosed.ts +0 -268
  213. package/parser/string/shift/operator/bounds.ts +0 -264
  214. package/parser/string/shift/operator/divisor.ts +0 -36
  215. package/parser/string/shift/operator/operator.ts +0 -82
  216. package/parser/string/shift/scanner.ts +0 -209
  217. package/parser/string/string.ts +0 -90
  218. package/parser/tuple.ts +0 -474
  219. package/scope.ts +0 -574
  220. package/type.ts +0 -347
  221. package/util.ts +0 -36
@@ -1,409 +0,0 @@
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
- })
@@ -1,102 +0,0 @@
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
- })
@@ -1,190 +0,0 @@
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
- // })