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