arktype 1.0.9-alpha → 1.0.11-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.
- package/README.md +53 -39
- package/dist/cjs/main.js +26 -18
- package/dist/cjs/nodes/branch.js +18 -18
- package/dist/cjs/nodes/compose.js +28 -11
- package/dist/cjs/nodes/discriminate.js +27 -27
- package/dist/cjs/nodes/node.js +24 -24
- package/dist/cjs/nodes/predicate.js +17 -17
- package/dist/cjs/nodes/rules/class.js +5 -5
- package/dist/cjs/nodes/rules/collapsibleSet.js +3 -3
- package/dist/cjs/nodes/rules/divisor.js +2 -2
- package/dist/cjs/nodes/rules/props.js +19 -19
- package/dist/cjs/nodes/rules/range.js +6 -6
- package/dist/cjs/nodes/rules/regex.js +3 -3
- package/dist/cjs/nodes/rules/rules.js +20 -20
- package/dist/cjs/parse/ast/config.js +2 -2
- package/dist/cjs/parse/ast/distributableFunction.js +9 -9
- package/dist/cjs/parse/ast/intersection.js +5 -5
- package/dist/cjs/parse/ast/keyof.js +31 -30
- package/dist/cjs/parse/ast/morph.js +10 -10
- package/dist/cjs/parse/ast/narrow.js +6 -6
- package/dist/cjs/parse/ast/tuple.js +21 -21
- package/dist/cjs/parse/definition.js +18 -20
- package/dist/cjs/parse/record.js +4 -4
- package/dist/cjs/parse/string/reduce/dynamic.js +44 -26
- package/dist/cjs/parse/string/reduce/shared.js +2 -2
- package/dist/cjs/parse/string/shift/operand/enclosed.js +2 -2
- package/dist/cjs/parse/string/shift/operand/operand.js +3 -3
- package/dist/cjs/parse/string/shift/operand/unenclosed.js +3 -3
- package/dist/cjs/parse/string/shift/operator/bounds.js +18 -18
- package/dist/cjs/parse/string/shift/operator/divisor.js +6 -6
- package/dist/cjs/parse/string/shift/operator/operator.js +6 -6
- package/dist/cjs/parse/string/shift/scanner.js +16 -1
- package/dist/cjs/parse/string/string.js +8 -8
- package/dist/cjs/scopes/ark.js +15 -15
- package/dist/cjs/scopes/cache.js +16 -3
- package/dist/cjs/scopes/expressions.js +9 -9
- package/dist/cjs/scopes/jsObjects.js +2 -42
- package/dist/cjs/scopes/scope.js +59 -31
- package/dist/cjs/scopes/tsKeywords.js +2 -2
- package/dist/cjs/scopes/type.js +6 -5
- package/dist/cjs/scopes/validation/creditCard.js +2 -2
- package/dist/cjs/scopes/validation/date.js +4 -4
- package/dist/cjs/scopes/validation/validation.js +17 -17
- package/dist/cjs/traverse/problems.js +41 -21
- package/dist/cjs/traverse/traverse.js +56 -31
- package/dist/cjs/utils/data.js +18 -4
- package/dist/cjs/utils/generics.js +3 -3
- package/dist/cjs/utils/numericLiterals.js +4 -4
- package/dist/cjs/utils/objectKinds.js +4 -4
- package/dist/cjs/utils/serialize.js +6 -6
- package/dist/mjs/main.js +5 -1
- package/dist/mjs/nodes/compose.js +17 -0
- package/dist/mjs/parse/ast/config.js +5 -1
- package/dist/mjs/parse/ast/keyof.js +2 -1
- package/dist/mjs/parse/definition.js +0 -1
- package/dist/mjs/parse/string/reduce/dynamic.js +18 -0
- package/dist/mjs/parse/string/shift/scanner.js +16 -1
- package/dist/mjs/scopes/cache.js +14 -1
- package/dist/mjs/scopes/expressions.js +88 -8
- package/dist/mjs/scopes/jsObjects.js +4 -41
- package/dist/mjs/scopes/scope.js +30 -2
- package/dist/mjs/scopes/tsKeywords.js +18 -1
- package/dist/mjs/scopes/type.js +1 -0
- package/dist/mjs/scopes/validation/validation.js +19 -1
- package/dist/mjs/traverse/problems.js +22 -2
- package/dist/mjs/traverse/traverse.js +25 -0
- package/dist/mjs/utils/data.js +14 -0
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/main.d.ts +8 -1
- package/dist/types/nodes/node.d.ts +6 -0
- package/dist/types/parse/ast/bound.d.ts +14 -0
- package/dist/types/parse/ast/config.d.ts +5 -0
- package/dist/types/parse/ast/divisor.d.ts +5 -0
- package/dist/types/parse/definition.d.ts +2 -4
- package/dist/types/parse/string/reduce/static.d.ts +12 -2
- package/dist/types/parse/string/shift/operand/operand.d.ts +3 -3
- package/dist/types/parse/string/shift/operand/unenclosed.d.ts +5 -5
- package/dist/types/parse/string/string.d.ts +1 -2
- package/dist/types/scopes/ark.d.ts +10 -22
- package/dist/types/scopes/expressions.d.ts +88 -0
- package/dist/types/scopes/jsObjects.d.ts +4 -10
- package/dist/types/scopes/tsKeywords.d.ts +18 -0
- package/dist/types/scopes/type.d.ts +4 -2
- package/dist/types/scopes/validation/validation.d.ts +19 -0
- package/dist/types/utils/generics.d.ts +2 -2
- package/package.json +17 -18
|
@@ -19,14 +19,14 @@ _export(exports, {
|
|
|
19
19
|
defaultProblemWriters: ()=>defaultProblemWriters,
|
|
20
20
|
compileProblemWriters: ()=>compileProblemWriters
|
|
21
21
|
});
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
const
|
|
29
|
-
const
|
|
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
30
|
function _checkPrivateRedeclaration(obj, privateCollection) {
|
|
31
31
|
if (privateCollection.has(obj)) {
|
|
32
32
|
throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
@@ -67,9 +67,23 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
|
67
67
|
_classApplyDescriptorSet(receiver, descriptor, value);
|
|
68
68
|
return value;
|
|
69
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
|
+
}
|
|
70
83
|
class ArkTypeError extends TypeError {
|
|
71
84
|
constructor(problems){
|
|
72
85
|
super(`${problems}`);
|
|
86
|
+
_defineProperty(this, "cause", void 0);
|
|
73
87
|
this.cause = problems;
|
|
74
88
|
}
|
|
75
89
|
}
|
|
@@ -81,12 +95,18 @@ class Problem {
|
|
|
81
95
|
return this.writers.addContext(this.reason, this.path);
|
|
82
96
|
}
|
|
83
97
|
get reason() {
|
|
84
|
-
return this.writers.writeReason(this.mustBe, new
|
|
98
|
+
return this.writers.writeReason(this.mustBe, new _dataJs.DataWrapper(this.data));
|
|
85
99
|
}
|
|
86
100
|
get mustBe() {
|
|
87
101
|
return typeof this.writers.mustBe === "string" ? this.writers.mustBe : this.writers.mustBe(this.source);
|
|
88
102
|
}
|
|
89
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);
|
|
90
110
|
this.code = code;
|
|
91
111
|
this.path = path;
|
|
92
112
|
this.data = data;
|
|
@@ -104,7 +124,7 @@ class ProblemArray extends Array {
|
|
|
104
124
|
}
|
|
105
125
|
add(code, source, opts) {
|
|
106
126
|
// copy the path to avoid future mutations affecting it
|
|
107
|
-
const path = opts?.path ??
|
|
127
|
+
const path = opts?.path ?? _pathsJs.Path.from(_classPrivateFieldGet(this, _state).path);
|
|
108
128
|
const data = // we have to check for the presence of the key explicitly since the
|
|
109
129
|
// data could be undefined or null
|
|
110
130
|
opts && "data" in opts ? opts.data : _classPrivateFieldGet(this, _state).data;
|
|
@@ -149,8 +169,8 @@ class ProblemArray extends Array {
|
|
|
149
169
|
}
|
|
150
170
|
constructor(state){
|
|
151
171
|
super();
|
|
152
|
-
this
|
|
153
|
-
this
|
|
172
|
+
_defineProperty(this, "byPath", {});
|
|
173
|
+
_defineProperty(this, "count", 0);
|
|
154
174
|
_classPrivateFieldInit(this, _state, {
|
|
155
175
|
writable: true,
|
|
156
176
|
value: void 0
|
|
@@ -160,8 +180,8 @@ class ProblemArray extends Array {
|
|
|
160
180
|
}
|
|
161
181
|
const Problems = ProblemArray;
|
|
162
182
|
const capitalize = (s)=>s[0].toUpperCase() + s.slice(1);
|
|
163
|
-
const domainsToDescriptions = (domains)=>domains.map((objectKind)=>
|
|
164
|
-
const objectKindsToDescriptions = (kinds)=>kinds.map((objectKind)=>
|
|
183
|
+
const domainsToDescriptions = (domains)=>domains.map((objectKind)=>_domainsJs.domainDescriptions[objectKind]);
|
|
184
|
+
const objectKindsToDescriptions = (kinds)=>kinds.map((objectKind)=>_objectKindsJs.objectKindDescriptions[objectKind]);
|
|
165
185
|
const describeBranches = (descriptions)=>{
|
|
166
186
|
if (descriptions.length === 0) {
|
|
167
187
|
return "never";
|
|
@@ -180,20 +200,20 @@ const describeBranches = (descriptions)=>{
|
|
|
180
200
|
return description;
|
|
181
201
|
};
|
|
182
202
|
const writeDefaultReason = (mustBe, was)=>`must be ${mustBe}${was && ` (was ${was})`}`;
|
|
183
|
-
const addDefaultContext = (reason, path)=>path.length === 0 ? capitalize(reason) : path.length === 1 && (0,
|
|
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}`;
|
|
184
204
|
const defaultProblemConfig = {
|
|
185
205
|
divisor: {
|
|
186
206
|
mustBe: (divisor)=>divisor === 1 ? `an integer` : `a multiple of ${divisor}`
|
|
187
207
|
},
|
|
188
208
|
class: {
|
|
189
209
|
mustBe: (expected)=>{
|
|
190
|
-
const possibleObjectKind = (0,
|
|
191
|
-
return possibleObjectKind ?
|
|
210
|
+
const possibleObjectKind = (0, _objectKindsJs.getExactConstructorObjectKind)(expected);
|
|
211
|
+
return possibleObjectKind ? _objectKindsJs.objectKindDescriptions[possibleObjectKind] : `an instance of ${expected.name}`;
|
|
192
212
|
},
|
|
193
213
|
writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.className)
|
|
194
214
|
},
|
|
195
215
|
domain: {
|
|
196
|
-
mustBe: (domain)=>
|
|
216
|
+
mustBe: (domain)=>_domainsJs.domainDescriptions[domain],
|
|
197
217
|
writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.domain)
|
|
198
218
|
},
|
|
199
219
|
missing: {
|
|
@@ -205,14 +225,14 @@ const defaultProblemConfig = {
|
|
|
205
225
|
writeReason: (mustBe)=>writeDefaultReason(mustBe, "")
|
|
206
226
|
},
|
|
207
227
|
bound: {
|
|
208
|
-
mustBe: (bound)=>`${
|
|
228
|
+
mustBe: (bound)=>`${_scannerJs.Scanner.comparatorDescriptions[bound.comparator]} ${bound.limit}${bound.units ? ` ${bound.units}` : ""}`,
|
|
209
229
|
writeReason: (mustBe, data)=>writeDefaultReason(mustBe, `${data.size}`)
|
|
210
230
|
},
|
|
211
231
|
regex: {
|
|
212
232
|
mustBe: (expression)=>`a string matching ${expression}`
|
|
213
233
|
},
|
|
214
234
|
value: {
|
|
215
|
-
mustBe:
|
|
235
|
+
mustBe: _serializeJs.stringify
|
|
216
236
|
},
|
|
217
237
|
branches: {
|
|
218
238
|
mustBe: (branchProblems)=>describeBranches(branchProblems.map((problem)=>`${problem.path} must be ${problem.parts ? describeBranches(problem.parts.map((part)=>part.mustBe)) : problem.mustBe}`)),
|
|
@@ -231,7 +251,7 @@ const defaultProblemConfig = {
|
|
|
231
251
|
mustBe: (cases)=>describeBranches(cases)
|
|
232
252
|
}
|
|
233
253
|
};
|
|
234
|
-
const problemCodes = (0,
|
|
254
|
+
const problemCodes = (0, _genericsJs.objectKeysOf)(defaultProblemConfig);
|
|
235
255
|
const compileDefaultProblemWriters = ()=>{
|
|
236
256
|
const result = {};
|
|
237
257
|
let code;
|
|
@@ -15,17 +15,17 @@ _export(exports, {
|
|
|
15
15
|
checkEntries: ()=>checkEntries,
|
|
16
16
|
checkRequiredProp: ()=>checkRequiredProp
|
|
17
17
|
});
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
const
|
|
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
29
|
function _checkPrivateRedeclaration(obj, privateCollection) {
|
|
30
30
|
if (privateCollection.has(obj)) {
|
|
31
31
|
throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
@@ -66,6 +66,19 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
|
66
66
|
_classApplyDescriptorSet(receiver, descriptor, value);
|
|
67
67
|
return value;
|
|
68
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
|
+
}
|
|
69
82
|
const initializeTraversalConfig = ()=>({
|
|
70
83
|
mustBe: [],
|
|
71
84
|
writeReason: [],
|
|
@@ -92,6 +105,10 @@ const traverseRoot = (t, data)=>{
|
|
|
92
105
|
return result;
|
|
93
106
|
};
|
|
94
107
|
const CheckResult = class {
|
|
108
|
+
constructor(){
|
|
109
|
+
_defineProperty(this, "data", void 0);
|
|
110
|
+
_defineProperty(this, "problems", void 0);
|
|
111
|
+
}
|
|
95
112
|
};
|
|
96
113
|
var _seen = /*#__PURE__*/ new WeakMap();
|
|
97
114
|
class TraversalState {
|
|
@@ -129,7 +146,7 @@ class TraversalState {
|
|
|
129
146
|
const id = resolution.qualifiedName;
|
|
130
147
|
// this assignment helps with narrowing
|
|
131
148
|
const data = this.data;
|
|
132
|
-
const isObject = (0,
|
|
149
|
+
const isObject = (0, _domainsJs.hasDomain)(data, "object");
|
|
133
150
|
if (isObject) {
|
|
134
151
|
const seenByCurrentType = _classPrivateFieldGet(this, _seen)[id];
|
|
135
152
|
if (seenByCurrentType) {
|
|
@@ -159,13 +176,13 @@ class TraversalState {
|
|
|
159
176
|
const lastFailFast = this.failFast;
|
|
160
177
|
this.failFast = true;
|
|
161
178
|
const lastProblems = this.problems;
|
|
162
|
-
const branchProblems = new
|
|
179
|
+
const branchProblems = new _problemsJs.Problems(this);
|
|
163
180
|
this.problems = branchProblems;
|
|
164
181
|
const lastPath = this.path;
|
|
165
182
|
const lastKeysToPrune = this.entriesToPrune;
|
|
166
183
|
let hasValidBranch = false;
|
|
167
184
|
for (const branch of branches){
|
|
168
|
-
this.path = new
|
|
185
|
+
this.path = new _pathsJs.Path();
|
|
169
186
|
this.entriesToPrune = [];
|
|
170
187
|
if (checkEntries(branch, this)) {
|
|
171
188
|
hasValidBranch = true;
|
|
@@ -180,14 +197,22 @@ class TraversalState {
|
|
|
180
197
|
return hasValidBranch || !this.problems.add("branches", branchProblems);
|
|
181
198
|
}
|
|
182
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);
|
|
183
208
|
_classPrivateFieldInit(this, _seen, {
|
|
184
209
|
writable: true,
|
|
185
210
|
value: void 0
|
|
186
211
|
});
|
|
187
212
|
this.data = data;
|
|
188
213
|
this.type = type;
|
|
189
|
-
this.path = new
|
|
190
|
-
this.problems = new
|
|
214
|
+
this.path = new _pathsJs.Path();
|
|
215
|
+
this.problems = new _problemsJs.Problems(this);
|
|
191
216
|
this.entriesToPrune = [];
|
|
192
217
|
this.failFast = false;
|
|
193
218
|
this.traversalConfig = initializeTraversalConfig();
|
|
@@ -195,7 +220,7 @@ class TraversalState {
|
|
|
195
220
|
this.rootScope = type.scope;
|
|
196
221
|
}
|
|
197
222
|
}
|
|
198
|
-
const traverse = (node, state)=>typeof node === "string" ? (0,
|
|
223
|
+
const traverse = (node, state)=>typeof node === "string" ? (0, _domainsJs.domainOf)(state.data) === node || !state.problems.add("domain", node) : checkEntries(node, state);
|
|
199
224
|
const checkEntries = (entries, state)=>{
|
|
200
225
|
let isValid = true;
|
|
201
226
|
for(let i = 0; i < entries.length; i++){
|
|
@@ -206,7 +231,7 @@ const checkEntries = (entries, state)=>{
|
|
|
206
231
|
if (state.failFast) {
|
|
207
232
|
return false;
|
|
208
233
|
}
|
|
209
|
-
if (i < entries.length - 1 &&
|
|
234
|
+
if (i < entries.length - 1 && _rulesJs.precedenceMap[k] < _rulesJs.precedenceMap[entries[i + 1][0]]) {
|
|
210
235
|
// if we've encountered a problem, there is at least one entry
|
|
211
236
|
// remaining, and the next entry is of a higher precedence level
|
|
212
237
|
// than the current entry, return immediately
|
|
@@ -274,14 +299,14 @@ const createPropChecker = (kind)=>(props, state)=>{
|
|
|
274
299
|
return isValid;
|
|
275
300
|
};
|
|
276
301
|
const entryCheckers = {
|
|
277
|
-
regex:
|
|
278
|
-
divisor:
|
|
302
|
+
regex: _regexJs.checkRegex,
|
|
303
|
+
divisor: _divisorJs.checkDivisor,
|
|
279
304
|
domains: (domains, state)=>{
|
|
280
|
-
const entries = domains[(0,
|
|
281
|
-
return entries ? checkEntries(entries, state) : !state.problems.add("cases", (0,
|
|
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)));
|
|
282
307
|
},
|
|
283
|
-
domain: (domain, state)=>(0,
|
|
284
|
-
bound:
|
|
308
|
+
domain: (domain, state)=>(0, _domainsJs.domainOf)(state.data) === domain || !state.problems.add("domain", domain),
|
|
309
|
+
bound: _rangeJs.checkBound,
|
|
285
310
|
optionalProp: (prop, state)=>{
|
|
286
311
|
if (prop[0] in state.data) {
|
|
287
312
|
return state.traverseKey(prop[0], prop[1]);
|
|
@@ -308,14 +333,14 @@ const entryCheckers = {
|
|
|
308
333
|
},
|
|
309
334
|
branches: (branches, state)=>state.traverseBranches(branches),
|
|
310
335
|
switch: (rule, state)=>{
|
|
311
|
-
const dataAtPath = (0,
|
|
312
|
-
const caseKey = (0,
|
|
313
|
-
if ((0,
|
|
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)) {
|
|
314
339
|
return checkEntries(rule.cases[caseKey], state);
|
|
315
340
|
}
|
|
316
|
-
const caseKeys = (0,
|
|
341
|
+
const caseKeys = (0, _genericsJs.objectKeysOf)(rule.cases);
|
|
317
342
|
const missingCasePath = state.path.concat(rule.path);
|
|
318
|
-
const caseDescriptions = rule.kind === "value" ? caseKeys : rule.kind === "domain" ? (0,
|
|
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`);
|
|
319
344
|
state.problems.add("cases", caseDescriptions, {
|
|
320
345
|
path: missingCasePath,
|
|
321
346
|
data: dataAtPath
|
|
@@ -323,7 +348,7 @@ const entryCheckers = {
|
|
|
323
348
|
return false;
|
|
324
349
|
},
|
|
325
350
|
alias: (name, state)=>state.traverseResolution(name),
|
|
326
|
-
class:
|
|
351
|
+
class: _classJs.checkClass,
|
|
327
352
|
narrow: (narrow, state)=>{
|
|
328
353
|
const lastProblemsCount = state.problems.count;
|
|
329
354
|
const result = narrow(state.data, state.problems);
|
|
@@ -339,7 +364,7 @@ const entryCheckers = {
|
|
|
339
364
|
if (state.problems.length) {
|
|
340
365
|
return false;
|
|
341
366
|
}
|
|
342
|
-
if (out instanceof
|
|
367
|
+
if (out instanceof _problemsJs.Problem) {
|
|
343
368
|
// if a problem was returned from the morph but not added, add it
|
|
344
369
|
state.problems.addProblem(out);
|
|
345
370
|
return false;
|
package/dist/cjs/utils/data.js
CHANGED
|
@@ -13,16 +13,29 @@ _export(exports, {
|
|
|
13
13
|
unitsOf: ()=>unitsOf,
|
|
14
14
|
DataWrapper: ()=>DataWrapper
|
|
15
15
|
});
|
|
16
|
-
const
|
|
17
|
-
const
|
|
16
|
+
const _domainsJs = require("./domains.js");
|
|
17
|
+
const _serializeJs = require("./serialize.js");
|
|
18
|
+
function _defineProperty(obj, key, value) {
|
|
19
|
+
if (key in obj) {
|
|
20
|
+
Object.defineProperty(obj, key, {
|
|
21
|
+
value: value,
|
|
22
|
+
enumerable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
writable: true
|
|
25
|
+
});
|
|
26
|
+
} else {
|
|
27
|
+
obj[key] = value;
|
|
28
|
+
}
|
|
29
|
+
return obj;
|
|
30
|
+
}
|
|
18
31
|
const sizeOf = (data)=>typeof data === "string" || Array.isArray(data) ? data.length : typeof data === "number" ? data : 0;
|
|
19
32
|
const unitsOf = (data)=>typeof data === "string" ? "characters" : Array.isArray(data) ? "items long" : "";
|
|
20
33
|
class DataWrapper {
|
|
21
34
|
toString() {
|
|
22
|
-
return (0,
|
|
35
|
+
return (0, _serializeJs.stringify)(this.value);
|
|
23
36
|
}
|
|
24
37
|
get domain() {
|
|
25
|
-
return (0,
|
|
38
|
+
return (0, _domainsJs.domainOf)(this.value);
|
|
26
39
|
}
|
|
27
40
|
get size() {
|
|
28
41
|
return sizeOf(this.value);
|
|
@@ -34,6 +47,7 @@ class DataWrapper {
|
|
|
34
47
|
return Object(this.value).constructor.name;
|
|
35
48
|
}
|
|
36
49
|
constructor(value){
|
|
50
|
+
_defineProperty(this, "value", void 0);
|
|
37
51
|
this.value = value;
|
|
38
52
|
}
|
|
39
53
|
}
|
|
@@ -23,7 +23,7 @@ _export(exports, {
|
|
|
23
23
|
hasNominalId: ()=>hasNominalId,
|
|
24
24
|
listFrom: ()=>listFrom
|
|
25
25
|
});
|
|
26
|
-
const
|
|
26
|
+
const _domainsJs = require("./domains.js");
|
|
27
27
|
const asConst = (t)=>t;
|
|
28
28
|
const isKeyOf = (k, obj)=>k in obj;
|
|
29
29
|
const entriesOf = (o)=>Object.entries(o);
|
|
@@ -51,12 +51,12 @@ const hasKey = (o, k)=>{
|
|
|
51
51
|
};
|
|
52
52
|
const hasSingleKey = (o, k)=>k in o && Object.keys(o).length === 1;
|
|
53
53
|
const keyCount = (o)=>Object.keys(o).length;
|
|
54
|
-
const hasKeys = (value)=>(0,
|
|
54
|
+
const hasKeys = (value)=>(0, _domainsJs.hasDomain)(value, "object") ? Object.keys(value).length !== 0 : false;
|
|
55
55
|
const id = Symbol("id");
|
|
56
56
|
const nominal = (o, name)=>Object.assign(o, {
|
|
57
57
|
[id]: name
|
|
58
58
|
});
|
|
59
|
-
const getNominalId = (data)=>(0,
|
|
59
|
+
const getNominalId = (data)=>(0, _domainsJs.hasDomain)(data, "object") && id in data ? data[id] : undefined;
|
|
60
60
|
const hasNominalId = (data, name)=>getNominalId(data) === name;
|
|
61
61
|
const listFrom = (data)=>Array.isArray(data) ? data : [
|
|
62
62
|
data
|
|
@@ -18,7 +18,7 @@ _export(exports, {
|
|
|
18
18
|
tryParseWellFormedInteger: ()=>tryParseWellFormedInteger,
|
|
19
19
|
tryParseWellFormedBigint: ()=>tryParseWellFormedBigint
|
|
20
20
|
});
|
|
21
|
-
const
|
|
21
|
+
const _errorsJs = require("./errors.js");
|
|
22
22
|
const wellFormedNumberMatcher = /^(?!^-0$)-?(?:0|[1-9]\d*)(?:\.\d*[1-9])?$/;
|
|
23
23
|
const isWellFormedNumber = (s)=>wellFormedNumberMatcher.test(s);
|
|
24
24
|
const numberLikeMatcher = /^-?\d*\.?\d*$/;
|
|
@@ -48,10 +48,10 @@ const parseWellFormed = (token, kind, errorOnFail)=>{
|
|
|
48
48
|
if (isKindLike(token, kind)) {
|
|
49
49
|
// If the definition looks like the correct numeric kind but is
|
|
50
50
|
// not well-formed, always throw.
|
|
51
|
-
return (0,
|
|
51
|
+
return (0, _errorsJs.throwParseError)(writeMalformedNumericLiteralMessage(token, kind));
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
|
-
return errorOnFail ? (0,
|
|
54
|
+
return errorOnFail ? (0, _errorsJs.throwParseError)(errorOnFail === true ? `Failed to parse ${numericLiteralDescriptions[kind]} from '${token}'` : errorOnFail) : undefined;
|
|
55
55
|
};
|
|
56
56
|
const tryParseWellFormedBigint = (def)=>{
|
|
57
57
|
if (def[def.length - 1] !== "n") {
|
|
@@ -70,6 +70,6 @@ const tryParseWellFormedBigint = (def)=>{
|
|
|
70
70
|
if (integerLikeMatcher.test(maybeIntegerLiteral)) {
|
|
71
71
|
// If the definition looks like a bigint but is
|
|
72
72
|
// not well-formed, throw.
|
|
73
|
-
return (0,
|
|
73
|
+
return (0, _errorsJs.throwParseError)(writeMalformedNumericLiteralMessage(def, "bigint"));
|
|
74
74
|
}
|
|
75
75
|
};
|
|
@@ -16,8 +16,8 @@ _export(exports, {
|
|
|
16
16
|
objectKindDescriptions: ()=>objectKindDescriptions,
|
|
17
17
|
getExactConstructorObjectKind: ()=>getExactConstructorObjectKind
|
|
18
18
|
});
|
|
19
|
-
const
|
|
20
|
-
const
|
|
19
|
+
const _domainsJs = require("./domains.js");
|
|
20
|
+
const _genericsJs = require("./generics.js");
|
|
21
21
|
const defaultObjectKinds = {
|
|
22
22
|
Array,
|
|
23
23
|
Date,
|
|
@@ -35,7 +35,7 @@ const defaultObjectKinds = {
|
|
|
35
35
|
Promise
|
|
36
36
|
};
|
|
37
37
|
const objectKindOf = (data, kinds)=>{
|
|
38
|
-
if ((0,
|
|
38
|
+
if ((0, _domainsJs.domainOf)(data) !== "object") {
|
|
39
39
|
return undefined;
|
|
40
40
|
}
|
|
41
41
|
const kindSet = kinds ?? defaultObjectKinds;
|
|
@@ -65,5 +65,5 @@ const objectKindDescriptions = {
|
|
|
65
65
|
};
|
|
66
66
|
const getExactConstructorObjectKind = (constructor)=>{
|
|
67
67
|
const constructorName = Object(constructor).name;
|
|
68
|
-
return constructorName && (0,
|
|
68
|
+
return constructorName && (0, _genericsJs.isKeyOf)(constructorName, defaultObjectKinds) && defaultObjectKinds[constructorName] === constructor ? constructorName : undefined;
|
|
69
69
|
};
|
|
@@ -14,12 +14,12 @@ _export(exports, {
|
|
|
14
14
|
serializePrimitive: ()=>serializePrimitive,
|
|
15
15
|
deserializePrimitive: ()=>deserializePrimitive
|
|
16
16
|
});
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
const
|
|
17
|
+
const _domainsJs = require("./domains.js");
|
|
18
|
+
const _genericsJs = require("./generics.js");
|
|
19
|
+
const _numericLiteralsJs = require("./numericLiterals.js");
|
|
20
20
|
const snapshot = (data, opts = {})=>serializeRecurse(data, opts, []);
|
|
21
21
|
const stringify = (data, indent)=>{
|
|
22
|
-
switch((0,
|
|
22
|
+
switch((0, _domainsJs.domainOf)(data)){
|
|
23
23
|
case "object":
|
|
24
24
|
return JSON.stringify(serializeRecurse(data, stringifyOpts, []), null, indent);
|
|
25
25
|
case "symbol":
|
|
@@ -34,7 +34,7 @@ const stringifyOpts = {
|
|
|
34
34
|
onFunction: (v)=>`(function${v.name && ` ${v.name}`})`
|
|
35
35
|
};
|
|
36
36
|
const serializeRecurse = (data, context, seen)=>{
|
|
37
|
-
switch((0,
|
|
37
|
+
switch((0, _domainsJs.domainOf)(data)){
|
|
38
38
|
case "object":
|
|
39
39
|
if (typeof data === "function") {
|
|
40
40
|
return stringifyOpts.onFunction(data);
|
|
@@ -65,7 +65,7 @@ const serializeRecurse = (data, context, seen)=>{
|
|
|
65
65
|
}
|
|
66
66
|
};
|
|
67
67
|
const serializePrimitive = (value)=>typeof value === "string" ? `'${value}'` : typeof value === "bigint" ? `${value}n` : `${value}`;
|
|
68
|
-
const deserializePrimitive = (serialized)=>(0,
|
|
68
|
+
const deserializePrimitive = (serialized)=>(0, _genericsJs.isKeyOf)(serialized, serializedKeywords) ? serializedKeywords[serialized] : serialized[0] === "'" ? serialized.slice(1, -1) : (0, _numericLiteralsJs.tryParseWellFormedBigint)(serialized) ?? (0, _numericLiteralsJs.tryParseWellFormedNumber)(serialized, true);
|
|
69
69
|
const serializedKeywords = {
|
|
70
70
|
true: true,
|
|
71
71
|
false: false,
|
package/dist/mjs/main.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
export { scope } from "./scopes/scope.js";
|
|
2
2
|
export { type, ark, arkScope } from "./scopes/ark.js";
|
|
3
|
-
export {
|
|
3
|
+
export { arrayOf, instanceOf, intersection, keyOf, morph, narrow, union, valueOf } from "./scopes/expressions.js";
|
|
4
|
+
export { jsObjectsScope } from "./scopes/jsObjects.js";
|
|
5
|
+
export { tsKeywordsScope } from "./scopes/tsKeywords.js";
|
|
6
|
+
export { validationScope } from "./scopes/validation/validation.js";
|
|
4
7
|
export { Problems, Problem } from "./traverse/problems.js";
|
|
8
|
+
export { parseConfigTuple } from "./parse/ast/config.js";
|
|
@@ -38,6 +38,19 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
|
38
38
|
_classApplyDescriptorSet(receiver, descriptor, value);
|
|
39
39
|
return value;
|
|
40
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
|
+
}
|
|
41
54
|
import { throwInternalError } from "../utils/errors.js";
|
|
42
55
|
import { objectKeysOf } from "../utils/generics.js";
|
|
43
56
|
import { Path } from "../utils/paths.js";
|
|
@@ -69,6 +82,10 @@ export class IntersectionState {
|
|
|
69
82
|
return empty;
|
|
70
83
|
}
|
|
71
84
|
constructor(type, lastOperator){
|
|
85
|
+
_defineProperty(this, "type", void 0);
|
|
86
|
+
_defineProperty(this, "lastOperator", void 0);
|
|
87
|
+
_defineProperty(this, "path", void 0);
|
|
88
|
+
_defineProperty(this, "domain", void 0);
|
|
72
89
|
_classPrivateFieldInit(this, _disjoints, {
|
|
73
90
|
writable: true,
|
|
74
91
|
value: void 0
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { parseDefinition } from "../definition.js";
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* @operator {@link parseConfigTuple | :}
|
|
4
|
+
* @docgenTable
|
|
5
|
+
* @tuple ["type", ":", config]
|
|
6
|
+
*/ export const parseConfigTuple = (def, ctx)=>({
|
|
3
7
|
node: ctx.type.scope.resolveTypeNode(parseDefinition(def[0], ctx)),
|
|
4
8
|
config: def[2]
|
|
5
9
|
});
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
var _keyNode, _keyType, _keyNode1, _keyNode2;
|
|
2
1
|
import { mappedKeys } from "../../nodes/rules/props.js";
|
|
3
2
|
import { throwInternalError } from "../../utils/errors.js";
|
|
4
3
|
import { deepFreeze } from "../../utils/freeze.js";
|
|
@@ -31,11 +30,13 @@ export const parseKeyOfTuple = (def, ctx)=>{
|
|
|
31
30
|
for (const key of sharedKeys){
|
|
32
31
|
const keyType = typeof key;
|
|
33
32
|
if (keyType === "string" || keyType === "number" || keyType === "symbol") {
|
|
33
|
+
var _keyNode, _keyType;
|
|
34
34
|
(_keyNode = keyNode)[_keyType = keyType] ?? (_keyNode[_keyType] = []);
|
|
35
35
|
keyNode[keyType].push({
|
|
36
36
|
value: key
|
|
37
37
|
});
|
|
38
38
|
} else if (key === wellFormedNonNegativeIntegerMatcher) {
|
|
39
|
+
var _keyNode1, _keyNode2;
|
|
39
40
|
(_keyNode1 = keyNode).string ?? (_keyNode1.string = []);
|
|
40
41
|
keyNode.string.push(arrayIndexStringBranch);
|
|
41
42
|
(_keyNode2 = keyNode).number ?? (_keyNode2.number = []);
|
|
@@ -42,6 +42,5 @@ export const parseDefinition = (def, ctx)=>{
|
|
|
42
42
|
}
|
|
43
43
|
};
|
|
44
44
|
export const as = Symbol("as");
|
|
45
|
-
export const unknownDefinitionMessage = "Cannot statically parse a definition inferred as unknown. Consider using 'as Infer<...>' to cast it.";
|
|
46
45
|
const isThunk = (def)=>typeof def === "function" && def.length === 0;
|
|
47
46
|
export const writeBadDefinitionTypeMessage = (actual)=>`Type definitions must be strings or objects (was ${actual})`;
|
|
@@ -1,3 +1,16 @@
|
|
|
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
|
+
}
|
|
1
14
|
import { isLiteralNode, rootIntersection, rootUnion, toArrayNode } from "../../../nodes/node.js";
|
|
2
15
|
import { minComparators } from "../../../nodes/rules/range.js";
|
|
3
16
|
import { throwInternalError, throwParseError } from "../../../utils/errors.js";
|
|
@@ -130,6 +143,11 @@ export class DynamicState {
|
|
|
130
143
|
return this;
|
|
131
144
|
}
|
|
132
145
|
constructor(def, ctx){
|
|
146
|
+
_defineProperty(this, "ctx", void 0);
|
|
147
|
+
_defineProperty(this, "scanner", void 0);
|
|
148
|
+
_defineProperty(this, "root", void 0);
|
|
149
|
+
_defineProperty(this, "branches", void 0);
|
|
150
|
+
_defineProperty(this, "groups", void 0);
|
|
133
151
|
this.ctx = ctx;
|
|
134
152
|
this.branches = {};
|
|
135
153
|
this.groups = [];
|
|
@@ -1,3 +1,16 @@
|
|
|
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
|
+
}
|
|
1
14
|
export class Scanner {
|
|
2
15
|
/** Get lookahead and advance scanner by one */ shift() {
|
|
3
16
|
return this.chars[this.i++] ?? "";
|
|
@@ -33,7 +46,9 @@ export class Scanner {
|
|
|
33
46
|
return this.lookahead in tokens;
|
|
34
47
|
}
|
|
35
48
|
constructor(def){
|
|
36
|
-
this
|
|
49
|
+
_defineProperty(this, "chars", void 0);
|
|
50
|
+
_defineProperty(this, "i", void 0);
|
|
51
|
+
_defineProperty(this, "finalized", false);
|
|
37
52
|
this.chars = [
|
|
38
53
|
...def
|
|
39
54
|
];
|
package/dist/mjs/scopes/cache.js
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
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
|
+
}
|
|
1
14
|
import { deepFreeze } from "../utils/freeze.js";
|
|
2
15
|
export class Cache {
|
|
3
16
|
get root() {
|
|
@@ -14,7 +27,7 @@ export class Cache {
|
|
|
14
27
|
return item;
|
|
15
28
|
}
|
|
16
29
|
constructor(){
|
|
17
|
-
this
|
|
30
|
+
_defineProperty(this, "cache", {});
|
|
18
31
|
}
|
|
19
32
|
}
|
|
20
33
|
export class FreezingCache extends Cache {
|