arktype 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/nodes/discriminate.js +86 -18
- package/dist/cjs/nodes/rules/class.js +2 -2
- package/dist/cjs/nodes/rules/divisor.js +1 -1
- package/dist/cjs/nodes/rules/props.js +1 -2
- package/dist/cjs/nodes/rules/range.js +1 -1
- package/dist/cjs/nodes/rules/regex.js +1 -1
- package/dist/cjs/parse/ast/morph.js +1 -1
- package/dist/cjs/parse/ast/tuple.js +3 -2
- package/dist/cjs/parse/string/shift/operator/bounds.js +1 -1
- package/dist/cjs/scopes/scope.js +5 -4
- package/dist/cjs/scopes/validation.js +16 -1
- package/dist/cjs/traverse/problems.js +22 -20
- package/dist/cjs/traverse/traverse.js +53 -46
- package/dist/cjs/utils/freeze.js +2 -3
- package/dist/cjs/utils/numericLiterals.js +2 -14
- package/dist/mjs/nodes/discriminate.js +87 -19
- package/dist/mjs/nodes/rules/class.js +2 -2
- package/dist/mjs/nodes/rules/divisor.js +1 -1
- package/dist/mjs/nodes/rules/props.js +1 -2
- package/dist/mjs/nodes/rules/range.js +1 -1
- package/dist/mjs/nodes/rules/regex.js +1 -1
- package/dist/mjs/parse/ast/morph.js +1 -1
- package/dist/mjs/parse/ast/tuple.js +3 -2
- package/dist/mjs/parse/string/shift/operator/bounds.js +2 -2
- package/dist/mjs/scopes/scope.js +5 -4
- package/dist/mjs/scopes/validation.js +16 -1
- package/dist/mjs/traverse/problems.js +20 -19
- package/dist/mjs/traverse/traverse.js +25 -18
- package/dist/mjs/utils/freeze.js +2 -3
- package/dist/mjs/utils/numericLiterals.js +1 -1
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/nodes/discriminate.d.ts +6 -8
- package/dist/types/nodes/rules/range.d.ts +2 -2
- package/dist/types/parse/ast/morph.d.ts +8 -5
- package/dist/types/parse/ast/narrow.d.ts +2 -2
- package/dist/types/parse/ast/tuple.d.ts +5 -5
- package/dist/types/parse/string/shift/scanner.d.ts +1 -1
- package/dist/types/scopes/expressions.d.ts +2 -2
- package/dist/types/scopes/scope.d.ts +2 -2
- package/dist/types/scopes/standard.d.ts +15 -0
- package/dist/types/scopes/validation.d.ts +4 -0
- package/dist/types/traverse/problems.d.ts +9 -7
- package/dist/types/traverse/traverse.d.ts +3 -2
- package/dist/types/utils/generics.d.ts +1 -1
- package/dist/types/utils/numericLiterals.d.ts +16 -0
- package/package.json +1 -1
|
@@ -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);
|
|
@@ -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
|
{
|
|
@@ -78,15 +78,17 @@ export class Problem {
|
|
|
78
78
|
}
|
|
79
79
|
var _state = /*#__PURE__*/ new WeakMap();
|
|
80
80
|
class ProblemArray extends Array {
|
|
81
|
-
|
|
81
|
+
add(code, source, opts) {
|
|
82
82
|
// copy the path to avoid future mutations affecting it
|
|
83
83
|
const path = opts?.path ?? Path.from(_classPrivateFieldGet(this, _state).path);
|
|
84
84
|
const data = // we have to check for the presence of the key explicitly since the
|
|
85
85
|
// data could be undefined or null
|
|
86
86
|
opts && "data" in opts ? opts.data : _classPrivateFieldGet(this, _state).data;
|
|
87
|
-
|
|
87
|
+
const problem = new Problem(code, path, data, source, _classPrivateFieldGet(this, _state).getConfigForProblemCode(code));
|
|
88
|
+
this.addProblem(problem);
|
|
89
|
+
return problem;
|
|
88
90
|
}
|
|
89
|
-
|
|
91
|
+
addProblem(problem) {
|
|
90
92
|
const pathKey = `${problem.path}`;
|
|
91
93
|
const existing = this.byPath[pathKey];
|
|
92
94
|
if (existing) {
|
|
@@ -108,7 +110,7 @@ class ProblemArray extends Array {
|
|
|
108
110
|
this.byPath[pathKey] = problem;
|
|
109
111
|
this.push(problem);
|
|
110
112
|
}
|
|
111
|
-
|
|
113
|
+
this.count++;
|
|
112
114
|
}
|
|
113
115
|
get summary() {
|
|
114
116
|
return this.join("\n");
|
|
@@ -122,6 +124,7 @@ class ProblemArray extends Array {
|
|
|
122
124
|
constructor(state){
|
|
123
125
|
super();
|
|
124
126
|
this.byPath = {};
|
|
127
|
+
this.count = 0;
|
|
125
128
|
_classPrivateFieldInit(this, _state, {
|
|
126
129
|
writable: true,
|
|
127
130
|
value: void 0
|
|
@@ -131,16 +134,15 @@ class ProblemArray extends Array {
|
|
|
131
134
|
}
|
|
132
135
|
export const Problems = ProblemArray;
|
|
133
136
|
const capitalize = (s)=>s[0].toUpperCase() + s.slice(1);
|
|
134
|
-
export const
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
if (domains.length === 0) {
|
|
137
|
+
export const domainsToDescriptions = (domains)=>domains.map((objectKind)=>domainDescriptions[objectKind]);
|
|
138
|
+
export const objectKindsToDescriptions = (kinds)=>kinds.map((objectKind)=>objectKindDescriptions[objectKind]);
|
|
139
|
+
export const describeBranches = (descriptions)=>{
|
|
140
|
+
if (descriptions.length === 0) {
|
|
139
141
|
return "never";
|
|
140
142
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
143
|
+
if (descriptions.length === 1) {
|
|
144
|
+
return descriptions[0];
|
|
145
|
+
}
|
|
144
146
|
let description = "";
|
|
145
147
|
for(let i = 0; i < descriptions.length - 1; i++){
|
|
146
148
|
description += descriptions[i];
|
|
@@ -173,10 +175,6 @@ export const defaultProblemWriters = compileDefaultProblemWriters({
|
|
|
173
175
|
mustBe: (domain)=>domainDescriptions[domain],
|
|
174
176
|
writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.domain)
|
|
175
177
|
},
|
|
176
|
-
domainBranches: {
|
|
177
|
-
mustBe: (domains)=>describeDomains(domains),
|
|
178
|
-
writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.domain)
|
|
179
|
-
},
|
|
180
178
|
missing: {
|
|
181
179
|
mustBe: ()=>"defined",
|
|
182
180
|
writeReason: (mustBe)=>writeDefaultReason(mustBe, "")
|
|
@@ -191,9 +189,6 @@ export const defaultProblemWriters = compileDefaultProblemWriters({
|
|
|
191
189
|
value: {
|
|
192
190
|
mustBe: stringify
|
|
193
191
|
},
|
|
194
|
-
valueBranches: {
|
|
195
|
-
mustBe: (values)=>describeBranches(values.map(stringify))
|
|
196
|
-
},
|
|
197
192
|
branches: {
|
|
198
193
|
mustBe: (branchProblems)=>describeBranches(branchProblems.map((problem)=>`${problem.path} must be ${problem.parts ? describeBranches(problem.parts.map((part)=>part.mustBe)) : problem.mustBe}`)),
|
|
199
194
|
writeReason: (mustBe, data)=>`${mustBe} (was ${data})`,
|
|
@@ -203,5 +198,11 @@ export const defaultProblemWriters = compileDefaultProblemWriters({
|
|
|
203
198
|
mustBe: (problems)=>"• " + problems.map((_)=>_.mustBe).join("\n• "),
|
|
204
199
|
writeReason: (mustBe, data)=>`${data} must be...\n${mustBe}`,
|
|
205
200
|
addContext: (reason, path)=>path.length ? `At ${path}, ${reason}` : reason
|
|
201
|
+
},
|
|
202
|
+
mustBe: {
|
|
203
|
+
mustBe: (mustBe)=>mustBe
|
|
204
|
+
},
|
|
205
|
+
cases: {
|
|
206
|
+
mustBe: (cases)=>describeBranches(cases)
|
|
206
207
|
}
|
|
207
208
|
});
|
|
@@ -45,10 +45,10 @@ 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";
|
|
48
49
|
import { hasKey, objectKeysOf } from "../utils/generics.js";
|
|
49
50
|
import { getPath, Path } from "../utils/paths.js";
|
|
50
|
-
import {
|
|
51
|
-
import { defaultProblemWriters, Problem, Problems } from "./problems.js";
|
|
51
|
+
import { defaultProblemWriters, domainsToDescriptions, objectKindsToDescriptions, Problem, Problems } from "./problems.js";
|
|
52
52
|
var _seen = /*#__PURE__*/ new WeakMap();
|
|
53
53
|
export class TraversalState {
|
|
54
54
|
getConfigForProblemCode(code) {
|
|
@@ -83,14 +83,15 @@ export class TraversalState {
|
|
|
83
83
|
const data = this.data;
|
|
84
84
|
const isObject = hasDomain(data, "object");
|
|
85
85
|
if (isObject) {
|
|
86
|
-
|
|
87
|
-
|
|
86
|
+
const seenByCurrentType = _classPrivateFieldGet(this, _seen)[id];
|
|
87
|
+
if (seenByCurrentType) {
|
|
88
|
+
if (seenByCurrentType.includes(data)) {
|
|
88
89
|
// if data has already been checked by this alias as part of
|
|
89
90
|
// a resolution higher up on the call stack, it must be valid
|
|
90
91
|
// or we wouldn't be here
|
|
91
92
|
return true;
|
|
92
93
|
}
|
|
93
|
-
|
|
94
|
+
seenByCurrentType.push(data);
|
|
94
95
|
} else {
|
|
95
96
|
_classPrivateFieldGet(this, _seen)[id] = [
|
|
96
97
|
data
|
|
@@ -124,7 +125,7 @@ export class TraversalState {
|
|
|
124
125
|
this.path = lastPath;
|
|
125
126
|
this.problems = lastProblems;
|
|
126
127
|
this.failFast = lastFailFast;
|
|
127
|
-
return hasValidBranch || this.problems.
|
|
128
|
+
return hasValidBranch || !this.problems.add("branches", branchProblems);
|
|
128
129
|
}
|
|
129
130
|
constructor(data, type){
|
|
130
131
|
_classPrivateFieldInit(this, _seen, {
|
|
@@ -142,7 +143,7 @@ export class TraversalState {
|
|
|
142
143
|
] : [];
|
|
143
144
|
}
|
|
144
145
|
}
|
|
145
|
-
export const traverse = (node, state)=>typeof node === "string" ? domainOf(state.data) === node || state.problems.
|
|
146
|
+
export const traverse = (node, state)=>typeof node === "string" ? domainOf(state.data) === node || !state.problems.add("domain", node) : checkEntries(node, state);
|
|
146
147
|
export const checkEntries = (entries, state)=>{
|
|
147
148
|
let isValid = true;
|
|
148
149
|
for(let i = 0; i < entries.length; i++){
|
|
@@ -167,19 +168,20 @@ export const checkRequiredProp = (prop, state)=>{
|
|
|
167
168
|
if (prop[0] in state.data) {
|
|
168
169
|
return state.traverseKey(prop[0], prop[1]);
|
|
169
170
|
}
|
|
170
|
-
|
|
171
|
+
state.problems.add("missing", undefined, {
|
|
171
172
|
path: state.path.concat(prop[0]),
|
|
172
173
|
data: undefined
|
|
173
174
|
});
|
|
175
|
+
return false;
|
|
174
176
|
};
|
|
175
177
|
const entryCheckers = {
|
|
176
178
|
regex: checkRegex,
|
|
177
179
|
divisor: checkDivisor,
|
|
178
180
|
domains: (domains, state)=>{
|
|
179
181
|
const entries = domains[domainOf(state.data)];
|
|
180
|
-
return entries ? checkEntries(entries, state) : state.problems.
|
|
182
|
+
return entries ? checkEntries(entries, state) : !state.problems.add("cases", domainsToDescriptions(objectKeysOf(domains)));
|
|
181
183
|
},
|
|
182
|
-
domain: (domain, state)=>domainOf(state.data) === domain || state.problems.
|
|
184
|
+
domain: (domain, state)=>domainOf(state.data) === domain || !state.problems.add("domain", domain),
|
|
183
185
|
bound: checkBound,
|
|
184
186
|
optionalProp: (prop, state)=>{
|
|
185
187
|
if (prop[0] in state.data) {
|
|
@@ -193,7 +195,8 @@ const entryCheckers = {
|
|
|
193
195
|
prerequisiteProp: checkRequiredProp,
|
|
194
196
|
indexProp: (node, state)=>{
|
|
195
197
|
if (!Array.isArray(state.data)) {
|
|
196
|
-
|
|
198
|
+
state.problems.add("class", "Array");
|
|
199
|
+
return false;
|
|
197
200
|
}
|
|
198
201
|
let isValid = true;
|
|
199
202
|
for(let i = 0; i < state.data.length; i++){
|
|
@@ -213,13 +216,12 @@ const entryCheckers = {
|
|
|
213
216
|
}
|
|
214
217
|
const caseKeys = objectKeysOf(rule.cases);
|
|
215
218
|
const missingCasePath = state.path.concat(rule.path);
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
data: dataAtPath
|
|
219
|
-
}) : state.problems.create("domainBranches", caseKeys, {
|
|
219
|
+
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`);
|
|
220
|
+
state.problems.add("cases", caseDescriptions, {
|
|
220
221
|
path: missingCasePath,
|
|
221
222
|
data: dataAtPath
|
|
222
223
|
});
|
|
224
|
+
return false;
|
|
223
225
|
},
|
|
224
226
|
alias: (name, state)=>state.traverseResolution(name),
|
|
225
227
|
class: checkClass,
|
|
@@ -230,11 +232,16 @@ const entryCheckers = {
|
|
|
230
232
|
state.configs.pop();
|
|
231
233
|
return result;
|
|
232
234
|
},
|
|
233
|
-
value: (value, state)=>state.data === value || state.problems.
|
|
235
|
+
value: (value, state)=>state.data === value || !state.problems.add("value", value),
|
|
234
236
|
morph: (morph, state)=>{
|
|
235
|
-
const out = morph(state.data);
|
|
237
|
+
const out = morph(state.data, state.problems);
|
|
238
|
+
if (state.problems.length) {
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
236
241
|
if (out instanceof Problem) {
|
|
237
|
-
|
|
242
|
+
// if a problem was returned from the morph but not added, add it
|
|
243
|
+
state.problems.addProblem(out);
|
|
244
|
+
return false;
|
|
238
245
|
}
|
|
239
246
|
state.data = out;
|
|
240
247
|
return true;
|
package/dist/mjs/utils/freeze.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
export const deepFreeze = (value)=>Object.isFrozen(value) ? value : Array.isArray(value) ? Object.freeze(value.map(deepFreeze)) : deepFreezeDictionary(value);
|
|
2
2
|
const deepFreezeDictionary = (value)=>{
|
|
3
|
-
const result = {};
|
|
4
3
|
for(const k in value){
|
|
5
|
-
|
|
4
|
+
deepFreeze(value[k]);
|
|
6
5
|
}
|
|
7
|
-
return
|
|
6
|
+
return value;
|
|
8
7
|
};
|
|
@@ -12,7 +12,7 @@ import { throwParseError } from "./errors.js";
|
|
|
12
12
|
* 2. The first digit of the value must not be 0, unless the entire integer portion is 0
|
|
13
13
|
* 3. If the value includes a decimal, its last digit may not be 0
|
|
14
14
|
* 4. The value may not be "-0"
|
|
15
|
-
*/ const wellFormedNumberMatcher = /^(?!^-0$)-?(?:0|[1-9]\d*)(?:\.\d*[1-9])?$/;
|
|
15
|
+
*/ export const wellFormedNumberMatcher = /^(?!^-0$)-?(?:0|[1-9]\d*)(?:\.\d*[1-9])?$/;
|
|
16
16
|
const isWellFormedNumber = (s)=>wellFormedNumberMatcher.test(s);
|
|
17
17
|
const numberLikeMatcher = /^-?\d*\.?\d*$/;
|
|
18
18
|
const isNumberLike = (s)=>s.length !== 0 && numberLikeMatcher.test(s);
|