arktype 0.2.0 → 0.3.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/{branches.js → branch.js} +56 -10
- package/dist/cjs/nodes/compose.js +1 -2
- package/dist/cjs/nodes/discriminate.js +7 -13
- package/dist/cjs/nodes/node.js +12 -2
- package/dist/cjs/nodes/predicate.js +7 -9
- package/dist/cjs/nodes/rules/class.js +10 -4
- package/dist/cjs/nodes/rules/divisor.js +1 -5
- package/dist/cjs/nodes/rules/props.js +75 -25
- package/dist/cjs/nodes/rules/range.js +1 -5
- package/dist/cjs/nodes/rules/regex.js +1 -5
- package/dist/cjs/nodes/rules/rules.js +19 -63
- package/dist/cjs/parse/string/reduce/dynamic.js +1 -2
- package/dist/cjs/parse/string/shift/operator/bounds.js +1 -1
- package/dist/cjs/parse/string/string.js +2 -2
- package/dist/cjs/parse/tuple/keyof.js +37 -18
- package/dist/cjs/parse/tuple/morph.js +2 -2
- package/dist/cjs/parse/tuple/tuple.js +16 -9
- package/dist/cjs/scopes/jsObjects.js +14 -14
- package/dist/cjs/scopes/type.js +4 -7
- package/dist/cjs/traverse/problems.js +12 -28
- package/dist/cjs/traverse/traverse.js +104 -133
- package/dist/cjs/utils/numericLiterals.js +2 -0
- package/dist/cjs/utils/objectKinds.js +1 -2
- package/dist/mjs/nodes/{branches.js → branch.js} +45 -2
- package/dist/mjs/nodes/compose.js +1 -2
- package/dist/mjs/nodes/discriminate.js +6 -12
- package/dist/mjs/nodes/node.js +10 -1
- package/dist/mjs/nodes/predicate.js +2 -3
- package/dist/mjs/nodes/rules/class.js +10 -4
- package/dist/mjs/nodes/rules/divisor.js +1 -5
- package/dist/mjs/nodes/rules/props.js +74 -27
- package/dist/mjs/nodes/rules/range.js +1 -5
- package/dist/mjs/nodes/rules/regex.js +1 -5
- package/dist/mjs/nodes/rules/rules.js +19 -62
- package/dist/mjs/parse/string/reduce/dynamic.js +2 -3
- package/dist/mjs/parse/string/shift/operator/bounds.js +1 -1
- package/dist/mjs/parse/string/string.js +2 -2
- package/dist/mjs/parse/tuple/keyof.js +38 -19
- package/dist/mjs/parse/tuple/morph.js +2 -2
- package/dist/mjs/parse/tuple/tuple.js +17 -10
- package/dist/mjs/scopes/jsObjects.js +14 -14
- package/dist/mjs/scopes/type.js +4 -7
- package/dist/mjs/traverse/problems.js +13 -29
- package/dist/mjs/traverse/traverse.js +105 -133
- package/dist/mjs/utils/numericLiterals.js +1 -0
- package/dist/mjs/utils/objectKinds.js +1 -1
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/nodes/branch.d.ts +36 -0
- package/dist/types/nodes/compose.d.ts +5 -10
- package/dist/types/nodes/discriminate.d.ts +1 -1
- package/dist/types/nodes/infer.d.ts +17 -16
- package/dist/types/nodes/node.d.ts +10 -5
- package/dist/types/nodes/predicate.d.ts +4 -32
- package/dist/types/nodes/rules/class.d.ts +3 -2
- package/dist/types/nodes/rules/divisor.d.ts +2 -1
- package/dist/types/nodes/rules/props.d.ts +19 -11
- package/dist/types/nodes/rules/range.d.ts +3 -2
- package/dist/types/nodes/rules/regex.d.ts +2 -1
- package/dist/types/nodes/rules/rules.d.ts +11 -13
- package/dist/types/parse/string/ast.d.ts +2 -1
- package/dist/types/parse/string/reduce/dynamic.d.ts +1 -1
- package/dist/types/parse/string/reduce/shared.d.ts +1 -1
- package/dist/types/parse/string/shift/operand/unenclosed.d.ts +1 -1
- package/dist/types/parse/string/shift/operator/bounds.d.ts +1 -1
- package/dist/types/parse/string/string.d.ts +1 -1
- package/dist/types/parse/tuple/keyof.d.ts +2 -2
- package/dist/types/scopes/type.d.ts +5 -11
- package/dist/types/traverse/problems.d.ts +8 -23
- package/dist/types/traverse/traverse.d.ts +27 -23
- package/dist/types/utils/generics.d.ts +0 -1
- package/dist/types/utils/numericLiterals.d.ts +1 -0
- package/dist/types/utils/objectKinds.d.ts +43 -43
- package/package.json +1 -2
- package/dist/cjs/nodes/functors.js +0 -35
- package/dist/cjs/nodes/rules/objectKind.js +0 -78
- package/dist/mjs/nodes/functors.js +0 -27
- package/dist/mjs/nodes/rules/objectKind.js +0 -63
- package/dist/types/nodes/branches.d.ts +0 -14
- package/dist/types/nodes/functors.d.ts +0 -6
- package/dist/types/nodes/rules/objectKind.d.ts +0 -15
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
var _definitions_code, _definitions_code1
|
|
1
|
+
var _definitions_code, _definitions_code1;
|
|
2
2
|
import { Scanner } from "../parse/string/shift/scanner.js";
|
|
3
3
|
import { domainDescriptions, domainOf } from "../utils/domains.js";
|
|
4
4
|
import { isWellFormedInteger } from "../utils/numericLiterals.js";
|
|
5
|
-
import { objectKindDescriptions
|
|
5
|
+
import { objectKindDescriptions } from "../utils/objectKinds.js";
|
|
6
6
|
import { Path } from "../utils/paths.js";
|
|
7
7
|
import { stringify } from "../utils/serialize.js";
|
|
8
8
|
export class ArkTypeError extends TypeError {
|
|
@@ -36,10 +36,11 @@ export class Problem {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
class ProblemArray extends Array {
|
|
39
|
-
add(code, data, source) {
|
|
40
|
-
|
|
41
|
-
Path.from(this.state.path)
|
|
42
|
-
const
|
|
39
|
+
add(code, data, source, opts) {
|
|
40
|
+
// copy the path to avoid future mutations affecting it
|
|
41
|
+
const path = opts?.path ?? Path.from(this.state.path);
|
|
42
|
+
const problem = new Problem(code, path, data, source, this.state.getConfigForProblemCode(code));
|
|
43
|
+
const pathKey = `${path}`;
|
|
43
44
|
const existing = this.byPath[pathKey];
|
|
44
45
|
if (existing) {
|
|
45
46
|
if (existing.parts) {
|
|
@@ -60,11 +61,9 @@ class ProblemArray extends Array {
|
|
|
60
61
|
this.byPath[pathKey] = problem;
|
|
61
62
|
this.push(problem);
|
|
62
63
|
}
|
|
64
|
+
return false;
|
|
63
65
|
}
|
|
64
66
|
get summary() {
|
|
65
|
-
if (this.length === 1) {
|
|
66
|
-
return this[0].message;
|
|
67
|
-
}
|
|
68
67
|
return this.join("\n");
|
|
69
68
|
}
|
|
70
69
|
toString() {
|
|
@@ -108,9 +107,6 @@ export class DataWrapper {
|
|
|
108
107
|
get domain() {
|
|
109
108
|
return domainOf(this.value);
|
|
110
109
|
}
|
|
111
|
-
get objectKind() {
|
|
112
|
-
return objectKindOf(this.value) ?? "non-object";
|
|
113
|
-
}
|
|
114
110
|
get size() {
|
|
115
111
|
return typeof this.value === "string" || Array.isArray(this.value) ? this.value.length : typeof this.value === "number" ? this.value : 0;
|
|
116
112
|
}
|
|
@@ -125,12 +121,12 @@ export class DataWrapper {
|
|
|
125
121
|
}
|
|
126
122
|
}
|
|
127
123
|
const writeDefaultReason = (mustBe, was)=>`must be ${mustBe}${was && ` (was ${was})`}`;
|
|
128
|
-
const
|
|
124
|
+
const addDefaultContext = (reason, path)=>path.length === 0 ? capitalize(reason) : path.length === 1 && isWellFormedInteger(path[0]) ? `Item at index ${path[0]} ${reason}` : `${path} ${reason}`;
|
|
129
125
|
const compileDefaultProblemWriters = (definitions)=>{
|
|
130
126
|
let code;
|
|
131
127
|
for(code in definitions){
|
|
132
128
|
(_definitions_code = definitions[code]).writeReason ?? (_definitions_code.writeReason = writeDefaultReason);
|
|
133
|
-
(_definitions_code1 = definitions[code]).addContext ?? (_definitions_code1.addContext =
|
|
129
|
+
(_definitions_code1 = definitions[code]).addContext ?? (_definitions_code1.addContext = addDefaultContext);
|
|
134
130
|
}
|
|
135
131
|
return definitions;
|
|
136
132
|
};
|
|
@@ -139,7 +135,7 @@ export const defaultProblemWriters = compileDefaultProblemWriters({
|
|
|
139
135
|
mustBe: (divisor)=>divisor === 1 ? `an integer` : `a multiple of ${divisor}`
|
|
140
136
|
},
|
|
141
137
|
class: {
|
|
142
|
-
mustBe: (
|
|
138
|
+
mustBe: (expected)=>typeof expected === "string" ? objectKindDescriptions[expected] : `an instance of ${expected.name}`,
|
|
143
139
|
writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.className)
|
|
144
140
|
},
|
|
145
141
|
domain: {
|
|
@@ -150,10 +146,6 @@ export const defaultProblemWriters = compileDefaultProblemWriters({
|
|
|
150
146
|
mustBe: (domains)=>describeDomains(domains),
|
|
151
147
|
writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.domain)
|
|
152
148
|
},
|
|
153
|
-
objectKind: {
|
|
154
|
-
mustBe: (kind)=>objectKindDescriptions[kind],
|
|
155
|
-
writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.objectKind)
|
|
156
|
-
},
|
|
157
149
|
missing: {
|
|
158
150
|
mustBe: ()=>"defined",
|
|
159
151
|
writeReason: (mustBe)=>writeDefaultReason(mustBe, "")
|
|
@@ -163,7 +155,7 @@ export const defaultProblemWriters = compileDefaultProblemWriters({
|
|
|
163
155
|
writeReason: (mustBe, data)=>writeDefaultReason(mustBe, `${data.size}`)
|
|
164
156
|
},
|
|
165
157
|
regex: {
|
|
166
|
-
mustBe: (expression)
|
|
158
|
+
mustBe: (expression)=>`a string matching ${expression}`
|
|
167
159
|
},
|
|
168
160
|
value: {
|
|
169
161
|
mustBe: stringify
|
|
@@ -172,15 +164,7 @@ export const defaultProblemWriters = compileDefaultProblemWriters({
|
|
|
172
164
|
mustBe: (values)=>describeBranches(values.map(stringify))
|
|
173
165
|
},
|
|
174
166
|
branches: {
|
|
175
|
-
mustBe: (branchProblems)=>{
|
|
176
|
-
const clausesByPath = {};
|
|
177
|
-
for (const problem of branchProblems){
|
|
178
|
-
const pathKey = `${problem.path}`;
|
|
179
|
-
(_clausesByPath = clausesByPath)[_pathKey = pathKey] ?? (_clausesByPath[_pathKey] = []);
|
|
180
|
-
clausesByPath[pathKey].push(problem.mustBe);
|
|
181
|
-
}
|
|
182
|
-
return describeBranches(Object.entries(clausesByPath).map(([path, clauses])=>`${path} must be ${clauses.length === 1 ? clauses[0] : describeBranches(clauses)}`));
|
|
183
|
-
},
|
|
167
|
+
mustBe: (branchProblems)=>describeBranches(branchProblems.map((problem)=>`${problem.path} must be ${problem.parts ? describeBranches(problem.parts.map((part)=>part.mustBe)) : problem.mustBe}`)),
|
|
184
168
|
writeReason: (mustBe, data)=>`${mustBe} (was ${data})`,
|
|
185
169
|
addContext: (reason, path)=>path.length ? `At ${path}, ${reason}` : reason
|
|
186
170
|
},
|
|
@@ -45,13 +45,12 @@ 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
47
|
import { domainOf, hasDomain } from "../utils/domains.js";
|
|
48
|
-
import { throwInternalError } from "../utils/errors.js";
|
|
49
48
|
import { hasKey, keysOf } from "../utils/generics.js";
|
|
50
|
-
import { objectKindOf } from "../utils/objectKinds.js";
|
|
51
49
|
import { getPath, Path } from "../utils/paths.js";
|
|
52
|
-
import { deserializePrimitive
|
|
50
|
+
import { deserializePrimitive } from "../utils/serialize.js";
|
|
53
51
|
import { defaultProblemWriters, Problems } from "./problems.js";
|
|
54
52
|
var _seen = /*#__PURE__*/ new WeakMap();
|
|
53
|
+
// TODO: include data wrapper in state
|
|
55
54
|
export class TraversalState {
|
|
56
55
|
getConfigForProblemCode(code) {
|
|
57
56
|
if (!this.configs.length) {
|
|
@@ -68,9 +67,22 @@ export class TraversalState {
|
|
|
68
67
|
}
|
|
69
68
|
return defaultProblemWriters[code];
|
|
70
69
|
}
|
|
71
|
-
|
|
70
|
+
traverseKey(key, node) {
|
|
71
|
+
const lastData = this.data;
|
|
72
|
+
this.data = this.data[key];
|
|
73
|
+
// TODO: make path externally readonly
|
|
74
|
+
this.path.push(key);
|
|
75
|
+
const isValid = traverse(node, this);
|
|
76
|
+
this.path.pop();
|
|
77
|
+
lastData[key] = this.data;
|
|
78
|
+
this.data = lastData;
|
|
79
|
+
return isValid;
|
|
80
|
+
}
|
|
81
|
+
traverseResolution(name) {
|
|
72
82
|
const resolution = this.type.meta.scope.resolve(name);
|
|
73
83
|
const id = resolution.meta.id;
|
|
84
|
+
// this assignment helps with narrowing
|
|
85
|
+
const data = this.data;
|
|
74
86
|
const isObject = hasDomain(data, "object");
|
|
75
87
|
if (isObject) {
|
|
76
88
|
if (hasKey(_classPrivateFieldGet(this, _seen), id)) {
|
|
@@ -78,7 +90,7 @@ export class TraversalState {
|
|
|
78
90
|
// if data has already been checked by this alias as part of
|
|
79
91
|
// a resolution higher up on the call stack, it must be valid
|
|
80
92
|
// or we wouldn't be here
|
|
81
|
-
return;
|
|
93
|
+
return true;
|
|
82
94
|
}
|
|
83
95
|
_classPrivateFieldGet(this, _seen)[id].push(data);
|
|
84
96
|
} else {
|
|
@@ -89,39 +101,39 @@ export class TraversalState {
|
|
|
89
101
|
}
|
|
90
102
|
const lastResolution = this.type;
|
|
91
103
|
this.type = resolution;
|
|
92
|
-
traverse(
|
|
104
|
+
const isValid = traverse(resolution.flat, this);
|
|
93
105
|
this.type = lastResolution;
|
|
94
106
|
if (isObject) {
|
|
95
107
|
_classPrivateFieldGet(this, _seen)[id].pop();
|
|
96
108
|
}
|
|
109
|
+
return isValid;
|
|
97
110
|
}
|
|
98
|
-
traverseBranches(
|
|
99
|
-
const lastProblems = this.problems;
|
|
100
|
-
const lastPath = this.path;
|
|
111
|
+
traverseBranches(branches) {
|
|
101
112
|
const lastFailFast = this.failFast;
|
|
102
113
|
this.failFast = true;
|
|
103
|
-
const
|
|
114
|
+
const lastProblems = this.problems;
|
|
115
|
+
const branchProblems = new Problems(this);
|
|
116
|
+
this.problems = branchProblems;
|
|
117
|
+
const lastPath = this.path;
|
|
118
|
+
let hasValidBranch = false;
|
|
104
119
|
for (const branch of branches){
|
|
105
|
-
this.problems = new Problems(this);
|
|
106
120
|
this.path = new Path();
|
|
107
|
-
checkEntries(
|
|
108
|
-
|
|
121
|
+
if (checkEntries(branch, this)) {
|
|
122
|
+
hasValidBranch = true;
|
|
109
123
|
break;
|
|
110
124
|
}
|
|
111
|
-
branchProblems.push(this.problems[0]);
|
|
112
125
|
}
|
|
113
|
-
this.failFast = lastFailFast;
|
|
114
126
|
this.path = lastPath;
|
|
115
127
|
this.problems = lastProblems;
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
128
|
+
this.failFast = lastFailFast;
|
|
129
|
+
return hasValidBranch || this.problems.add("branches", this.data, branchProblems);
|
|
119
130
|
}
|
|
120
|
-
constructor(type){
|
|
131
|
+
constructor(data, type){
|
|
121
132
|
_classPrivateFieldInit(this, _seen, {
|
|
122
133
|
writable: true,
|
|
123
134
|
value: void 0
|
|
124
135
|
});
|
|
136
|
+
this.data = data;
|
|
125
137
|
this.type = type;
|
|
126
138
|
this.path = new Path();
|
|
127
139
|
this.problems = new Problems(this);
|
|
@@ -132,143 +144,103 @@ export class TraversalState {
|
|
|
132
144
|
] : [];
|
|
133
145
|
}
|
|
134
146
|
}
|
|
135
|
-
export const traverse = (data, node, state)
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
return checkEntries(data, node, state);
|
|
143
|
-
};
|
|
144
|
-
export const checkEntries = (data, entries, state)=>{
|
|
145
|
-
let problemsPrecedence = Number.POSITIVE_INFINITY;
|
|
146
|
-
const initialProblemsCount = state.problems.length;
|
|
147
|
-
for (const [k, v] of entries){
|
|
148
|
-
if (precedenceMap[k] > problemsPrecedence) {
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
147
|
+
export const traverse = (node, state)=>typeof node === "string" ? domainOf(state.data) === node || state.problems.add("domain", state.data, node) : checkEntries(node, state);
|
|
148
|
+
export const checkEntries = (entries, state)=>{
|
|
149
|
+
let isValid = true;
|
|
150
|
+
for(let i = 0; i < entries.length; i++){
|
|
151
|
+
const [k, v] = entries[i];
|
|
151
152
|
if (k === "morph") {
|
|
152
153
|
if (typeof v === "function") {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
return out;
|
|
160
|
-
} else {
|
|
161
|
-
checkers[k](data, v, state);
|
|
162
|
-
if (state.problems.length > initialProblemsCount) {
|
|
163
|
-
if (state.failFast) {
|
|
164
|
-
return;
|
|
154
|
+
// TODO: allow problem from morph
|
|
155
|
+
state.data = v(state.data);
|
|
156
|
+
} else {
|
|
157
|
+
for (const morph of v){
|
|
158
|
+
state.data = morph(state.data);
|
|
165
159
|
}
|
|
166
|
-
problemsPrecedence = precedenceMap[k];
|
|
167
160
|
}
|
|
161
|
+
return true;
|
|
168
162
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
} else {
|
|
181
|
-
traverse(data[propKey], propNode, state);
|
|
163
|
+
const entryAllowsData = entryCheckers[k](v, state);
|
|
164
|
+
isValid && (isValid = entryAllowsData);
|
|
165
|
+
if (!isValid) {
|
|
166
|
+
if (state.failFast) {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
if (i < entries.length - 1 && precedenceMap[k] < precedenceMap[entries[i + 1][0]]) {
|
|
170
|
+
// if we've encountered a problem, there is at least one entry
|
|
171
|
+
// remaining, and the next entry is of a higher precedence level
|
|
172
|
+
// than the current entry, return immediately
|
|
173
|
+
return false;
|
|
182
174
|
}
|
|
183
|
-
state.path.pop();
|
|
184
|
-
}
|
|
185
|
-
state.path = rootPath;
|
|
186
|
-
};
|
|
187
|
-
const checkRequiredProps = createPropChecker("requiredProps");
|
|
188
|
-
const checkOptionalProps = createPropChecker("optionalProps");
|
|
189
|
-
export const checkObjectKind = (data, rule, state)=>{
|
|
190
|
-
const dataObjectKind = objectKindOf(data);
|
|
191
|
-
if (typeof rule === "string") {
|
|
192
|
-
if (dataObjectKind !== rule) {
|
|
193
|
-
return state.problems.add("objectKind", data, rule);
|
|
194
|
-
}
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
if (dataObjectKind !== rule[0]) {
|
|
198
|
-
return state.problems.add("objectKind", data, rule[0]);
|
|
199
|
-
}
|
|
200
|
-
if (dataObjectKind === "Array" && typeof rule[2] === "number") {
|
|
201
|
-
const actual = data.length;
|
|
202
|
-
const expected = rule[2];
|
|
203
|
-
if (expected !== actual) {
|
|
204
|
-
return state.problems.add("bound", data, {
|
|
205
|
-
comparator: "==",
|
|
206
|
-
limit: expected,
|
|
207
|
-
units: "items"
|
|
208
|
-
});
|
|
209
175
|
}
|
|
210
176
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
state.path.pop();
|
|
217
|
-
i++;
|
|
218
|
-
}
|
|
219
|
-
} else {
|
|
220
|
-
return throwInternalError(`Unexpected objectKind entry ${stringify(rule)}`);
|
|
177
|
+
return isValid;
|
|
178
|
+
};
|
|
179
|
+
export const checkRequiredProp = (prop, state)=>{
|
|
180
|
+
if (prop[0] in state.data) {
|
|
181
|
+
return state.traverseKey(prop[0], prop[1]);
|
|
221
182
|
}
|
|
222
|
-
return
|
|
183
|
+
return state.problems.add("missing", undefined, undefined, {
|
|
184
|
+
path: state.path.concat(prop[0])
|
|
185
|
+
});
|
|
223
186
|
};
|
|
224
|
-
const
|
|
187
|
+
const entryCheckers = {
|
|
225
188
|
regex: checkRegex,
|
|
226
189
|
divisor: checkDivisor,
|
|
227
|
-
domains: (
|
|
228
|
-
const entries = domains[domainOf(data)];
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
190
|
+
domains: (domains, state)=>{
|
|
191
|
+
const entries = domains[domainOf(state.data)];
|
|
192
|
+
return entries ? checkEntries(entries, state) : state.problems.add("domainBranches", state.data, keysOf(domains));
|
|
193
|
+
},
|
|
194
|
+
// TODO: remove data from problem params
|
|
195
|
+
domain: (domain, state)=>domainOf(state.data) === domain || state.problems.add("domain", state.data, domain),
|
|
196
|
+
bound: checkBound,
|
|
197
|
+
optionalProp: (prop, state)=>{
|
|
198
|
+
if (prop[0] in state.data) {
|
|
199
|
+
return state.traverseKey(prop[0], prop[1]);
|
|
233
200
|
}
|
|
201
|
+
return true;
|
|
234
202
|
},
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
203
|
+
// these checks work the same way, the keys are only distinct so that
|
|
204
|
+
// prerequisite props can have a higher precedence
|
|
205
|
+
requiredProp: checkRequiredProp,
|
|
206
|
+
prerequisiteProp: checkRequiredProp,
|
|
207
|
+
indexProp: (node, state)=>{
|
|
208
|
+
if (!Array.isArray(state.data)) {
|
|
209
|
+
return state.problems.add("class", state.data, "Array");
|
|
210
|
+
}
|
|
211
|
+
let isValid = true;
|
|
212
|
+
for(let i = 0; i < state.data.length; i++){
|
|
213
|
+
isValid && (isValid = state.traverseKey(`${i}`, node));
|
|
214
|
+
if (!isValid && state.failFast) {
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
238
217
|
}
|
|
218
|
+
return isValid;
|
|
239
219
|
},
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
optionalProps: checkOptionalProps,
|
|
244
|
-
branches: (data, branches, state)=>state.traverseBranches(data, branches),
|
|
245
|
-
switch: (data, rule, state)=>{
|
|
246
|
-
const dataAtPath = getPath(data, rule.path);
|
|
220
|
+
branches: (branches, state)=>state.traverseBranches(branches),
|
|
221
|
+
switch: (rule, state)=>{
|
|
222
|
+
const dataAtPath = getPath(state.data, rule.path);
|
|
247
223
|
const caseKey = serializeCase(rule.kind, dataAtPath);
|
|
248
224
|
if (hasKey(rule.cases, caseKey)) {
|
|
249
|
-
return checkEntries(
|
|
225
|
+
return checkEntries(rule.cases[caseKey], state);
|
|
250
226
|
}
|
|
251
227
|
const caseKeys = keysOf(rule.cases);
|
|
252
|
-
const
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
}
|
|
259
|
-
state.path = lastPath;
|
|
228
|
+
const missingCasePath = state.path.concat(rule.path);
|
|
229
|
+
return rule.kind === "value" ? state.problems.add("valueBranches", dataAtPath, caseKeys.map((k)=>deserializePrimitive(k)), {
|
|
230
|
+
path: missingCasePath
|
|
231
|
+
}) : state.problems.add("domainBranches", dataAtPath, caseKeys, {
|
|
232
|
+
path: missingCasePath
|
|
233
|
+
});
|
|
260
234
|
},
|
|
261
|
-
alias: (
|
|
235
|
+
alias: (name, state)=>state.traverseResolution(name),
|
|
262
236
|
class: checkClass,
|
|
263
|
-
|
|
264
|
-
|
|
237
|
+
// TODO: fix
|
|
238
|
+
narrow: (narrow, state)=>narrow(state.data, state.problems),
|
|
239
|
+
config: ({ config , node }, state)=>{
|
|
265
240
|
state.configs.push(config);
|
|
266
|
-
traverse(
|
|
241
|
+
const result = traverse(node, state);
|
|
267
242
|
state.configs.pop();
|
|
243
|
+
return result;
|
|
268
244
|
},
|
|
269
|
-
value: (
|
|
270
|
-
if (data !== value) {
|
|
271
|
-
state.problems.add("value", data, value);
|
|
272
|
-
}
|
|
273
|
-
}
|
|
245
|
+
value: (value, state)=>state.data === value || state.problems.add("value", state.data, value)
|
|
274
246
|
};
|
|
@@ -22,6 +22,7 @@ const isNumberLike = (s)=>s.length !== 0 && numberLikeMatcher.test(s);
|
|
|
22
22
|
* 2. The value may not be "-0"
|
|
23
23
|
*/ export const wellFormedIntegerMatcher = /^(?:0|(?:-?[1-9]\d*))$/;
|
|
24
24
|
export const isWellFormedInteger = (s)=>wellFormedIntegerMatcher.test(s);
|
|
25
|
+
export const wellFormedNonNegativeIntegerMatcher = /^(?:0|(?:[1-9]\d*))$/;
|
|
25
26
|
const integerLikeMatcher = /^-?\d+$/;
|
|
26
27
|
const isIntegerLike = (s)=>integerLikeMatcher.test(s);
|
|
27
28
|
const numericLiteralDescriptions = {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { domainOf } from "./domains.js";
|
|
2
2
|
// Built-in object constructors based on a subset of:
|
|
3
3
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
|
|
4
|
-
const defaultObjectKinds = {
|
|
4
|
+
export const defaultObjectKinds = {
|
|
5
5
|
Array,
|
|
6
6
|
Date,
|
|
7
7
|
Error,
|