arktype 1.0.13-alpha → 1.0.15-alpha

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 (247) hide show
  1. package/README.md +68 -11
  2. package/dist/main.js +29 -0
  3. package/dist/nodes/branch.js +135 -0
  4. package/dist/nodes/compose.js +99 -0
  5. package/dist/{cjs/nodes → nodes}/discriminate.js +100 -92
  6. package/dist/nodes/node.js +134 -0
  7. package/dist/nodes/predicate.js +88 -0
  8. package/dist/nodes/rules/class.js +23 -0
  9. package/dist/nodes/rules/collapsibleSet.js +34 -0
  10. package/dist/nodes/rules/divisor.js +19 -0
  11. package/dist/nodes/rules/props.js +130 -0
  12. package/dist/nodes/rules/range.js +108 -0
  13. package/dist/nodes/rules/regex.js +17 -0
  14. package/dist/nodes/rules/rules.js +102 -0
  15. package/dist/parse/ast/ast.js +2 -0
  16. package/dist/{types/parse → parse}/ast/bound.d.ts +4 -1
  17. package/dist/parse/ast/bound.js +7 -0
  18. package/dist/parse/ast/config.js +14 -0
  19. package/dist/parse/ast/distributableFunction.js +40 -0
  20. package/dist/parse/ast/divisor.js +5 -0
  21. package/dist/parse/ast/intersection.js +21 -0
  22. package/dist/parse/ast/keyof.js +107 -0
  23. package/dist/parse/ast/morph.js +50 -0
  24. package/dist/parse/ast/narrow.js +17 -0
  25. package/dist/{types/parse → parse}/ast/node.d.ts +3 -1
  26. package/dist/parse/ast/node.js +2 -0
  27. package/dist/parse/ast/tuple.js +72 -0
  28. package/dist/parse/ast/union.js +5 -0
  29. package/dist/parse/ast/utils.js +2 -0
  30. package/dist/parse/definition.js +47 -0
  31. package/dist/parse/record.js +31 -0
  32. package/dist/parse/string/reduce/dynamic.js +159 -0
  33. package/dist/parse/string/reduce/shared.js +13 -0
  34. package/dist/parse/string/reduce/static.js +2 -0
  35. package/dist/parse/string/shift/operand/enclosed.js +37 -0
  36. package/dist/parse/string/shift/operand/operand.js +15 -0
  37. package/dist/parse/string/shift/operand/unenclosed.js +45 -0
  38. package/dist/parse/string/shift/operator/bounds.js +83 -0
  39. package/dist/parse/string/shift/operator/divisor.js +23 -0
  40. package/dist/parse/string/shift/operator/operator.js +34 -0
  41. package/dist/parse/string/shift/scanner.js +97 -0
  42. package/dist/parse/string/string.js +35 -0
  43. package/dist/scopes/ark.js +27 -0
  44. package/dist/scopes/cache.js +28 -0
  45. package/dist/{mjs/scopes → scopes}/expressions.js +20 -9
  46. package/dist/scopes/jsObjects.js +20 -0
  47. package/dist/scopes/scope.js +184 -0
  48. package/dist/scopes/tsKeywords.js +49 -0
  49. package/dist/scopes/type.js +40 -0
  50. package/dist/{mjs/scopes → scopes}/validation/creditCard.js +15 -11
  51. package/dist/{mjs/scopes → scopes}/validation/date.js +21 -16
  52. package/dist/scopes/validation/validation.js +74 -0
  53. package/dist/traverse/problems.js +234 -0
  54. package/dist/traverse/traverse.js +330 -0
  55. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  56. package/dist/{mjs/utils → utils}/chainableNoOpProxy.js +6 -2
  57. package/dist/utils/data.js +39 -0
  58. package/dist/utils/domains.js +27 -0
  59. package/dist/utils/errors.js +17 -0
  60. package/dist/utils/freeze.js +16 -0
  61. package/dist/utils/generics.js +53 -0
  62. package/dist/utils/numericLiterals.js +88 -0
  63. package/dist/utils/objectKinds.js +69 -0
  64. package/dist/{mjs/utils → utils}/paths.js +8 -3
  65. package/dist/utils/serialize.js +71 -0
  66. package/dist/utils/unionToTuple.js +2 -0
  67. package/package.json +25 -39
  68. package/dist/cjs/main.js +0 -38
  69. package/dist/cjs/nodes/branch.js +0 -140
  70. package/dist/cjs/nodes/compose.js +0 -157
  71. package/dist/cjs/nodes/node.js +0 -139
  72. package/dist/cjs/nodes/predicate.js +0 -86
  73. package/dist/cjs/nodes/rules/class.js +0 -25
  74. package/dist/cjs/nodes/rules/collapsibleSet.js +0 -48
  75. package/dist/cjs/nodes/rules/divisor.js +0 -29
  76. package/dist/cjs/nodes/rules/props.js +0 -160
  77. package/dist/cjs/nodes/rules/range.js +0 -105
  78. package/dist/cjs/nodes/rules/regex.js +0 -26
  79. package/dist/cjs/nodes/rules/rules.js +0 -123
  80. package/dist/cjs/package.json +0 -3
  81. package/dist/cjs/parse/ast/ast.js +0 -4
  82. package/dist/cjs/parse/ast/bound.js +0 -9
  83. package/dist/cjs/parse/ast/config.js +0 -13
  84. package/dist/cjs/parse/ast/distributableFunction.js +0 -48
  85. package/dist/cjs/parse/ast/divisor.js +0 -9
  86. package/dist/cjs/parse/ast/intersection.js +0 -30
  87. package/dist/cjs/parse/ast/keyof.js +0 -111
  88. package/dist/cjs/parse/ast/morph.js +0 -58
  89. package/dist/cjs/parse/ast/narrow.js +0 -22
  90. package/dist/cjs/parse/ast/node.js +0 -4
  91. package/dist/cjs/parse/ast/tuple.js +0 -94
  92. package/dist/cjs/parse/ast/union.js +0 -9
  93. package/dist/cjs/parse/ast/utils.js +0 -4
  94. package/dist/cjs/parse/definition.js +0 -61
  95. package/dist/cjs/parse/record.js +0 -37
  96. package/dist/cjs/parse/string/reduce/dynamic.js +0 -167
  97. package/dist/cjs/parse/string/reduce/shared.js +0 -23
  98. package/dist/cjs/parse/string/reduce/static.js +0 -4
  99. package/dist/cjs/parse/string/shift/operand/enclosed.js +0 -54
  100. package/dist/cjs/parse/string/shift/operand/operand.js +0 -11
  101. package/dist/cjs/parse/string/shift/operand/unenclosed.js +0 -53
  102. package/dist/cjs/parse/string/shift/operator/bounds.js +0 -87
  103. package/dist/cjs/parse/string/shift/operator/divisor.js +0 -35
  104. package/dist/cjs/parse/string/shift/operator/operator.js +0 -26
  105. package/dist/cjs/parse/string/shift/scanner.js +0 -114
  106. package/dist/cjs/parse/string/string.js +0 -43
  107. package/dist/cjs/scopes/ark.js +0 -45
  108. package/dist/cjs/scopes/cache.js +0 -52
  109. package/dist/cjs/scopes/expressions.js +0 -29
  110. package/dist/cjs/scopes/jsObjects.js +0 -93
  111. package/dist/cjs/scopes/scope.js +0 -323
  112. package/dist/cjs/scopes/tsKeywords.js +0 -113
  113. package/dist/cjs/scopes/type.js +0 -50
  114. package/dist/cjs/scopes/validation/creditCard.js +0 -47
  115. package/dist/cjs/scopes/validation/date.js +0 -62
  116. package/dist/cjs/scopes/validation/validation.js +0 -104
  117. package/dist/cjs/traverse/problems.js +0 -281
  118. package/dist/cjs/traverse/traverse.js +0 -387
  119. package/dist/cjs/utils/chainableNoOpProxy.js +0 -23
  120. package/dist/cjs/utils/data.js +0 -53
  121. package/dist/cjs/utils/domains.js +0 -30
  122. package/dist/cjs/utils/errors.js +0 -26
  123. package/dist/cjs/utils/freeze.js +0 -15
  124. package/dist/cjs/utils/generics.js +0 -63
  125. package/dist/cjs/utils/numericLiterals.js +0 -75
  126. package/dist/cjs/utils/objectKinds.js +0 -69
  127. package/dist/cjs/utils/paths.js +0 -32
  128. package/dist/cjs/utils/serialize.js +0 -74
  129. package/dist/cjs/utils/unionToTuple.js +0 -4
  130. package/dist/mjs/main.js +0 -8
  131. package/dist/mjs/nodes/branch.js +0 -123
  132. package/dist/mjs/nodes/compose.js +0 -135
  133. package/dist/mjs/nodes/discriminate.js +0 -247
  134. package/dist/mjs/nodes/node.js +0 -117
  135. package/dist/mjs/nodes/predicate.js +0 -69
  136. package/dist/mjs/nodes/rules/class.js +0 -11
  137. package/dist/mjs/nodes/rules/collapsibleSet.js +0 -34
  138. package/dist/mjs/nodes/rules/divisor.js +0 -15
  139. package/dist/mjs/nodes/rules/props.js +0 -142
  140. package/dist/mjs/nodes/rules/range.js +0 -86
  141. package/dist/mjs/nodes/rules/regex.js +0 -11
  142. package/dist/mjs/nodes/rules/rules.js +0 -106
  143. package/dist/mjs/package.json +0 -3
  144. package/dist/mjs/parse/ast/ast.js +0 -1
  145. package/dist/mjs/parse/ast/bound.js +0 -1
  146. package/dist/mjs/parse/ast/config.js +0 -9
  147. package/dist/mjs/parse/ast/distributableFunction.js +0 -34
  148. package/dist/mjs/parse/ast/divisor.js +0 -1
  149. package/dist/mjs/parse/ast/intersection.js +0 -16
  150. package/dist/mjs/parse/ast/keyof.js +0 -103
  151. package/dist/mjs/parse/ast/morph.js +0 -44
  152. package/dist/mjs/parse/ast/narrow.js +0 -14
  153. package/dist/mjs/parse/ast/node.js +0 -1
  154. package/dist/mjs/parse/ast/tuple.js +0 -80
  155. package/dist/mjs/parse/ast/union.js +0 -1
  156. package/dist/mjs/parse/ast/utils.js +0 -1
  157. package/dist/mjs/parse/definition.js +0 -46
  158. package/dist/mjs/parse/record.js +0 -29
  159. package/dist/mjs/parse/string/reduce/dynamic.js +0 -159
  160. package/dist/mjs/parse/string/reduce/shared.js +0 -6
  161. package/dist/mjs/parse/string/reduce/static.js +0 -1
  162. package/dist/mjs/parse/string/shift/operand/enclosed.js +0 -39
  163. package/dist/mjs/parse/string/shift/operand/operand.js +0 -3
  164. package/dist/mjs/parse/string/shift/operand/unenclosed.js +0 -36
  165. package/dist/mjs/parse/string/shift/operator/bounds.js +0 -70
  166. package/dist/mjs/parse/string/shift/operator/divisor.js +0 -21
  167. package/dist/mjs/parse/string/shift/operator/operator.js +0 -13
  168. package/dist/mjs/parse/string/shift/scanner.js +0 -106
  169. package/dist/mjs/parse/string/string.js +0 -28
  170. package/dist/mjs/scopes/ark.js +0 -28
  171. package/dist/mjs/scopes/cache.js +0 -38
  172. package/dist/mjs/scopes/jsObjects.js +0 -82
  173. package/dist/mjs/scopes/scope.js +0 -303
  174. package/dist/mjs/scopes/tsKeywords.js +0 -116
  175. package/dist/mjs/scopes/type.js +0 -35
  176. package/dist/mjs/scopes/validation/validation.js +0 -108
  177. package/dist/mjs/traverse/problems.js +0 -260
  178. package/dist/mjs/traverse/traverse.js +0 -370
  179. package/dist/mjs/utils/data.js +0 -38
  180. package/dist/mjs/utils/domains.js +0 -15
  181. package/dist/mjs/utils/errors.js +0 -10
  182. package/dist/mjs/utils/freeze.js +0 -7
  183. package/dist/mjs/utils/generics.js +0 -38
  184. package/dist/mjs/utils/numericLiterals.js +0 -72
  185. package/dist/mjs/utils/objectKinds.js +0 -55
  186. package/dist/mjs/utils/serialize.js +0 -58
  187. package/dist/mjs/utils/unionToTuple.js +0 -1
  188. /package/dist/{types/main.d.ts → main.d.ts} +0 -0
  189. /package/dist/{types/nodes → nodes}/branch.d.ts +0 -0
  190. /package/dist/{types/nodes → nodes}/compose.d.ts +0 -0
  191. /package/dist/{types/nodes → nodes}/discriminate.d.ts +0 -0
  192. /package/dist/{types/nodes → nodes}/node.d.ts +0 -0
  193. /package/dist/{types/nodes → nodes}/predicate.d.ts +0 -0
  194. /package/dist/{types/nodes → nodes}/rules/class.d.ts +0 -0
  195. /package/dist/{types/nodes → nodes}/rules/collapsibleSet.d.ts +0 -0
  196. /package/dist/{types/nodes → nodes}/rules/divisor.d.ts +0 -0
  197. /package/dist/{types/nodes → nodes}/rules/props.d.ts +0 -0
  198. /package/dist/{types/nodes → nodes}/rules/range.d.ts +0 -0
  199. /package/dist/{types/nodes → nodes}/rules/regex.d.ts +0 -0
  200. /package/dist/{types/nodes → nodes}/rules/rules.d.ts +0 -0
  201. /package/dist/{types/parse → parse}/ast/ast.d.ts +0 -0
  202. /package/dist/{types/parse → parse}/ast/config.d.ts +0 -0
  203. /package/dist/{types/parse → parse}/ast/distributableFunction.d.ts +0 -0
  204. /package/dist/{types/parse → parse}/ast/divisor.d.ts +0 -0
  205. /package/dist/{types/parse → parse}/ast/intersection.d.ts +0 -0
  206. /package/dist/{types/parse → parse}/ast/keyof.d.ts +0 -0
  207. /package/dist/{types/parse → parse}/ast/morph.d.ts +0 -0
  208. /package/dist/{types/parse → parse}/ast/narrow.d.ts +0 -0
  209. /package/dist/{types/parse → parse}/ast/tuple.d.ts +0 -0
  210. /package/dist/{types/parse → parse}/ast/union.d.ts +0 -0
  211. /package/dist/{types/parse → parse}/ast/utils.d.ts +0 -0
  212. /package/dist/{types/parse → parse}/definition.d.ts +0 -0
  213. /package/dist/{types/parse → parse}/record.d.ts +0 -0
  214. /package/dist/{types/parse → parse}/string/reduce/dynamic.d.ts +0 -0
  215. /package/dist/{types/parse → parse}/string/reduce/shared.d.ts +0 -0
  216. /package/dist/{types/parse → parse}/string/reduce/static.d.ts +0 -0
  217. /package/dist/{types/parse → parse}/string/shift/operand/enclosed.d.ts +0 -0
  218. /package/dist/{types/parse → parse}/string/shift/operand/operand.d.ts +0 -0
  219. /package/dist/{types/parse → parse}/string/shift/operand/unenclosed.d.ts +0 -0
  220. /package/dist/{types/parse → parse}/string/shift/operator/bounds.d.ts +0 -0
  221. /package/dist/{types/parse → parse}/string/shift/operator/divisor.d.ts +0 -0
  222. /package/dist/{types/parse → parse}/string/shift/operator/operator.d.ts +0 -0
  223. /package/dist/{types/parse → parse}/string/shift/scanner.d.ts +0 -0
  224. /package/dist/{types/parse → parse}/string/string.d.ts +0 -0
  225. /package/dist/{types/scopes → scopes}/ark.d.ts +0 -0
  226. /package/dist/{types/scopes → scopes}/cache.d.ts +0 -0
  227. /package/dist/{types/scopes → scopes}/expressions.d.ts +0 -0
  228. /package/dist/{types/scopes → scopes}/jsObjects.d.ts +0 -0
  229. /package/dist/{types/scopes → scopes}/scope.d.ts +0 -0
  230. /package/dist/{types/scopes → scopes}/tsKeywords.d.ts +0 -0
  231. /package/dist/{types/scopes → scopes}/type.d.ts +0 -0
  232. /package/dist/{types/scopes → scopes}/validation/creditCard.d.ts +0 -0
  233. /package/dist/{types/scopes → scopes}/validation/date.d.ts +0 -0
  234. /package/dist/{types/scopes → scopes}/validation/validation.d.ts +0 -0
  235. /package/dist/{types/traverse → traverse}/problems.d.ts +0 -0
  236. /package/dist/{types/traverse → traverse}/traverse.d.ts +0 -0
  237. /package/dist/{types/utils → utils}/chainableNoOpProxy.d.ts +0 -0
  238. /package/dist/{types/utils → utils}/data.d.ts +0 -0
  239. /package/dist/{types/utils → utils}/domains.d.ts +0 -0
  240. /package/dist/{types/utils → utils}/errors.d.ts +0 -0
  241. /package/dist/{types/utils → utils}/freeze.d.ts +0 -0
  242. /package/dist/{types/utils → utils}/generics.d.ts +0 -0
  243. /package/dist/{types/utils → utils}/numericLiterals.d.ts +0 -0
  244. /package/dist/{types/utils → utils}/objectKinds.d.ts +0 -0
  245. /package/dist/{types/utils → utils}/paths.d.ts +0 -0
  246. /package/dist/{types/utils → utils}/serialize.d.ts +0 -0
  247. /package/dist/{types/utils → utils}/unionToTuple.d.ts +0 -0
