arktype 1.0.28-alpha → 1.1.0-dev-1

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 (194) hide show
  1. package/dist/main.d.ts +1018 -0
  2. package/dist/main.js +1450 -0
  3. package/package.json +36 -108
  4. package/LICENSE +0 -7
  5. package/README.md +0 -351
  6. package/dist/cjs/main.js +0 -38
  7. package/dist/cjs/nodes/branch.js +0 -140
  8. package/dist/cjs/nodes/compose.js +0 -163
  9. package/dist/cjs/nodes/discriminate.js +0 -265
  10. package/dist/cjs/nodes/node.js +0 -139
  11. package/dist/cjs/nodes/predicate.js +0 -86
  12. package/dist/cjs/nodes/rules/class.js +0 -25
  13. package/dist/cjs/nodes/rules/collapsibleSet.js +0 -48
  14. package/dist/cjs/nodes/rules/divisor.js +0 -29
  15. package/dist/cjs/nodes/rules/props.js +0 -167
  16. package/dist/cjs/nodes/rules/range.js +0 -105
  17. package/dist/cjs/nodes/rules/regex.js +0 -26
  18. package/dist/cjs/nodes/rules/rules.js +0 -123
  19. package/dist/cjs/package.json +0 -3
  20. package/dist/cjs/parse/ast/ast.js +0 -4
  21. package/dist/cjs/parse/ast/bound.js +0 -16
  22. package/dist/cjs/parse/ast/config.js +0 -13
  23. package/dist/cjs/parse/ast/distributableFunction.js +0 -48
  24. package/dist/cjs/parse/ast/divisor.js +0 -9
  25. package/dist/cjs/parse/ast/intersection.js +0 -30
  26. package/dist/cjs/parse/ast/keyof.js +0 -111
  27. package/dist/cjs/parse/ast/morph.js +0 -58
  28. package/dist/cjs/parse/ast/narrow.js +0 -22
  29. package/dist/cjs/parse/ast/node.js +0 -4
  30. package/dist/cjs/parse/ast/tuple.js +0 -94
  31. package/dist/cjs/parse/ast/union.js +0 -9
  32. package/dist/cjs/parse/ast/utils.js +0 -4
  33. package/dist/cjs/parse/definition.js +0 -61
  34. package/dist/cjs/parse/record.js +0 -37
  35. package/dist/cjs/parse/string/reduce/dynamic.js +0 -167
  36. package/dist/cjs/parse/string/reduce/shared.js +0 -23
  37. package/dist/cjs/parse/string/reduce/static.js +0 -4
  38. package/dist/cjs/parse/string/shift/operand/enclosed.js +0 -54
  39. package/dist/cjs/parse/string/shift/operand/operand.js +0 -11
  40. package/dist/cjs/parse/string/shift/operand/unenclosed.js +0 -53
  41. package/dist/cjs/parse/string/shift/operator/bounds.js +0 -87
  42. package/dist/cjs/parse/string/shift/operator/divisor.js +0 -35
  43. package/dist/cjs/parse/string/shift/operator/operator.js +0 -26
  44. package/dist/cjs/parse/string/shift/scanner.js +0 -114
  45. package/dist/cjs/parse/string/string.js +0 -43
  46. package/dist/cjs/scopes/ark.js +0 -45
  47. package/dist/cjs/scopes/cache.js +0 -52
  48. package/dist/cjs/scopes/expressions.js +0 -29
  49. package/dist/cjs/scopes/jsObjects.js +0 -93
  50. package/dist/cjs/scopes/scope.js +0 -323
  51. package/dist/cjs/scopes/tsKeywords.js +0 -113
  52. package/dist/cjs/scopes/type.js +0 -50
  53. package/dist/cjs/scopes/validation/creditCard.js +0 -47
  54. package/dist/cjs/scopes/validation/date.js +0 -62
  55. package/dist/cjs/scopes/validation/validation.js +0 -108
  56. package/dist/cjs/traverse/problems.js +0 -281
  57. package/dist/cjs/traverse/traverse.js +0 -390
  58. package/dist/cjs/utils/chainableNoOpProxy.js +0 -23
  59. package/dist/cjs/utils/data.js +0 -53
  60. package/dist/cjs/utils/domains.js +0 -30
  61. package/dist/cjs/utils/errors.js +0 -26
  62. package/dist/cjs/utils/freeze.js +0 -15
  63. package/dist/cjs/utils/generics.js +0 -63
  64. package/dist/cjs/utils/numericLiterals.js +0 -75
  65. package/dist/cjs/utils/objectKinds.js +0 -69
  66. package/dist/cjs/utils/paths.js +0 -32
  67. package/dist/cjs/utils/serialize.js +0 -74
  68. package/dist/cjs/utils/unionToTuple.js +0 -4
  69. package/dist/mjs/main.js +0 -8
  70. package/dist/mjs/nodes/branch.js +0 -123
  71. package/dist/mjs/nodes/compose.js +0 -141
  72. package/dist/mjs/nodes/discriminate.js +0 -250
  73. package/dist/mjs/nodes/node.js +0 -117
  74. package/dist/mjs/nodes/predicate.js +0 -69
  75. package/dist/mjs/nodes/rules/class.js +0 -11
  76. package/dist/mjs/nodes/rules/collapsibleSet.js +0 -34
  77. package/dist/mjs/nodes/rules/divisor.js +0 -15
  78. package/dist/mjs/nodes/rules/props.js +0 -149
  79. package/dist/mjs/nodes/rules/range.js +0 -86
  80. package/dist/mjs/nodes/rules/regex.js +0 -11
  81. package/dist/mjs/nodes/rules/rules.js +0 -106
  82. package/dist/mjs/package.json +0 -3
  83. package/dist/mjs/parse/ast/ast.js +0 -1
  84. package/dist/mjs/parse/ast/bound.js +0 -2
  85. package/dist/mjs/parse/ast/config.js +0 -9
  86. package/dist/mjs/parse/ast/distributableFunction.js +0 -34
  87. package/dist/mjs/parse/ast/divisor.js +0 -1
  88. package/dist/mjs/parse/ast/intersection.js +0 -16
  89. package/dist/mjs/parse/ast/keyof.js +0 -103
  90. package/dist/mjs/parse/ast/morph.js +0 -44
  91. package/dist/mjs/parse/ast/narrow.js +0 -14
  92. package/dist/mjs/parse/ast/node.js +0 -1
  93. package/dist/mjs/parse/ast/tuple.js +0 -80
  94. package/dist/mjs/parse/ast/union.js +0 -1
  95. package/dist/mjs/parse/ast/utils.js +0 -1
  96. package/dist/mjs/parse/definition.js +0 -46
  97. package/dist/mjs/parse/record.js +0 -29
  98. package/dist/mjs/parse/string/reduce/dynamic.js +0 -159
  99. package/dist/mjs/parse/string/reduce/shared.js +0 -6
  100. package/dist/mjs/parse/string/reduce/static.js +0 -1
  101. package/dist/mjs/parse/string/shift/operand/enclosed.js +0 -39
  102. package/dist/mjs/parse/string/shift/operand/operand.js +0 -3
  103. package/dist/mjs/parse/string/shift/operand/unenclosed.js +0 -36
  104. package/dist/mjs/parse/string/shift/operator/bounds.js +0 -70
  105. package/dist/mjs/parse/string/shift/operator/divisor.js +0 -21
  106. package/dist/mjs/parse/string/shift/operator/operator.js +0 -13
  107. package/dist/mjs/parse/string/shift/scanner.js +0 -106
  108. package/dist/mjs/parse/string/string.js +0 -28
  109. package/dist/mjs/scopes/ark.js +0 -28
  110. package/dist/mjs/scopes/cache.js +0 -38
  111. package/dist/mjs/scopes/expressions.js +0 -89
  112. package/dist/mjs/scopes/jsObjects.js +0 -82
  113. package/dist/mjs/scopes/scope.js +0 -303
  114. package/dist/mjs/scopes/tsKeywords.js +0 -116
  115. package/dist/mjs/scopes/type.js +0 -35
  116. package/dist/mjs/scopes/validation/creditCard.js +0 -34
  117. package/dist/mjs/scopes/validation/date.js +0 -48
  118. package/dist/mjs/scopes/validation/validation.js +0 -112
  119. package/dist/mjs/traverse/problems.js +0 -260
  120. package/dist/mjs/traverse/traverse.js +0 -373
  121. package/dist/mjs/utils/chainableNoOpProxy.js +0 -15
  122. package/dist/mjs/utils/data.js +0 -38
  123. package/dist/mjs/utils/domains.js +0 -15
  124. package/dist/mjs/utils/errors.js +0 -10
  125. package/dist/mjs/utils/freeze.js +0 -7
  126. package/dist/mjs/utils/generics.js +0 -38
  127. package/dist/mjs/utils/numericLiterals.js +0 -72
  128. package/dist/mjs/utils/objectKinds.js +0 -55
  129. package/dist/mjs/utils/paths.js +0 -18
  130. package/dist/mjs/utils/serialize.js +0 -58
  131. package/dist/mjs/utils/unionToTuple.js +0 -1
  132. package/dist/tsconfig.temp.tsbuildinfo +0 -1
  133. package/dist/types/main.d.ts +0 -15
  134. package/dist/types/nodes/branch.d.ts +0 -29
  135. package/dist/types/nodes/compose.d.ts +0 -98
  136. package/dist/types/nodes/discriminate.d.ts +0 -26
  137. package/dist/types/nodes/node.d.ts +0 -69
  138. package/dist/types/nodes/predicate.d.ts +0 -18
  139. package/dist/types/nodes/rules/class.d.ts +0 -4
  140. package/dist/types/nodes/rules/collapsibleSet.d.ts +0 -4
  141. package/dist/types/nodes/rules/divisor.d.ts +0 -3
  142. package/dist/types/nodes/rules/props.d.ts +0 -43
  143. package/dist/types/nodes/rules/range.d.ts +0 -34
  144. package/dist/types/nodes/rules/regex.d.ts +0 -5
  145. package/dist/types/nodes/rules/rules.d.ts +0 -33
  146. package/dist/types/parse/ast/ast.d.ts +0 -24
  147. package/dist/types/parse/ast/bound.d.ts +0 -27
  148. package/dist/types/parse/ast/config.d.ts +0 -15
  149. package/dist/types/parse/ast/distributableFunction.d.ts +0 -18
  150. package/dist/types/parse/ast/divisor.d.ts +0 -13
  151. package/dist/types/parse/ast/intersection.d.ts +0 -31
  152. package/dist/types/parse/ast/keyof.d.ts +0 -10
  153. package/dist/types/parse/ast/morph.d.ts +0 -18
  154. package/dist/types/parse/ast/narrow.d.ts +0 -21
  155. package/dist/types/parse/ast/node.d.ts +0 -35
  156. package/dist/types/parse/ast/tuple.d.ts +0 -41
  157. package/dist/types/parse/ast/union.d.ts +0 -17
  158. package/dist/types/parse/ast/utils.d.ts +0 -7
  159. package/dist/types/parse/definition.d.ts +0 -30
  160. package/dist/types/parse/record.d.ts +0 -26
  161. package/dist/types/parse/string/reduce/dynamic.d.ts +0 -34
  162. package/dist/types/parse/string/reduce/shared.d.ts +0 -25
  163. package/dist/types/parse/string/reduce/static.d.ts +0 -130
  164. package/dist/types/parse/string/shift/operand/enclosed.d.ts +0 -28
  165. package/dist/types/parse/string/shift/operand/operand.d.ts +0 -9
  166. package/dist/types/parse/string/shift/operand/unenclosed.d.ts +0 -18
  167. package/dist/types/parse/string/shift/operator/bounds.d.ts +0 -21
  168. package/dist/types/parse/string/shift/operator/divisor.d.ts +0 -13
  169. package/dist/types/parse/string/shift/operator/operator.d.ts +0 -13
  170. package/dist/types/parse/string/shift/scanner.d.ts +0 -89
  171. package/dist/types/parse/string/string.d.ts +0 -25
  172. package/dist/types/scopes/ark.d.ts +0 -209
  173. package/dist/types/scopes/cache.d.ts +0 -14
  174. package/dist/types/scopes/expressions.d.ts +0 -136
  175. package/dist/types/scopes/jsObjects.d.ts +0 -26
  176. package/dist/types/scopes/scope.d.ts +0 -99
  177. package/dist/types/scopes/tsKeywords.d.ts +0 -50
  178. package/dist/types/scopes/type.d.ts +0 -67
  179. package/dist/types/scopes/validation/creditCard.d.ts +0 -2
  180. package/dist/types/scopes/validation/date.d.ts +0 -23
  181. package/dist/types/scopes/validation/validation.d.ts +0 -49
  182. package/dist/types/traverse/problems.d.ts +0 -88
  183. package/dist/types/traverse/traverse.d.ts +0 -55
  184. package/dist/types/utils/chainableNoOpProxy.d.ts +0 -14
  185. package/dist/types/utils/data.d.ts +0 -13
  186. package/dist/types/utils/domains.d.ts +0 -32
  187. package/dist/types/utils/errors.d.ts +0 -6
  188. package/dist/types/utils/freeze.d.ts +0 -2
  189. package/dist/types/utils/generics.d.ts +0 -125
  190. package/dist/types/utils/numericLiterals.d.ts +0 -46
  191. package/dist/types/utils/objectKinds.d.ts +0 -84
  192. package/dist/types/utils/paths.d.ts +0 -9
  193. package/dist/types/utils/serialize.d.ts +0 -37
  194. package/dist/types/utils/unionToTuple.d.ts +0 -11
