arktype 1.0.4-alpha → 1.0.6-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 +96 -18
- package/dist/cjs/nodes/compose.js +3 -3
- package/dist/cjs/nodes/discriminate.js +2 -1
- package/dist/cjs/nodes/node.js +3 -3
- package/dist/cjs/nodes/rules/class.js +1 -3
- package/dist/cjs/parse/ast/keyof.js +3 -2
- package/dist/cjs/parse/ast/tuple.js +1 -1
- package/dist/cjs/parse/definition.js +1 -1
- package/dist/cjs/parse/string/shift/operand/operand.js +1 -1
- package/dist/cjs/parse/string/shift/operator/bounds.js +1 -1
- package/dist/cjs/parse/string/shift/operator/operator.js +1 -1
- package/dist/cjs/scopes/jsObjects.js +14 -14
- package/dist/cjs/scopes/type.js +4 -12
- package/dist/cjs/traverse/problems.js +3 -1
- package/dist/cjs/traverse/traverse.js +53 -10
- package/dist/cjs/utils/generics.js +0 -5
- package/dist/mjs/nodes/compose.js +2 -2
- package/dist/mjs/nodes/discriminate.js +3 -2
- package/dist/mjs/nodes/node.js +5 -5
- package/dist/mjs/nodes/rules/class.js +2 -4
- package/dist/mjs/parse/ast/keyof.js +3 -2
- package/dist/mjs/parse/ast/tuple.js +1 -1
- package/dist/mjs/parse/definition.js +1 -1
- package/dist/mjs/parse/string/shift/operand/operand.js +1 -1
- package/dist/mjs/parse/string/shift/operator/bounds.js +1 -1
- package/dist/mjs/parse/string/shift/operator/operator.js +3 -1
- package/dist/mjs/scopes/jsObjects.js +14 -14
- package/dist/mjs/scopes/type.js +5 -12
- package/dist/mjs/traverse/problems.js +3 -1
- package/dist/mjs/traverse/traverse.js +52 -10
- package/dist/mjs/utils/generics.js +0 -4
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/main.d.ts +1 -0
- package/dist/types/nodes/compose.d.ts +1 -1
- package/dist/types/nodes/rules/class.d.ts +1 -1
- package/dist/types/nodes/rules/rules.d.ts +2 -3
- package/dist/types/parse/ast/morph.d.ts +2 -1
- package/dist/types/parse/ast/node.d.ts +5 -8
- package/dist/types/parse/definition.d.ts +5 -5
- package/dist/types/parse/string/shift/operand/operand.d.ts +1 -1
- package/dist/types/scopes/ark.d.ts +4 -12
- package/dist/types/scopes/jsObjects.d.ts +2 -6
- package/dist/types/scopes/type.d.ts +5 -10
- package/dist/types/traverse/problems.d.ts +2 -2
- package/dist/types/traverse/traverse.d.ts +8 -1
- package/dist/types/utils/generics.d.ts +2 -3
- package/dist/types/utils/numericLiterals.d.ts +2 -2
- package/package.json +11 -7
|
@@ -2,7 +2,7 @@ import { writeUndiscriminatableMorphUnionMessage } from "../parse/ast/union.js";
|
|
|
2
2
|
import { domainOf } from "../utils/domains.js";
|
|
3
3
|
import { throwInternalError, throwParseError } from "../utils/errors.js";
|
|
4
4
|
import { isKeyOf, keyCount, objectKeysOf } from "../utils/generics.js";
|
|
5
|
-
import { isArray, objectKindOf } from "../utils/objectKinds.js";
|
|
5
|
+
import { defaultObjectKinds, isArray, objectKindOf } from "../utils/objectKinds.js";
|
|
6
6
|
import { Path } from "../utils/paths.js";
|
|
7
7
|
import { serializePrimitive } from "../utils/serialize.js";
|
|
8
8
|
import { branchIntersection, flattenBranch } from "./branch.js";
|
|
@@ -228,7 +228,8 @@ export const serializeDefinitionIfAllowed = (kind, definition)=>{
|
|
|
228
228
|
case "domain":
|
|
229
229
|
return definition;
|
|
230
230
|
case "class":
|
|
231
|
-
|
|
231
|
+
const constructorName = Object(definition).name;
|
|
232
|
+
return constructorName && isKeyOf(constructorName, defaultObjectKinds) && defaultObjectKinds[constructorName] === definition ? constructorName : undefined;
|
|
232
233
|
default:
|
|
233
234
|
return;
|
|
234
235
|
}
|
package/dist/mjs/nodes/node.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { compileDisjointReasonsMessage } from "../parse/ast/intersection.js";
|
|
2
|
-
import { throwParseError } from "../utils/errors.js";
|
|
2
|
+
import { throwInternalError, throwParseError } from "../utils/errors.js";
|
|
3
3
|
import { entriesOf, hasKey, hasKeys, objectKeysOf } from "../utils/generics.js";
|
|
4
4
|
import { Path } from "../utils/paths.js";
|
|
5
|
-
import { anonymousDisjoint, composeKeyedIntersection, IntersectionState, isDisjoint, isEquality,
|
|
5
|
+
import { anonymousDisjoint, composeKeyedIntersection, IntersectionState, isDisjoint, isEquality, undefinedOperandsMessage } from "./compose.js";
|
|
6
6
|
import { flattenPredicate, isLiteralCondition, predicateIntersection, predicateUnion } from "./predicate.js";
|
|
7
7
|
import { mappedKeys } from "./rules/props.js";
|
|
8
8
|
export const isConfigNode = (node)=>"config" in node;
|
|
@@ -18,7 +18,7 @@ export const nodeIntersection = (l, r, state)=>{
|
|
|
18
18
|
};
|
|
19
19
|
const typeNodeIntersection = composeKeyedIntersection((domain, l, r, context)=>{
|
|
20
20
|
if (l === undefined) {
|
|
21
|
-
return r === undefined ?
|
|
21
|
+
return r === undefined ? throwInternalError(undefinedOperandsMessage) : undefined;
|
|
22
22
|
}
|
|
23
23
|
if (r === undefined) {
|
|
24
24
|
return undefined;
|
|
@@ -41,7 +41,7 @@ export const rootUnion = (l, r, type)=>{
|
|
|
41
41
|
...rDomains
|
|
42
42
|
});
|
|
43
43
|
for (const domain of domains){
|
|
44
|
-
result[domain] = hasKey(lDomains, domain) ? hasKey(rDomains, domain) ? predicateUnion(domain, lDomains[domain], rDomains[domain], type) : lDomains[domain] : hasKey(rDomains, domain) ? rDomains[domain] :
|
|
44
|
+
result[domain] = hasKey(lDomains, domain) ? hasKey(rDomains, domain) ? predicateUnion(domain, lDomains[domain], rDomains[domain], type) : lDomains[domain] : hasKey(rDomains, domain) ? rDomains[domain] : throwInternalError(undefinedOperandsMessage);
|
|
45
45
|
}
|
|
46
46
|
return result;
|
|
47
47
|
};
|
|
@@ -109,7 +109,7 @@ export const resolutionExtendsDomain = (resolution, domain)=>{
|
|
|
109
109
|
};
|
|
110
110
|
export const toArrayNode = (node)=>({
|
|
111
111
|
object: {
|
|
112
|
-
class:
|
|
112
|
+
class: Array,
|
|
113
113
|
props: {
|
|
114
114
|
[mappedKeys.index]: node
|
|
115
115
|
}
|
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { objectKindOf } from "../../utils/objectKinds.js";
|
|
2
2
|
import { composeIntersection, equality } from "../compose.js";
|
|
3
3
|
export const classIntersection = composeIntersection((l, r, state)=>{
|
|
4
|
-
|
|
5
|
-
const rClass = typeof r === "string" ? defaultObjectKinds[r] : r;
|
|
6
|
-
return lClass === rClass ? equality() : lClass instanceof rClass ? l : rClass instanceof lClass ? r : state.addDisjoint("class", l, r);
|
|
4
|
+
return l === r ? equality() : l instanceof r ? l : r instanceof l ? r : state.addDisjoint("class", l, r);
|
|
7
5
|
});
|
|
8
6
|
export const checkClass = (expectedClass, state)=>{
|
|
9
7
|
if (typeof expectedClass === "string") {
|
|
@@ -15,7 +15,7 @@ const arrayIndexNumberBranch = deepFreeze({
|
|
|
15
15
|
range: {
|
|
16
16
|
min: {
|
|
17
17
|
comparator: ">=",
|
|
18
|
-
limit:
|
|
18
|
+
limit: 0
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
21
|
divisor: 1
|
|
@@ -43,7 +43,7 @@ export const parseKeyOfTuple = (def, ctx)=>{
|
|
|
43
43
|
} else {
|
|
44
44
|
return throwInternalError(`Unexpected keyof key '${stringify(key)}'`);
|
|
45
45
|
}
|
|
46
|
-
}
|
|
46
|
+
/* c8 ignore stop*/ }
|
|
47
47
|
return Object.fromEntries(Object.entries(keyNode).map(([domain, branches])=>[
|
|
48
48
|
domain,
|
|
49
49
|
branches.length === 1 ? branches[0] : branches
|
|
@@ -79,6 +79,7 @@ const keysOfObjectBranch = (branch)=>{
|
|
|
79
79
|
if ("props" in branch) {
|
|
80
80
|
for (const key of Object.keys(branch.props)){
|
|
81
81
|
if (key === mappedKeys.index) {
|
|
82
|
+
// if any number is a valid key push this RegExp
|
|
82
83
|
result.push(wellFormedNonNegativeIntegerMatcher);
|
|
83
84
|
} else if (!result.includes(key)) {
|
|
84
85
|
result.push(key);
|
|
@@ -42,6 +42,6 @@ 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
|
|
45
|
+
export const unknownDefinitionMessage = "Cannot statically parse a definition inferred as unknown. Consider using 'as Infer<...>' to cast it.";
|
|
46
46
|
const isThunk = (def)=>typeof def === "function" && def.length === 0;
|
|
47
47
|
export const writeBadDefinitionTypeMessage = (actual)=>`Type definitions must be strings or objects (was ${actual})`;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { enclosingChar, parseEnclosed } from "./enclosed.js";
|
|
2
2
|
import { parseUnenclosed, writeMissingOperandMessage } from "./unenclosed.js";
|
|
3
|
-
export const parseOperand = (s)=>s.scanner.lookahead === "" ? s.error(writeMissingOperandMessage(s)) : s.scanner.lookahead === "(" ? s.shiftedByOne().reduceGroupOpen() : s.scanner.lookaheadIsIn(enclosingChar) ? parseEnclosed(s, s.scanner.shift()) : parseUnenclosed(s);
|
|
3
|
+
export const parseOperand = (s)=>s.scanner.lookahead === "" ? s.error(writeMissingOperandMessage(s)) : s.scanner.lookahead === "(" ? s.shiftedByOne().reduceGroupOpen() : s.scanner.lookaheadIsIn(enclosingChar) ? parseEnclosed(s, s.scanner.shift()) : s.scanner.lookahead === " " ? parseOperand(s.shiftedByOne()) : parseUnenclosed(s);
|
|
@@ -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)=>"class" in branch && branch.class ===
|
|
57
|
+
return listFrom(resolution.object).every((branch)=>"class" in branch && branch.class === Array);
|
|
58
58
|
default:
|
|
59
59
|
return false;
|
|
60
60
|
}
|
|
@@ -3,9 +3,11 @@ import { isKeyOf } from "../../../../utils/generics.js";
|
|
|
3
3
|
import { Scanner } from "../scanner.js";
|
|
4
4
|
import { parseBound } from "./bounds.js";
|
|
5
5
|
import { parseDivisor } from "./divisor.js";
|
|
6
|
+
// @snipStart:parseOperator
|
|
6
7
|
export const parseOperator = (s)=>{
|
|
7
8
|
const lookahead = s.scanner.shift();
|
|
8
|
-
return lookahead === "" ? s.finalize() : lookahead === "[" ? s.scanner.shift() === "]" ? s.rootToArray() : s.error(incompleteArrayTokenMessage) : isKeyOf(lookahead, Scanner.branchTokens) ? s.pushRootToBranch(lookahead) : lookahead === ")" ? s.finalizeGroup() : isKeyOf(lookahead, Scanner.comparatorStartChars) ? parseBound(s, lookahead) : lookahead === "%" ? parseDivisor(s) : lookahead === " " ? parseOperator(s) :
|
|
9
|
+
return lookahead === "" ? s.finalize() : lookahead === "[" ? s.scanner.shift() === "]" ? s.rootToArray() : s.error(incompleteArrayTokenMessage) : isKeyOf(lookahead, Scanner.branchTokens) ? s.pushRootToBranch(lookahead) : lookahead === ")" ? s.finalizeGroup() : isKeyOf(lookahead, Scanner.comparatorStartChars) ? parseBound(s, lookahead) : lookahead === "%" ? parseDivisor(s) : lookahead === " " ? parseOperator(s) : throwInternalError(writeUnexpectedCharacterMessage(lookahead));
|
|
9
10
|
};
|
|
11
|
+
// @snipEnd
|
|
10
12
|
export const writeUnexpectedCharacterMessage = (char)=>`Unexpected character '${char}'`;
|
|
11
13
|
export const incompleteArrayTokenMessage = `Missing expected ']'`;
|
|
@@ -4,7 +4,7 @@ export const jsObjectsScope = scope({
|
|
|
4
4
|
"node",
|
|
5
5
|
{
|
|
6
6
|
object: {
|
|
7
|
-
class:
|
|
7
|
+
class: Function
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
10
|
],
|
|
@@ -12,7 +12,7 @@ export const jsObjectsScope = scope({
|
|
|
12
12
|
"node",
|
|
13
13
|
{
|
|
14
14
|
object: {
|
|
15
|
-
class:
|
|
15
|
+
class: Array
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
],
|
|
@@ -20,7 +20,7 @@ export const jsObjectsScope = scope({
|
|
|
20
20
|
"node",
|
|
21
21
|
{
|
|
22
22
|
object: {
|
|
23
|
-
class:
|
|
23
|
+
class: Date
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
],
|
|
@@ -28,7 +28,7 @@ export const jsObjectsScope = scope({
|
|
|
28
28
|
"node",
|
|
29
29
|
{
|
|
30
30
|
object: {
|
|
31
|
-
class:
|
|
31
|
+
class: Error
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
],
|
|
@@ -36,7 +36,7 @@ export const jsObjectsScope = scope({
|
|
|
36
36
|
"node",
|
|
37
37
|
{
|
|
38
38
|
object: {
|
|
39
|
-
class:
|
|
39
|
+
class: Map
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
],
|
|
@@ -44,7 +44,7 @@ export const jsObjectsScope = scope({
|
|
|
44
44
|
"node",
|
|
45
45
|
{
|
|
46
46
|
object: {
|
|
47
|
-
class:
|
|
47
|
+
class: RegExp
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
],
|
|
@@ -52,7 +52,7 @@ export const jsObjectsScope = scope({
|
|
|
52
52
|
"node",
|
|
53
53
|
{
|
|
54
54
|
object: {
|
|
55
|
-
class:
|
|
55
|
+
class: Set
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
],
|
|
@@ -60,7 +60,7 @@ export const jsObjectsScope = scope({
|
|
|
60
60
|
"node",
|
|
61
61
|
{
|
|
62
62
|
object: {
|
|
63
|
-
class:
|
|
63
|
+
class: Object
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
],
|
|
@@ -68,7 +68,7 @@ export const jsObjectsScope = scope({
|
|
|
68
68
|
"node",
|
|
69
69
|
{
|
|
70
70
|
object: {
|
|
71
|
-
class:
|
|
71
|
+
class: String
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
],
|
|
@@ -76,7 +76,7 @@ export const jsObjectsScope = scope({
|
|
|
76
76
|
"node",
|
|
77
77
|
{
|
|
78
78
|
object: {
|
|
79
|
-
class:
|
|
79
|
+
class: Number
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
],
|
|
@@ -84,7 +84,7 @@ export const jsObjectsScope = scope({
|
|
|
84
84
|
"node",
|
|
85
85
|
{
|
|
86
86
|
object: {
|
|
87
|
-
class:
|
|
87
|
+
class: Boolean
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
90
|
],
|
|
@@ -92,7 +92,7 @@ export const jsObjectsScope = scope({
|
|
|
92
92
|
"node",
|
|
93
93
|
{
|
|
94
94
|
object: {
|
|
95
|
-
class:
|
|
95
|
+
class: WeakMap
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
],
|
|
@@ -100,7 +100,7 @@ export const jsObjectsScope = scope({
|
|
|
100
100
|
"node",
|
|
101
101
|
{
|
|
102
102
|
object: {
|
|
103
|
-
class:
|
|
103
|
+
class: WeakSet
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
106
|
],
|
|
@@ -108,7 +108,7 @@ export const jsObjectsScope = scope({
|
|
|
108
108
|
"node",
|
|
109
109
|
{
|
|
110
110
|
object: {
|
|
111
|
-
class:
|
|
111
|
+
class: Promise
|
|
112
112
|
}
|
|
113
113
|
}
|
|
114
114
|
]
|
package/dist/mjs/scopes/type.js
CHANGED
|
@@ -1,16 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { traverseRoot } from "../traverse/traverse.js";
|
|
2
2
|
import { chainableNoOpProxy } from "../utils/chainableNoOpProxy.js";
|
|
3
|
-
import { composeNamed, getNominalId } from "../utils/generics.js";
|
|
4
3
|
export const initializeType = (name, definition, config, scope)=>{
|
|
5
|
-
const namedTraverse = composeNamed((data)=>{
|
|
6
|
-
const state = new TraversalState(data, t);
|
|
7
|
-
traverse(t.flat, state);
|
|
8
|
-
return state.problems.count ? {
|
|
9
|
-
problems: state.problems
|
|
10
|
-
} : {
|
|
11
|
-
data: state.data
|
|
12
|
-
};
|
|
13
|
-
}, name);
|
|
14
4
|
const root = {
|
|
15
5
|
// temporarily initialize node/flat to aliases that will be included in
|
|
16
6
|
// the final type in case of cyclic resolutions
|
|
@@ -29,9 +19,12 @@ export const initializeType = (name, definition, config, scope)=>{
|
|
|
29
19
|
includesMorph: false,
|
|
30
20
|
config
|
|
31
21
|
};
|
|
22
|
+
// define within a key to dynamically assign a name to the function
|
|
23
|
+
const namedTraverse = {
|
|
24
|
+
[name]: (data)=>traverseRoot(namedTraverse, data)
|
|
25
|
+
}[name];
|
|
32
26
|
const t = Object.assign(namedTraverse, root);
|
|
33
27
|
return t;
|
|
34
28
|
};
|
|
35
29
|
export const isType = (value)=>value?.infer === chainableNoOpProxy;
|
|
36
|
-
export const isCheckResult = (value)=>getNominalId(value) !== undefined;
|
|
37
30
|
export const isAnonymousName = (name)=>name[0] === "λ";
|
|
@@ -87,7 +87,9 @@ class ProblemArray extends Array {
|
|
|
87
87
|
const data = // we have to check for the presence of the key explicitly since the
|
|
88
88
|
// data could be undefined or null
|
|
89
89
|
opts && "data" in opts ? opts.data : _classPrivateFieldGet(this, _state).data;
|
|
90
|
-
const problem = new Problem(
|
|
90
|
+
const problem = new Problem(// avoid a bunch of errors from TS trying to discriminate the
|
|
91
|
+
// problem input based on the code
|
|
92
|
+
code, path, data, source, _classPrivateFieldGet(this, _state).getProblemConfig(code));
|
|
91
93
|
this.addProblem(problem);
|
|
92
94
|
return problem;
|
|
93
95
|
}
|
|
@@ -44,7 +44,6 @@ import { checkDivisor } from "../nodes/rules/divisor.js";
|
|
|
44
44
|
import { checkBound } from "../nodes/rules/range.js";
|
|
45
45
|
import { checkRegex } from "../nodes/rules/regex.js";
|
|
46
46
|
import { precedenceMap } from "../nodes/rules/rules.js";
|
|
47
|
-
import { isCheckResult } from "../scopes/type.js";
|
|
48
47
|
import { domainOf, hasDomain } from "../utils/domains.js";
|
|
49
48
|
import { throwInternalError } from "../utils/errors.js";
|
|
50
49
|
import { hasKey, objectKeysOf } from "../utils/generics.js";
|
|
@@ -61,6 +60,22 @@ const problemWriterKeys = [
|
|
|
61
60
|
"writeReason",
|
|
62
61
|
"addContext"
|
|
63
62
|
];
|
|
63
|
+
export const traverseRoot = (t, data)=>{
|
|
64
|
+
const state = new TraversalState(data, t);
|
|
65
|
+
traverse(t.flat, state);
|
|
66
|
+
const result = new CheckResult(state);
|
|
67
|
+
if (state.problems.count) {
|
|
68
|
+
result.problems = state.problems;
|
|
69
|
+
} else {
|
|
70
|
+
for (const [o, k] of state.entriesToPrune){
|
|
71
|
+
delete o[k];
|
|
72
|
+
}
|
|
73
|
+
result.data = state.data;
|
|
74
|
+
}
|
|
75
|
+
return result;
|
|
76
|
+
};
|
|
77
|
+
const CheckResult = class {
|
|
78
|
+
};
|
|
64
79
|
var _seen = /*#__PURE__*/ new WeakMap();
|
|
65
80
|
export class TraversalState {
|
|
66
81
|
getProblemConfig(code) {
|
|
@@ -133,15 +148,19 @@ export class TraversalState {
|
|
|
133
148
|
const branchProblems = new Problems(this);
|
|
134
149
|
this.problems = branchProblems;
|
|
135
150
|
const lastPath = this.path;
|
|
151
|
+
const lastKeysToPrune = this.entriesToPrune;
|
|
136
152
|
let hasValidBranch = false;
|
|
137
153
|
for (const branch of branches){
|
|
138
154
|
this.path = new Path();
|
|
155
|
+
this.entriesToPrune = [];
|
|
139
156
|
if (checkEntries(branch, this)) {
|
|
140
157
|
hasValidBranch = true;
|
|
158
|
+
lastKeysToPrune.push(...this.entriesToPrune);
|
|
141
159
|
break;
|
|
142
160
|
}
|
|
143
161
|
}
|
|
144
162
|
this.path = lastPath;
|
|
163
|
+
this.entriesToPrune = lastKeysToPrune;
|
|
145
164
|
this.problems = lastProblems;
|
|
146
165
|
this.failFast = lastFailFast;
|
|
147
166
|
return hasValidBranch || !this.problems.add("branches", branchProblems);
|
|
@@ -155,6 +174,7 @@ export class TraversalState {
|
|
|
155
174
|
this.type = type;
|
|
156
175
|
this.path = new Path();
|
|
157
176
|
this.problems = new Problems(this);
|
|
177
|
+
this.entriesToPrune = [];
|
|
158
178
|
this.failFast = false;
|
|
159
179
|
this.traversalConfig = initializeTraversalConfig();
|
|
160
180
|
_classPrivateFieldSet(this, _seen, {});
|
|
@@ -194,27 +214,49 @@ export const checkRequiredProp = (prop, state)=>{
|
|
|
194
214
|
};
|
|
195
215
|
const createPropChecker = (kind)=>(props, state)=>{
|
|
196
216
|
let isValid = true;
|
|
197
|
-
const
|
|
217
|
+
const remainingUnseenRequired = {
|
|
198
218
|
...props.required
|
|
199
219
|
};
|
|
200
220
|
for(const k in state.data){
|
|
201
221
|
if (props.required[k]) {
|
|
202
|
-
isValid
|
|
203
|
-
delete
|
|
222
|
+
isValid && (isValid = state.traverseKey(k, props.required[k]));
|
|
223
|
+
delete remainingUnseenRequired[k];
|
|
204
224
|
} else if (props.optional[k]) {
|
|
205
|
-
isValid
|
|
225
|
+
isValid && (isValid = state.traverseKey(k, props.optional[k]));
|
|
206
226
|
} else if (kind === "distilledProps") {
|
|
207
|
-
|
|
227
|
+
if (state.failFast) {
|
|
228
|
+
// If we're in a union (i.e. failFast is enabled) in
|
|
229
|
+
// distilled mode, we need to wait to prune distilled keys
|
|
230
|
+
// to avoid mutating data based on a branch that will not be
|
|
231
|
+
// included in the final result. Instead, we push the object
|
|
232
|
+
// and key to state to handle after traversal is complete.
|
|
233
|
+
state.entriesToPrune.push([
|
|
234
|
+
state.data,
|
|
235
|
+
k
|
|
236
|
+
]);
|
|
237
|
+
} else {
|
|
238
|
+
// If we're not in a union, we can safely distill right away
|
|
239
|
+
delete state.data[k];
|
|
240
|
+
}
|
|
208
241
|
} else {
|
|
242
|
+
isValid = false;
|
|
209
243
|
state.problems.add("extraneous", state.data[k], {
|
|
210
244
|
path: state.path.concat(k)
|
|
211
245
|
});
|
|
212
|
-
isValid = false;
|
|
213
246
|
}
|
|
214
247
|
if (!isValid && state.failFast) {
|
|
215
248
|
return false;
|
|
216
249
|
}
|
|
217
250
|
}
|
|
251
|
+
const unseenRequired = Object.keys(remainingUnseenRequired);
|
|
252
|
+
if (unseenRequired.length) {
|
|
253
|
+
for (const k of unseenRequired){
|
|
254
|
+
state.problems.add("missing", undefined, {
|
|
255
|
+
path: state.path.concat(k)
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
218
260
|
return isValid;
|
|
219
261
|
};
|
|
220
262
|
const entryCheckers = {
|
|
@@ -259,8 +301,8 @@ const entryCheckers = {
|
|
|
259
301
|
}
|
|
260
302
|
const caseKeys = objectKeysOf(rule.cases);
|
|
261
303
|
const missingCasePath = state.path.concat(rule.path);
|
|
262
|
-
const caseDescriptions = rule.kind === "value" ? caseKeys : rule.kind === "domain" ? domainsToDescriptions(caseKeys) : rule.kind === "class" ? objectKindsToDescriptions(caseKeys) : throwInternalError(`Unexpectedly encountered rule kind '${rule.kind}' during traversal`);
|
|
263
|
-
state.problems.add("cases", caseDescriptions, {
|
|
304
|
+
const caseDescriptions = rule.kind === "value" ? caseKeys : rule.kind === "domain" ? domainsToDescriptions(caseKeys) : rule.kind === "class" ? objectKindsToDescriptions(caseKeys) : /* c8 ignore start*/ throwInternalError(`Unexpectedly encountered rule kind '${rule.kind}' during traversal`);
|
|
305
|
+
/* c8 ignore stop*/ state.problems.add("cases", caseDescriptions, {
|
|
264
306
|
path: missingCasePath,
|
|
265
307
|
data: dataAtPath
|
|
266
308
|
});
|
|
@@ -288,7 +330,7 @@ const entryCheckers = {
|
|
|
288
330
|
state.problems.addProblem(out);
|
|
289
331
|
return false;
|
|
290
332
|
}
|
|
291
|
-
if (
|
|
333
|
+
if (out instanceof CheckResult) {
|
|
292
334
|
if (out.problems) {
|
|
293
335
|
for (const problem of out.problems){
|
|
294
336
|
state.problems.addProblem(problem);
|
|
@@ -31,10 +31,6 @@ const id = Symbol("id");
|
|
|
31
31
|
export const nominal = (o, name)=>Object.assign(o, {
|
|
32
32
|
[id]: name
|
|
33
33
|
});
|
|
34
|
-
export const composeNamed = (f, name)=>// define within a key to dynamically assign a name to the function
|
|
35
|
-
({
|
|
36
|
-
[name]: (...args)=>nominal(f(...args), name)
|
|
37
|
-
})[name];
|
|
38
34
|
export const getNominalId = (data)=>hasDomain(data, "object") && id in data ? data[id] : undefined;
|
|
39
35
|
export const hasNominalId = (data, name)=>getNominalId(data) === name;
|
|
40
36
|
export const listFrom = (data)=>Array.isArray(data) ? data : [
|