arktype 2.0.0-dev.5 → 2.0.0-dev.7

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 (376) hide show
  1. package/__tests__/array.test.ts +70 -38
  2. package/__tests__/badDefinitionType.test.ts +9 -2
  3. package/__tests__/basis.test.ts +6 -2
  4. package/__tests__/bounds.test.ts +49 -218
  5. package/__tests__/cast.test.ts +7 -2
  6. package/__tests__/completions.test.ts +8 -3
  7. package/__tests__/config.test.ts +8 -2
  8. package/__tests__/cyclic.test.ts +141 -137
  9. package/__tests__/dateLiteral.test.ts +6 -2
  10. package/__tests__/declared.test.ts +2 -2
  11. package/__tests__/divisor.test.ts +28 -12
  12. package/__tests__/enclosed.test.ts +32 -23
  13. package/__tests__/examples.test.ts +56 -56
  14. package/__tests__/expressions.test.ts +184 -106
  15. package/__tests__/generic.test.ts +307 -287
  16. package/__tests__/group.test.ts +59 -51
  17. package/__tests__/imports.test.ts +44 -37
  18. package/__tests__/instanceof.test.ts +14 -12
  19. package/__tests__/intersection.test.ts +16 -36
  20. package/__tests__/keyTraversal.test.ts +6 -1
  21. package/__tests__/keyof.test.ts +57 -43
  22. package/__tests__/keywords.test.ts +144 -135
  23. package/__tests__/literal.test.ts +12 -7
  24. package/__tests__/match.test.ts +234 -236
  25. package/__tests__/morph.test.ts +169 -153
  26. package/__tests__/narrow.test.ts +14 -9
  27. package/__tests__/objectLiteral.test.ts +172 -184
  28. package/__tests__/realWorld.test.ts +60 -59
  29. package/__tests__/regex.test.ts +17 -7
  30. package/__tests__/scope.test.ts +59 -14
  31. package/__tests__/serialization.test.ts +19 -0
  32. package/__tests__/string.test.ts +11 -7
  33. package/__tests__/submodule.test.ts +24 -37
  34. package/__tests__/this.test.ts +83 -57
  35. package/__tests__/thunk.test.ts +13 -7
  36. package/__tests__/traverse.test.ts +22 -12
  37. package/__tests__/tsGenerics.test.ts +12 -12
  38. package/__tests__/type.test.ts +38 -0
  39. package/__tests__/typeReference.test.ts +5 -5
  40. package/__tests__/unenclosed.test.ts +106 -96
  41. package/__tests__/union.test.ts +207 -212
  42. package/ark.ts +44 -0
  43. package/generic.ts +67 -0
  44. package/main.ts +6 -17
  45. package/match.ts +41 -51
  46. package/module.ts +15 -0
  47. package/out/ark.d.ts +12 -40
  48. package/out/ark.d.ts.map +1 -1
  49. package/out/ark.js +9 -20
  50. package/out/ark.js.map +1 -1
  51. package/out/base.d.ts +198 -122
  52. package/out/base.d.ts.map +1 -1
  53. package/out/base.js +317 -271
  54. package/out/base.js.map +1 -1
  55. package/out/builtins/builtins.d.ts +21 -17
  56. package/out/builtins/builtins.js +15 -12
  57. package/out/builtins/jsObjects.d.ts +17 -17
  58. package/out/builtins/jsObjects.js +18 -15
  59. package/out/builtins/parsing.d.ts +13 -13
  60. package/out/builtins/parsing.js +57 -54
  61. package/out/builtins/tsGenerics.d.ts +2 -2
  62. package/out/builtins/tsGenerics.js +2 -2
  63. package/out/builtins/tsKeywords.d.ts +21 -21
  64. package/out/builtins/tsKeywords.js +23 -20
  65. package/out/builtins/utils/creditCard.d.ts +3 -3
  66. package/out/builtins/utils/creditCard.js +31 -32
  67. package/out/builtins/utils/date.d.ts +39 -20
  68. package/out/builtins/utils/date.js +51 -48
  69. package/out/builtins/validation.d.ts +17 -17
  70. package/out/builtins/validation.js +66 -62
  71. package/out/config.d.ts +9 -6
  72. package/out/config.js +17 -16
  73. package/out/constraints/ast.d.ts +263 -152
  74. package/out/constraints/ast.js +2 -2
  75. package/out/constraints/constraint.d.ts +196 -66
  76. package/out/constraints/constraint.js +21 -17
  77. package/out/constraints/predicate.d.ts +60 -42
  78. package/out/constraints/predicate.js +48 -43
  79. package/out/constraints/props/index.d.ts +35 -29
  80. package/out/constraints/props/index.js +54 -46
  81. package/out/constraints/props/optional.d.ts +31 -28
  82. package/out/constraints/props/optional.js +54 -53
  83. package/out/constraints/props/prop.d.ts +42 -39
  84. package/out/constraints/props/prop.js +104 -102
  85. package/out/constraints/props/props.d.ts +35 -32
  86. package/out/constraints/props/props.js +117 -102
  87. package/out/constraints/props/props.js.map +1 -1
  88. package/out/constraints/props/required.d.ts +40 -37
  89. package/out/constraints/props/required.js +78 -73
  90. package/out/constraints/props/sequence.d.ts +74 -56
  91. package/out/constraints/props/sequence.js +371 -322
  92. package/out/constraints/props/shared.d.ts +3 -3
  93. package/out/constraints/props/shared.js +4 -4
  94. package/out/constraints/refinements/after.d.ts +26 -18
  95. package/out/constraints/refinements/after.js +34 -33
  96. package/out/constraints/refinements/before.d.ts +26 -18
  97. package/out/constraints/refinements/before.js +41 -39
  98. package/out/constraints/refinements/divisor.d.ts +47 -37
  99. package/out/constraints/refinements/divisor.js +48 -37
  100. package/out/constraints/refinements/exactLength.d.ts +40 -36
  101. package/out/constraints/refinements/exactLength.js +50 -42
  102. package/out/constraints/refinements/kinds.d.ts +25 -25
  103. package/out/constraints/refinements/kinds.js +16 -16
  104. package/out/constraints/refinements/length.d.ts +45 -39
  105. package/out/constraints/refinements/length.js +51 -43
  106. package/out/constraints/refinements/max.d.ts +24 -17
  107. package/out/constraints/refinements/max.js +33 -29
  108. package/out/constraints/refinements/maxLength.d.ts +25 -17
  109. package/out/constraints/refinements/maxLength.js +34 -32
  110. package/out/constraints/refinements/min.d.ts +24 -17
  111. package/out/constraints/refinements/min.js +26 -23
  112. package/out/constraints/refinements/minLength.d.ts +25 -17
  113. package/out/constraints/refinements/minLength.js +31 -30
  114. package/out/constraints/refinements/range.d.ts +107 -78
  115. package/out/constraints/refinements/range.js +83 -66
  116. package/out/constraints/refinements/regex.d.ts +42 -38
  117. package/out/constraints/refinements/regex.js +35 -34
  118. package/out/constraints/refinements/shared.d.ts +20 -5
  119. package/out/constraints/refinements/shared.js +9 -9
  120. package/out/generic.d.ts +22 -0
  121. package/out/generic.d.ts.map +1 -0
  122. package/out/generic.js +23 -0
  123. package/out/generic.js.map +1 -0
  124. package/out/inference.d.ts +132 -35
  125. package/out/inference.js +2 -2
  126. package/out/keywords/ark.d.ts +60 -50
  127. package/out/keywords/ark.js +25 -25
  128. package/out/keywords/internal.d.ts +11 -11
  129. package/out/keywords/internal.js +13 -10
  130. package/out/keywords/jsObject.d.ts +18 -18
  131. package/out/keywords/jsObject.js +18 -15
  132. package/out/keywords/parsing.d.ts +14 -14
  133. package/out/keywords/parsing.js +53 -50
  134. package/out/keywords/root.d.ts +6 -6
  135. package/out/keywords/root.js +4 -4
  136. package/out/keywords/tsGenerics.d.ts +2 -2
  137. package/out/keywords/tsGenerics.js +2 -2
  138. package/out/keywords/tsPrimitive.d.ts +22 -22
  139. package/out/keywords/tsPrimitive.js +25 -22
  140. package/out/keywords/utils/creditCard.d.ts +11 -8
  141. package/out/keywords/utils/creditCard.js +31 -32
  142. package/out/keywords/utils/date.d.ts +43 -23
  143. package/out/keywords/utils/date.js +51 -48
  144. package/out/keywords/validation.d.ts +18 -18
  145. package/out/keywords/validation.js +67 -63
  146. package/out/kinds.d.ts +97 -67
  147. package/out/kinds.js +28 -28
  148. package/out/main.d.ts +6 -7
  149. package/out/main.d.ts.map +1 -1
  150. package/out/main.js +5 -2
  151. package/out/main.js.map +1 -1
  152. package/out/match.d.ts +6 -10
  153. package/out/match.d.ts.map +1 -1
  154. package/out/match.js +1 -1
  155. package/out/match.js.map +1 -1
  156. package/out/module.d.ts +11 -0
  157. package/out/module.d.ts.map +1 -0
  158. package/out/module.js +4 -0
  159. package/out/module.js.map +1 -0
  160. package/out/parse.d.ts +28 -22
  161. package/out/parse.js +157 -144
  162. package/out/parser/definition.d.ts +8 -10
  163. package/out/parser/definition.d.ts.map +1 -1
  164. package/out/parser/definition.js +5 -6
  165. package/out/parser/definition.js.map +1 -1
  166. package/out/parser/generic.d.ts +3 -4
  167. package/out/parser/generic.d.ts.map +1 -1
  168. package/out/parser/generic.js +6 -8
  169. package/out/parser/generic.js.map +1 -1
  170. package/out/parser/objectLiteral.d.ts +5 -6
  171. package/out/parser/objectLiteral.d.ts.map +1 -1
  172. package/out/parser/objectLiteral.js +10 -15
  173. package/out/parser/objectLiteral.js.map +1 -1
  174. package/out/parser/semantic/bounds.d.ts +4 -3
  175. package/out/parser/semantic/bounds.d.ts.map +1 -1
  176. package/out/parser/semantic/bounds.js.map +1 -1
  177. package/out/parser/semantic/divisor.d.ts +4 -3
  178. package/out/parser/semantic/divisor.d.ts.map +1 -1
  179. package/out/parser/semantic/infer.d.ts +7 -10
  180. package/out/parser/semantic/infer.d.ts.map +1 -1
  181. package/out/parser/semantic/utils.d.ts.map +1 -1
  182. package/out/parser/semantic/validate.d.ts +13 -11
  183. package/out/parser/semantic/validate.d.ts.map +1 -1
  184. package/out/parser/semantic/validate.js +1 -0
  185. package/out/parser/semantic/validate.js.map +1 -1
  186. package/out/parser/string/reduce/dynamic.d.ts +7 -8
  187. package/out/parser/string/reduce/dynamic.d.ts.map +1 -1
  188. package/out/parser/string/reduce/dynamic.js +26 -22
  189. package/out/parser/string/reduce/dynamic.js.map +1 -1
  190. package/out/parser/string/reduce/shared.d.ts +2 -2
  191. package/out/parser/string/reduce/shared.d.ts.map +1 -1
  192. package/out/parser/string/reduce/static.d.ts +8 -5
  193. package/out/parser/string/reduce/static.d.ts.map +1 -1
  194. package/out/parser/string/shift/operand/date.d.ts +1 -1
  195. package/out/parser/string/shift/operand/date.d.ts.map +1 -1
  196. package/out/parser/string/shift/operand/date.js +2 -2
  197. package/out/parser/string/shift/operand/date.js.map +1 -1
  198. package/out/parser/string/shift/operand/enclosed.d.ts +1 -1
  199. package/out/parser/string/shift/operand/enclosed.d.ts.map +1 -1
  200. package/out/parser/string/shift/operand/enclosed.js +1 -1
  201. package/out/parser/string/shift/operand/enclosed.js.map +1 -1
  202. package/out/parser/string/shift/operand/genericArgs.d.ts +8 -8
  203. package/out/parser/string/shift/operand/genericArgs.d.ts.map +1 -1
  204. package/out/parser/string/shift/operand/genericArgs.js +6 -8
  205. package/out/parser/string/shift/operand/genericArgs.js.map +1 -1
  206. package/out/parser/string/shift/operand/operand.d.ts +3 -3
  207. package/out/parser/string/shift/operand/operand.d.ts.map +1 -1
  208. package/out/parser/string/shift/operand/operand.js +8 -11
  209. package/out/parser/string/shift/operand/operand.js.map +1 -1
  210. package/out/parser/string/shift/operand/unenclosed.d.ts +10 -16
  211. package/out/parser/string/shift/operand/unenclosed.d.ts.map +1 -1
  212. package/out/parser/string/shift/operand/unenclosed.js +19 -24
  213. package/out/parser/string/shift/operand/unenclosed.js.map +1 -1
  214. package/out/parser/string/shift/operator/bounds.d.ts +6 -8
  215. package/out/parser/string/shift/operator/bounds.d.ts.map +1 -1
  216. package/out/parser/string/shift/operator/bounds.js +20 -29
  217. package/out/parser/string/shift/operator/bounds.js.map +1 -1
  218. package/out/parser/string/shift/operator/divisor.d.ts +1 -2
  219. package/out/parser/string/shift/operator/divisor.d.ts.map +1 -1
  220. package/out/parser/string/shift/operator/divisor.js.map +1 -1
  221. package/out/parser/string/shift/operator/operator.d.ts +1 -1
  222. package/out/parser/string/shift/operator/operator.d.ts.map +1 -1
  223. package/out/parser/string/shift/operator/operator.js +12 -18
  224. package/out/parser/string/shift/operator/operator.js.map +1 -1
  225. package/out/parser/string/shift/scanner.d.ts +2 -1
  226. package/out/parser/string/shift/scanner.d.ts.map +1 -1
  227. package/out/parser/string/shift/scanner.js +12 -11
  228. package/out/parser/string/shift/scanner.js.map +1 -1
  229. package/out/parser/string/string.d.ts +7 -4
  230. package/out/parser/string/string.d.ts.map +1 -1
  231. package/out/parser/string/string.js.map +1 -1
  232. package/out/parser/tuple.d.ts +27 -30
  233. package/out/parser/tuple.d.ts.map +1 -1
  234. package/out/parser/tuple.js +33 -35
  235. package/out/parser/tuple.js.map +1 -1
  236. package/out/schema.d.ts +171 -40
  237. package/out/schema.js +2 -2
  238. package/out/schemaScope.d.ts +170 -100
  239. package/out/schemaScope.js +231 -208
  240. package/out/scope.d.ts +43 -187
  241. package/out/scope.d.ts.map +1 -1
  242. package/out/scope.js +56 -326
  243. package/out/scope.js.map +1 -1
  244. package/out/shared/compile.d.ts +19 -19
  245. package/out/shared/compile.js +77 -65
  246. package/out/shared/context.d.ts +51 -26
  247. package/out/shared/context.js +50 -45
  248. package/out/shared/declare.d.ts +61 -45
  249. package/out/shared/declare.js +2 -2
  250. package/out/shared/disjoint.d.ts +85 -73
  251. package/out/shared/disjoint.js +108 -80
  252. package/out/shared/disjoint.js.map +1 -1
  253. package/out/shared/errors.d.ts +85 -45
  254. package/out/shared/errors.js +101 -101
  255. package/out/shared/implement.d.ts +351 -158
  256. package/out/shared/implement.js +61 -62
  257. package/out/shared/implement.js.map +1 -1
  258. package/out/shared/inference.d.ts +5 -5
  259. package/out/shared/inference.js +2 -2
  260. package/out/shared/intersections.d.ts +57 -12
  261. package/out/shared/intersections.js +2 -2
  262. package/out/shared/traversal.d.ts +57 -39
  263. package/out/shared/traversal.js +95 -95
  264. package/out/shared/utils.d.ts +23 -7
  265. package/out/shared/utils.d.ts.map +1 -1
  266. package/out/shared/utils.js +14 -9
  267. package/out/shared/utils.js.map +1 -1
  268. package/out/type.d.ts +24 -32
  269. package/out/type.d.ts.map +1 -1
  270. package/out/type.js +2 -51
  271. package/out/type.js.map +1 -1
  272. package/out/types/basis.d.ts +44 -21
  273. package/out/types/basis.js +13 -13
  274. package/out/types/discriminate.d.ts +33 -19
  275. package/out/types/discriminate.js +116 -110
  276. package/out/types/domain.d.ts +54 -37
  277. package/out/types/domain.js +39 -36
  278. package/out/types/intersection.d.ts +121 -61
  279. package/out/types/intersection.js +355 -326
  280. package/out/types/morph.d.ts +148 -70
  281. package/out/types/morph.js +97 -93
  282. package/out/types/proto.d.ts +48 -39
  283. package/out/types/proto.js +58 -46
  284. package/out/types/type.d.ts +137 -55
  285. package/out/types/type.d.ts.map +1 -1
  286. package/out/types/type.js +121 -109
  287. package/out/types/type.js.map +1 -1
  288. package/out/types/union.d.ts +66 -43
  289. package/out/types/union.js +299 -257
  290. package/out/types/unit.d.ts +41 -36
  291. package/out/types/unit.js +53 -45
  292. package/out/util.d.ts +30 -21
  293. package/out/util.js +8 -7
  294. package/package.json +3 -2
  295. package/parser/definition.ts +64 -81
  296. package/parser/generic.ts +35 -36
  297. package/parser/objectLiteral.ts +78 -92
  298. package/parser/semantic/bounds.ts +25 -33
  299. package/parser/semantic/divisor.ts +7 -11
  300. package/parser/semantic/infer.ts +69 -80
  301. package/parser/semantic/utils.ts +10 -13
  302. package/parser/semantic/validate.ts +81 -71
  303. package/parser/string/reduce/dynamic.ts +42 -39
  304. package/parser/string/reduce/shared.ts +1 -1
  305. package/parser/string/reduce/static.ts +50 -52
  306. package/parser/string/shift/operand/date.ts +5 -5
  307. package/parser/string/shift/operand/enclosed.ts +12 -13
  308. package/parser/string/shift/operand/genericArgs.ts +45 -44
  309. package/parser/string/shift/operand/operand.ts +32 -37
  310. package/parser/string/shift/operand/unenclosed.ts +99 -134
  311. package/parser/string/shift/operator/bounds.ts +78 -75
  312. package/parser/string/shift/operator/divisor.ts +11 -13
  313. package/parser/string/shift/operator/operator.ts +35 -46
  314. package/parser/string/shift/scanner.ts +39 -36
  315. package/parser/string/string.ts +26 -30
  316. package/parser/tuple.ts +206 -248
  317. package/scope.ts +185 -704
  318. package/type.ts +101 -140
  319. package/__tests__/errors.test.ts +0 -115
  320. package/__tests__/schema.bench.ts +0 -14
  321. package/__tests__/schema.test.ts +0 -60
  322. package/__tests__/typeMethods.test.ts +0 -68
  323. package/__tests__/unit.test.ts +0 -61
  324. package/base.ts +0 -520
  325. package/config.ts +0 -25
  326. package/constraints/ast.ts +0 -288
  327. package/constraints/constraint.ts +0 -91
  328. package/constraints/predicate.ts +0 -102
  329. package/constraints/props/index.ts +0 -93
  330. package/constraints/props/prop.ts +0 -146
  331. package/constraints/props/props.ts +0 -148
  332. package/constraints/props/sequence.ts +0 -473
  333. package/constraints/props/shared.ts +0 -5
  334. package/constraints/refinements/after.ts +0 -68
  335. package/constraints/refinements/before.ts +0 -75
  336. package/constraints/refinements/divisor.ts +0 -76
  337. package/constraints/refinements/exactLength.ts +0 -75
  338. package/constraints/refinements/kinds.ts +0 -31
  339. package/constraints/refinements/max.ts +0 -61
  340. package/constraints/refinements/maxLength.ts +0 -66
  341. package/constraints/refinements/min.ts +0 -54
  342. package/constraints/refinements/minLength.ts +0 -63
  343. package/constraints/refinements/range.ts +0 -181
  344. package/constraints/refinements/regex.ts +0 -59
  345. package/keywords/ark.ts +0 -101
  346. package/keywords/internal.ts +0 -29
  347. package/keywords/jsObject.ts +0 -39
  348. package/keywords/parsing.ts +0 -87
  349. package/keywords/root.ts +0 -9
  350. package/keywords/tsGenerics.ts +0 -28
  351. package/keywords/tsPrimitive.ts +0 -52
  352. package/keywords/utils/creditCard.ts +0 -37
  353. package/keywords/utils/date.ts +0 -112
  354. package/keywords/validation.ts +0 -115
  355. package/kinds.ts +0 -115
  356. package/parse.ts +0 -221
  357. package/schema.ts +0 -263
  358. package/shared/compile.ts +0 -92
  359. package/shared/declare.ts +0 -75
  360. package/shared/disjoint.ts +0 -203
  361. package/shared/errors.ts +0 -204
  362. package/shared/implement.ts +0 -327
  363. package/shared/inference.ts +0 -9
  364. package/shared/intersections.ts +0 -53
  365. package/shared/traversal.ts +0 -146
  366. package/shared/utils.ts +0 -30
  367. package/types/basis.ts +0 -62
  368. package/types/discriminate.ts +0 -168
  369. package/types/domain.ts +0 -79
  370. package/types/intersection.ts +0 -539
  371. package/types/morph.ts +0 -263
  372. package/types/proto.ts +0 -89
  373. package/types/type.ts +0 -319
  374. package/types/union.ts +0 -451
  375. package/types/unit.ts +0 -78
  376. package/util.ts +0 -39
