arktype 0.4.0 → 0.5.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.
- package/dist/cjs/nodes/branch.js +18 -18
- 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/cjs/utils/serialize.js +3 -3
- 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/divisor.d.ts +3 -3
- package/dist/types/parse/ast/morph.d.ts +8 -5
- package/dist/types/parse/ast/narrow.d.ts +8 -8
- 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/dist/types/utils/paths.d.ts +1 -1
- package/dist/types/utils/unionToTuple.d.ts +2 -2
- package/package.json +7 -7
package/dist/cjs/nodes/branch.js
CHANGED
|
@@ -15,11 +15,11 @@ _export(exports, {
|
|
|
15
15
|
flattenBranch: ()=>flattenBranch,
|
|
16
16
|
branchIntersection: ()=>branchIntersection
|
|
17
17
|
});
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
18
|
+
const _intersectionTs = require("../parse/ast/intersection.js");
|
|
19
|
+
const _domainsTs = require("../utils/domains.js");
|
|
20
|
+
const _errorsTs = require("../utils/errors.js");
|
|
21
|
+
const _composeTs = require("./compose.js");
|
|
22
|
+
const _rulesTs = require("./rules/rules.js");
|
|
23
23
|
const isBranchComparison = (comparison)=>comparison?.lBranches !== undefined;
|
|
24
24
|
const compareBranches = (lConditions, rConditions, state)=>{
|
|
25
25
|
const result = {
|
|
@@ -42,7 +42,7 @@ const compareBranches = (lConditions, rConditions, state)=>{
|
|
|
42
42
|
}
|
|
43
43
|
const r = rPairs.condition;
|
|
44
44
|
const subresult = branchIntersection(l, r, state);
|
|
45
|
-
if ((0,
|
|
45
|
+
if ((0, _composeTs.isDisjoint)(subresult)) {
|
|
46
46
|
// doesn't tell us about any redundancies or add a distinct pair
|
|
47
47
|
return null;
|
|
48
48
|
} else if (subresult === l) {
|
|
@@ -59,7 +59,7 @@ const compareBranches = (lConditions, rConditions, state)=>{
|
|
|
59
59
|
// distinct branches.
|
|
60
60
|
rPairs.distinct = null;
|
|
61
61
|
return null;
|
|
62
|
-
} else if ((0,
|
|
62
|
+
} else if ((0, _composeTs.isEquality)(subresult)) {
|
|
63
63
|
// Combination of l and r subtype cases.
|
|
64
64
|
result.equalities.push([
|
|
65
65
|
lIndex,
|
|
@@ -68,13 +68,13 @@ const compareBranches = (lConditions, rConditions, state)=>{
|
|
|
68
68
|
lImpliesR = true;
|
|
69
69
|
rPairs.distinct = null;
|
|
70
70
|
return null;
|
|
71
|
-
} else if ((0,
|
|
71
|
+
} else if ((0, _domainsTs.hasDomain)(subresult, "object")) {
|
|
72
72
|
// Neither branch is a subtype of the other, return
|
|
73
73
|
// the result of the intersection as a candidate
|
|
74
74
|
// branch for the final union
|
|
75
75
|
return subresult;
|
|
76
76
|
}
|
|
77
|
-
return (0,
|
|
77
|
+
return (0, _errorsTs.throwInternalError)(`Unexpected predicate intersection result of type '${(0, _domainsTs.domainOf)(subresult)}'`);
|
|
78
78
|
});
|
|
79
79
|
if (!lImpliesR) {
|
|
80
80
|
for(let i = 0; i < pairs.length; i++){
|
|
@@ -90,7 +90,7 @@ const compareBranches = (lConditions, rConditions, state)=>{
|
|
|
90
90
|
const isMorphBranch = (branch)=>"morph" in branch;
|
|
91
91
|
const flattenBranch = (branch, ctx)=>{
|
|
92
92
|
if (isMorphBranch(branch)) {
|
|
93
|
-
const result = (0,
|
|
93
|
+
const result = (0, _rulesTs.flattenRules)(branch.rules, ctx);
|
|
94
94
|
if (branch.morph) {
|
|
95
95
|
if (typeof branch.morph === "function") {
|
|
96
96
|
result.push([
|
|
@@ -108,31 +108,31 @@ const flattenBranch = (branch, ctx)=>{
|
|
|
108
108
|
}
|
|
109
109
|
return result;
|
|
110
110
|
}
|
|
111
|
-
return (0,
|
|
111
|
+
return (0, _rulesTs.flattenRules)(branch, ctx);
|
|
112
112
|
};
|
|
113
113
|
const rulesOf = (branch)=>branch.rules ?? branch;
|
|
114
114
|
const branchIntersection = (l, r, state)=>{
|
|
115
115
|
const lRules = rulesOf(l);
|
|
116
116
|
const rRules = rulesOf(r);
|
|
117
|
-
const rulesResult = (0,
|
|
117
|
+
const rulesResult = (0, _rulesTs.rulesIntersection)(lRules, rRules, state);
|
|
118
118
|
if ("morph" in l) {
|
|
119
119
|
if ("morph" in r) {
|
|
120
120
|
if (l.morph === r.morph) {
|
|
121
|
-
return (0,
|
|
121
|
+
return (0, _composeTs.isEquality)(rulesResult) || (0, _composeTs.isDisjoint)(rulesResult) ? rulesResult : {
|
|
122
122
|
rules: rulesResult,
|
|
123
123
|
morph: l.morph
|
|
124
124
|
};
|
|
125
125
|
}
|
|
126
|
-
return state.lastOperator === "&" ? (0,
|
|
126
|
+
return state.lastOperator === "&" ? (0, _errorsTs.throwParseError)((0, _intersectionTs.writeImplicitNeverMessage)(state.path, "Intersection", "of morphs")) : {};
|
|
127
127
|
}
|
|
128
|
-
return (0,
|
|
129
|
-
rules: (0,
|
|
128
|
+
return (0, _composeTs.isDisjoint)(rulesResult) ? rulesResult : {
|
|
129
|
+
rules: (0, _composeTs.isEquality)(rulesResult) ? l.rules : rulesResult,
|
|
130
130
|
morph: l.morph
|
|
131
131
|
};
|
|
132
132
|
}
|
|
133
133
|
if ("morph" in r) {
|
|
134
|
-
return (0,
|
|
135
|
-
rules: (0,
|
|
134
|
+
return (0, _composeTs.isDisjoint)(rulesResult) ? rulesResult : {
|
|
135
|
+
rules: (0, _composeTs.isEquality)(rulesResult) ? r.rules : rulesResult,
|
|
136
136
|
morph: r.morph
|
|
137
137
|
};
|
|
138
138
|
}
|
|
@@ -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
|
});
|