@@ -1,281 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: all[name]
9
- });
10
- }
11
- _export(exports, {
12
- ArkTypeError: ()=>ArkTypeError,
13
- Problem: ()=>Problem,
14
- Problems: ()=>Problems,
15
- domainsToDescriptions: ()=>domainsToDescriptions,
16
- objectKindsToDescriptions: ()=>objectKindsToDescriptions,
17
- describeBranches: ()=>describeBranches,
18
- problemCodes: ()=>problemCodes,
19
- defaultProblemWriters: ()=>defaultProblemWriters,
20
- compileProblemWriters: ()=>compileProblemWriters
21
- });
22
- const _scannerJs = require("../parse/string/shift/scanner.js");
23
- const _dataJs = require("../utils/data.js");
24
- const _domainsJs = require("../utils/domains.js");
25
- const _genericsJs = require("../utils/generics.js");
26
- const _numericLiteralsJs = require("../utils/numericLiterals.js");
27
- const _objectKindsJs = require("../utils/objectKinds.js");
28
- const _pathsJs = require("../utils/paths.js");
29
- const _serializeJs = require("../utils/serialize.js");
30
- function _checkPrivateRedeclaration(obj, privateCollection) {
31
- if (privateCollection.has(obj)) {
32
- throw new TypeError("Cannot initialize the same private elements twice on an object");
33
- }
34
- }
35
- function _classApplyDescriptorGet(receiver, descriptor) {
36
- if (descriptor.get) {
37
- return descriptor.get.call(receiver);
38
- }
39
- return descriptor.value;
40
- }
41
- function _classApplyDescriptorSet(receiver, descriptor, value) {
42
- if (descriptor.set) {
43
- descriptor.set.call(receiver, value);
44
- } else {
45
- if (!descriptor.writable) {
46
- throw new TypeError("attempted to set read only private field");
47
- }
48
- descriptor.value = value;
49
- }
50
- }
51
- function _classExtractFieldDescriptor(receiver, privateMap, action) {
52
- if (!privateMap.has(receiver)) {
53
- throw new TypeError("attempted to " + action + " private field on non-instance");
54
- }
55
- return privateMap.get(receiver);
56
- }
57
- function _classPrivateFieldGet(receiver, privateMap) {
58
- var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get");
59
- return _classApplyDescriptorGet(receiver, descriptor);
60
- }
61
- function _classPrivateFieldInit(obj, privateMap, value) {
62
- _checkPrivateRedeclaration(obj, privateMap);
63
- privateMap.set(obj, value);
64
- }
65
- function _classPrivateFieldSet(receiver, privateMap, value) {
66
- var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set");
67
- _classApplyDescriptorSet(receiver, descriptor, value);
68
- return value;
69
- }
70
- function _defineProperty(obj, key, value) {
71
- if (key in obj) {
72
- Object.defineProperty(obj, key, {
73
- value: value,
74
- enumerable: true,
75
- configurable: true,
76
- writable: true
77
- });
78
- } else {
79
- obj[key] = value;
80
- }
81
- return obj;
82
- }
83
- class ArkTypeError extends TypeError {
84
- constructor(problems){
85
- super(`${problems}`);
86
- _defineProperty(this, "cause", void 0);
87
- this.cause = problems;
88
- }
89
- }
90
- class Problem {
91
- toString() {
92
- return this.message;
93
- }
94
- get message() {
95
- return this.writers.addContext(this.reason, this.path);
96
- }
97
- get reason() {
98
- return this.writers.writeReason(this.mustBe, new _dataJs.DataWrapper(this.data));
99
- }
100
- get mustBe() {
101
- return typeof this.writers.mustBe === "string" ? this.writers.mustBe : this.writers.mustBe(this.source);
102
- }
103
- constructor(code, path, data, source, writers){
104
- _defineProperty(this, "code", void 0);
105
- _defineProperty(this, "path", void 0);
106
- _defineProperty(this, "data", void 0);
107
- _defineProperty(this, "source", void 0);
108
- _defineProperty(this, "writers", void 0);
109
- _defineProperty(this, "parts", void 0);
110
- this.code = code;
111
- this.path = path;
112
- this.data = data;
113
- this.source = source;
114
- this.writers = writers;
115
- if (this.code === "multi") {
116
- this.parts = this.source;
117
- }
118
- }
119
- }
120
- var _state = /*#__PURE__*/ new WeakMap();
121
- class ProblemArray extends Array {
122
- mustBe(description, opts) {
123
- return this.add("custom", description, opts);
124
- }
125
- add(code, source, opts) {
126
- // copy the path to avoid future mutations affecting it
127
- const path = _pathsJs.Path.from(opts?.path ?? _classPrivateFieldGet(this, _state).path);
128
- const data = // we have to check for the presence of the key explicitly since the
129
- // data could be undefined or null
130
- opts && "data" in opts ? opts.data : _classPrivateFieldGet(this, _state).data;
131
- const problem = new Problem(// avoid a bunch of errors from TS trying to discriminate the
132
- // problem input based on the code
133
- code, path, data, source, _classPrivateFieldGet(this, _state).getProblemConfig(code));
134
- this.addProblem(problem);
135
- return problem;
136
- }
137
- addProblem(problem) {
138
- const pathKey = `${problem.path}`;
139
- const existing = this.byPath[pathKey];
140
- if (existing) {
141
- if (existing.parts) {
142
- existing.parts.push(problem);
143
- } else {
144
- const problemIntersection = new Problem("multi", existing.path, existing.data, [
145
- existing,
146
- problem
147
- ], _classPrivateFieldGet(this, _state).getProblemConfig("multi"));
148
- const existingIndex = this.indexOf(existing);
149
- // If existing is found (which it always should be unless this was externally mutated),
150
- // replace it with the new problem intersection. In case it isn't for whatever reason,
151
- // just append the intersection.
152
- this[existingIndex === -1 ? this.length : existingIndex] = problemIntersection;
153
- this.byPath[pathKey] = problemIntersection;
154
- }
155
- } else {
156
- this.byPath[pathKey] = problem;
157
- this.push(problem);
158
- }
159
- this.count++;
160
- }
161
- get summary() {
162
- return `${this}`;
163
- }
164
- toString() {
165
- return this.join("\n");
166
- }
167
- throw() {
168
- throw new ArkTypeError(this);
169
- }
170
- constructor(state){
171
- super();
172
- _defineProperty(this, "byPath", {});
173
- _defineProperty(this, "count", 0);
174
- _classPrivateFieldInit(this, _state, {
175
- writable: true,
176
- value: void 0
177
- });
178
- _classPrivateFieldSet(this, _state, state);
179
- }
180
- }
181
- const Problems = ProblemArray;
182
- const capitalize = (s)=>s[0].toUpperCase() + s.slice(1);
183
- const domainsToDescriptions = (domains)=>domains.map((objectKind)=>_domainsJs.domainDescriptions[objectKind]);
184
- const objectKindsToDescriptions = (kinds)=>kinds.map((objectKind)=>_objectKindsJs.objectKindDescriptions[objectKind]);
185
- const describeBranches = (descriptions)=>{
186
- if (descriptions.length === 0) {
187
- return "never";
188
- }
189
- if (descriptions.length === 1) {
190
- return descriptions[0];
191
- }
192
- let description = "";
193
- for(let i = 0; i < descriptions.length - 1; i++){
194
- description += descriptions[i];
195
- if (i < descriptions.length - 2) {
196
- description += ", ";
197
- }
198
- }
199
- description += ` or ${descriptions[descriptions.length - 1]}`;
200
- return description;
201
- };
202
- const writeDefaultReason = (mustBe, was)=>`must be ${mustBe}${was && ` (was ${was})`}`;
203
- const addDefaultContext = (reason, path)=>path.length === 0 ? capitalize(reason) : path.length === 1 && (0, _numericLiteralsJs.isWellFormedInteger)(path[0]) ? `Item at index ${path[0]} ${reason}` : `${path} ${reason}`;
204
- const defaultProblemConfig = {
205
- divisor: {
206
- mustBe: (divisor)=>divisor === 1 ? `an integer` : `a multiple of ${divisor}`
207
- },
208
- class: {
209
- mustBe: (expected)=>{
210
- const possibleObjectKind = (0, _objectKindsJs.getExactConstructorObjectKind)(expected);
211
- return possibleObjectKind ? _objectKindsJs.objectKindDescriptions[possibleObjectKind] : `an instance of ${expected.name}`;
212
- },
213
- writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.className)
214
- },
215
- domain: {
216
- mustBe: (domain)=>_domainsJs.domainDescriptions[domain],
217
- writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.domain)
218
- },
219
- missing: {
220
- mustBe: ()=>"defined",
221
- writeReason: (mustBe)=>writeDefaultReason(mustBe, "")
222
- },
223
- extraneous: {
224
- mustBe: ()=>"removed",
225
- writeReason: (mustBe)=>writeDefaultReason(mustBe, "")
226
- },
227
- bound: {
228
- mustBe: (bound)=>`${_scannerJs.Scanner.comparatorDescriptions[bound.comparator]} ${bound.limit}${bound.units ? ` ${bound.units}` : ""}`,
229
- writeReason: (mustBe, data)=>writeDefaultReason(mustBe, `${data.size}`)
230
- },
231
- regex: {
232
- mustBe: (expression)=>`a string matching ${expression}`
233
- },
234
- value: {
235
- mustBe: _serializeJs.stringify
236
- },
237
- branches: {
238
- mustBe: (branchProblems)=>describeBranches(branchProblems.map((problem)=>`${problem.path} must be ${problem.parts ? describeBranches(problem.parts.map((part)=>part.mustBe)) : problem.mustBe}`)),
239
- writeReason: (mustBe, data)=>`${mustBe} (was ${data})`,
240
- addContext: (reason, path)=>path.length ? `At ${path}, ${reason}` : reason
241
- },
242
- multi: {
243
- mustBe: (problems)=>"• " + problems.map((_)=>_.mustBe).join("\n• "),
244
- writeReason: (mustBe, data)=>`${data} must be...\n${mustBe}`,
245
- addContext: (reason, path)=>path.length ? `At ${path}, ${reason}` : reason
246
- },
247
- custom: {
248
- mustBe: (mustBe)=>mustBe
249
- },
250
- cases: {
251
- mustBe: (cases)=>describeBranches(cases)
252
- }
253
- };
254
- const problemCodes = (0, _genericsJs.objectKeysOf)(defaultProblemConfig);
255
- const compileDefaultProblemWriters = ()=>{
256
- const result = {};
257
- let code;
258
- for (code of problemCodes){
259
- result[code] = {
260
- mustBe: defaultProblemConfig[code].mustBe,
261
- writeReason: defaultProblemConfig[code].writeReason ?? writeDefaultReason,
262
- addContext: defaultProblemConfig[code].addContext ?? addDefaultContext
263
- };
264
- }
265
- return result;
266
- };
267
- const defaultProblemWriters = compileDefaultProblemWriters();
268
- const compileProblemWriters = (input)=>{
269
- if (!input) {
270
- return defaultProblemWriters;
271
- }
272
- const result = {};
273
- for (const code of problemCodes){
274
- result[code] = {
275
- mustBe: input[code]?.mustBe ?? defaultProblemConfig[code].mustBe,
276
- writeReason: input[code]?.writeReason ?? defaultProblemConfig[code].writeReason ?? input.writeReason ?? writeDefaultReason,
277
- addContext: input[code]?.addContext ?? defaultProblemConfig[code].addContext ?? input.addContext ?? addDefaultContext
278
- };
279
- }
280
- return result;
281
- };
@@ -1,387 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: all[name]
9
- });
10
- }
11
- _export(exports, {
12
- traverseRoot: ()=>traverseRoot,
13
- TraversalState: ()=>TraversalState,
14
- traverse: ()=>traverse,
15
- checkEntries: ()=>checkEntries,
16
- checkRequiredProp: ()=>checkRequiredProp
17
- });
18
- const _discriminateJs = require("../nodes/discriminate.js");
19
- const _classJs = require("../nodes/rules/class.js");
20
- const _divisorJs = require("../nodes/rules/divisor.js");
21
- const _rangeJs = require("../nodes/rules/range.js");
22
- const _regexJs = require("../nodes/rules/regex.js");
23
- const _rulesJs = require("../nodes/rules/rules.js");
24
- const _domainsJs = require("../utils/domains.js");
25
- const _errorsJs = require("../utils/errors.js");
26
- const _genericsJs = require("../utils/generics.js");
27
- const _pathsJs = require("../utils/paths.js");
28
- const _problemsJs = require("./problems.js");
29
- function _checkPrivateRedeclaration(obj, privateCollection) {
30
- if (privateCollection.has(obj)) {
31
- throw new TypeError("Cannot initialize the same private elements twice on an object");
32
- }
33
- }
34
- function _classApplyDescriptorGet(receiver, descriptor) {
35
- if (descriptor.get) {
36
- return descriptor.get.call(receiver);
37
- }
38
- return descriptor.value;
39
- }
40
- function _classApplyDescriptorSet(receiver, descriptor, value) {
41
- if (descriptor.set) {
42
- descriptor.set.call(receiver, value);
43
- } else {
44
- if (!descriptor.writable) {
45
- throw new TypeError("attempted to set read only private field");
46
- }
47
- descriptor.value = value;
48
- }
49
- }
50
- function _classExtractFieldDescriptor(receiver, privateMap, action) {
51
- if (!privateMap.has(receiver)) {
52
- throw new TypeError("attempted to " + action + " private field on non-instance");
53
- }
54
- return privateMap.get(receiver);
55
- }
56
- function _classPrivateFieldGet(receiver, privateMap) {
57
- var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get");
58
- return _classApplyDescriptorGet(receiver, descriptor);
59
- }
60
- function _classPrivateFieldInit(obj, privateMap, value) {
61
- _checkPrivateRedeclaration(obj, privateMap);
62
- privateMap.set(obj, value);
63
- }
64
- function _classPrivateFieldSet(receiver, privateMap, value) {
65
- var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set");
66
- _classApplyDescriptorSet(receiver, descriptor, value);
67
- return value;
68
- }
69
- function _defineProperty(obj, key, value) {
70
- if (key in obj) {
71
- Object.defineProperty(obj, key, {
72
- value: value,
73
- enumerable: true,
74
- configurable: true,
75
- writable: true
76
- });
77
- } else {
78
- obj[key] = value;
79
- }
80
- return obj;
81
- }
82
- const initializeTraversalConfig = ()=>({
83
- mustBe: [],
84
- writeReason: [],
85
- addContext: [],
86
- keys: []
87
- });
88
- const problemWriterKeys = [
89
- "mustBe",
90
- "writeReason",
91
- "addContext"
92
- ];
93
- const traverseRoot = (t, data)=>{
94
- const state = new TraversalState(data, t);
95
- traverse(t.flat, state);
96
- const result = new CheckResult(state);
97
- if (state.problems.count) {
98
- result.problems = state.problems;
99
- } else {
100
- for (const [o, k] of state.entriesToPrune){
101
- delete o[k];
102
- }
103
- result.data = state.data;
104
- }
105
- return result;
106
- };
107
- const CheckResult = class {
108
- constructor(){
109
- _defineProperty(this, "data", void 0);
110
- _defineProperty(this, "problems", void 0);
111
- }
112
- };
113
- var _seen = /*#__PURE__*/ new WeakMap();
114
- class TraversalState {
115
- getProblemConfig(code) {
116
- const result = {};
117
- for (const k of problemWriterKeys){
118
- result[k] = this.traversalConfig[k][0] ?? this.rootScope.config.codes[code][k];
119
- }
120
- return result;
121
- }
122
- traverseConfig(configEntries, node) {
123
- for (const entry of configEntries){
124
- this.traversalConfig[entry[0]].unshift(entry[1]);
125
- }
126
- const isValid = traverse(node, this);
127
- for (const entry of configEntries){
128
- this.traversalConfig[entry[0]].shift();
129
- }
130
- return isValid;
131
- }
132
- traverseKey(key, node) {
133
- const lastData = this.data;
134
- this.data = this.data[key];
135
- this.path.push(key);
136
- const isValid = traverse(node, this);
137
- this.path.pop();
138
- if (lastData[key] !== this.data) {
139
- lastData[key] = this.data;
140
- }
141
- this.data = lastData;
142
- return isValid;
143
- }
144
- traverseResolution(name) {
145
- const resolution = this.type.scope.resolve(name);
146
- const id = resolution.qualifiedName;
147
- // this assignment helps with narrowing
148
- const data = this.data;
149
- const isObject = (0, _domainsJs.hasDomain)(data, "object");
150
- if (isObject) {
151
- const seenByCurrentType = _classPrivateFieldGet(this, _seen)[id];
152
- if (seenByCurrentType) {
153
- if (seenByCurrentType.includes(data)) {
154
- // if data has already been checked by this alias as part of
155
- // a resolution higher up on the call stack, it must be valid
156
- // or we wouldn't be here
157
- return true;
158
- }
159
- seenByCurrentType.push(data);
160
- } else {
161
- _classPrivateFieldGet(this, _seen)[id] = [
162
- data
163
- ];
164
- }
165
- }
166
- const lastType = this.type;
167
- this.type = resolution;
168
- const isValid = traverse(resolution.flat, this);
169
- this.type = lastType;
170
- if (isObject) {
171
- _classPrivateFieldGet(this, _seen)[id].pop();
172
- }
173
- return isValid;
174
- }
175
- traverseBranches(branches) {
176
- const lastFailFast = this.failFast;
177
- this.failFast = true;
178
- const lastProblems = this.problems;
179
- const branchProblems = new _problemsJs.Problems(this);
180
- this.problems = branchProblems;
181
- const lastPath = this.path;
182
- const lastKeysToPrune = this.entriesToPrune;
183
- let hasValidBranch = false;
184
- for (const branch of branches){
185
- this.path = new _pathsJs.Path();
186
- this.entriesToPrune = [];
187
- if (checkEntries(branch, this)) {
188
- hasValidBranch = true;
189
- lastKeysToPrune.push(...this.entriesToPrune);
190
- break;
191
- }
192
- }
193
- this.path = lastPath;
194
- this.entriesToPrune = lastKeysToPrune;
195
- this.problems = lastProblems;
196
- this.failFast = lastFailFast;
197
- return hasValidBranch || !this.problems.add("branches", branchProblems);
198
- }
199
- constructor(data, type){
200
- _defineProperty(this, "data", void 0);
201
- _defineProperty(this, "type", void 0);
202
- _defineProperty(this, "path", void 0);
203
- _defineProperty(this, "problems", void 0);
204
- _defineProperty(this, "entriesToPrune", void 0);
205
- _defineProperty(this, "failFast", void 0);
206
- _defineProperty(this, "traversalConfig", void 0);
207
- _defineProperty(this, "rootScope", void 0);
208
- _classPrivateFieldInit(this, _seen, {
209
- writable: true,
210
- value: void 0
211
- });
212
- this.data = data;
213
- this.type = type;
214
- this.path = new _pathsJs.Path();
215
- this.problems = new _problemsJs.Problems(this);
216
- this.entriesToPrune = [];
217
- this.failFast = false;
218
- this.traversalConfig = initializeTraversalConfig();
219
- _classPrivateFieldSet(this, _seen, {});
220
- this.rootScope = type.scope;
221
- }
222
- }
223
- const traverse = (node, state)=>typeof node === "string" ? (0, _domainsJs.domainOf)(state.data) === node || !state.problems.add("domain", node) : checkEntries(node, state);
224
- const checkEntries = (entries, state)=>{
225
- let isValid = true;
226
- for(let i = 0; i < entries.length; i++){
227
- const [k, v] = entries[i];
228
- const entryAllowsData = entryCheckers[k](v, state);
229
- isValid && (isValid = entryAllowsData);
230
- if (!isValid) {
231
- if (state.failFast) {
232
- return false;
233
- }
234
- if (i < entries.length - 1 && _rulesJs.precedenceMap[k] < _rulesJs.precedenceMap[entries[i + 1][0]]) {
235
- // if we've encountered a problem, there is at least one entry
236
- // remaining, and the next entry is of a higher precedence level
237
- // than the current entry, return immediately
238
- return false;
239
- }
240
- }
241
- }
242
- return isValid;
243
- };
244
- const checkRequiredProp = (prop, state)=>{
245
- if (prop[0] in state.data) {
246
- return state.traverseKey(prop[0], prop[1]);
247
- }
248
- state.problems.add("missing", undefined, {
249
- path: state.path.concat(prop[0]),
250
- data: undefined
251
- });
252
- return false;
253
- };
254
- const createPropChecker = (kind)=>(props, state)=>{
255
- let isValid = true;
256
- const remainingUnseenRequired = {
257
- ...props.required
258
- };
259
- for(const k in state.data){
260
- if (props.required[k]) {
261
- isValid = state.traverseKey(k, props.required[k]) && isValid;
262
- delete remainingUnseenRequired[k];
263
- } else if (props.optional[k]) {
264
- isValid = state.traverseKey(k, props.optional[k]) && isValid;
265
- } else if (kind === "distilledProps") {
266
- if (state.failFast) {
267
- // If we're in a union (i.e. failFast is enabled) in
268
- // distilled mode, we need to wait to prune distilled keys
269
- // to avoid mutating data based on a branch that will not be
270
- // included in the final result. Instead, we push the object
271
- // and key to state to handle after traversal is complete.
272
- state.entriesToPrune.push([
273
- state.data,
274
- k
275
- ]);
276
- } else {
277
- // If we're not in a union, we can safely distill right away
278
- delete state.data[k];
279
- }
280
- } else {
281
- isValid = false;
282
- state.problems.add("extraneous", state.data[k], {
283
- path: state.path.concat(k)
284
- });
285
- }
286
- if (!isValid && state.failFast) {
287
- return false;
288
- }
289
- }
290
- const unseenRequired = Object.keys(remainingUnseenRequired);
291
- if (unseenRequired.length) {
292
- for (const k of unseenRequired){
293
- state.problems.add("missing", undefined, {
294
- path: state.path.concat(k)
295
- });
296
- }
297
- return false;
298
- }
299
- return isValid;
300
- };
301
- const entryCheckers = {
302
- regex: _regexJs.checkRegex,
303
- divisor: _divisorJs.checkDivisor,
304
- domains: (domains, state)=>{
305
- const entries = domains[(0, _domainsJs.domainOf)(state.data)];
306
- return entries ? checkEntries(entries, state) : !state.problems.add("cases", (0, _problemsJs.domainsToDescriptions)((0, _genericsJs.objectKeysOf)(domains)));
307
- },
308
- domain: (domain, state)=>(0, _domainsJs.domainOf)(state.data) === domain || !state.problems.add("domain", domain),
309
- bound: _rangeJs.checkBound,
310
- optionalProp: (prop, state)=>{
311
- if (prop[0] in state.data) {
312
- return state.traverseKey(prop[0], prop[1]);
313
- }
314
- return true;
315
- },
316
- // these checks work the same way, the keys are only distinct so that
317
- // prerequisite props can have a higher precedence
318
- requiredProp: checkRequiredProp,
319
- prerequisiteProp: checkRequiredProp,
320
- indexProp: (node, state)=>{
321
- if (!Array.isArray(state.data)) {
322
- state.problems.add("class", Array);
323
- return false;
324
- }
325
- let isValid = true;
326
- for(let i = 0; i < state.data.length; i++){
327
- isValid = state.traverseKey(`${i}`, node) && isValid;
328
- if (!isValid && state.failFast) {
329
- return false;
330
- }
331
- }
332
- return isValid;
333
- },
334
- branches: (branches, state)=>state.traverseBranches(branches),
335
- switch: (rule, state)=>{
336
- const dataAtPath = (0, _pathsJs.getPath)(state.data, rule.path);
337
- const caseKey = (0, _discriminateJs.serializeCase)(rule.kind, dataAtPath);
338
- if ((0, _genericsJs.hasKey)(rule.cases, caseKey)) {
339
- return checkEntries(rule.cases[caseKey], state);
340
- }
341
- const caseKeys = (0, _genericsJs.objectKeysOf)(rule.cases);
342
- const missingCasePath = state.path.concat(rule.path);
343
- const caseDescriptions = rule.kind === "value" ? caseKeys : rule.kind === "domain" ? (0, _problemsJs.domainsToDescriptions)(caseKeys) : rule.kind === "class" ? (0, _problemsJs.objectKindsToDescriptions)(caseKeys) : (0, _errorsJs.throwInternalError)(`Unexpectedly encountered rule kind '${rule.kind}' during traversal`);
344
- state.problems.add("cases", caseDescriptions, {
345
- path: missingCasePath,
346
- data: dataAtPath
347
- });
348
- return false;
349
- },
350
- alias: (name, state)=>state.traverseResolution(name),
351
- class: _classJs.checkClass,
352
- narrow: (narrow, state)=>{
353
- const lastProblemsCount = state.problems.count;
354
- const result = narrow(state.data, state.problems);
355
- if (!result && state.problems.count === lastProblemsCount) {
356
- state.problems.mustBe(narrow.name ? `valid according to ${narrow.name}` : "valid");
357
- }
358
- return result;
359
- },
360
- config: ({ config , node }, state)=>state.traverseConfig(config, node),
361
- value: (value, state)=>state.data === value || !state.problems.add("value", value),
362
- morph: (morph, state)=>{
363
- const out = morph(state.data, state.problems);
364
- if (state.problems.length) {
365
- return false;
366
- }
367
- if (out instanceof _problemsJs.Problem) {
368
- // if a problem was returned from the morph but not added, add it
369
- state.problems.addProblem(out);
370
- return false;
371
- }
372
- if (out instanceof CheckResult) {
373
- if (out.problems) {
374
- for (const problem of out.problems){
375
- state.problems.addProblem(problem);
376
- }
377
- return false;
378
- }
379
- state.data = out.data;
380
- return true;
381
- }
382
- state.data = out;
383
- return true;
384
- },
385
- distilledProps: createPropChecker("distilledProps"),
386
- strictProps: createPropChecker("strictProps")
387
- };
@@ -1,23 +0,0 @@
1
- /**
2
- * Can be used to allow arbitrarily chained property access and function calls.
3
- * This is useful for expressions whose meaning is not attached to a value,
4
- * e.g. to allow the extraction of types using typeof without depending on
5
- * the existence of a real object that conforms to that type's structure:
6
- *
7
- * @example
8
- * const myType: MyType = chainableNoOpProxy
9
- *
10
- * // The following types are equivalent
11
- * type ExtractedType = typeof myType.a.b.c
12
- * type DirectlyExtractedType = MyType["a"]["b"]["c"]
13
- */ "use strict";
14
- Object.defineProperty(exports, "__esModule", {
15
- value: true
16
- });
17
- Object.defineProperty(exports, "chainableNoOpProxy", {
18
- enumerable: true,
19
- get: ()=>chainableNoOpProxy
20
- });
21
- const chainableNoOpProxy = new Proxy(()=>chainableNoOpProxy, {
22
- get: ()=>chainableNoOpProxy
23
- });