@@ -1,172 +1,211 @@
1
- import { appendUnique, groupBy, isArray } from "@arktype/util";
2
- import { Disjoint } from "../shared/disjoint.js";
3
- import { typeKindsRightOf } from "../shared/implement.js";
4
- import { BaseType, defineRightwardIntersections } from "./type.js";
5
- export const unionChildKinds = typeKindsRightOf("union");
1
+ import { appendUnique, groupBy, isArray } from "@arktype/util"
2
+ import { Disjoint } from "../shared/disjoint.js"
3
+ import { typeKindsRightOf } from "../shared/implement.js"
4
+ import { BaseType, defineRightwardIntersections } from "./type.js"
5
+ export const unionChildKinds = typeKindsRightOf("union")
6
6
  export class UnionNode extends BaseType {
7
- static implementation = this.implement({
8
- hasAssociatedError: true,
9
- collapsibleKey: "branches",
10
- keys: {
11
- ordered: {},
12
- branches: {
13
- child: true,
14
- parse: (schema, ctx) => {
15
- const branches = schema.map((branch) => ctx.$.node(branch, { allowedKinds: unionChildKinds }));
16
- const def = ctx.raw;
17
- if (isArray(def) || def.ordered !== true) {
18
- branches.sort((l, r) => (l.innerId < r.innerId ? -1 : 1));
19
- }
20
- return branches;
21
- }
22
- }
23
- },
24
- normalize: (schema) => (isArray(schema) ? { branches: schema } : schema),
25
- reduce: (inner, $) => {
26
- const reducedBranches = reduceBranches(inner);
27
- if (reducedBranches.length === 1) {
28
- return reducedBranches[0];
29
- }
30
- if (reducedBranches.length === inner.branches.length) {
31
- return;
32
- }
33
- return $.node("union", {
34
- ...inner,
35
- branches: reducedBranches
36
- }, { prereduced: true });
37
- },
38
- defaults: {
39
- description(node) {
40
- if (node.isBoolean)
41
- return "boolean";
42
- return describeBranches(node.branches.map((branch) => branch.description));
43
- },
44
- expected(ctx) {
45
- const byPath = groupBy(ctx.errors, "propString");
46
- const pathDescriptions = Object.entries(byPath).map(([path, errors]) => {
47
- errors.map((_) => _.expected);
48
- const branchesAtPath = [];
49
- errors.forEach((errorAtPath) =>
50
- // avoid duplicate messages when multiple branches
51
- // are invalid due to the same error
52
- appendUnique(branchesAtPath, errorAtPath.expected));
53
- const expected = describeBranches(branchesAtPath);
54
- const actual = ctx.errors.reduce((acc, e) => e.actual && !acc.includes(e.actual)
55
- ? `${acc && acc + ", "}${e.actual}`
56
- : acc, "");
57
- return `${path && path + " "}must be ${expected}${actual && ` (was ${actual})`}`;
58
- });
59
- return describeBranches(pathDescriptions);
60
- },
61
- problem(ctx) {
62
- return ctx.expected;
63
- },
64
- message(ctx) {
65
- return ctx.problem;
66
- }
67
- },
68
- intersections: {
69
- union: (l, r) => {
70
- if ((l.branches.length === 0 || r.branches.length === 0) &&
71
- l.branches.length !== r.branches.length) {
72
- // if exactly one operand is never, we can use it to discriminate based on presence
73
- return Disjoint.from("presence", l.branches.length !== 0, r.branches.length !== 0);
74
- }
75
- let resultBranches;
76
- if (l.ordered) {
77
- if (r.ordered) {
78
- return Disjoint.from("indiscriminableMorphs", l, r);
79
- }
80
- resultBranches = intersectBranches(r.branches, l.branches);
81
- if (resultBranches instanceof Disjoint) {
82
- resultBranches.invert();
83
- }
84
- }
85
- else {
86
- resultBranches = intersectBranches(l.branches, r.branches);
87
- }
88
- if (resultBranches instanceof Disjoint) {
89
- return resultBranches;
90
- }
91
- return l.$.node("union", l.ordered || r.ordered
92
- ? {
93
- branches: resultBranches,
94
- ordered: true
95
- }
96
- : { branches: resultBranches });
97
- },
98
- ...defineRightwardIntersections("union", (l, r, $) => {
99
- const branches = intersectBranches(l.branches, [r]);
100
- if (branches instanceof Disjoint) {
101
- return branches;
102
- }
103
- if (branches.length === 1) {
104
- return branches[0];
105
- }
106
- return $.node(l.ordered ? { branches, ordered: true } : { branches });
107
- })
108
- }
109
- });
110
- discriminant = null; //discriminate(inner.branches)
111
- isBoolean = this.branches.length === 2 &&
112
- this.branches[0].hasUnit(false) &&
113
- this.branches[1].hasUnit(true);
114
- expression = this.isBoolean
115
- ? "boolean"
116
- : this.branches.map((branch) => branch.nestableExpression).join(" | ");
117
- traverseAllows = (data, ctx) => this.branches.some((b) => b.traverseAllows(data, ctx));
118
- traverseApply = (data, ctx) => {
119
- const errors = [];
120
- for (let i = 0; i < this.branches.length; i++) {
121
- ctx.pushBranch();
122
- this.branches[i].traverseApply(data, ctx);
123
- if (!ctx.hasError())
124
- return ctx.morphs.push(...ctx.popBranch().morphs);
125
- errors.push(ctx.popBranch().error);
126
- }
127
- ctx.error({ code: "union", errors });
128
- };
129
- compile(js) {
130
- if (js.traversalKind === "Apply") {
131
- js.const("errors", "[]");
132
- this.branches.forEach((branch) => js
133
- .line("ctx.pushBranch()")
134
- .line(js.invoke(branch))
135
- .if("!ctx.hasError()", () => js.return("ctx.morphs.push(...ctx.popBranch().morphs)"))
136
- .line("errors.push(ctx.popBranch().error)"));
137
- js.line(`ctx.error({ code: "union", errors })`);
138
- }
139
- else {
140
- this.branches.forEach((branch) => js.if(`${js.invoke(branch)}`, () => js.return(true)));
141
- js.return(false);
142
- }
143
- }
144
- rawKeyOf() {
145
- return this.branches.reduce((result, branch) => result.and(branch.rawKeyOf()), this.$.keywords.unknown);
146
- }
147
- get nestableExpression() {
148
- // avoid adding unnecessary parentheses around boolean since it's
149
- // already collapsed to a single keyword
150
- return this.isBoolean ? "boolean" : super.nestableExpression;
151
- }
7
+ static implementation = this.implement({
8
+ hasAssociatedError: true,
9
+ collapsibleKey: "branches",
10
+ keys: {
11
+ ordered: {},
12
+ branches: {
13
+ child: true,
14
+ parse: (schema, ctx) => {
15
+ const branches = schema.map((branch) =>
16
+ ctx.$.node(branch, { allowedKinds: unionChildKinds })
17
+ )
18
+ const def = ctx.raw
19
+ if (isArray(def) || def.ordered !== true) {
20
+ branches.sort((l, r) =>
21
+ l.innerId < r.innerId ? -1 : 1
22
+ )
23
+ }
24
+ return branches
25
+ }
26
+ }
27
+ },
28
+ normalize: (schema) =>
29
+ isArray(schema) ? { branches: schema } : schema,
30
+ reduce: (inner, $) => {
31
+ const reducedBranches = reduceBranches(inner)
32
+ if (reducedBranches.length === 1) {
33
+ return reducedBranches[0]
34
+ }
35
+ if (reducedBranches.length === inner.branches.length) {
36
+ return
37
+ }
38
+ return $.node(
39
+ "union",
40
+ {
41
+ ...inner,
42
+ branches: reducedBranches
43
+ },
44
+ { prereduced: true }
45
+ )
46
+ },
47
+ defaults: {
48
+ description(node) {
49
+ if (node.isBoolean) return "boolean"
50
+ return describeBranches(
51
+ node.branches.map((branch) => branch.description)
52
+ )
53
+ },
54
+ expected(ctx) {
55
+ const byPath = groupBy(ctx.errors, "propString")
56
+ const pathDescriptions = Object.entries(byPath).map(
57
+ ([path, errors]) => {
58
+ errors.map((_) => _.expected)
59
+ const branchesAtPath = []
60
+ errors.forEach((errorAtPath) =>
61
+ // avoid duplicate messages when multiple branches
62
+ // are invalid due to the same error
63
+ appendUnique(branchesAtPath, errorAtPath.expected)
64
+ )
65
+ const expected = describeBranches(branchesAtPath)
66
+ const actual = ctx.errors.reduce(
67
+ (acc, e) =>
68
+ e.actual && !acc.includes(e.actual)
69
+ ? `${acc && acc + ", "}${e.actual}`
70
+ : acc,
71
+ ""
72
+ )
73
+ return `${path && path + " "}must be ${expected}${
74
+ actual && ` (was ${actual})`
75
+ }`
76
+ }
77
+ )
78
+ return describeBranches(pathDescriptions)
79
+ },
80
+ problem(ctx) {
81
+ return ctx.expected
82
+ },
83
+ message(ctx) {
84
+ return ctx.problem
85
+ }
86
+ },
87
+ intersections: {
88
+ union: (l, r) => {
89
+ if (
90
+ (l.branches.length === 0 || r.branches.length === 0) &&
91
+ l.branches.length !== r.branches.length
92
+ ) {
93
+ // if exactly one operand is never, we can use it to discriminate based on presence
94
+ return Disjoint.from(
95
+ "presence",
96
+ l.branches.length !== 0,
97
+ r.branches.length !== 0
98
+ )
99
+ }
100
+ let resultBranches
101
+ if (l.ordered) {
102
+ if (r.ordered) {
103
+ return Disjoint.from("indiscriminableMorphs", l, r)
104
+ }
105
+ resultBranches = intersectBranches(r.branches, l.branches)
106
+ if (resultBranches instanceof Disjoint) {
107
+ resultBranches.invert()
108
+ }
109
+ } else {
110
+ resultBranches = intersectBranches(l.branches, r.branches)
111
+ }
112
+ if (resultBranches instanceof Disjoint) {
113
+ return resultBranches
114
+ }
115
+ return l.$.node(
116
+ "union",
117
+ l.ordered || r.ordered
118
+ ? {
119
+ branches: resultBranches,
120
+ ordered: true
121
+ }
122
+ : { branches: resultBranches }
123
+ )
124
+ },
125
+ ...defineRightwardIntersections("union", (l, r, $) => {
126
+ const branches = intersectBranches(l.branches, [r])
127
+ if (branches instanceof Disjoint) {
128
+ return branches
129
+ }
130
+ if (branches.length === 1) {
131
+ return branches[0]
132
+ }
133
+ return $.node(
134
+ l.ordered ? { branches, ordered: true } : { branches }
135
+ )
136
+ })
137
+ }
138
+ })
139
+ discriminant = null //discriminate(inner.branches)
140
+ isBoolean =
141
+ this.branches.length === 2 &&
142
+ this.branches[0].hasUnit(false) &&
143
+ this.branches[1].hasUnit(true)
144
+ expression = this.isBoolean
145
+ ? "boolean"
146
+ : this.branches.map((branch) => branch.nestableExpression).join(" | ")
147
+ traverseAllows = (data, ctx) =>
148
+ this.branches.some((b) => b.traverseAllows(data, ctx))
149
+ traverseApply = (data, ctx) => {
150
+ const errors = []
151
+ for (let i = 0; i < this.branches.length; i++) {
152
+ ctx.pushBranch()
153
+ this.branches[i].traverseApply(data, ctx)
154
+ if (!ctx.hasError())
155
+ return ctx.morphs.push(...ctx.popBranch().morphs)
156
+ errors.push(ctx.popBranch().error)
157
+ }
158
+ ctx.error({ code: "union", errors })
159
+ }
160
+ compile(js) {
161
+ if (js.traversalKind === "Apply") {
162
+ js.const("errors", "[]")
163
+ this.branches.forEach((branch) =>
164
+ js
165
+ .line("ctx.pushBranch()")
166
+ .line(js.invoke(branch))
167
+ .if("!ctx.hasError()", () =>
168
+ js.return("ctx.morphs.push(...ctx.popBranch().morphs)")
169
+ )
170
+ .line("errors.push(ctx.popBranch().error)")
171
+ )
172
+ js.line(`ctx.error({ code: "union", errors })`)
173
+ } else {
174
+ this.branches.forEach((branch) =>
175
+ js.if(`${js.invoke(branch)}`, () => js.return(true))
176
+ )
177
+ js.return(false)
178
+ }
179
+ }
180
+ rawKeyOf() {
181
+ return this.branches.reduce(
182
+ (result, branch) => result.and(branch.rawKeyOf()),
183
+ this.$.keywords.unknown
184
+ )
185
+ }
186
+ get nestableExpression() {
187
+ // avoid adding unnecessary parentheses around boolean since it's
188
+ // already collapsed to a single keyword
189
+ return this.isBoolean ? "boolean" : super.nestableExpression
190
+ }
152
191
  }
