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
|
@@ -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
|
};
|
|
@@ -9,6 +9,7 @@ function _export(target, all) {
|
|
|
9
9
|
});
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
|
+
wellFormedNumberMatcher: ()=>wellFormedNumberMatcher,
|
|
12
13
|
wellFormedIntegerMatcher: ()=>wellFormedIntegerMatcher,
|
|
13
14
|
isWellFormedInteger: ()=>isWellFormedInteger,
|
|
14
15
|
wellFormedNonNegativeIntegerMatcher: ()=>wellFormedNonNegativeIntegerMatcher,
|
|
@@ -18,20 +19,7 @@ _export(exports, {
|
|
|
18
19
|
tryParseWellFormedBigint: ()=>tryParseWellFormedBigint
|
|
19
20
|
});
|
|
20
21
|
const _errorsTs = require("./errors.js");
|
|
21
|
-
|
|
22
|
-
* The goal of the number literal and bigint literal regular expressions is to:
|
|
23
|
-
*
|
|
24
|
-
* 1. Ensure definitions form a bijection with the values they represent.
|
|
25
|
-
* 2. Attempt to mirror TypeScript's own format for stringification of numeric
|
|
26
|
-
* values such that the regex should match a given definition if any only if
|
|
27
|
-
* a precise literal type will be inferred (in TS4.8+).
|
|
28
|
-
*/ /**
|
|
29
|
-
* Matches a well-formatted numeric expression according to the following rules:
|
|
30
|
-
* 1. Must include an integer portion (i.e. '.321' must be written as '0.321')
|
|
31
|
-
* 2. The first digit of the value must not be 0, unless the entire integer portion is 0
|
|
32
|
-
* 3. If the value includes a decimal, its last digit may not be 0
|
|
33
|
-
* 4. The value may not be "-0"
|
|
34
|
-
*/ const wellFormedNumberMatcher = /^(?!^-0$)-?(?:0|[1-9]\d*)(?:\.\d*[1-9])?$/;
|
|
22
|
+
const wellFormedNumberMatcher = /^(?!^-0$)-?(?:0|[1-9]\d*)(?:\.\d*[1-9])?$/;
|
|
35
23
|
const isWellFormedNumber = (s)=>wellFormedNumberMatcher.test(s);
|
|
36
24
|
const numberLikeMatcher = /^-?\d*\.?\d*$/;
|
|
37
25
|
const isNumberLike = (s)=>s.length !== 0 && numberLikeMatcher.test(s);
|
|
@@ -16,8 +16,8 @@ _export(exports, {
|
|
|
16
16
|
deserializePrimitive: ()=>deserializePrimitive
|
|
17
17
|
});
|
|
18
18
|
const _domainsTs = require("./domains.js");
|
|
19
|
-
const
|
|
20
|
-
const
|
|
19
|
+
const _genericsTs = require("./generics.js");
|
|
20
|
+
const _numericLiteralsTs = require("./numericLiterals.js");
|
|
21
21
|
const snapshot = (data, opts = {})=>snapshotRecurse(data, opts, []);
|
|
22
22
|
const snapshotRecurse = (v, context, seen)=>{
|
|
23
23
|
switch((0, _domainsTs.domainOf)(v)){
|
|
@@ -89,7 +89,7 @@ const stringify = (data, indent)=>{
|
|
|
89
89
|
}
|
|
90
90
|
};
|
|
91
91
|
const serializePrimitive = (value)=>typeof value === "string" ? `'${value}'` : typeof value === "bigint" ? `${value}n` : `${value}`;
|
|
92
|
-
const deserializePrimitive = (serialized)=>(0,
|
|
92
|
+
const deserializePrimitive = (serialized)=>(0, _genericsTs.isKeyOf)(serialized, serializedKeywords) ? serializedKeywords[serialized] : serialized[0] === "'" ? serialized.slice(1, -1) : (0, _numericLiteralsTs.tryParseWellFormedBigint)(serialized) ?? (0, _numericLiteralsTs.tryParseWellFormedNumber)(serialized, true);
|
|
93
93
|
const serializedKeywords = {
|
|
94
94
|
true: true,
|
|
95
95
|
false: false,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { writeUndiscriminatableMorphUnionMessage } from "../parse/ast/union.js";
|
|
2
2
|
import { domainOf } from "../utils/domains.js";
|
|
3
|
-
import { throwParseError } from "../utils/errors.js";
|
|
4
|
-
import {
|
|
3
|
+
import { throwInternalError, throwParseError } from "../utils/errors.js";
|
|
4
|
+
import { isKeyOf, keyCount, objectKeysOf } from "../utils/generics.js";
|
|
5
5
|
import { isArray, objectKindOf } from "../utils/objectKinds.js";
|
|
6
6
|
import { Path } from "../utils/paths.js";
|
|
7
7
|
import { serializePrimitive } from "../utils/serialize.js";
|
|
@@ -27,10 +27,12 @@ const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
|
|
|
27
27
|
];
|
|
28
28
|
}
|
|
29
29
|
const cases = {};
|
|
30
|
-
|
|
31
|
-
for(caseKey in bestDiscriminant.indexCases){
|
|
30
|
+
for(const caseKey in bestDiscriminant.indexCases){
|
|
32
31
|
const nextIndices = bestDiscriminant.indexCases[caseKey];
|
|
33
32
|
cases[caseKey] = discriminate(originalBranches, nextIndices, discriminants, ctx);
|
|
33
|
+
if (caseKey !== "default") {
|
|
34
|
+
pruneDiscriminant(cases[caseKey], bestDiscriminant.path, bestDiscriminant, ctx);
|
|
35
|
+
}
|
|
34
36
|
}
|
|
35
37
|
return [
|
|
36
38
|
[
|
|
@@ -43,9 +45,63 @@ const discriminate = (originalBranches, remainingIndices, discriminants, ctx)=>{
|
|
|
43
45
|
]
|
|
44
46
|
];
|
|
45
47
|
};
|
|
48
|
+
const pruneDiscriminant = (entries, segments, discriminant, ctx)=>{
|
|
49
|
+
for(let i = 0; i < entries.length; i++){
|
|
50
|
+
const [k, v] = entries[i];
|
|
51
|
+
if (!segments.length) {
|
|
52
|
+
if (discriminant.kind === "domain") {
|
|
53
|
+
if (k === "domain" || k === "domains") {
|
|
54
|
+
entries.splice(i, 1);
|
|
55
|
+
return;
|
|
56
|
+
} else if (k === "class" || k === "value") {
|
|
57
|
+
// these keys imply a domain, but also add additional
|
|
58
|
+
// information, so can't be pruned
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
} else if (discriminant.kind === k) {
|
|
62
|
+
entries.splice(i, 1);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
} else if ((k === "requiredProp" || k === "prerequisiteProp" || k === "optionalProp") && v[0] === segments[0]) {
|
|
66
|
+
if (typeof v[1] === "string") {
|
|
67
|
+
if (discriminant.kind !== "domain") {
|
|
68
|
+
return throwPruneFailure(discriminant);
|
|
69
|
+
}
|
|
70
|
+
entries.splice(i, 1);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
pruneDiscriminant(v[1], segments.slice(1), discriminant, ctx);
|
|
74
|
+
if (v[1].length === 0) {
|
|
75
|
+
entries.splice(i, 1);
|
|
76
|
+
}
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
// check for branch keys, which must be traversed even if there are no
|
|
80
|
+
// segments left
|
|
81
|
+
if (k === "domains") {
|
|
82
|
+
if (keyCount(v) !== 1 || !v.object) {
|
|
83
|
+
return throwPruneFailure(discriminant);
|
|
84
|
+
}
|
|
85
|
+
pruneDiscriminant(v.object, segments, discriminant, ctx);
|
|
86
|
+
return;
|
|
87
|
+
} else if (k === "switch") {
|
|
88
|
+
for(const caseKey in v.cases){
|
|
89
|
+
pruneDiscriminant(v.cases[caseKey], segments, discriminant, ctx);
|
|
90
|
+
}
|
|
91
|
+
return;
|
|
92
|
+
} else if (k === "branches") {
|
|
93
|
+
for (const branch of v){
|
|
94
|
+
pruneDiscriminant(branch, segments, discriminant, ctx);
|
|
95
|
+
}
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return throwPruneFailure(discriminant);
|
|
100
|
+
};
|
|
101
|
+
const throwPruneFailure = (discriminant)=>throwInternalError(`Unexpectedly failed to discriminate ${discriminant.kind} at path '${discriminant.path}'`);
|
|
46
102
|
const discriminantKinds = {
|
|
47
103
|
domain: true,
|
|
48
|
-
|
|
104
|
+
class: true,
|
|
49
105
|
value: true
|
|
50
106
|
};
|
|
51
107
|
const calculateDiscriminants = (branches, ctx)=>{
|
|
@@ -68,8 +124,8 @@ const calculateDiscriminants = (branches, ctx)=>{
|
|
|
68
124
|
if (!isKeyOf(kind, discriminantKinds)) {
|
|
69
125
|
continue;
|
|
70
126
|
}
|
|
71
|
-
const lSerialized =
|
|
72
|
-
const rSerialized =
|
|
127
|
+
const lSerialized = serializeDefinitionIfAllowed(kind, l);
|
|
128
|
+
const rSerialized = serializeDefinitionIfAllowed(kind, r);
|
|
73
129
|
if (lSerialized === undefined || rSerialized === undefined) {
|
|
74
130
|
continue;
|
|
75
131
|
}
|
|
@@ -87,19 +143,21 @@ const calculateDiscriminants = (branches, ctx)=>{
|
|
|
87
143
|
continue;
|
|
88
144
|
}
|
|
89
145
|
const cases = discriminants.casesByDisjoint[qualifiedDisjoint];
|
|
90
|
-
|
|
146
|
+
const existingLBranch = cases[lSerialized];
|
|
147
|
+
if (!existingLBranch) {
|
|
91
148
|
cases[lSerialized] = [
|
|
92
149
|
lIndex
|
|
93
150
|
];
|
|
94
|
-
} else if (!
|
|
95
|
-
|
|
151
|
+
} else if (!existingLBranch.includes(lIndex)) {
|
|
152
|
+
existingLBranch.push(lIndex);
|
|
96
153
|
}
|
|
97
|
-
|
|
154
|
+
const existingRBranch = cases[rSerialized];
|
|
155
|
+
if (!existingRBranch) {
|
|
98
156
|
cases[rSerialized] = [
|
|
99
157
|
rIndex
|
|
100
158
|
];
|
|
101
|
-
} else if (!
|
|
102
|
-
|
|
159
|
+
} else if (!existingRBranch.includes(rIndex)) {
|
|
160
|
+
existingRBranch.push(rIndex);
|
|
103
161
|
}
|
|
104
162
|
}
|
|
105
163
|
}
|
|
@@ -127,8 +185,7 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
|
|
|
127
185
|
...remainingIndices
|
|
128
186
|
];
|
|
129
187
|
let score = 0;
|
|
130
|
-
|
|
131
|
-
for(caseKey in indexCases){
|
|
188
|
+
for(const caseKey in indexCases){
|
|
132
189
|
const filteredIndices = indexCases[caseKey].filter((i)=>{
|
|
133
190
|
const remainingIndex = remainingIndices.indexOf(i);
|
|
134
191
|
if (remainingIndex !== -1) {
|
|
@@ -164,16 +221,27 @@ const findBestDiscriminant = (remainingIndices, discriminants)=>{
|
|
|
164
221
|
}
|
|
165
222
|
return bestDiscriminant;
|
|
166
223
|
};
|
|
167
|
-
export const
|
|
224
|
+
export const serializeDefinitionIfAllowed = (kind, definition)=>{
|
|
225
|
+
switch(kind){
|
|
226
|
+
case "value":
|
|
227
|
+
return serializeIfPrimitive(definition);
|
|
228
|
+
case "domain":
|
|
229
|
+
return definition;
|
|
230
|
+
case "class":
|
|
231
|
+
return typeof definition === "string" ? definition : undefined;
|
|
232
|
+
default:
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
};
|
|
168
236
|
const serializeIfPrimitive = (data)=>{
|
|
169
237
|
const domain = domainOf(data);
|
|
170
238
|
return domain === "object" || domain === "symbol" ? undefined : serializePrimitive(data);
|
|
171
239
|
};
|
|
172
|
-
const
|
|
240
|
+
const serializeData = {
|
|
173
241
|
value: (data)=>serializeIfPrimitive(data) ?? "default",
|
|
174
|
-
|
|
242
|
+
class: (data)=>objectKindOf(data) ?? "default",
|
|
175
243
|
domain: domainOf
|
|
176
244
|
};
|
|
177
|
-
export const serializeCase = (kind, data)=>
|
|
245
|
+
export const serializeCase = (kind, data)=>serializeData[kind](data);
|
|
178
246
|
const branchIncludesMorph = (branch, $)=>"morph" in branch ? true : "props" in branch ? Object.values(branch.props).some((prop)=>nodeIncludesMorph(propToNode(prop), $)) : false;
|
|
179
247
|
const nodeIncludesMorph = (node, $)=>typeof node === "string" ? $.resolve(node).meta.includesMorph : Object.values(node).some((predicate)=>predicate === true ? false : isArray(predicate) ? predicate.some((branch)=>branchIncludesMorph(branch, $)) : branchIncludesMorph(predicate, $));
|
|
@@ -7,7 +7,7 @@ export const classIntersection = composeIntersection((l, r, state)=>{
|
|
|
7
7
|
});
|
|
8
8
|
export const checkClass = (expectedClass, state)=>{
|
|
9
9
|
if (typeof expectedClass === "string") {
|
|
10
|
-
return objectKindOf(state.data) === expectedClass || state.problems.
|
|
10
|
+
return objectKindOf(state.data) === expectedClass || !state.problems.add("class", expectedClass);
|
|
11
11
|
}
|
|
12
|
-
return state.data instanceof expectedClass || state.problems.
|
|
12
|
+
return state.data instanceof expectedClass || !state.problems.add("class", expectedClass);
|
|
13
13
|
};
|
|
@@ -12,4 +12,4 @@ const greatestCommonDivisor = (l, r)=>{
|
|
|
12
12
|
}
|
|
13
13
|
return greatestCommonDivisor;
|
|
14
14
|
};
|
|
15
|
-
export const checkDivisor = (divisor, state)=>state.data % divisor === 0 || state.problems.
|
|
15
|
+
export const checkDivisor = (divisor, state)=>state.data % divisor === 0 || !state.problems.add("divisor", divisor);
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { hasKey } from "../../utils/generics.js";
|
|
2
1
|
import { composeIntersection, composeKeyedIntersection, equality, isDisjoint, isEquality } from "../compose.js";
|
|
3
2
|
import { flattenNode, isLiteralNode, nodeIntersection } from "../node.js";
|
|
4
3
|
export const isOptional = (prop)=>prop[0] === "?";
|
|
@@ -18,7 +17,7 @@ export const propsIntersection = composeIntersection((l, r, state)=>{
|
|
|
18
17
|
return result;
|
|
19
18
|
}
|
|
20
19
|
const lengthValue = getTupleLengthIfPresent(result);
|
|
21
|
-
if (lengthValue === undefined || !
|
|
20
|
+
if (lengthValue === undefined || !(mappedKeys.index in result)) {
|
|
22
21
|
return result;
|
|
23
22
|
}
|
|
24
23
|
// if we are at this point, we have an array with an exact length (i.e.
|
|
@@ -74,7 +74,7 @@ export const flattenRange = (entries, range, ctx)=>{
|
|
|
74
74
|
]);
|
|
75
75
|
}
|
|
76
76
|
};
|
|
77
|
-
export const checkBound = (bound, state)=>comparatorCheckers[bound.comparator](sizeOf(state.data), bound.limit) || state.problems.
|
|
77
|
+
export const checkBound = (bound, state)=>comparatorCheckers[bound.comparator](sizeOf(state.data), bound.limit) || !state.problems.add("bound", bound);
|
|
78
78
|
const comparatorCheckers = {
|
|
79
79
|
"<": (size, limit)=>size < limit,
|
|
80
80
|
">": (size, limit)=>size > limit,
|
|
@@ -7,5 +7,5 @@ export const getRegex = (source)=>{
|
|
|
7
7
|
}
|
|
8
8
|
return regexCache[source];
|
|
9
9
|
};
|
|
10
|
-
export const checkRegex = (source, state)=>getRegex(source).test(state.data) || state.problems.
|
|
10
|
+
export const checkRegex = (source, state)=>getRegex(source).test(state.data) || !state.problems.add("regex", `/${source}/`);
|
|
11
11
|
export const regexIntersection = composeIntersection(collapsibleListUnion);
|
|
@@ -40,4 +40,4 @@ const applyMorph = (branch, morph)=>isMorphBranch(branch) ? {
|
|
|
40
40
|
rules: branch,
|
|
41
41
|
morph
|
|
42
42
|
};
|
|
43
|
-
export const writeMalformedMorphExpressionMessage = (value)=>`Morph expression requires a function following '
|
|
43
|
+
export const writeMalformedMorphExpressionMessage = (value)=>`Morph expression requires a function following '|>' (was ${typeof value})`;
|
|
@@ -53,8 +53,9 @@ const indexOneParsers = {
|
|
|
53
53
|
"|": parseBranchTuple,
|
|
54
54
|
"&": parseBranchTuple,
|
|
55
55
|
"[]": parseArrayTuple,
|
|
56
|
-
"
|
|
57
|
-
"
|
|
56
|
+
"=>": parseNarrowTuple,
|
|
57
|
+
"|>": parseMorphTuple,
|
|
58
|
+
":": ()=>({})
|
|
58
59
|
};
|
|
59
60
|
const prefixParsers = {
|
|
60
61
|
keyof: parseKeyOfTuple,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { stringifyRange } from "../../../../nodes/compose.js";
|
|
2
2
|
import { compareStrictness, maxComparators, minComparators } from "../../../../nodes/rules/range.js";
|
|
3
3
|
import { throwInternalError } from "../../../../utils/errors.js";
|
|
4
|
-
import {
|
|
4
|
+
import { isKeyOf, listFrom, objectKeysOf } from "../../../../utils/generics.js";
|
|
5
5
|
import { tryParseWellFormedNumber } from "../../../../utils/numericLiterals.js";
|
|
6
6
|
import { writeUnboundableMessage } from "../../../ast/bound.js";
|
|
7
7
|
import { writeUnpairableComparatorMessage } from "../../reduce/shared.js";
|
|
@@ -54,7 +54,7 @@ const distributeRange = (range, s)=>{
|
|
|
54
54
|
if (resolution.object === true) {
|
|
55
55
|
return false;
|
|
56
56
|
}
|
|
57
|
-
return listFrom(resolution.object).every((branch)=>
|
|
57
|
+
return listFrom(resolution.object).every((branch)=>"class" in branch && branch.class === "Array");
|
|
58
58
|
default:
|
|
59
59
|
return false;
|
|
60
60
|
}
|
package/dist/mjs/scopes/scope.js
CHANGED
|
@@ -80,6 +80,7 @@ import { flattenType } from "../nodes/node.js";
|
|
|
80
80
|
import { parseDefinition } from "../parse/definition.js";
|
|
81
81
|
import { chainableNoOpProxy } from "../utils/chainableNoOpProxy.js";
|
|
82
82
|
import { throwInternalError, throwParseError } from "../utils/errors.js";
|
|
83
|
+
import { deepFreeze } from "../utils/freeze.js";
|
|
83
84
|
import { Path } from "../utils/paths.js";
|
|
84
85
|
import { Cache, FreezingCache } from "./cache.js";
|
|
85
86
|
import { initializeType } from "./type.js";
|
|
@@ -176,12 +177,12 @@ export class Scope {
|
|
|
176
177
|
], opts),
|
|
177
178
|
narrow: (def, fn, opts)=>this.type([
|
|
178
179
|
def,
|
|
179
|
-
"
|
|
180
|
+
"=>",
|
|
180
181
|
fn
|
|
181
182
|
], opts),
|
|
182
183
|
morph: (def, fn, opts)=>this.type([
|
|
183
184
|
def,
|
|
184
|
-
"
|
|
185
|
+
"|>",
|
|
185
186
|
fn
|
|
186
187
|
], opts)
|
|
187
188
|
};
|
|
@@ -268,8 +269,8 @@ function resolveRecurse(name, onUnresolvable, seen) {
|
|
|
268
269
|
seen.push(resolution);
|
|
269
270
|
resolution = _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, resolution, "throw", seen).node;
|
|
270
271
|
}
|
|
271
|
-
type.node = resolution;
|
|
272
|
-
type.flat = flattenType(type);
|
|
272
|
+
type.node = deepFreeze(resolution);
|
|
273
|
+
type.flat = deepFreeze(flattenType(type));
|
|
273
274
|
return type;
|
|
274
275
|
}
|
|
275
276
|
export const scope = (aliases, opts = {})=>new Scope(aliases, opts);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { wellFormedIntegerMatcher, wellFormedNumberMatcher } from "../utils/numericLiterals.js";
|
|
1
2
|
import { scope } from "./scope.js";
|
|
2
3
|
// https://github.com/validatorjs/validator.js
|
|
3
4
|
export const isLuhnValid = (creditCardInput)=>{
|
|
@@ -31,11 +32,25 @@ export const validationScope = scope({
|
|
|
31
32
|
lowercase: /^[a-z]*$/,
|
|
32
33
|
uppercase: /^[A-Z]*$/,
|
|
33
34
|
// https://github.com/validatorjs/validator.js
|
|
34
|
-
creditCard:
|
|
35
|
+
creditCard: [
|
|
36
|
+
/^(?: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}))$/,
|
|
37
|
+
"=>",
|
|
38
|
+
(s, problems)=>isLuhnValid(s) || !problems.add("mustBe", "a valid credit card number")
|
|
39
|
+
],
|
|
35
40
|
// https://www.regular-expressions.info
|
|
36
41
|
email: /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/,
|
|
37
42
|
// https://github.com/validatorjs/validator.js
|
|
38
43
|
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}$/,
|
|
44
|
+
parsedNumber: [
|
|
45
|
+
wellFormedNumberMatcher,
|
|
46
|
+
"|>",
|
|
47
|
+
(s)=>parseFloat(s)
|
|
48
|
+
],
|
|
49
|
+
parsedInteger: [
|
|
50
|
+
wellFormedIntegerMatcher,
|
|
51
|
+
"|>",
|
|
52
|
+
(s)=>parseInt(s)
|
|
53
|
+
],
|
|
39
54
|
integer: [
|
|
40
55
|
"node",
|
|
41
56
|
{
|