arktype 0.4.0 → 0.5.0
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/dist/cjs/nodes/discriminate.js +86 -18
- package/dist/cjs/nodes/rules/class.js +2 -2
- package/dist/cjs/nodes/rules/divisor.js +1 -1
- package/dist/cjs/nodes/rules/props.js +1 -2
- package/dist/cjs/nodes/rules/range.js +1 -1
- package/dist/cjs/nodes/rules/regex.js +1 -1
- package/dist/cjs/parse/ast/morph.js +1 -1
- package/dist/cjs/parse/ast/tuple.js +3 -2
- package/dist/cjs/parse/string/shift/operator/bounds.js +1 -1
- package/dist/cjs/scopes/scope.js +5 -4
- package/dist/cjs/scopes/validation.js +16 -1
- package/dist/cjs/traverse/problems.js +22 -20
- package/dist/cjs/traverse/traverse.js +53 -46
- package/dist/cjs/utils/freeze.js +2 -3
- package/dist/cjs/utils/numericLiterals.js +2 -14
- package/dist/mjs/nodes/discriminate.js +87 -19
- package/dist/mjs/nodes/rules/class.js +2 -2
- package/dist/mjs/nodes/rules/divisor.js +1 -1
- package/dist/mjs/nodes/rules/props.js +1 -2
- package/dist/mjs/nodes/rules/range.js +1 -1
- package/dist/mjs/nodes/rules/regex.js +1 -1
- package/dist/mjs/parse/ast/morph.js +1 -1
- package/dist/mjs/parse/ast/tuple.js +3 -2
- package/dist/mjs/parse/string/shift/operator/bounds.js +2 -2
- package/dist/mjs/scopes/scope.js +5 -4
- package/dist/mjs/scopes/validation.js +16 -1
- package/dist/mjs/traverse/problems.js +20 -19
- package/dist/mjs/traverse/traverse.js +25 -18
- package/dist/mjs/utils/freeze.js +2 -3
- package/dist/mjs/utils/numericLiterals.js +1 -1
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/nodes/discriminate.d.ts +6 -8
- package/dist/types/nodes/rules/range.d.ts +2 -2
- package/dist/types/parse/ast/morph.d.ts +8 -5
- package/dist/types/parse/ast/narrow.d.ts +2 -2
- package/dist/types/parse/ast/tuple.d.ts +5 -5
- package/dist/types/parse/string/shift/scanner.d.ts +1 -1
- package/dist/types/scopes/expressions.d.ts +2 -2
- package/dist/types/scopes/scope.d.ts +2 -2
- package/dist/types/scopes/standard.d.ts +15 -0
- package/dist/types/scopes/validation.d.ts +4 -0
- package/dist/types/traverse/problems.d.ts +9 -7
- package/dist/types/traverse/traverse.d.ts +3 -2
- package/dist/types/utils/generics.d.ts +1 -1
- package/dist/types/utils/numericLiterals.d.ts +16 -0
- package/package.json +1 -1
|
@@ -10,7 +10,7 @@ function _export(target, all) {
|
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
12
|
flattenBranches: ()=>flattenBranches,
|
|
13
|
-
|
|
13
|
+
serializeDefinitionIfAllowed: ()=>serializeDefinitionIfAllowed,
|
|
14
14
|
serializeCase: ()=>serializeCase
|
|
15
15
|
});
|
|
16
16
|
const _unionTs = require("../parse/ast/union.js");
|
|
@@ -42,10 +42,12 @@ const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
|
|
|
42
42
|
];
|
|
43
43
|
}
|
|
44
44
|
const cases = {};
|
|
45
|
-
|
|
46
|
-
for(caseKey in bestDiscriminant.indexCases){
|
|
45
|
+
for(const caseKey in bestDiscriminant.indexCases){
|
|
47
46
|
const nextIndices = bestDiscriminant.indexCases[caseKey];
|
|
48
47
|
cases[caseKey] = discriminate(originalBranches, nextIndices, discriminants, ctx);
|
|
48
|
+
if (caseKey !== "default") {
|
|
49
|
+
pruneDiscriminant(cases[caseKey], bestDiscriminant.path, bestDiscriminant, ctx);
|
|
50
|
+
}
|
|
49
51
|
}
|
|
50
52
|
return [
|
|
51
53
|
[
|
|
@@ -58,9 +60,63 @@ const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
|
|
|
58
60
|
]
|
|
59
61
|
];
|
|
60
62
|
};
|
|
63
|
+
const pruneDiscriminant = (entries, segments, discriminant, ctx)=>{
|
|
64
|
+
for(let i = 0; i < entries.length; i++){
|
|
65
|
+
const [k, v] = entries[i];
|
|
66
|
+
if (!segments.length) {
|
|
67
|
+
if (discriminant.kind === "domain") {
|
|
68
|
+
if (k === "domain" || k === "domains") {
|
|
69
|
+
entries.splice(i, 1);
|
|
70
|
+
return;
|
|
71
|
+
} else if (k === "class" || k === "value") {
|
|
72
|
+
// these keys imply a domain, but also add additional
|
|
73
|
+
// information, so can't be pruned
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
} else if (discriminant.kind === k) {
|
|
77
|
+
entries.splice(i, 1);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
} else if ((k === "requiredProp" || k === "prerequisiteProp" || k === "optionalProp") && v[0] === segments[0]) {
|
|
81
|
+
if (typeof v[1] === "string") {
|
|
82
|
+
if (discriminant.kind !== "domain") {
|
|
83
|
+
return throwPruneFailure(discriminant);
|
|
84
|
+
}
|
|
85
|
+
entries.splice(i, 1);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
pruneDiscriminant(v[1], segments.slice(1), discriminant, ctx);
|
|
89
|
+
if (v[1].length === 0) {
|
|
90
|
+
entries.splice(i, 1);
|
|
91
|
+
}
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
// check for branch keys, which must be traversed even if there are no
|
|
95
|
+
// segments left
|
|
96
|
+
if (k === "domains") {
|
|
97
|
+
if ((0, _genericsTs.keyCount)(v) !== 1 || !v.object) {
|
|
98
|
+
return throwPruneFailure(discriminant);
|
|
99
|
+
}
|
|
100
|
+
pruneDiscriminant(v.object, segments, discriminant, ctx);
|
|
101
|
+
return;
|
|
102
|
+
} else if (k === "switch") {
|
|
103
|
+
for(const caseKey in v.cases){
|
|
104
|
+
pruneDiscriminant(v.cases[caseKey], segments, discriminant, ctx);
|
|
105
|
+
}
|
|
106
|
+
return;
|
|
107
|
+
} else if (k === "branches") {
|
|
108
|
+
for (const branch of v){
|
|
109
|
+
pruneDiscriminant(branch, segments, discriminant, ctx);
|
|
110
|
+
}
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return throwPruneFailure(discriminant);
|
|
115
|
+
};
|
|
116
|
+
const throwPruneFailure = (discriminant)=>(0, _errorsTs.throwInternalError)(`Unexpectedly failed to discriminate ${discriminant.kind} at path '${discriminant.path}'`);
|
|
61
117
|
const discriminantKinds = {
|
|
62
118
|
domain: true,
|
|
63
|
-
|
|
119
|
+
class: true,
|
|
64
120
|
value: true
|
|
65
121
|
};
|
|
66
122
|
const calculateDiscriminants = (branches, ctx)=>{
|
|
@@ -83,8 +139,8 @@ const calculateDiscriminants = (branches, ctx)=>{
|
|
|
83
139
|
if (!(0, _genericsTs.isKeyOf)(kind, discriminantKinds)) {
|
|
84
140
|
continue;
|
|
85
141
|
}
|
|
86
|
-
const lSerialized =
|
|
87
|
-
const rSerialized =
|
|
142
|
+
const lSerialized = serializeDefinitionIfAllowed(kind, l);
|
|
143
|
+
const rSerialized = serializeDefinitionIfAllowed(kind, r);
|
|
88
144
|
if (lSerialized === undefined || rSerialized === undefined) {
|
|
89
145
|
continue;
|
|
90
146
|
}
|
|
@@ -102,19 +158,21 @@ const calculateDiscriminants = (branches, ctx)=>{
|
|
|
102
158
|
continue;
|
|
103
159
|
}
|
|
104
160
|
const cases = discriminants.casesByDisjoint[qualifiedDisjoint];
|
|
105
|
-
|
|
161
|
+
const existingLBranch = cases[lSerialized];
|
|
162
|
+
if (!existingLBranch) {
|
|
106
163
|
cases[lSerialized] = [
|
|
107
164
|
lIndex
|
|
108
165
|
];
|
|
109
|
-
} else if (!
|
|
110
|
-
|
|
166
|
+
} else if (!existingLBranch.includes(lIndex)) {
|
|
167
|
+
existingLBranch.push(lIndex);
|
|
111
168
|
}
|
|
112
|
-
|
|
169
|
+
const existingRBranch = cases[rSerialized];
|
|
170
|
+
if (!existingRBranch) {
|
|
113
171
|
cases[rSerialized] = [
|
|
114
172
|
rIndex
|
|
115
173
|
];
|
|
116
|
-
} else if (!
|
|
117
|
-
|
|
174
|
+
} else if (!existingRBranch.includes(rIndex)) {
|
|
175
|
+
existingRBranch.push(rIndex);
|
|
118
176
|
}
|
|
119
177
|
}
|
|
120
178
|
}
|
|
@@ -142,8 +200,7 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
|
|
|
142
200
|
...remainingIndices
|
|
143
201
|
];
|
|
144
202
|
let score = 0;
|
|
145
|
-
|
|
146
|
-
for(caseKey in indexCases){
|
|
203
|
+
for(const caseKey in indexCases){
|
|
147
204
|
const filteredIndices = indexCases[caseKey].filter((i)=>{
|
|
148
205
|
const remainingIndex = remainingIndices.indexOf(i);
|
|
149
206
|
if (remainingIndex !== -1) {
|
|
@@ -179,16 +236,27 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
|
|
|
179
236
|
}
|
|
180
237
|
return bestDiscriminant;
|
|
181
238
|
};
|
|
182
|
-
const
|
|
239
|
+
const serializeDefinitionIfAllowed = (kind, definition)=>{
|
|
240
|
+
switch(kind){
|
|
241
|
+
case "value":
|
|
242
|
+
return serializeIfPrimitive(definition);
|
|
243
|
+
case "domain":
|
|
244
|
+
return definition;
|
|
245
|
+
case "class":
|
|
246
|
+
return typeof definition === "string" ? definition : undefined;
|
|
247
|
+
default:
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
};
|
|
183
251
|
const serializeIfPrimitive = (data)=>{
|
|
184
252
|
const domain = (0, _domainsTs.domainOf)(data);
|
|
185
253
|
return domain === "object" || domain === "symbol" ? undefined : (0, _serializeTs.serializePrimitive)(data);
|
|
186
254
|
};
|
|
187
|
-
const
|
|
255
|
+
const serializeData = {
|
|
188
256
|
value: (data)=>serializeIfPrimitive(data) ?? "default",
|
|
189
|
-
|
|
257
|
+
class: (data)=>(0, _objectKindsTs.objectKindOf)(data) ?? "default",
|
|
190
258
|
domain: _domainsTs.domainOf
|
|
191
259
|
};
|
|
192
|
-
const serializeCase = (kind, data)=>
|
|
260
|
+
const serializeCase = (kind, data)=>serializeData[kind](data);
|
|
193
261
|
const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph((0, _propsTs.propToNode)(prop), $)) : false;
|
|
194
262
|
const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).meta.includesMorph : Object.values(node).some((predicate)=>predicate === true ? false : (0, _objectKindsTs.isArray)(predicate) ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));
|
|
@@ -21,7 +21,7 @@ const classIntersection = (0, _composeTs.composeIntersection)((l, r, state)=>{
|
|
|
21
21
|
});
|
|
22
22
|
const checkClass = (expectedClass, state)=>{
|
|
23
23
|
if (typeof expectedClass === "string") {
|
|
24
|
-
return (0, _objectKindsTs.objectKindOf)(state.data) === expectedClass || state.problems.
|
|
24
|
+
return (0, _objectKindsTs.objectKindOf)(state.data) === expectedClass || !state.problems.add("class", expectedClass);
|
|
25
25
|
}
|
|
26
|
-
return state.data instanceof expectedClass || state.problems.
|
|
26
|
+
return state.data instanceof expectedClass || !state.problems.add("class", expectedClass);
|
|
27
27
|
};
|
|
@@ -26,4 +26,4 @@ const greatestCommonDivisor = (l, r)=>{
|
|
|
26
26
|
}
|
|
27
27
|
return greatestCommonDivisor;
|
|
28
28
|
};
|
|
29
|
-
const checkDivisor = (divisor, state)=>state.data % divisor === 0 || state.problems.
|
|
29
|
+
const checkDivisor = (divisor, state)=>state.data % divisor === 0 || !state.problems.add("divisor", divisor);
|
|
@@ -16,7 +16,6 @@ _export(exports, {
|
|
|
16
16
|
propsIntersection: ()=>propsIntersection,
|
|
17
17
|
flattenProps: ()=>flattenProps
|
|
18
18
|
});
|
|
19
|
-
const _genericsTs = require("../../utils/generics.js");
|
|
20
19
|
const _composeTs = require("../compose.js");
|
|
21
20
|
const _nodeTs = require("../node.js");
|
|
22
21
|
const isOptional = (prop)=>prop[0] === "?";
|
|
@@ -36,7 +35,7 @@ const propsIntersection = (0, _composeTs.composeIntersection)((l, r, state)=>{
|
|
|
36
35
|
return result;
|
|
37
36
|
}
|
|
38
37
|
const lengthValue = getTupleLengthIfPresent(result);
|
|
39
|
-
if (lengthValue === undefined || !(
|
|
38
|
+
if (lengthValue === undefined || !(mappedKeys.index in result)) {
|
|
40
39
|
return result;
|
|
41
40
|
}
|
|
42
41
|
// if we are at this point, we have an array with an exact length (i.e.
|
|
@@ -93,7 +93,7 @@ const flattenRange = (entries, range, ctx)=>{
|
|
|
93
93
|
]);
|
|
94
94
|
}
|
|
95
95
|
};
|
|
96
|
-
const checkBound = (bound, state)=>comparatorCheckers[bound.comparator]((0, _dataTs.sizeOf)(state.data), bound.limit) || state.problems.
|
|
96
|
+
const checkBound = (bound, state)=>comparatorCheckers[bound.comparator]((0, _dataTs.sizeOf)(state.data), bound.limit) || !state.problems.add("bound", bound);
|
|
97
97
|
const comparatorCheckers = {
|
|
98
98
|
"<": (size, limit)=>size < limit,
|
|
99
99
|
">": (size, limit)=>size > limit,
|
|
@@ -22,5 +22,5 @@ const getRegex = (source)=>{
|
|
|
22
22
|
}
|
|
23
23
|
return regexCache[source];
|
|
24
24
|
};
|
|
25
|
-
const checkRegex = (source, state)=>getRegex(source).test(state.data) || state.problems.
|
|
25
|
+
const checkRegex = (source, state)=>getRegex(source).test(state.data) || !state.problems.add("regex", `/${source}/`);
|
|
26
26
|
const regexIntersection = (0, _composeTs.composeIntersection)(_collapsibleSetTs.collapsibleListUnion);
|
|
@@ -54,4 +54,4 @@ const applyMorph = (branch, morph)=>(0, _branchTs.isMorphBranch)(branch) ? {
|
|
|
54
54
|
rules: branch,
|
|
55
55
|
morph
|
|
56
56
|
};
|
|
57
|
-
const writeMalformedMorphExpressionMessage = (value)=>`Morph expression requires a function following '
|
|
57
|
+
const writeMalformedMorphExpressionMessage = (value)=>`Morph expression requires a function following '|>' (was ${typeof value})`;
|
|
@@ -67,8 +67,9 @@ const indexOneParsers = {
|
|
|
67
67
|
"|": parseBranchTuple,
|
|
68
68
|
"&": parseBranchTuple,
|
|
69
69
|
"[]": parseArrayTuple,
|
|
70
|
-
"
|
|
71
|
-
"
|
|
70
|
+
"=>": _narrowTs.parseNarrowTuple,
|
|
71
|
+
"|>": _morphTs.parseMorphTuple,
|
|
72
|
+
":": ()=>({})
|
|
72
73
|
};
|
|
73
74
|
const prefixParsers = {
|
|
74
75
|
keyof: _keyofTs.parseKeyOfTuple,
|
|
@@ -71,7 +71,7 @@ const distributeRange = (range, s)=>{
|
|
|
71
71
|
if (resolution.object === true) {
|
|
72
72
|
return false;
|
|
73
73
|
}
|
|
74
|
-
return (0, _genericsTs.listFrom)(resolution.object).every((branch)=>
|
|
74
|
+
return (0, _genericsTs.listFrom)(resolution.object).every((branch)=>"class" in branch && branch.class === "Array");
|
|
75
75
|
default:
|
|
76
76
|
return false;
|
|
77
77
|
}
|
package/dist/cjs/scopes/scope.js
CHANGED
|
@@ -18,6 +18,7 @@ const _nodeTs = require("../nodes/node.js");
|
|
|
18
18
|
const _definitionTs = require("../parse/definition.js");
|
|
19
19
|
const _chainableNoOpProxyTs = require("../utils/chainableNoOpProxy.js");
|
|
20
20
|
const _errorsTs = require("../utils/errors.js");
|
|
21
|
+
const _freezeTs = require("../utils/freeze.js");
|
|
21
22
|
const _pathsTs = require("../utils/paths.js");
|
|
22
23
|
const _cacheTs = require("./cache.js");
|
|
23
24
|
const _typeTs = require("./type.js");
|
|
@@ -192,12 +193,12 @@ class Scope {
|
|
|
192
193
|
], opts),
|
|
193
194
|
narrow: (def, fn, opts)=>this.type([
|
|
194
195
|
def,
|
|
195
|
-
"
|
|
196
|
+
"=>",
|
|
196
197
|
fn
|
|
197
198
|
], opts),
|
|
198
199
|
morph: (def, fn, opts)=>this.type([
|
|
199
200
|
def,
|
|
200
|
-
"
|
|
201
|
+
"|>",
|
|
201
202
|
fn
|
|
202
203
|
], opts)
|
|
203
204
|
};
|
|
@@ -284,8 +285,8 @@ function resolveRecurse(name, onUnresolvable, seen) {
|
|
|
284
285
|
seen.push(resolution);
|
|
285
286
|
resolution = _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, resolution, "throw", seen).node;
|
|
286
287
|
}
|
|
287
|
-
type.node = resolution;
|
|
288
|
-
type.flat = (0, _nodeTs.flattenType)(type);
|
|
288
|
+
type.node = (0, _freezeTs.deepFreeze)(resolution);
|
|
289
|
+
type.flat = (0, _freezeTs.deepFreeze)((0, _nodeTs.flattenType)(type));
|
|
289
290
|
return type;
|
|
290
291
|
}
|
|
291
292
|
const scope = (aliases, opts = {})=>new Scope(aliases, opts);
|
|
@@ -13,6 +13,7 @@ _export(exports, {
|
|
|
13
13
|
validationScope: ()=>validationScope,
|
|
14
14
|
validation: ()=>validation
|
|
15
15
|
});
|
|
16
|
+
const _numericLiteralsTs = require("../utils/numericLiterals.js");
|
|
16
17
|
const _scopeTs = require("./scope.js");
|
|
17
18
|
const isLuhnValid = (creditCardInput)=>{
|
|
18
19
|
const sanitized = creditCardInput.replace(/[- ]+/g, "");
|
|
@@ -44,11 +45,25 @@ const validationScope = (0, _scopeTs.scope)({
|
|
|
44
45
|
lowercase: /^[a-z]*$/,
|
|
45
46
|
uppercase: /^[A-Z]*$/,
|
|
46
47
|
// https://github.com/validatorjs/validator.js
|
|
47
|
-
creditCard:
|
|
48
|
+
creditCard: [
|
|
49
|
+
/^(?:4[0-9]{12}(?:[0-9]{3,6})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12,15}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11}|6[27][0-9]{14}|^(81[0-9]{14,17}))$/,
|
|
50
|
+
"=>",
|
|
51
|
+
(s, problems)=>isLuhnValid(s) || !problems.add("mustBe", "a valid credit card number")
|
|
52
|
+
],
|
|
48
53
|
// https://www.regular-expressions.info
|
|
49
54
|
email: /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/,
|
|
50
55
|
// https://github.com/validatorjs/validator.js
|
|
51
56
|
uuid: /^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/,
|
|
57
|
+
parsedNumber: [
|
|
58
|
+
_numericLiteralsTs.wellFormedNumberMatcher,
|
|
59
|
+
"|>",
|
|
60
|
+
(s)=>parseFloat(s)
|
|
61
|
+
],
|
|
62
|
+
parsedInteger: [
|
|
63
|
+
_numericLiteralsTs.wellFormedIntegerMatcher,
|
|
64
|
+
"|>",
|
|
65
|
+
(s)=>parseInt(s)
|
|
66
|
+
],
|
|
52
67
|
integer: [
|
|
53
68
|
"node",
|
|
54
69
|
{
|
|
@@ -12,7 +12,8 @@ _export(exports, {
|
|
|
12
12
|
ArkTypeError: ()=>ArkTypeError,
|
|
13
13
|
Problem: ()=>Problem,
|
|
14
14
|
Problems: ()=>Problems,
|
|
15
|
-
|
|
15
|
+
domainsToDescriptions: ()=>domainsToDescriptions,
|
|
16
|
+
objectKindsToDescriptions: ()=>objectKindsToDescriptions,
|
|
16
17
|
describeBranches: ()=>describeBranches,
|
|
17
18
|
defaultProblemWriters: ()=>defaultProblemWriters
|
|
18
19
|
});
|
|
@@ -96,15 +97,17 @@ class Problem {
|
|
|
96
97
|
}
|
|
97
98
|
var _state = /*#__PURE__*/ new WeakMap();
|
|
98
99
|
class ProblemArray extends Array {
|
|
99
|
-
|
|
100
|
+
add(code, source, opts) {
|
|
100
101
|
// copy the path to avoid future mutations affecting it
|
|
101
102
|
const path = opts?.path ?? _pathsTs.Path.from(_classPrivateFieldGet(this, _state).path);
|
|
102
103
|
const data = // we have to check for the presence of the key explicitly since the
|
|
103
104
|
// data could be undefined or null
|
|
104
105
|
opts && "data" in opts ? opts.data : _classPrivateFieldGet(this, _state).data;
|
|
105
|
-
|
|
106
|
+
const problem = new Problem(code, path, data, source, _classPrivateFieldGet(this, _state).getConfigForProblemCode(code));
|
|
107
|
+
this.addProblem(problem);
|
|
108
|
+
return problem;
|
|
106
109
|
}
|
|
107
|
-
|
|
110
|
+
addProblem(problem) {
|
|
108
111
|
const pathKey = `${problem.path}`;
|
|
109
112
|
const existing = this.byPath[pathKey];
|
|
110
113
|
if (existing) {
|
|
@@ -126,7 +129,7 @@ class ProblemArray extends Array {
|
|
|
126
129
|
this.byPath[pathKey] = problem;
|
|
127
130
|
this.push(problem);
|
|
128
131
|
}
|
|
129
|
-
|
|
132
|
+
this.count++;
|
|
130
133
|
}
|
|
131
134
|
get summary() {
|
|
132
135
|
return this.join("\n");
|
|
@@ -140,6 +143,7 @@ class ProblemArray extends Array {
|
|
|
140
143
|
constructor(state){
|
|
141
144
|
super();
|
|
142
145
|
this.byPath = {};
|
|
146
|
+
this.count = 0;
|
|
143
147
|
_classPrivateFieldInit(this, _state, {
|
|
144
148
|
writable: true,
|
|
145
149
|
value: void 0
|
|
@@ -149,16 +153,15 @@ class ProblemArray extends Array {
|
|
|
149
153
|
}
|
|
150
154
|
const Problems = ProblemArray;
|
|
151
155
|
const capitalize = (s)=>s[0].toUpperCase() + s.slice(1);
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
if (domains.length === 0) {
|
|
156
|
+
const domainsToDescriptions = (domains)=>domains.map((objectKind)=>_domainsTs.domainDescriptions[objectKind]);
|
|
157
|
+
const objectKindsToDescriptions = (kinds)=>kinds.map((objectKind)=>_objectKindsTs.objectKindDescriptions[objectKind]);
|
|
158
|
+
const describeBranches = (descriptions)=>{
|
|
159
|
+
if (descriptions.length === 0) {
|
|
157
160
|
return "never";
|
|
158
161
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
+
if (descriptions.length === 1) {
|
|
163
|
+
return descriptions[0];
|
|
164
|
+
}
|
|
162
165
|
let description = "";
|
|
163
166
|
for(let i = 0; i < descriptions.length - 1; i++){
|
|
164
167
|
description += descriptions[i];
|
|
@@ -191,10 +194,6 @@ const defaultProblemWriters = compileDefaultProblemWriters({
|
|
|
191
194
|
mustBe: (domain)=>_domainsTs.domainDescriptions[domain],
|
|
192
195
|
writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.domain)
|
|
193
196
|
},
|
|
194
|
-
domainBranches: {
|
|
195
|
-
mustBe: (domains)=>describeDomains(domains),
|
|
196
|
-
writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.domain)
|
|
197
|
-
},
|
|
198
197
|
missing: {
|
|
199
198
|
mustBe: ()=>"defined",
|
|
200
199
|
writeReason: (mustBe)=>writeDefaultReason(mustBe, "")
|
|
@@ -209,9 +208,6 @@ const defaultProblemWriters = compileDefaultProblemWriters({
|
|
|
209
208
|
value: {
|
|
210
209
|
mustBe: _serializeTs.stringify
|
|
211
210
|
},
|
|
212
|
-
valueBranches: {
|
|
213
|
-
mustBe: (values)=>describeBranches(values.map(_serializeTs.stringify))
|
|
214
|
-
},
|
|
215
211
|
branches: {
|
|
216
212
|
mustBe: (branchProblems)=>describeBranches(branchProblems.map((problem)=>`${problem.path} must be ${problem.parts ? describeBranches(problem.parts.map((part)=>part.mustBe)) : problem.mustBe}`)),
|
|
217
213
|
writeReason: (mustBe, data)=>`${mustBe} (was ${data})`,
|
|
@@ -221,5 +217,11 @@ const defaultProblemWriters = compileDefaultProblemWriters({
|
|
|
221
217
|
mustBe: (problems)=>"• " + problems.map((_)=>_.mustBe).join("\n• "),
|
|
222
218
|
writeReason: (mustBe, data)=>`${data} must be...\n${mustBe}`,
|
|
223
219
|
addContext: (reason, path)=>path.length ? `At ${path}, ${reason}` : reason
|
|
220
|
+
},
|
|
221
|
+
mustBe: {
|
|
222
|
+
mustBe: (mustBe)=>mustBe
|
|
223
|
+
},
|
|
224
|
+
cases: {
|
|
225
|
+
mustBe: (cases)=>describeBranches(cases)
|
|
224
226
|
}
|
|
225
227
|
});
|
|
@@ -14,17 +14,17 @@ _export(exports, {
|
|
|
14
14
|
checkEntries: ()=>checkEntries,
|
|
15
15
|
checkRequiredProp: ()=>checkRequiredProp
|
|
16
16
|
});
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
const
|
|
17
|
+
const _discriminateTs = require("../nodes/discriminate.js");
|
|
18
|
+
const _classTs = require("../nodes/rules/class.js");
|
|
19
|
+
const _divisorTs = require("../nodes/rules/divisor.js");
|
|
20
|
+
const _rangeTs = require("../nodes/rules/range.js");
|
|
21
|
+
const _regexTs = require("../nodes/rules/regex.js");
|
|
22
|
+
const _rulesTs = require("../nodes/rules/rules.js");
|
|
23
|
+
const _domainsTs = require("../utils/domains.js");
|
|
24
|
+
const _errorsTs = require("../utils/errors.js");
|
|
25
|
+
const _genericsTs = require("../utils/generics.js");
|
|
26
|
+
const _pathsTs = require("../utils/paths.js");
|
|
27
|
+
const _problemsTs = require("./problems.js");
|
|
28
28
|
function _checkPrivateRedeclaration(obj, privateCollection) {
|
|
29
29
|
if (privateCollection.has(obj)) {
|
|
30
30
|
throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
@@ -69,18 +69,18 @@ var _seen = /*#__PURE__*/ new WeakMap();
|
|
|
69
69
|
class TraversalState {
|
|
70
70
|
getConfigForProblemCode(code) {
|
|
71
71
|
if (!this.configs.length) {
|
|
72
|
-
return
|
|
72
|
+
return _problemsTs.defaultProblemWriters[code];
|
|
73
73
|
}
|
|
74
74
|
for(let i = this.configs.length - 1; i >= 0; i--){
|
|
75
75
|
if (this.configs[i][code] || this.configs[i]["defaults"]) {
|
|
76
76
|
return {
|
|
77
|
-
...
|
|
77
|
+
..._problemsTs.defaultProblemWriters[code],
|
|
78
78
|
...this.configs[i]["defaults"],
|
|
79
79
|
...this.configs[i][code]
|
|
80
80
|
};
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
|
-
return
|
|
83
|
+
return _problemsTs.defaultProblemWriters[code];
|
|
84
84
|
}
|
|
85
85
|
traverseKey(key, node) {
|
|
86
86
|
const lastData = this.data;
|
|
@@ -97,16 +97,17 @@ class TraversalState {
|
|
|
97
97
|
const id = resolution.meta.id;
|
|
98
98
|
// this assignment helps with narrowing
|
|
99
99
|
const data = this.data;
|
|
100
|
-
const isObject = (0,
|
|
100
|
+
const isObject = (0, _domainsTs.hasDomain)(data, "object");
|
|
101
101
|
if (isObject) {
|
|
102
|
-
|
|
103
|
-
|
|
102
|
+
const seenByCurrentType = _classPrivateFieldGet(this, _seen)[id];
|
|
103
|
+
if (seenByCurrentType) {
|
|
104
|
+
if (seenByCurrentType.includes(data)) {
|
|
104
105
|
// if data has already been checked by this alias as part of
|
|
105
106
|
// a resolution higher up on the call stack, it must be valid
|
|
106
107
|
// or we wouldn't be here
|
|
107
108
|
return true;
|
|
108
109
|
}
|
|
109
|
-
|
|
110
|
+
seenByCurrentType.push(data);
|
|
110
111
|
} else {
|
|
111
112
|
_classPrivateFieldGet(this, _seen)[id] = [
|
|
112
113
|
data
|
|
@@ -126,12 +127,12 @@ class TraversalState {
|
|
|
126
127
|
const lastFailFast = this.failFast;
|
|
127
128
|
this.failFast = true;
|
|
128
129
|
const lastProblems = this.problems;
|
|
129
|
-
const branchProblems = new
|
|
130
|
+
const branchProblems = new _problemsTs.Problems(this);
|
|
130
131
|
this.problems = branchProblems;
|
|
131
132
|
const lastPath = this.path;
|
|
132
133
|
let hasValidBranch = false;
|
|
133
134
|
for (const branch of branches){
|
|
134
|
-
this.path = new
|
|
135
|
+
this.path = new _pathsTs.Path();
|
|
135
136
|
if (checkEntries(branch, this)) {
|
|
136
137
|
hasValidBranch = true;
|
|
137
138
|
break;
|
|
@@ -140,7 +141,7 @@ class TraversalState {
|
|
|
140
141
|
this.path = lastPath;
|
|
141
142
|
this.problems = lastProblems;
|
|
142
143
|
this.failFast = lastFailFast;
|
|
143
|
-
return hasValidBranch || this.problems.
|
|
144
|
+
return hasValidBranch || !this.problems.add("branches", branchProblems);
|
|
144
145
|
}
|
|
145
146
|
constructor(data, type){
|
|
146
147
|
_classPrivateFieldInit(this, _seen, {
|
|
@@ -149,8 +150,8 @@ class TraversalState {
|
|
|
149
150
|
});
|
|
150
151
|
this.data = data;
|
|
151
152
|
this.type = type;
|
|
152
|
-
this.path = new
|
|
153
|
-
this.problems = new
|
|
153
|
+
this.path = new _pathsTs.Path();
|
|
154
|
+
this.problems = new _problemsTs.Problems(this);
|
|
154
155
|
this.failFast = false;
|
|
155
156
|
_classPrivateFieldSet(this, _seen, {});
|
|
156
157
|
this.configs = type.meta.scope.config ? [
|
|
@@ -158,7 +159,7 @@ class TraversalState {
|
|
|
158
159
|
] : [];
|
|
159
160
|
}
|
|
160
161
|
}
|
|
161
|
-
const traverse = (node, state)=>typeof node === "string" ? (0,
|
|
162
|
+
const traverse = (node, state)=>typeof node === "string" ? (0, _domainsTs.domainOf)(state.data) === node || !state.problems.add("domain", node) : checkEntries(node, state);
|
|
162
163
|
const checkEntries = (entries, state)=>{
|
|
163
164
|
let isValid = true;
|
|
164
165
|
for(let i = 0; i < entries.length; i++){
|
|
@@ -169,7 +170,7 @@ const checkEntries = (entries, state)=>{
|
|
|
169
170
|
if (state.failFast) {
|
|
170
171
|
return false;
|
|
171
172
|
}
|
|
172
|
-
if (i < entries.length - 1 &&
|
|
173
|
+
if (i < entries.length - 1 && _rulesTs.precedenceMap[k] < _rulesTs.precedenceMap[entries[i + 1][0]]) {
|
|
173
174
|
// if we've encountered a problem, there is at least one entry
|
|
174
175
|
// remaining, and the next entry is of a higher precedence level
|
|
175
176
|
// than the current entry, return immediately
|
|
@@ -183,20 +184,21 @@ const checkRequiredProp = (prop, state)=>{
|
|
|
183
184
|
if (prop[0] in state.data) {
|
|
184
185
|
return state.traverseKey(prop[0], prop[1]);
|
|
185
186
|
}
|
|
186
|
-
|
|
187
|
+
state.problems.add("missing", undefined, {
|
|
187
188
|
path: state.path.concat(prop[0]),
|
|
188
189
|
data: undefined
|
|
189
190
|
});
|
|
191
|
+
return false;
|
|
190
192
|
};
|
|
191
193
|
const entryCheckers = {
|
|
192
|
-
regex:
|
|
193
|
-
divisor:
|
|
194
|
+
regex: _regexTs.checkRegex,
|
|
195
|
+
divisor: _divisorTs.checkDivisor,
|
|
194
196
|
domains: (domains, state)=>{
|
|
195
|
-
const entries = domains[(0,
|
|
196
|
-
return entries ? checkEntries(entries, state) : state.problems.
|
|
197
|
+
const entries = domains[(0, _domainsTs.domainOf)(state.data)];
|
|
198
|
+
return entries ? checkEntries(entries, state) : !state.problems.add("cases", (0, _problemsTs.domainsToDescriptions)((0, _genericsTs.objectKeysOf)(domains)));
|
|
197
199
|
},
|
|
198
|
-
domain: (domain, state)=>(0,
|
|
199
|
-
bound:
|
|
200
|
+
domain: (domain, state)=>(0, _domainsTs.domainOf)(state.data) === domain || !state.problems.add("domain", domain),
|
|
201
|
+
bound: _rangeTs.checkBound,
|
|
200
202
|
optionalProp: (prop, state)=>{
|
|
201
203
|
if (prop[0] in state.data) {
|
|
202
204
|
return state.traverseKey(prop[0], prop[1]);
|
|
@@ -209,7 +211,8 @@ const entryCheckers = {
|
|
|
209
211
|
prerequisiteProp: checkRequiredProp,
|
|
210
212
|
indexProp: (node, state)=>{
|
|
211
213
|
if (!Array.isArray(state.data)) {
|
|
212
|
-
|
|
214
|
+
state.problems.add("class", "Array");
|
|
215
|
+
return false;
|
|
213
216
|
}
|
|
214
217
|
let isValid = true;
|
|
215
218
|
for(let i = 0; i < state.data.length; i++){
|
|
@@ -222,23 +225,22 @@ const entryCheckers = {
|
|
|
222
225
|
},
|
|
223
226
|
branches: (branches, state)=>state.traverseBranches(branches),
|
|
224
227
|
switch: (rule, state)=>{
|
|
225
|
-
const dataAtPath = (0,
|
|
226
|
-
const caseKey = (0,
|
|
227
|
-
if ((0,
|
|
228
|
+
const dataAtPath = (0, _pathsTs.getPath)(state.data, rule.path);
|
|
229
|
+
const caseKey = (0, _discriminateTs.serializeCase)(rule.kind, dataAtPath);
|
|
230
|
+
if ((0, _genericsTs.hasKey)(rule.cases, caseKey)) {
|
|
228
231
|
return checkEntries(rule.cases[caseKey], state);
|
|
229
232
|
}
|
|
230
|
-
const caseKeys = (0,
|
|
233
|
+
const caseKeys = (0, _genericsTs.objectKeysOf)(rule.cases);
|
|
231
234
|
const missingCasePath = state.path.concat(rule.path);
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
data: dataAtPath
|
|
235
|
-
}) : state.problems.create("domainBranches", caseKeys, {
|
|
235
|
+
const caseDescriptions = rule.kind === "value" ? caseKeys : rule.kind === "domain" ? (0, _problemsTs.domainsToDescriptions)(caseKeys) : rule.kind === "class" ? (0, _problemsTs.objectKindsToDescriptions)(caseKeys) : (0, _errorsTs.throwInternalError)(`Unexpectedly encountered rule kind '${rule.kind}' during traversal`);
|
|
236
|
+
state.problems.add("cases", caseDescriptions, {
|
|
236
237
|
path: missingCasePath,
|
|
237
238
|
data: dataAtPath
|
|
238
239
|
});
|
|
240
|
+
return false;
|
|
239
241
|
},
|
|
240
242
|
alias: (name, state)=>state.traverseResolution(name),
|
|
241
|
-
class:
|
|
243
|
+
class: _classTs.checkClass,
|
|
242
244
|
narrow: (narrow, state)=>narrow(state.data, state.problems),
|
|
243
245
|
config: ({ config , node }, state)=>{
|
|
244
246
|
state.configs.push(config);
|
|
@@ -246,11 +248,16 @@ const entryCheckers = {
|
|
|
246
248
|
state.configs.pop();
|
|
247
249
|
return result;
|
|
248
250
|
},
|
|
249
|
-
value: (value, state)=>state.data === value || state.problems.
|
|
251
|
+
value: (value, state)=>state.data === value || !state.problems.add("value", value),
|
|
250
252
|
morph: (morph, state)=>{
|
|
251
|
-
const out = morph(state.data);
|
|
252
|
-
if (
|
|
253
|
-
return
|
|
253
|
+
const out = morph(state.data, state.problems);
|
|
254
|
+
if (state.problems.length) {
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
if (out instanceof _problemsTs.Problem) {
|
|
258
|
+
// if a problem was returned from the morph but not added, add it
|
|
259
|
+
state.problems.addProblem(out);
|
|
260
|
+
return false;
|
|
254
261
|
}
|
|
255
262
|
state.data = out;
|
|
256
263
|
return true;
|
package/dist/cjs/utils/freeze.js
CHANGED
|
@@ -8,9 +8,8 @@ Object.defineProperty(exports, "deepFreeze", {
|
|
|
8
8
|
});
|
|
9
9
|
const deepFreeze = (value)=>Object.isFrozen(value) ? value : Array.isArray(value) ? Object.freeze(value.map(deepFreeze)) : deepFreezeDictionary(value);
|
|
10
10
|
const deepFreezeDictionary = (value)=>{
|
|
11
|
-
const result = {};
|
|
12
11
|
for(const k in value){
|
|
13
|
-
|
|
12
|
+
deepFreeze(value[k]);
|
|
14
13
|
}
|
|
15
|
-
return
|
|
14
|
+
return value;
|
|
16
15
|
};
|