arktype 2.0.0-dev.5 → 2.0.0-dev.6

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 +4 -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
package/parse.ts DELETED
@@ -1,221 +0,0 @@
1
- import {
2
- entriesOf,
3
- hasDomain,
4
- isArray,
5
- isKeyOf,
6
- registry,
7
- throwParseError,
8
- type Json,
9
- type JsonData,
10
- type PartialRecord,
11
- type evaluate,
12
- type listable,
13
- type valueOf
14
- } from "@arktype/util"
15
- import {
16
- BaseNode,
17
- type BaseAttachments,
18
- type Node,
19
- type UnknownNode
20
- } from "./base.js"
21
- import { nodesByKind, type Schema, type reducibleKindOf } from "./kinds.js"
22
- import type { Scope } from "./scope.js"
23
- import type { BaseNodeDeclaration } from "./shared/declare.js"
24
- import { Disjoint } from "./shared/disjoint.js"
25
- import {
26
- defaultValueSerializer,
27
- precedenceOfKind,
28
- type KeyDefinitions,
29
- type NodeKind,
30
- type UnknownNodeImplementation
31
- } from "./shared/implement.js"
32
-
33
- export type SchemaParseOptions = {
34
- alias?: string
35
- prereduced?: boolean
36
- // TODO: check if reduceTo works across scopes
37
- /** Instead of creating the node, compute the innerId of the definition and
38
- * point it to the specified resolution.
39
- *
40
- * Useful for defining reductions like number|string|bigint|symbol|object|true|false|null|undefined => unknown
41
- **/
42
- reduceTo?: UnknownNode
43
- root?: boolean
44
- allowedKinds?: readonly NodeKind[]
45
- }
46
-
47
- export type SchemaParseContext = evaluate<
48
- SchemaParseOptions & {
49
- $: Scope
50
- raw: unknown
51
- }
52
- >
53
-
54
- const typeCountsByPrefix: PartialRecord<string, number> = {}
55
-
56
- const baseKeys: PartialRecord<string, valueOf<KeyDefinitions<any>>> = {
57
- description: { meta: true }
58
- } satisfies KeyDefinitions<BaseNodeDeclaration> as never
59
-
60
- export function parseAttachments<defKind extends NodeKind>(
61
- kind: defKind,
62
- schema: Schema<defKind>,
63
- ctx: SchemaParseContext
64
- ): Node<reducibleKindOf<defKind>>
65
- // eslint-disable-next-line prefer-arrow-functions/prefer-arrow-functions
66
- export function parseAttachments(
67
- kind: NodeKind,
68
- schema: unknown,
69
- ctx: SchemaParseContext
70
- ): UnknownNode {
71
- const cls = nodesByKind[kind]
72
- const impl: UnknownNodeImplementation = cls.implementation as never
73
- if (schema instanceof BaseNode && schema.kind === kind) {
74
- return schema as never
75
- }
76
- const normalizedDefinition: any = impl.normalize?.(schema) ?? schema
77
- // check again after normalization in case a node is a valid collapsed
78
- // schema for the kind (e.g. sequence can collapse to element accepting a Node)
79
- if (normalizedDefinition instanceof BaseNode) {
80
- return normalizedDefinition.kind === kind
81
- ? (normalizedDefinition as never)
82
- : throwMismatchedNodeSchemaError(kind, normalizedDefinition.kind)
83
- }
84
- const inner: Record<string, unknown> = {}
85
- // ensure node entries are parsed in order of precedence, with non-children
86
- // parsed first
87
- const schemaEntries = entriesOf(normalizedDefinition).sort(
88
- ([lKey], [rKey]) =>
89
- isKeyOf(lKey, nodesByKind)
90
- ? isKeyOf(rKey, nodesByKind)
91
- ? precedenceOfKind(lKey) - precedenceOfKind(rKey)
92
- : 1
93
- : isKeyOf(rKey, nodesByKind)
94
- ? -1
95
- : lKey < rKey
96
- ? -1
97
- : 1
98
- )
99
- const children: UnknownNode[] = []
100
- for (const entry of schemaEntries) {
101
- const k = entry[0]
102
- const keyImpl = impl.keys[k] ?? baseKeys[k]
103
- if (!keyImpl) {
104
- return throwParseError(`Key ${k} is not valid on ${kind} schema`)
105
- }
106
- const v = keyImpl.parse ? keyImpl.parse(entry[1], ctx) : entry[1]
107
- if (v !== undefined || keyImpl.preserveUndefined) {
108
- inner[k] = v
109
- }
110
- }
111
- const entries = entriesOf(inner)
112
-
113
- let json: Record<string, unknown> = {}
114
- let typeJson: Record<string, unknown> = {}
115
- let collapsibleJson: Record<string, unknown> = {}
116
- entries.forEach(([k, v]) => {
117
- const keyImpl = impl.keys[k] ?? baseKeys[k]
118
- if (keyImpl.child) {
119
- const listableNode = v as listable<UnknownNode>
120
- if (isArray(listableNode)) {
121
- json[k] = listableNode.map((node) => node.collapsibleJson)
122
- children.push(...listableNode)
123
- } else {
124
- json[k] = listableNode.collapsibleJson
125
- children.push(listableNode)
126
- }
127
- } else {
128
- json[k] = keyImpl.serialize
129
- ? keyImpl.serialize(v)
130
- : defaultValueSerializer(v)
131
- }
132
-
133
- if (!keyImpl.meta) {
134
- typeJson[k] = json[k]
135
- }
136
- if (!keyImpl.implied) {
137
- collapsibleJson[k] = json[k]
138
- }
139
- })
140
-
141
- // check keys on collapsibleJson instead of schema in case one or more keys is
142
- // implied, e.g. minVariadicLength on a SequenceNode
143
- const collapsibleKeys = Object.keys(collapsibleJson)
144
- if (
145
- collapsibleKeys.length === 1 &&
146
- collapsibleKeys[0] === impl.collapsibleKey
147
- ) {
148
- collapsibleJson = collapsibleJson[impl.collapsibleKey] as never
149
- if (
150
- // if the collapsibleJson is still an object
151
- hasDomain(collapsibleJson, "object") &&
152
- // and the JSON did not include any implied keys
153
- Object.keys(json).length === 1
154
- ) {
155
- // we can replace it with its collapsed value
156
- json = collapsibleJson
157
- typeJson = collapsibleJson
158
- }
159
- }
160
- const innerId = JSON.stringify({ kind, ...json })
161
- if (ctx.reduceTo) {
162
- return (ctx.$.nodeCache[innerId] = ctx.reduceTo)
163
- }
164
-
165
- const typeId = JSON.stringify({ kind, ...typeJson })
166
-
167
- if (impl.reduce && !ctx.prereduced) {
168
- const reduced = impl.reduce(inner, ctx.$)
169
- if (reduced) {
170
- if (reduced instanceof Disjoint) return reduced.throw()
171
-
172
- // if we're defining the resolution of an alias and the result is
173
- // reduced to another node, add the alias to that node if it doesn't
174
- // already have one.
175
- if (ctx.alias) {
176
- reduced.alias ??= ctx.alias
177
- }
178
- // we can't cache this reduction for now in case the reduction involved
179
- // impliedSiblings
180
- return reduced
181
- }
182
- }
183
-
184
- // we have to wait until after reduction to return a cached entry,
185
- // since reduction can add impliedSiblings
186
- if (innerId in ctx.$.nodeCache) {
187
- return ctx.$.nodeCache[innerId]
188
- }
189
-
190
- const prefix = ctx.alias ?? kind
191
- typeCountsByPrefix[prefix] ??= 0
192
- const reference = `${prefix}${++typeCountsByPrefix[prefix]!}`
193
- const attachments = {
194
- reference,
195
- kind,
196
- inner,
197
- entries,
198
- json: json as Json,
199
- typeJson: typeJson as Json,
200
- collapsibleJson: collapsibleJson as JsonData,
201
- children,
202
- innerId,
203
- typeId,
204
- $: ctx.$
205
- } satisfies BaseAttachments as Record<string, any>
206
- if (ctx.alias) {
207
- attachments.alias = ctx.alias
208
- }
209
- for (const k in inner) {
210
- // avoid conflict with builtin cached getters
211
- if (k !== "description" && k !== "in" && k !== "out") {
212
- attachments[k] = inner[k]
213
- }
214
- }
215
- return (ctx.$.nodeCache[innerId] = new cls(attachments as never))
216
- }
217
-
218
- const throwMismatchedNodeSchemaError = (expected: NodeKind, actual: NodeKind) =>
219
- throwParseError(
220
- `Node of kind ${actual} is not valid as a ${expected} definition`
221
- )
package/schema.ts DELETED
@@ -1,263 +0,0 @@
1
- import type {
2
- Constructor,
3
- ErrorMessage,
4
- NonEnumerableDomain,
5
- array,
6
- conform,
7
- describe,
8
- inferDomain,
9
- instanceOf,
10
- isAny
11
- } from "@arktype/util"
12
- import type { Node, TypeSchema, UnknownNode } from "./base.js"
13
- import type { Prerequisite, Schema, reducibleKindOf } from "./kinds.js"
14
- import type { SchemaParseOptions } from "./parse.js"
15
- import type { BasisKind, ConstraintKind, NodeKind } from "./shared/implement.js"
16
- import type { DomainNode, DomainSchema } from "./types/domain.js"
17
- import type {
18
- IntersectionNode,
19
- IntersectionSchema
20
- } from "./types/intersection.js"
21
- import type {
22
- Morph,
23
- MorphChildDefinition,
24
- MorphChildKind,
25
- MorphNode,
26
- MorphSchema,
27
- Out,
28
- inferMorphOut
29
- } from "./types/morph.js"
30
- import type { ProtoNode, ProtoSchema } from "./types/proto.js"
31
- import type {
32
- NormalizedUnionSchema,
33
- UnionChildNode,
34
- UnionNode,
35
- UnionSchema
36
- } from "./types/union.js"
37
- import type { UnitNode, UnitSchema } from "./types/unit.js"
38
-
39
- export type SchemaParser<$> = <const schema>(
40
- schema: validateSchema<schema, $>
41
- ) => ["schema", schema]
42
-
43
- export type NodeParser<$> = {
44
- <kind extends NodeKind, const schema extends Schema<kind>>(
45
- kind: kind,
46
- schema: schema,
47
- opts?: SchemaParseOptions
48
- ): Node<reducibleKindOf<kind>, instantiateSchema<schema, $>["infer"], $>
49
- <const schema extends TypeSchema>(
50
- schema: schema,
51
- opts?: SchemaParseOptions
52
- ): instantiateSchema<schema, $>
53
- }
54
-
55
- export type validateSchema<schema, $> = schema extends UnknownNode
56
- ? schema
57
- : schema extends array
58
- ? { [i in keyof schema]: validateSchemaBranch<schema[i], $> }
59
- : schema extends NormalizedUnionSchema<infer branches>
60
- ? conform<
61
- schema,
62
- NormalizedUnionSchema & {
63
- branches: {
64
- [i in keyof branches]: validateSchemaBranch<branches[i], $>
65
- }
66
- }
67
- >
68
- : validateSchemaBranch<schema, $>
69
-
70
- export type instantiateSchema<schema, $> = schema extends UnionSchema<
71
- infer branches
72
- >
73
- ? branches["length"] extends 0
74
- ? UnionNode<never, "union">
75
- : branches["length"] extends 1
76
- ? instantiateSchemaBranch<branches[0], $>
77
- : Node<
78
- reducibleKindOf<"union">,
79
- instantiateSchemaBranch<branches[number], $>["infer"]
80
- >
81
- : instantiateSchemaBranch<schema, $>
82
-
83
- type validateSchemaBranch<schema, $> = schema extends UnknownNode
84
- ? schema
85
- : keyof schema & ("morph" | "in" | "out") extends never
86
- ? validateMorphChild<schema, $>
87
- : validateMorphSchema<schema, $>
88
-
89
- type instantiateSchemaBranch<schema, $> = schema extends UnknownNode
90
- ? schema
91
- : schema extends MorphSchema
92
- ? instantiateMorphSchema<schema, $>
93
- : schema extends MorphChildDefinition
94
- ? instantiateMorphChild<schema, $>
95
- : UnionChildNode
96
-
97
- type NonIntersectableBasisSchema =
98
- | NonEnumerableDomain
99
- | Constructor
100
- | UnitSchema
101
-
102
- type validateMorphChild<schema, $> = [schema] extends [
103
- NonIntersectableBasisSchema
104
- ]
105
- ? schema
106
- : validateIntersectionSchema<schema, $>
107
-
108
- type instantiateMorphChild<schema, $> =
109
- schema extends NonIntersectableBasisSchema
110
- ? instantiateBasis<schema, $>
111
- : schema extends IntersectionSchema
112
- ? instantiateIntersectionSchema<schema, $>
113
- : Node<MorphChildKind>
114
-
115
- type validateMorphSchema<def, $> = {
116
- [k in keyof def]: k extends "in" | "out"
117
- ? validateMorphChild<def[k], $>
118
- : k extends keyof MorphSchema
119
- ? MorphSchema[k]
120
- : `'${k & string}' is not a valid morph schema key`
121
- }
122
-
123
- type instantiateMorphSchema<schema, $> = schema extends MorphSchema
124
- ? MorphNode<
125
- (
126
- In: schema["in"] extends {}
127
- ? instantiateMorphChild<schema["in"], $>["infer"]
128
- : unknown
129
- ) => schema["out"] extends {}
130
- ? Out<instantiateMorphChild<schema["out"], $>["infer"]>
131
- : schema["morphs"] extends
132
- | Morph<any, infer o>
133
- | readonly [...unknown[], Morph<any, infer o>]
134
- ? Out<inferMorphOut<o>>
135
- : never
136
- >
137
- : never
138
-
139
- type exactBasisMessageOnError<schema, expected> = {
140
- [k in keyof schema]: k extends keyof expected
141
- ? conform<schema[k], expected[k]>
142
- : ErrorMessage<
143
- k extends ConstraintKind
144
- ? `${k} has a prerequisite of ${describe<Prerequisite<k>>}`
145
- : `'${k & string}' is not on an intersection schema`
146
- >
147
- }
148
-
149
- export type validateIntersectionSchema<schema, $> = exactBasisMessageOnError<
150
- schema,
151
- IntersectionSchema<inferBasisOf<schema, $>>
152
- >
153
-
154
- type inferBasisOf<schema, $> = "proto" extends keyof schema
155
- ? instantiateBasis<conform<schema["proto"], ProtoSchema>, $>["infer"]
156
- : "domain" extends keyof schema
157
- ? instantiateBasis<conform<schema["domain"], DomainSchema>, $>["infer"]
158
- : unknown
159
-
160
- export type instantiateIntersectionSchema<schema, $> = keyof schema &
161
- ConstraintKind extends never
162
- ? "proto" extends keyof schema
163
- ? ProtoNode<inferBasisOf<schema, $>, $>
164
- : "domain" extends keyof schema
165
- ? DomainNode<inferBasisOf<schema, $>, $>
166
- : IntersectionNode
167
- : IntersectionNode<inferBasisOf<schema, $>, $>
168
-
169
- export type instantiateBasis<schema extends Schema<BasisKind>, $> =
170
- //allow any to be used to access all constraints
171
- isAny<schema> extends true
172
- ? any
173
- : schema extends NonEnumerableDomain
174
- ? DomainNode<inferDomain<schema>, $>
175
- : schema extends Constructor<infer instance>
176
- ? ProtoNode<instance, $>
177
- : schema extends DomainSchema<infer domain>
178
- ? DomainNode<inferDomain<domain>, $>
179
- : schema extends ProtoSchema<infer proto>
180
- ? ProtoNode<instanceOf<proto>, $>
181
- : schema extends UnitSchema<infer is>
182
- ? UnitNode<is, $>
183
- : never
184
-
185
- // export type inferPropsInput<input extends PropsInput> =
186
- // input extends PropsInputTuple<infer named, infer indexed>
187
- // ? inferIndexed<indexed, inferNamedProps<named, indexed>>
188
- // : input extends NamedPropsInput
189
- // ? inferNamedProps<input, []>
190
- // : never
191
-
192
- // type inferIndexed<
193
- // indexed extends readonly IndexedPropInput[],
194
- // result = unknown
195
- // > = indexed extends readonly [
196
- // infer entry extends IndexedPropInput,
197
- // ...infer tail extends IndexedPropInput[]
198
- // ]
199
- // ? inferIndexed<
200
- // tail,
201
- // entry["key"] extends { readonly regex: VariadicIndexMatcherLiteral }
202
- // ? result extends List
203
- // ? [...result, ...inferTypeInput<entry["value"]>[]]
204
- // : never
205
- // : entry["key"] extends {
206
- // readonly regex: NonVariadicIndexMatcherLiteral
207
- // }
208
- // ? inferTypeInput<entry["value"]>[]
209
- // : Record<
210
- // Extract<inferTypeInput<entry["key"]>, PropertyKey>,
211
- // inferTypeInput<entry["value"]>
212
- // >
213
- // >
214
- // : result
215
-
216
- // type inferNamedProps<
217
- // named extends NamedPropsInput,
218
- // indexed extends readonly IndexedPropInput[]
219
- // > = [named, indexed[0]["key"]] extends
220
- // | [TupleLengthProps, unknown]
221
- // | [unknown, { readonly regex: VariadicIndexMatcherLiteral }]
222
- // ? inferNonVariadicTupleProps<named> &
223
- // inferObjectLiteralProps<Omit<named, "length" | NumberLiteral | number>>
224
- // : inferObjectLiteralProps<named>
225
-
226
- // type inferObjectLiteralProps<named extends NamedPropsInput> = {} extends named
227
- // ? unknown
228
- // : evaluate<
229
- // {
230
- // [k in requiredKeyOf<named>]: inferPropValue<named[k]["value"]>
231
- // } & {
232
- // [k in optionalKeyOf<named>]?: inferPropValue<named[k]["value"]>
233
- // }
234
- // >
235
-
236
- // type inferPropValue<value extends PropValueInput> = value extends Thunk<
237
- // infer ret
238
- // >
239
- // ? inferResolvedPropValue<ret>
240
- // : inferResolvedPropValue<value>
241
-
242
- // type inferResolvedPropValue<value> = value extends TypeNode<infer t>
243
- // ? t
244
- // : inferTypeInput<Extract<value, TypeInput>>
245
-
246
- // type stringifiedNumericKeyOf<t> = `${Extract<keyof t, number | NumberLiteral>}`
247
-
248
- // type inferNonVariadicTupleProps<
249
- // named extends NamedPropsInput,
250
- // result extends unknown[] = []
251
- // > = `${result["length"]}` extends stringifiedNumericKeyOf<named>
252
- // ? inferNonVariadicTupleProps<
253
- // named,
254
- // [...result, inferPropValue<named[`${result["length"]}`]["value"]>]
255
- // >
256
- // : result
257
-
258
- // type TupleLengthProps<length extends number = number> = {
259
- // readonly length: {
260
- // readonly prerequisite: true
261
- // readonly value: { readonly basis: readonly ["===", length] }
262
- // }
263
- // }
package/shared/compile.ts DELETED
@@ -1,92 +0,0 @@
1
- import { CompiledFunction, compileSerializedValue } from "@arktype/util"
2
- import type { Node, UnknownNode } from "../base.js"
3
- import type { Discriminant } from "../types/discriminate.js"
4
- import type { PrimitiveKind } from "./implement.js"
5
- import type { TraversalKind } from "./traversal.js"
6
-
7
- export type InvokeOptions = {
8
- arg?: string
9
- kind?: TraversalKind
10
- }
11
-
12
- export class NodeCompiler extends CompiledFunction<["data", "ctx"]> {
13
- path: string[] = []
14
- discriminants: Discriminant[] = []
15
-
16
- constructor(public traversalKind: TraversalKind) {
17
- super("data", "ctx")
18
- }
19
-
20
- invoke(node: UnknownNode, opts?: InvokeOptions): string {
21
- const invokedKind = opts?.kind ?? this.traversalKind
22
- const method = `${node.reference}${invokedKind}`
23
- const arg = opts?.arg ?? this.data
24
- if (this.requiresContextFor(node)) {
25
- return `this.${method}(${arg}, ${this.ctx})`
26
- }
27
- return `this.${method}(${arg})`
28
- }
29
-
30
- requiresContextFor(node: UnknownNode): boolean {
31
- return (
32
- this.traversalKind === "Apply" || node.includesContextDependentPredicate
33
- )
34
- }
35
-
36
- checkReferenceKey(keyExpression: string, node: UnknownNode): this {
37
- const requiresContext = this.requiresContextFor(node)
38
- if (requiresContext) {
39
- this.line(`${this.ctx}.path.push(${keyExpression})`)
40
- }
41
- this.check(node, {
42
- arg: `${this.data}${this.index(keyExpression)}`
43
- })
44
- if (requiresContext) {
45
- this.line(`${this.ctx}.path.pop()`)
46
- }
47
- return this
48
- }
49
-
50
- check(node: UnknownNode, opts?: InvokeOptions): this {
51
- return this.traversalKind === "Allows"
52
- ? this.if(`!${this.invoke(node, opts)}`, () => this.return(false))
53
- : this.line(this.invoke(node, opts))
54
- }
55
-
56
- compilePrimitive(node: Node<PrimitiveKind>): this {
57
- const pathString = this.path.join()
58
- if (
59
- node.kind === "domain" &&
60
- node.domain === "object" &&
61
- this.discriminants.some((d) => d.path.join().startsWith(pathString))
62
- ) {
63
- // if we've already checked a path at least as long as the current one,
64
- // we don't need to revalidate that we're in an object
65
- return this
66
- }
67
- if (
68
- (node.kind === "domain" || node.kind === "unit") &&
69
- this.discriminants.some(
70
- (d) =>
71
- d.path.join() === pathString &&
72
- (node.kind === "domain"
73
- ? d.kind === "domain" || d.kind === "value"
74
- : d.kind === "value")
75
- )
76
- ) {
77
- // if the discriminant has already checked the domain at the current path
78
- // (or an exact value, implying a domain), we don't need to recheck it
79
- return this
80
- }
81
- if (this.traversalKind === "Allows") {
82
- return this.return(node.compiledCondition)
83
- }
84
- return this.if(node.compiledNegation, () =>
85
- this.line(`${this.ctx}.error(${node.compiledErrorContext})`)
86
- )
87
- }
88
-
89
- writeMethod(name: string): string {
90
- return `${name}(${this.argNames.join(", ")}){\n${this.body} }\n`
91
- }
92
- }
package/shared/declare.ts DELETED
@@ -1,75 +0,0 @@
1
- import type { evaluate, merge } from "@arktype/util"
2
- import type { NarrowedAttachments, Node } from "../base.js"
3
- import type { reducibleKindOf } from "../kinds.js"
4
- import type { Disjoint } from "./disjoint.js"
5
- import type { NodeKind } from "./implement.js"
6
-
7
- export interface BaseMeta {
8
- readonly description?: string
9
- }
10
-
11
- export const metaKeys: { [k in keyof BaseMeta]: 1 } = { description: 1 }
12
-
13
- interface DeclarationInput {
14
- kind: NodeKind
15
- schema: unknown
16
- normalizedSchema: BaseMeta
17
- inner: BaseMeta
18
- reducibleTo?: NodeKind
19
- intersectionIsOpen?: true
20
- errorContext?: object
21
- prerequisite?: unknown
22
- childKind?: NodeKind
23
- }
24
-
25
- export interface BaseErrorContext<kind extends NodeKind = NodeKind> {
26
- code: kind
27
- }
28
-
29
- export type defaultErrorContext<d extends DeclarationInput> = evaluate<
30
- BaseErrorContext<d["kind"]> & d["inner"]
31
- >
32
-
33
- export type declareNode<
34
- d extends {
35
- [k in keyof d]: k extends keyof DeclarationInput
36
- ? DeclarationInput[k]
37
- : never
38
- } & DeclarationInput
39
- > = merge<
40
- {
41
- intersectionIsOpen: false
42
- prerequisite: prerequisiteOf<d>
43
- childKind: never
44
- reducibleTo: d["kind"]
45
- errorContext: null
46
- },
47
- d & {
48
- errorContext: d["errorContext"] extends {}
49
- ? BaseErrorContext<d["kind"]>
50
- : null
51
- }
52
- >
53
-
54
- type prerequisiteOf<d extends DeclarationInput> = "prerequisite" extends keyof d
55
- ? d["prerequisite"]
56
- : unknown
57
-
58
- export type BaseAttachmentsOf<d extends BaseNodeDeclaration> =
59
- NarrowedAttachments<d> & d["inner"]
60
-
61
- export type BaseNodeDeclaration = {
62
- kind: NodeKind
63
- schema: unknown
64
- normalizedSchema: BaseMeta
65
- inner: BaseMeta
66
- reducibleTo: NodeKind
67
- prerequisite: any
68
- intersectionIsOpen: boolean
69
- childKind: NodeKind
70
- errorContext: BaseErrorContext | null
71
- }
72
-
73
- export type ownIntersectionResult<d extends BaseNodeDeclaration> =
74
- | Node<reducibleKindOf<d["kind"]>>
75
- | Disjoint