@@ -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,15 +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
- */ export const chainableNoOpProxy = new Proxy(()=>chainableNoOpProxy, {
14
- get: ()=>chainableNoOpProxy
15
- });
@@ -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
- };
@@ -1,7 +0,0 @@
1
- export const deepFreeze = (value)=>Object.isFrozen(value) ? value : Array.isArray(value) ? Object.freeze(value.map(deepFreeze)) : deepFreezeDictionary(value);
2
- const deepFreezeDictionary = (value)=>{
3
- for(const k in value){
4
- deepFreeze(value[k]);
5
- }
6
- return value;
7
- };
@@ -1,38 +0,0 @@
1
- import { hasDomain } from "./domains.js";
2
- export const asConst = (t)=>t;
3
- export const isKeyOf = (k, obj)=>k in obj;
4
- export const entriesOf = (o)=>Object.entries(o);
5
- export const objectKeysOf = (o)=>Object.keys(o);
6
- /** Mimics output of TS's keyof operator at runtime */ export const prototypeKeysOf = (value)=>{
7
- const result = [];
8
- while(value !== Object.prototype && value !== null && value !== undefined){
9
- for (const k of Object.getOwnPropertyNames(value)){
10
- if (!result.includes(k)) {
11
- result.push(k);
12
- }
13
- }
14
- for (const symbol of Object.getOwnPropertySymbols(value)){
15
- if (!result.includes(symbol)) {
16
- result.push(symbol);
17
- }
18
- }
19
- value = Object.getPrototypeOf(value);
20
- }
21
- return result;
22
- };
23
- export const hasKey = (o, k)=>{
24
- const valueAtKey = o?.[k];
25
- return valueAtKey !== undefined && valueAtKey !== null;
26
- };
27
- export const hasSingleKey = (o, k)=>k in o && Object.keys(o).length === 1;
28
- export const keyCount = (o)=>Object.keys(o).length;
29
- export const hasKeys = (value)=>hasDomain(value, "object") ? Object.keys(value).length !== 0 : false;
30
- const id = Symbol("id");
31
- export const nominal = (o, name)=>Object.assign(o, {
32
- [id]: name
33
- });
34
- export const getNominalId = (data)=>hasDomain(data, "object") && id in data ? data[id] : undefined;
35
- export const hasNominalId = (data, name)=>getNominalId(data) === name;
36
- export const listFrom = (data)=>Array.isArray(data) ? data : [
37
- data
38
- ];
@@ -1,72 +0,0 @@
1
- import { throwParseError } from "./errors.js";
2
- /**
3
- * The goal of the number literal and bigint literal regular expressions is to:
4
- *
5
- * 1. Ensure definitions form a bijection with the values they represent.
6
- * 2. Attempt to mirror TypeScript's own format for stringification of numeric
7
- * values such that the regex should match a given definition if any only if
8
- * a precise literal type will be inferred (in TS4.8+).
9
- */ /**
10
- * Matches a well-formatted numeric expression according to the following rules:
11
- * 1. Must include an integer portion (i.e. '.321' must be written as '0.321')
12
- * 2. The first digit of the value must not be 0, unless the entire integer portion is 0
13
- * 3. If the value includes a decimal, its last digit may not be 0
14
- * 4. The value may not be "-0"
15
- */ export const wellFormedNumberMatcher = /^(?!^-0$)-?(?:0|[1-9]\d*)(?:\.\d*[1-9])?$/;
16
- const isWellFormedNumber = (s)=>wellFormedNumberMatcher.test(s);
17
- const numberLikeMatcher = /^-?\d*\.?\d*$/;
18
- const isNumberLike = (s)=>s.length !== 0 && numberLikeMatcher.test(s);
19
- /**
20
- * Matches a well-formatted integer according to the following rules:
21
- * 1. Must begin with an integer, the first digit of which cannot be 0 unless the entire value is 0
22
- * 2. The value may not be "-0"
23
- */ export const wellFormedIntegerMatcher = /^(?:0|(?:-?[1-9]\d*))$/;
24
- export const isWellFormedInteger = (s)=>wellFormedIntegerMatcher.test(s);
25
- export const wellFormedNonNegativeIntegerMatcher = /^(?:0|(?:[1-9]\d*))$/;
26
- const integerLikeMatcher = /^-?\d+$/;
27
- const isIntegerLike = (s)=>integerLikeMatcher.test(s);
28
- const numericLiteralDescriptions = {
29
- number: "a number",
30
- bigint: "a bigint",
31
- integer: "an integer"
32
- };
33
- export const writeMalformedNumericLiteralMessage = (def, kind)=>`'${def}' was parsed as ${numericLiteralDescriptions[kind]} but could not be narrowed to a literal value. Avoid unnecessary leading or trailing zeros and other abnormal notation`;
34
- const isWellFormed = (def, kind)=>kind === "number" ? isWellFormedNumber(def) : isWellFormedInteger(def);
35
- const parseKind = (def, kind)=>kind === "number" ? Number(def) : Number.parseInt(def);
36
- const isKindLike = (def, kind)=>kind === "number" ? isNumberLike(def) : isIntegerLike(def);
37
- export const tryParseWellFormedNumber = (token, errorOnFail)=>parseWellFormed(token, "number", errorOnFail);
38
- export const tryParseWellFormedInteger = (token, errorOnFail)=>parseWellFormed(token, "integer", errorOnFail);
39
- const parseWellFormed = (token, kind, errorOnFail)=>{
40
- const value = parseKind(token, kind);
41
- if (!Number.isNaN(value)) {
42
- if (isWellFormed(token, kind)) {
43
- return value;
44
- }
45
- if (isKindLike(token, kind)) {
46
- // If the definition looks like the correct numeric kind but is
47
- // not well-formed, always throw.
48
- return throwParseError(writeMalformedNumericLiteralMessage(token, kind));
49
- }
50
- }
51
- return errorOnFail ? throwParseError(errorOnFail === true ? `Failed to parse ${numericLiteralDescriptions[kind]} from '${token}'` : errorOnFail) : undefined;
52
- };
53
- export const tryParseWellFormedBigint = (def)=>{
54
- if (def[def.length - 1] !== "n") {
55
- return;
56
- }
57
- const maybeIntegerLiteral = def.slice(0, -1);
58
- let value;
59
- try {
60
- value = BigInt(maybeIntegerLiteral);
61
- } catch {
62
- return;
63
- }
64
- if (wellFormedIntegerMatcher.test(maybeIntegerLiteral)) {
65
- return value;
66
- }
67
- if (integerLikeMatcher.test(maybeIntegerLiteral)) {
68
- // If the definition looks like a bigint but is
69
- // not well-formed, throw.
70
- return throwParseError(writeMalformedNumericLiteralMessage(def, "bigint"));
71
- }
72
- };
@@ -1,55 +0,0 @@
1
- import { domainOf } from "./domains.js";
2
- import { isKeyOf } from "./generics.js";
3
- // Built-in object constructors based on a subset of:
4
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
5
- export const defaultObjectKinds = {
6
- Array,
7
- Date,
8
- Error,
9
- Function,
10
- Map,
11
- RegExp,
12
- Set,
13
- Object,
14
- String,
15
- Number,
16
- Boolean,
17
- WeakMap,
18
- WeakSet,
19
- Promise
20
- };
21
- export const objectKindOf = (data, kinds)=>{
22
- if (domainOf(data) !== "object") {
23
- return undefined;
24
- }
25
- const kindSet = kinds ?? defaultObjectKinds;
26
- let prototype = Object.getPrototypeOf(data);
27
- while(prototype?.constructor && (!kindSet[prototype.constructor.name] || !(data instanceof kindSet[prototype.constructor.name]))){
28
- prototype = Object.getPrototypeOf(prototype);
29
- }
30
- return prototype?.constructor?.name;
31
- };
32
- export const hasObjectKind = (data, kind, kinds)=>objectKindOf(data, kinds) === kind;
33
- export const isArray = (data)=>Array.isArray(data);
34
- /** Each defaultObjectKind's completion for the phrase "Must be _____" */ export const objectKindDescriptions = {
35
- Object: "an object",
36
- Array: "an array",
37
- Function: "a function",
38
- Date: "a Date",
39
- RegExp: "a RegExp",
40
- Error: "an Error",
41
- Map: "a Map",
42
- Set: "a Set",
43
- String: "a String object",
44
- Number: "a Number object",
45
- Boolean: "a Boolean object",
46
- Promise: "a Promise",
47
- WeakMap: "a WeakMap",
48
- WeakSet: "a WeakSet"
49
- };
50
- // this will only return an object kind if it's the root constructor
51
- // example TypeError would return undefined not 'Error'
52
- export const getExactConstructorObjectKind = (constructor)=>{
53
- const constructorName = Object(constructor).name;
54
- return constructorName && isKeyOf(constructorName, defaultObjectKinds) && defaultObjectKinds[constructorName] === constructor ? constructorName : undefined;
55
- };
@@ -1,18 +0,0 @@
1
- export class Path extends Array {
2
- static fromString(s, delimiter = "/") {
3
- return s === delimiter ? new Path() : new Path(...s.split(delimiter));
4
- }
5
- toString(delimiter = "/") {
6
- return this.length ? this.join(delimiter) : delimiter;
7
- }
8
- }
9
- export const getPath = (root, path)=>{
10
- let result = root;
11
- for (const segment of path){
12
- if (typeof result !== "object" || result === null) {
13
- return undefined;
14
- }
15
- result = result[segment];
16
- }
17
- return result;
18
- };