arktype 0.1.3 → 0.2.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/README.md +1 -1
- package/dist/api.cjs +4 -3
- package/dist/api.d.ts +3 -2
- package/dist/api.mjs +2 -1
- package/dist/cjs/nodes/node.js +20 -2
- package/dist/cjs/nodes/predicate.js +2 -0
- package/dist/cjs/nodes/rules/regex.js +4 -42
- package/dist/cjs/nodes/rules/rules.js +16 -12
- package/dist/cjs/parse/definition.js +2 -2
- package/dist/cjs/parse/string/ast.js +1 -1
- package/dist/cjs/parse/tuple/keyof.js +6 -7
- package/dist/cjs/parse/tuple/morph.js +7 -6
- package/dist/cjs/scopes/cache.js +39 -0
- package/dist/cjs/scopes/jsObjects.js +133 -0
- package/dist/cjs/scopes/scope.js +244 -0
- package/dist/cjs/scopes/standard.js +44 -0
- package/dist/cjs/scopes/tsKeywords.js +113 -0
- package/dist/cjs/scopes/type.js +70 -0
- package/dist/cjs/scopes/validation.js +64 -0
- package/dist/cjs/traverse/problems.js +50 -74
- package/dist/cjs/traverse/traverse.js +25 -3
- package/dist/cjs/utils/numericLiterals.js +1 -0
- package/dist/mjs/nodes/node.js +20 -2
- package/dist/mjs/nodes/predicate.js +1 -0
- package/dist/mjs/nodes/rules/regex.js +3 -41
- package/dist/mjs/nodes/rules/rules.js +17 -13
- package/dist/mjs/parse/definition.js +1 -1
- package/dist/mjs/parse/string/ast.js +1 -1
- package/dist/mjs/parse/tuple/keyof.js +6 -7
- package/dist/mjs/parse/tuple/morph.js +7 -6
- package/dist/mjs/scopes/cache.js +25 -0
- package/dist/mjs/scopes/jsObjects.js +119 -0
- package/dist/mjs/{main.js → scopes/scope.js} +23 -349
- package/dist/mjs/scopes/standard.js +27 -0
- package/dist/mjs/scopes/tsKeywords.js +99 -0
- package/dist/mjs/scopes/type.js +56 -0
- package/dist/mjs/scopes/validation.js +51 -0
- package/dist/mjs/traverse/problems.js +49 -73
- package/dist/mjs/traverse/traverse.js +26 -4
- package/dist/mjs/utils/numericLiterals.js +1 -1
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/nodes/compose.d.ts +1 -1
- package/dist/types/nodes/infer.d.ts +4 -4
- package/dist/types/nodes/node.d.ts +11 -7
- package/dist/types/nodes/predicate.d.ts +43 -54
- package/dist/types/nodes/rules/range.d.ts +1 -1
- package/dist/types/nodes/rules/regex.d.ts +1 -11
- package/dist/types/nodes/rules/rules.d.ts +2 -10
- package/dist/types/parse/definition.d.ts +1 -1
- package/dist/types/parse/record.d.ts +2 -2
- package/dist/types/parse/string/ast.d.ts +5 -4
- 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/reduce/static.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/tuple/keyof.d.ts +8 -0
- package/dist/types/parse/tuple/morph.d.ts +1 -1
- package/dist/types/parse/tuple/narrow.d.ts +1 -1
- package/dist/types/parse/tuple/tuple.d.ts +7 -13
- package/dist/types/scopes/cache.d.ts +14 -0
- package/dist/types/scopes/jsObjects.d.ts +36 -0
- package/dist/types/scopes/scope.d.ts +76 -0
- package/dist/types/scopes/standard.d.ts +280 -0
- package/dist/types/scopes/tsKeywords.d.ts +32 -0
- package/dist/types/scopes/type.d.ts +53 -0
- package/dist/types/scopes/validation.d.ts +21 -0
- package/dist/types/traverse/problems.d.ts +29 -32
- package/dist/types/traverse/traverse.d.ts +10 -8
- package/dist/types/utils/generics.d.ts +2 -1
- package/dist/types/utils/numericLiterals.d.ts +1 -0
- package/package.json +2 -2
- package/dist/cjs/main.js +0 -574
- package/dist/types/main.d.ts +0 -321
|
@@ -15,16 +15,15 @@ _export(exports, {
|
|
|
15
15
|
describeDomains: ()=>describeDomains,
|
|
16
16
|
describeBranches: ()=>describeBranches,
|
|
17
17
|
DataWrapper: ()=>DataWrapper,
|
|
18
|
-
defaultProblemWriters: ()=>defaultProblemWriters
|
|
19
|
-
compileProblemOptions: ()=>compileProblemOptions
|
|
18
|
+
defaultProblemWriters: ()=>defaultProblemWriters
|
|
20
19
|
});
|
|
21
20
|
const _scannerTs = require("../parse/string/shift/scanner.js");
|
|
22
21
|
const _domainsTs = require("../utils/domains.js");
|
|
23
|
-
const
|
|
22
|
+
const _numericLiteralsTs = require("../utils/numericLiterals.js");
|
|
24
23
|
const _objectKindsTs = require("../utils/objectKinds.js");
|
|
25
24
|
const _pathsTs = require("../utils/paths.js");
|
|
26
25
|
const _serializeTs = require("../utils/serialize.js");
|
|
27
|
-
var _definitions_code, _definitions_code1,
|
|
26
|
+
var _definitions_code, _definitions_code1, _clausesByPath, _pathKey;
|
|
28
27
|
class ArkTypeError extends TypeError {
|
|
29
28
|
constructor(problems){
|
|
30
29
|
super(problems.summary);
|
|
@@ -36,46 +35,50 @@ class Problem {
|
|
|
36
35
|
return this.message;
|
|
37
36
|
}
|
|
38
37
|
get message() {
|
|
39
|
-
return this.writers.
|
|
38
|
+
return this.writers.addContext(this.reason, this.path);
|
|
40
39
|
}
|
|
41
40
|
get reason() {
|
|
42
|
-
return this.writers.
|
|
41
|
+
return this.writers.writeReason(this.mustBe, new DataWrapper(this.data));
|
|
43
42
|
}
|
|
44
43
|
get mustBe() {
|
|
45
|
-
return this.writers.mustBe(this.
|
|
44
|
+
return this.writers.mustBe(this.source);
|
|
46
45
|
}
|
|
47
|
-
|
|
48
|
-
return this.writers.was(new DataWrapper(this.data));
|
|
49
|
-
}
|
|
50
|
-
constructor(code, path, data, rule, writers){
|
|
46
|
+
constructor(code, path, data, source, writers){
|
|
51
47
|
this.code = code;
|
|
52
48
|
this.path = path;
|
|
53
49
|
this.data = data;
|
|
54
|
-
this.
|
|
50
|
+
this.source = source;
|
|
55
51
|
this.writers = writers;
|
|
52
|
+
if (this.code === "multi") {
|
|
53
|
+
this.parts = this.source;
|
|
54
|
+
}
|
|
56
55
|
}
|
|
57
56
|
}
|
|
58
57
|
class ProblemArray extends Array {
|
|
59
|
-
add(code, data,
|
|
58
|
+
add(code, data, source) {
|
|
60
59
|
const problem = new Problem(code, // copy the path to avoid future mutations affecting it
|
|
61
|
-
_pathsTs.Path.from(this.state.path), data,
|
|
60
|
+
_pathsTs.Path.from(this.state.path), data, source, this.state.getConfigForProblemCode(code));
|
|
62
61
|
const pathKey = `${this.state.path}`;
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
62
|
+
const existing = this.byPath[pathKey];
|
|
63
|
+
if (existing) {
|
|
64
|
+
if (existing.parts) {
|
|
65
|
+
existing.parts.push(problem);
|
|
66
|
+
} else {
|
|
67
|
+
const problemIntersection = new Problem("multi", existing.path, data, [
|
|
68
|
+
existing,
|
|
69
|
+
problem
|
|
70
|
+
], this.state.getConfigForProblemCode("multi"));
|
|
71
|
+
const existingIndex = this.indexOf(existing);
|
|
72
|
+
// If existing is found (which it always should be unless this was externally mutated),
|
|
73
|
+
// replace it with the new problem intersection. In case it isn't for whatever reason,
|
|
74
|
+
// just append the intersection.
|
|
75
|
+
this[existingIndex === -1 ? this.length : existingIndex] = problemIntersection;
|
|
76
|
+
this.byPath[pathKey] = problemIntersection;
|
|
77
|
+
}
|
|
78
|
+
} else {
|
|
79
|
+
this.byPath[pathKey] = problem;
|
|
80
|
+
this.push(problem);
|
|
81
|
+
}
|
|
79
82
|
}
|
|
80
83
|
get summary() {
|
|
81
84
|
if (this.length === 1) {
|
|
@@ -140,48 +143,46 @@ class DataWrapper {
|
|
|
140
143
|
this.value = value;
|
|
141
144
|
}
|
|
142
145
|
}
|
|
143
|
-
const
|
|
144
|
-
const
|
|
145
|
-
const writeDefaultProblemMessage = (reason, path)=>path.length ? `${path} ${reason}` : capitalize(reason);
|
|
146
|
+
const writeDefaultReason = (mustBe, was)=>`must be ${mustBe}${was && ` (was ${was})`}`;
|
|
147
|
+
const writeDefaultProblemMessage = (reason, path)=>path.length === 0 ? capitalize(reason) : path.length === 1 && (0, _numericLiteralsTs.isWellFormedInteger)(path[0]) ? `Item at index ${path[0]} ${reason}` : `${path} ${reason}`;
|
|
146
148
|
const compileDefaultProblemWriters = (definitions)=>{
|
|
147
149
|
let code;
|
|
148
150
|
for(code in definitions){
|
|
149
|
-
(_definitions_code = definitions[code]).
|
|
150
|
-
(_definitions_code1 = definitions[code]).
|
|
151
|
-
(_definitions_code2 = definitions[code]).message ?? (_definitions_code2.message = writeDefaultProblemMessage);
|
|
151
|
+
(_definitions_code = definitions[code]).writeReason ?? (_definitions_code.writeReason = writeDefaultReason);
|
|
152
|
+
(_definitions_code1 = definitions[code]).addContext ?? (_definitions_code1.addContext = writeDefaultProblemMessage);
|
|
152
153
|
}
|
|
153
154
|
return definitions;
|
|
154
155
|
};
|
|
155
156
|
const defaultProblemWriters = compileDefaultProblemWriters({
|
|
156
157
|
divisor: {
|
|
157
|
-
mustBe: (divisor)=>divisor === 1 ? `an integer` : `
|
|
158
|
+
mustBe: (divisor)=>divisor === 1 ? `an integer` : `a multiple of ${divisor}`
|
|
158
159
|
},
|
|
159
160
|
class: {
|
|
160
161
|
mustBe: (constructor)=>`an instance of ${constructor.name}`,
|
|
161
|
-
|
|
162
|
+
writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.className)
|
|
162
163
|
},
|
|
163
164
|
domain: {
|
|
164
165
|
mustBe: (domain)=>_domainsTs.domainDescriptions[domain],
|
|
165
|
-
|
|
166
|
+
writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.domain)
|
|
166
167
|
},
|
|
167
168
|
domainBranches: {
|
|
168
169
|
mustBe: (domains)=>describeDomains(domains),
|
|
169
|
-
|
|
170
|
+
writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.domain)
|
|
170
171
|
},
|
|
171
172
|
objectKind: {
|
|
172
173
|
mustBe: (kind)=>_objectKindsTs.objectKindDescriptions[kind],
|
|
173
|
-
|
|
174
|
+
writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.objectKind)
|
|
174
175
|
},
|
|
175
176
|
missing: {
|
|
176
177
|
mustBe: ()=>"defined",
|
|
177
|
-
|
|
178
|
+
writeReason: (mustBe)=>writeDefaultReason(mustBe, "")
|
|
178
179
|
},
|
|
179
180
|
bound: {
|
|
180
181
|
mustBe: (bound)=>`${_scannerTs.Scanner.comparatorDescriptions[bound.comparator]} ${bound.limit}${bound.units ? ` ${bound.units}` : ""}`,
|
|
181
|
-
|
|
182
|
+
writeReason: (mustBe, data)=>writeDefaultReason(mustBe, `${data.size}`)
|
|
182
183
|
},
|
|
183
184
|
regex: {
|
|
184
|
-
mustBe: (
|
|
185
|
+
mustBe: (expression)=>expression[0] === "/" ? `a string matching ${expression}` : `a valid ${expression}`
|
|
185
186
|
},
|
|
186
187
|
value: {
|
|
187
188
|
mustBe: _serializeTs.stringify
|
|
@@ -199,37 +200,12 @@ const defaultProblemWriters = compileDefaultProblemWriters({
|
|
|
199
200
|
}
|
|
200
201
|
return describeBranches(Object.entries(clausesByPath).map(([path, clauses])=>`${path} must be ${clauses.length === 1 ? clauses[0] : describeBranches(clauses)}`));
|
|
201
202
|
},
|
|
202
|
-
|
|
203
|
-
|
|
203
|
+
writeReason: (mustBe, data)=>`${mustBe} (was ${data})`,
|
|
204
|
+
addContext: (reason, path)=>path.length ? `At ${path}, ${reason}` : reason
|
|
204
205
|
},
|
|
205
206
|
multi: {
|
|
206
|
-
mustBe: (problems)=>"• " + problems.join("\n• "),
|
|
207
|
-
|
|
207
|
+
mustBe: (problems)=>"• " + problems.map((_)=>_.mustBe).join("\n• "),
|
|
208
|
+
writeReason: (mustBe, data)=>`${data} must be...\n${mustBe}`,
|
|
209
|
+
addContext: (reason, path)=>path.length ? `At ${path}, ${reason}` : reason
|
|
208
210
|
}
|
|
209
211
|
});
|
|
210
|
-
const codes = (0, _genericsTs.keysOf)(defaultProblemWriters);
|
|
211
|
-
const compileProblemOptions = (opts, base = defaultProblemWriters)=>{
|
|
212
|
-
if (!opts) {
|
|
213
|
-
return base;
|
|
214
|
-
}
|
|
215
|
-
const { all , ...byCode } = opts;
|
|
216
|
-
const result = {};
|
|
217
|
-
let code;
|
|
218
|
-
if (all) {
|
|
219
|
-
for (code of codes){
|
|
220
|
-
result[code] = {
|
|
221
|
-
...base[code],
|
|
222
|
-
...all,
|
|
223
|
-
...byCode[code]
|
|
224
|
-
};
|
|
225
|
-
}
|
|
226
|
-
} else {
|
|
227
|
-
for (code of codes){
|
|
228
|
-
result[code] = byCode[code] ? {
|
|
229
|
-
...base[code],
|
|
230
|
-
...byCode[code]
|
|
231
|
-
} : base[code];
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
return result;
|
|
235
|
-
};
|
|
@@ -69,6 +69,21 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
|
69
69
|
}
|
|
70
70
|
var _seen = /*#__PURE__*/ new WeakMap();
|
|
71
71
|
class TraversalState {
|
|
72
|
+
getConfigForProblemCode(code) {
|
|
73
|
+
if (!this.configs.length) {
|
|
74
|
+
return _problemsJs.defaultProblemWriters[code];
|
|
75
|
+
}
|
|
76
|
+
for(let i = this.configs.length - 1; i >= 0; i--){
|
|
77
|
+
if (this.configs[i][code] || this.configs[i]["defaults"]) {
|
|
78
|
+
return {
|
|
79
|
+
..._problemsJs.defaultProblemWriters[code],
|
|
80
|
+
...this.configs[i]["defaults"],
|
|
81
|
+
...this.configs[i][code]
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return _problemsJs.defaultProblemWriters[code];
|
|
86
|
+
}
|
|
72
87
|
traverseResolution(data, name) {
|
|
73
88
|
const resolution = this.type.meta.scope.resolve(name);
|
|
74
89
|
const id = resolution.meta.id;
|
|
@@ -88,11 +103,10 @@ class TraversalState {
|
|
|
88
103
|
];
|
|
89
104
|
}
|
|
90
105
|
}
|
|
91
|
-
|
|
92
|
-
const lastType = this.type;
|
|
106
|
+
const lastResolution = this.type;
|
|
93
107
|
this.type = resolution;
|
|
94
108
|
traverse(data, resolution.flat, this);
|
|
95
|
-
this.type =
|
|
109
|
+
this.type = lastResolution;
|
|
96
110
|
if (isObject) {
|
|
97
111
|
_classPrivateFieldGet(this, _seen)[id].pop();
|
|
98
112
|
}
|
|
@@ -129,6 +143,9 @@ class TraversalState {
|
|
|
129
143
|
this.problems = new _problemsJs.Problems(this);
|
|
130
144
|
this.failFast = false;
|
|
131
145
|
_classPrivateFieldSet(this, _seen, {});
|
|
146
|
+
this.configs = type.meta.scope.config ? [
|
|
147
|
+
type.meta.scope.config
|
|
148
|
+
] : [];
|
|
132
149
|
}
|
|
133
150
|
}
|
|
134
151
|
const traverse = (data, node, state)=>{
|
|
@@ -260,6 +277,11 @@ const checkers = {
|
|
|
260
277
|
alias: (data, name, state)=>state.traverseResolution(data, name),
|
|
261
278
|
class: _classJs.checkClass,
|
|
262
279
|
narrow: (data, narrow, state)=>narrow(data, state.problems),
|
|
280
|
+
config: (data, { config , node }, state)=>{
|
|
281
|
+
state.configs.push(config);
|
|
282
|
+
traverse(data, node, state);
|
|
283
|
+
state.configs.pop();
|
|
284
|
+
},
|
|
263
285
|
value: (data, value, state)=>{
|
|
264
286
|
if (data !== value) {
|
|
265
287
|
state.problems.add("value", data, value);
|
|
@@ -10,6 +10,7 @@ function _export(target, all) {
|
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
12
|
wellFormedIntegerMatcher: ()=>wellFormedIntegerMatcher,
|
|
13
|
+
isWellFormedInteger: ()=>isWellFormedInteger,
|
|
13
14
|
writeMalformedNumericLiteralMessage: ()=>writeMalformedNumericLiteralMessage,
|
|
14
15
|
tryParseWellFormedNumber: ()=>tryParseWellFormedNumber,
|
|
15
16
|
tryParseWellFormedInteger: ()=>tryParseWellFormedInteger,
|
package/dist/mjs/nodes/node.js
CHANGED
|
@@ -50,11 +50,29 @@ export const flattenType = (type)=>{
|
|
|
50
50
|
path: new Path(),
|
|
51
51
|
lastDomain: "undefined"
|
|
52
52
|
};
|
|
53
|
-
return
|
|
53
|
+
return type.meta.config ? [
|
|
54
|
+
[
|
|
55
|
+
"config",
|
|
56
|
+
{
|
|
57
|
+
config: type.meta.config,
|
|
58
|
+
node: flattenNode(type.node, ctx)
|
|
59
|
+
}
|
|
60
|
+
]
|
|
61
|
+
] : flattenNode(type.node, ctx);
|
|
54
62
|
};
|
|
55
63
|
export const flattenNode = (node, ctx)=>{
|
|
56
64
|
if (typeof node === "string") {
|
|
57
|
-
|
|
65
|
+
const resolution = ctx.type.meta.scope.resolve(node);
|
|
66
|
+
const updatedScopeConfig = resolution.meta.scope !== ctx.type.meta.scope && resolution.meta.scope.config;
|
|
67
|
+
return updatedScopeConfig ? [
|
|
68
|
+
[
|
|
69
|
+
"config",
|
|
70
|
+
{
|
|
71
|
+
config: updatedScopeConfig,
|
|
72
|
+
node: resolution.flat
|
|
73
|
+
}
|
|
74
|
+
]
|
|
75
|
+
] : resolution.flat;
|
|
58
76
|
}
|
|
59
77
|
const domains = keysOf(node);
|
|
60
78
|
if (domains.length === 1) {
|
|
@@ -4,6 +4,7 @@ import { compareBranches, isBranchComparison } from "./branches.js";
|
|
|
4
4
|
import { equality, IntersectionState, isEquality } from "./compose.js";
|
|
5
5
|
import { flattenBranches } from "./discriminate.js";
|
|
6
6
|
import { branchIntersection, flattenBranch } from "./rules/rules.js";
|
|
7
|
+
export const branchIsTransformation = (branch)=>"rules" in branch;
|
|
7
8
|
const emptyRulesIfTrue = (predicate)=>predicate === true ? {} : predicate;
|
|
8
9
|
export const comparePredicates = (l, r, context)=>{
|
|
9
10
|
if (l === true && r === true) {
|
|
@@ -1,53 +1,15 @@
|
|
|
1
1
|
import { composeIntersection } from "../compose.js";
|
|
2
2
|
import { collapsibleListUnion } from "./collapsibleSet.js";
|
|
3
3
|
const regexCache = {};
|
|
4
|
-
// Non-trivial expressions should have an explanation or atttribution
|
|
5
|
-
export const sources = {
|
|
6
|
-
// Character sets
|
|
7
|
-
alpha: /^[A-Z]*$/i,
|
|
8
|
-
alphanumeric: /^[A-Z\d]*$/i,
|
|
9
|
-
lowercase: /^[a-z]*$/,
|
|
10
|
-
uppercase: /^[A-Z]*$/,
|
|
11
|
-
// https://github.com/validatorjs/validator.js
|
|
12
|
-
creditCard: /^(?: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}))$/,
|
|
13
|
-
// https://www.regular-expressions.info
|
|
14
|
-
email: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i,
|
|
15
|
-
// https://github.com/validatorjs/validator.js
|
|
16
|
-
uuid: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
|
|
17
|
-
};
|
|
18
4
|
export const getRegex = (source)=>{
|
|
19
5
|
if (!regexCache[source]) {
|
|
20
6
|
regexCache[source] = new RegExp(source);
|
|
21
7
|
}
|
|
22
8
|
return regexCache[source];
|
|
23
9
|
};
|
|
24
|
-
export const checkRegex = (data,
|
|
25
|
-
if (!
|
|
26
|
-
state.problems.add("regex", data,
|
|
10
|
+
export const checkRegex = (data, source, state)=>{
|
|
11
|
+
if (!getRegex(source).test(data)) {
|
|
12
|
+
state.problems.add("regex", data, state.path.length === 0 && state.type.name !== "type" ? state.type.name : `/${source}/`);
|
|
27
13
|
}
|
|
28
14
|
};
|
|
29
15
|
export const regexIntersection = composeIntersection(collapsibleListUnion);
|
|
30
|
-
// https://github.com/validatorjs/validator.js
|
|
31
|
-
export const isLuhnValid = (creditCardInput)=>{
|
|
32
|
-
const sanitized = creditCardInput.replace(/[- ]+/g, "");
|
|
33
|
-
let sum = 0;
|
|
34
|
-
let digit;
|
|
35
|
-
let tmpNum;
|
|
36
|
-
let shouldDouble;
|
|
37
|
-
for(let i = sanitized.length - 1; i >= 0; i--){
|
|
38
|
-
digit = sanitized.substring(i, i + 1);
|
|
39
|
-
tmpNum = parseInt(digit, 10);
|
|
40
|
-
if (shouldDouble) {
|
|
41
|
-
tmpNum *= 2;
|
|
42
|
-
if (tmpNum >= 10) {
|
|
43
|
-
sum += tmpNum % 10 + 1;
|
|
44
|
-
} else {
|
|
45
|
-
sum += tmpNum;
|
|
46
|
-
}
|
|
47
|
-
} else {
|
|
48
|
-
sum += tmpNum;
|
|
49
|
-
}
|
|
50
|
-
shouldDouble = !shouldDouble;
|
|
51
|
-
}
|
|
52
|
-
return !!(sum % 10 === 0 ? sanitized : false);
|
|
53
|
-
};
|
|
@@ -2,14 +2,15 @@ import { writeImplicitNeverMessage } from "../../parse/string/ast.js";
|
|
|
2
2
|
import { throwParseError } from "../../utils/errors.js";
|
|
3
3
|
import { listFrom } from "../../utils/generics.js";
|
|
4
4
|
import { composeIntersection, composeKeyedIntersection, equality, isDisjoint, isEquality } from "../compose.js";
|
|
5
|
+
import { branchIsTransformation } from "../predicate.js";
|
|
5
6
|
import { classIntersection } from "./class.js";
|
|
6
7
|
import { collapsibleListUnion } from "./collapsibleSet.js";
|
|
7
8
|
import { divisorIntersection } from "./divisor.js";
|
|
8
9
|
import { flattenObjectKind, objectKindIntersection } from "./objectKind.js";
|
|
9
10
|
import { flattenProps, propsIntersection } from "./props.js";
|
|
10
11
|
import { flattenRange, rangeIntersection } from "./range.js";
|
|
11
|
-
import {
|
|
12
|
-
const rulesOf = (branch)=>branch.
|
|
12
|
+
import { regexIntersection } from "./regex.js";
|
|
13
|
+
const rulesOf = (branch)=>branch.rules ?? branch;
|
|
13
14
|
export const branchIntersection = (l, r, state)=>{
|
|
14
15
|
const lRules = rulesOf(l);
|
|
15
16
|
const rRules = rulesOf(r);
|
|
@@ -18,20 +19,20 @@ export const branchIntersection = (l, r, state)=>{
|
|
|
18
19
|
if ("morph" in r) {
|
|
19
20
|
if (l.morph === r.morph) {
|
|
20
21
|
return isEquality(rulesResult) || isDisjoint(rulesResult) ? rulesResult : {
|
|
21
|
-
|
|
22
|
+
rules: rulesResult,
|
|
22
23
|
morph: l.morph
|
|
23
24
|
};
|
|
24
25
|
}
|
|
25
|
-
return state.lastOperator === "&" ? throwParseError(writeImplicitNeverMessage(state.path, "of morphs")) : {};
|
|
26
|
+
return state.lastOperator === "&" ? throwParseError(writeImplicitNeverMessage(state.path, "Intersection", "of morphs")) : {};
|
|
26
27
|
}
|
|
27
28
|
return isDisjoint(rulesResult) ? rulesResult : {
|
|
28
|
-
|
|
29
|
+
rules: isEquality(rulesResult) ? l.rules : rulesResult,
|
|
29
30
|
morph: l.morph
|
|
30
31
|
};
|
|
31
32
|
}
|
|
32
33
|
if ("morph" in r) {
|
|
33
34
|
return isDisjoint(rulesResult) ? rulesResult : {
|
|
34
|
-
|
|
35
|
+
rules: isEquality(rulesResult) ? r.rules : rulesResult,
|
|
35
36
|
morph: r.morph
|
|
36
37
|
};
|
|
37
38
|
}
|
|
@@ -56,7 +57,7 @@ const ruleFlatteners = {
|
|
|
56
57
|
for (const source of listFrom(rule)){
|
|
57
58
|
entries.push([
|
|
58
59
|
"regex",
|
|
59
|
-
|
|
60
|
+
source
|
|
60
61
|
]);
|
|
61
62
|
}
|
|
62
63
|
},
|
|
@@ -91,6 +92,7 @@ const ruleFlatteners = {
|
|
|
91
92
|
};
|
|
92
93
|
export const precedenceMap = {
|
|
93
94
|
// Critical: No other checks are performed if these fail
|
|
95
|
+
config: 0,
|
|
94
96
|
domain: 0,
|
|
95
97
|
value: 0,
|
|
96
98
|
domains: 0,
|
|
@@ -112,12 +114,14 @@ export const precedenceMap = {
|
|
|
112
114
|
morph: 4
|
|
113
115
|
};
|
|
114
116
|
export const flattenBranch = (branch, ctx)=>{
|
|
115
|
-
if (
|
|
116
|
-
const result = flattenRules(branch.
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
117
|
+
if (branchIsTransformation(branch)) {
|
|
118
|
+
const result = flattenRules(branch.rules, ctx);
|
|
119
|
+
if (branch.morph) {
|
|
120
|
+
result.push([
|
|
121
|
+
"morph",
|
|
122
|
+
branch.morph
|
|
123
|
+
]);
|
|
124
|
+
}
|
|
121
125
|
return result;
|
|
122
126
|
}
|
|
123
127
|
return flattenRules(branch, ctx);
|
|
@@ -13,7 +13,7 @@ export const compileDisjointReasonsMessage = (disjoints)=>{
|
|
|
13
13
|
}
|
|
14
14
|
return message;
|
|
15
15
|
};
|
|
16
|
-
export const writeImplicitNeverMessage = (path, description)=>`${path.length ? `At ${path}: ` : ""}
|
|
16
|
+
export const writeImplicitNeverMessage = (path, operator, description)=>`${path.length ? `At ${path}: ` : ""}${operator} ${description ? `${description} ` : ""}results in an unsatisfiable type`;
|
|
17
17
|
export const writeUndiscriminatableMorphUnionMessage = (path)=>`${path === "/" ? "A" : `At ${path}, a`} union including one or more morphs must be discriminatable`;
|
|
18
18
|
export const writeIndivisibleMessage = (root)=>`Divisibility operand ${root} must be a number`;
|
|
19
19
|
export const writeUnboundableMessage = (root)=>`Bounded expression ${root} must be a number, string or array`;
|
|
@@ -2,15 +2,14 @@ import { resolutionExtendsDomain } from "../../nodes/predicate.js";
|
|
|
2
2
|
import { throwParseError } from "../../utils/errors.js";
|
|
3
3
|
import { hasKey, listFrom } from "../../utils/generics.js";
|
|
4
4
|
import { parseDefinition } from "../definition.js";
|
|
5
|
+
import { writeImplicitNeverMessage } from "../string/ast.js";
|
|
5
6
|
export const parseKeyOfTuple = (def, ctx)=>{
|
|
6
7
|
const resolution = ctx.type.meta.scope.resolveNode(parseDefinition(def[1], ctx));
|
|
7
8
|
if (!resolutionExtendsDomain(resolution, "object")) {
|
|
8
|
-
return throwParseError("
|
|
9
|
+
return throwParseError(writeImplicitNeverMessage(ctx.path, "keyof"));
|
|
9
10
|
}
|
|
10
11
|
if (resolution.object === true) {
|
|
11
|
-
return
|
|
12
|
-
string: true
|
|
13
|
-
};
|
|
12
|
+
return writeImplicitNeverMessage(ctx.path, "keyof");
|
|
14
13
|
}
|
|
15
14
|
const branchKeys = [];
|
|
16
15
|
const objectBranches = listFrom(resolution.object);
|
|
@@ -18,7 +17,7 @@ export const parseKeyOfTuple = (def, ctx)=>{
|
|
|
18
17
|
for(let i = 1; i < objectBranches.length; i++){
|
|
19
18
|
const keys = getPropsFromBranch(objectBranches[i]);
|
|
20
19
|
if (!keys.length) {
|
|
21
|
-
return
|
|
20
|
+
return writeImplicitNeverMessage(ctx.path, "keyof");
|
|
22
21
|
}
|
|
23
22
|
branchKeys.push(keys);
|
|
24
23
|
}
|
|
@@ -31,8 +30,8 @@ export const parseKeyOfTuple = (def, ctx)=>{
|
|
|
31
30
|
});
|
|
32
31
|
}
|
|
33
32
|
});
|
|
34
|
-
return {
|
|
33
|
+
return result.length ? {
|
|
35
34
|
string: result
|
|
36
|
-
};
|
|
35
|
+
} : writeImplicitNeverMessage(ctx.path, "keyof");
|
|
37
36
|
};
|
|
38
37
|
const getPropsFromBranch = (branch)=>hasKey(branch, "props") ? Object.keys(branch.props) : [];
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { branchIsTransformation } from "../../nodes/predicate.js";
|
|
1
2
|
import { throwInternalError, throwParseError } from "../../utils/errors.js";
|
|
2
3
|
import { isArray } from "../../utils/objectKinds.js";
|
|
3
4
|
import { stringify } from "../../utils/serialize.js";
|
|
@@ -15,7 +16,7 @@ export const parseMorphTuple = (def, ctx)=>{
|
|
|
15
16
|
const predicate = resolution[domain];
|
|
16
17
|
if (predicate === true) {
|
|
17
18
|
result[domain] = {
|
|
18
|
-
|
|
19
|
+
rules: {},
|
|
19
20
|
morph
|
|
20
21
|
};
|
|
21
22
|
} else if (typeof predicate === "object") {
|
|
@@ -26,17 +27,17 @@ export const parseMorphTuple = (def, ctx)=>{
|
|
|
26
27
|
}
|
|
27
28
|
return result;
|
|
28
29
|
};
|
|
29
|
-
const applyMorph = (branch, morph)=>branch
|
|
30
|
-
|
|
31
|
-
morph: Array.isArray(branch.morph) ? [
|
|
30
|
+
const applyMorph = (branch, morph)=>branchIsTransformation(branch) ? {
|
|
31
|
+
...branch,
|
|
32
|
+
morph: branch.morph ? Array.isArray(branch.morph) ? [
|
|
32
33
|
...branch.morph,
|
|
33
34
|
morph
|
|
34
35
|
] : [
|
|
35
36
|
branch.morph,
|
|
36
37
|
morph
|
|
37
|
-
]
|
|
38
|
+
] : morph
|
|
38
39
|
} : {
|
|
39
|
-
|
|
40
|
+
rules: branch,
|
|
40
41
|
morph
|
|
41
42
|
};
|
|
42
43
|
export const writeMalformedMorphExpressionMessage = (value)=>`Morph expression requires a function following '=>' (was ${typeof value})`;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { deepFreeze } from "../utils/freeze.js";
|
|
2
|
+
export class Cache {
|
|
3
|
+
get root() {
|
|
4
|
+
return this.cache;
|
|
5
|
+
}
|
|
6
|
+
has(name) {
|
|
7
|
+
return name in this.cache;
|
|
8
|
+
}
|
|
9
|
+
get(name) {
|
|
10
|
+
return this.cache[name];
|
|
11
|
+
}
|
|
12
|
+
set(name, item) {
|
|
13
|
+
this.cache[name] = item;
|
|
14
|
+
return item;
|
|
15
|
+
}
|
|
16
|
+
constructor(){
|
|
17
|
+
this.cache = {};
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export class FreezingCache extends Cache {
|
|
21
|
+
set(name, item) {
|
|
22
|
+
this.cache[name] = deepFreeze(item);
|
|
23
|
+
return item;
|
|
24
|
+
}
|
|
25
|
+
}
|