153
192
  const describeBranches = (descriptions) => {
154
- if (descriptions.length === 0) {
155
- return "never";
156
- }
157
- if (descriptions.length === 1) {
158
- return descriptions[0];
159
- }
160
- let description = "";
161
- for (let i = 0; i < descriptions.length - 1; i++) {
162
- description += descriptions[i];
163
- if (i < descriptions.length - 2) {
164
- description += ", ";
165
- }
166
- }
167
- description += ` or ${descriptions[descriptions.length - 1]}`;
168
- return description;
169
- };
193
+ if (descriptions.length === 0) {
194
+ return "never"
195
+ }
196
+ if (descriptions.length === 1) {
197
+ return descriptions[0]
198
+ }
199
+ let description = ""
200
+ for (let i = 0; i < descriptions.length - 1; i++) {
201
+ description += descriptions[i]
202
+ if (i < descriptions.length - 2) {
203
+ description += ", "
204
+ }
205
+ }
206
+ description += ` or ${descriptions[descriptions.length - 1]}`
207
+ return description
208
+ }
170
209
  // private static compileDiscriminatedLiteral(cases: DiscriminatedCases) {
171
210
  // // TODO: error messages for traversal
172
211
  // const caseKeys = Object.keys(cases)
@@ -243,95 +282,98 @@ const describeBranches = (descriptions) => {
243
282
  // }`
244
283
  // }
245
284
  export const intersectBranches = (l, r) => {
246
- // If the corresponding r branch is identified as a subtype of an l branch, the
247
- // value at rIndex is set to null so we can avoid including previous/future
248
- // inersections in the reduced result.
249
- const batchesByR = r.map(() => []);
250
- for (let lIndex = 0; lIndex < l.length; lIndex++) {
251
- let candidatesByR = {};
252
- for (let rIndex = 0; rIndex < r.length; rIndex++) {
253
- if (batchesByR[rIndex] === null) {
254
- // rBranch is a subtype of an lBranch and
255
- // will not yield any distinct intersection
256
- continue;
257
- }
258
- if (l[lIndex].equals(r[rIndex])) {
259
- // Combination of subtype and supertype cases
260
- batchesByR[rIndex] = null;
261
- candidatesByR = {};
262
- break;
263
- }
264
- const branchIntersection = l[lIndex].intersect(r[rIndex]);
265
- if (branchIntersection instanceof Disjoint) {
266
- // Doesn't tell us anything useful about their relationships
267
- // with other branches
268
- continue;
269
- }
270
- if (branchIntersection.equals(l[lIndex])) {
271
- // If the current l branch is a subtype of r, intersections
272
- // with previous and remaining branches of r won't lead to
273
- // distinct intersections.
274
- batchesByR[rIndex].push(l[lIndex]);
275
- candidatesByR = {};
276
- break;
277
- }
278
- if (branchIntersection.equals(r[rIndex])) {
279
- // If the current r branch is a subtype of l, set its batch to
280
- // null, removing any previous intersections and preventing any
281
- // of its remaining intersections from being computed.
282
- batchesByR[rIndex] = null;
283
- }
284
- else {
285
- // If neither l nor r is a subtype of the other, add their
286
- // intersection as a candidate (could still be removed if it is
287
- // determined l or r is a subtype of a remaining branch).
288
- candidatesByR[rIndex] = branchIntersection;
289
- }
290
- }
291
- for (const rIndex in candidatesByR) {
292
- // batchesByR at rIndex should never be null if it is in candidatesByR
293
- batchesByR[rIndex][lIndex] = candidatesByR[rIndex];
294
- }
295
- }
296
- // Compile the reduced intersection result, including:
297
- // 1. Remaining candidates resulting from distinct intersections or strict subtypes of r
298
- // 2. Original r branches corresponding to indices with a null batch (subtypes of l)
299
- const resultBranches = batchesByR.flatMap(
300
- // ensure unions returned from branchable intersections like sequence are flattened
301
- (batch, i) => batch?.flatMap((branch) => branch.branches) ?? r[i]);
302
- return resultBranches.length === 0
303
- ? Disjoint.from("union", l, r)
304
- : resultBranches;
305
- };
285
+ // If the corresponding r branch is identified as a subtype of an l branch, the
286
+ // value at rIndex is set to null so we can avoid including previous/future
287
+ // inersections in the reduced result.
288
+ const batchesByR = r.map(() => [])
289
+ for (let lIndex = 0; lIndex < l.length; lIndex++) {
290
+ let candidatesByR = {}
291
+ for (let rIndex = 0; rIndex < r.length; rIndex++) {
292
+ if (batchesByR[rIndex] === null) {
293
+ // rBranch is a subtype of an lBranch and
294
+ // will not yield any distinct intersection
295
+ continue
296
+ }
297
+ if (l[lIndex].equals(r[rIndex])) {
298
+ // Combination of subtype and supertype cases
299
+ batchesByR[rIndex] = null
300
+ candidatesByR = {}
301
+ break
302
+ }
303
+ const branchIntersection = l[lIndex].intersect(r[rIndex])
304
+ if (branchIntersection instanceof Disjoint) {
305
+ // Doesn't tell us anything useful about their relationships
306
+ // with other branches
307
+ continue
308
+ }
309
+ if (branchIntersection.equals(l[lIndex])) {
310
+ // If the current l branch is a subtype of r, intersections
311
+ // with previous and remaining branches of r won't lead to
312
+ // distinct intersections.
313
+ batchesByR[rIndex].push(l[lIndex])
314
+ candidatesByR = {}
315
+ break
316
+ }
317
+ if (branchIntersection.equals(r[rIndex])) {
318
+ // If the current r branch is a subtype of l, set its batch to
319
+ // null, removing any previous intersections and preventing any
320
+ // of its remaining intersections from being computed.
321
+ batchesByR[rIndex] = null
322
+ } else {
323
+ // If neither l nor r is a subtype of the other, add their
324
+ // intersection as a candidate (could still be removed if it is
325
+ // determined l or r is a subtype of a remaining branch).
326
+ candidatesByR[rIndex] = branchIntersection
327
+ }
328
+ }
329
+ for (const rIndex in candidatesByR) {
330
+ // batchesByR at rIndex should never be null if it is in candidatesByR
331
+ batchesByR[rIndex][lIndex] = candidatesByR[rIndex]
332
+ }
333
+ }
334
+ // Compile the reduced intersection result, including:
335
+ // 1. Remaining candidates resulting from distinct intersections or strict subtypes of r
336
+ // 2. Original r branches corresponding to indices with a null batch (subtypes of l)
337
+ const resultBranches = batchesByR.flatMap(
338
+ // ensure unions returned from branchable intersections like sequence are flattened
339
+ (batch, i) => batch?.flatMap((branch) => branch.branches) ?? r[i]
340
+ )
341
+ return resultBranches.length === 0
342
+ ? Disjoint.from("union", l, r)
343
+ : resultBranches
344
+ }
306
345
  export const reduceBranches = ({ branches, ordered }) => {
307
- if (branches.length < 2) {
308
- return branches;
309
- }
310
- const uniquenessByIndex = branches.map(() => true);
311
- for (let i = 0; i < branches.length; i++) {
312
- for (let j = i + 1; j < branches.length && uniquenessByIndex[i] && uniquenessByIndex[j]; j++) {
313
- if (branches[i].equals(branches[j])) {
314
- // if the two branches are equal, only "j" is marked as
315
- // redundant so at least one copy could still be included in
316
- // the final set of branches.
317
- uniquenessByIndex[j] = false;
318
- continue;
319
- }
320
- const intersection = branches[i].intersect(branches[j]);
321
- if (intersection instanceof Disjoint) {
322
- continue;
323
- }
324
- if (intersection.equals(branches[i])) {
325
- if (!ordered) {
326
- // preserve ordered branches that are a subtype of a subsequent branch
327
- uniquenessByIndex[i] = false;
328
- }
329
- }
330
- else if (intersection.equals(branches[j])) {
331
- uniquenessByIndex[j] = false;
332
- }
333
- }
334
- }
335
- return branches.filter((_, i) => uniquenessByIndex[i]);
336
- };
337
- //# sourceMappingURL=union.js.map
346
+ if (branches.length < 2) {
347
+ return branches
348
+ }
349
+ const uniquenessByIndex = branches.map(() => true)
350
+ for (let i = 0; i < branches.length; i++) {
351
+ for (
352
+ let j = i + 1;
353
+ j < branches.length && uniquenessByIndex[i] && uniquenessByIndex[j];
354
+ j++
355
+ ) {
356
+ if (branches[i].equals(branches[j])) {
357
+ // if the two branches are equal, only "j" is marked as
358
+ // redundant so at least one copy could still be included in
359
+ // the final set of branches.
360
+ uniquenessByIndex[j] = false
361
+ continue
362
+ }
363
+ const intersection = branches[i].intersect(branches[j])
364
+ if (intersection instanceof Disjoint) {
365
+ continue
366
+ }
367
+ if (intersection.equals(branches[i])) {
368
+ if (!ordered) {
369
+ // preserve ordered branches that are a subtype of a subsequent branch
370
+ uniquenessByIndex[i] = false
371
+ }
372
+ } else if (intersection.equals(branches[j])) {
373
+ uniquenessByIndex[j] = false
374
+ }
375
+ }
376
+ }
377
+ return branches.filter((_, i) => uniquenessByIndex[i])
378
+ }
379
+ //# sourceMappingURL=union.js.map