arktype 0.5.1 → 0.6.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/api.cjs +4 -2
- package/dist/api.d.ts +1 -1
- package/dist/api.mjs +1 -1
- package/dist/cjs/nodes/branch.js +3 -3
- package/dist/cjs/nodes/discriminate.js +2 -2
- package/dist/cjs/nodes/node.js +13 -18
- package/dist/cjs/nodes/predicate.js +1 -6
- package/dist/cjs/nodes/rules/props.js +39 -0
- package/dist/cjs/nodes/rules/rules.js +5 -2
- package/dist/cjs/parse/ast/config.js +15 -0
- package/dist/cjs/parse/ast/distributableFunction.js +1 -1
- package/dist/cjs/parse/ast/keyof.js +1 -1
- package/dist/cjs/parse/ast/morph.js +3 -3
- package/dist/cjs/{nodes/infer.js → parse/ast/node.js} +0 -0
- package/dist/cjs/parse/ast/tuple.js +2 -1
- package/dist/cjs/parse/definition.js +2 -2
- package/dist/cjs/parse/string/reduce/dynamic.js +2 -2
- package/dist/cjs/parse/string/shift/operand/unenclosed.js +1 -1
- package/dist/cjs/parse/string/string.js +3 -3
- package/dist/cjs/scopes/{standard.js → ark.js} +9 -8
- package/dist/cjs/scopes/expressions.js +9 -9
- package/dist/cjs/scopes/scope.js +73 -63
- package/dist/cjs/scopes/type.js +10 -29
- package/dist/cjs/scopes/validation/creditCard.js +47 -0
- package/dist/cjs/scopes/validation/date.js +63 -0
- package/dist/cjs/scopes/validation/validation.js +90 -0
- package/dist/cjs/traverse/problems.js +45 -16
- package/dist/cjs/traverse/traverse.js +63 -26
- package/dist/mjs/nodes/branch.js +2 -2
- package/dist/mjs/nodes/discriminate.js +2 -2
- package/dist/mjs/nodes/node.js +13 -19
- package/dist/mjs/nodes/predicate.js +1 -5
- package/dist/mjs/nodes/rules/props.js +39 -0
- package/dist/mjs/nodes/rules/rules.js +5 -2
- package/dist/mjs/parse/ast/config.js +7 -0
- package/dist/mjs/parse/ast/distributableFunction.js +1 -1
- package/dist/mjs/parse/ast/keyof.js +1 -1
- package/dist/mjs/parse/ast/morph.js +4 -4
- package/dist/mjs/{nodes/infer.js → parse/ast/node.js} +0 -0
- package/dist/mjs/parse/ast/tuple.js +2 -1
- package/dist/mjs/parse/definition.js +2 -2
- package/dist/mjs/parse/string/reduce/dynamic.js +2 -2
- package/dist/mjs/parse/string/shift/operand/unenclosed.js +1 -1
- package/dist/mjs/parse/string/string.js +3 -3
- package/dist/mjs/scopes/{standard.js → ark.js} +8 -7
- package/dist/mjs/scopes/expressions.js +9 -9
- package/dist/mjs/scopes/scope.js +69 -63
- package/dist/mjs/scopes/type.js +10 -29
- package/dist/mjs/scopes/validation/creditCard.js +34 -0
- package/dist/mjs/scopes/validation/date.js +49 -0
- package/dist/mjs/scopes/validation/validation.js +76 -0
- package/dist/mjs/traverse/problems.js +42 -15
- package/dist/mjs/traverse/traverse.js +64 -27
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/nodes/branch.d.ts +6 -6
- package/dist/types/nodes/node.d.ts +10 -5
- package/dist/types/nodes/predicate.d.ts +2 -6
- package/dist/types/nodes/rules/props.d.ts +15 -0
- package/dist/types/nodes/rules/rules.d.ts +2 -2
- package/dist/types/parse/ast/ast.d.ts +1 -1
- package/dist/types/parse/ast/config.d.ts +10 -0
- package/dist/types/parse/ast/narrow.d.ts +1 -1
- package/dist/types/{nodes/infer.d.ts → parse/ast/node.d.ts} +12 -12
- package/dist/types/parse/ast/tuple.d.ts +4 -3
- package/dist/types/scopes/{standard.d.ts → ark.d.ts} +12 -4
- package/dist/types/scopes/expressions.d.ts +3 -3
- package/dist/types/scopes/scope.d.ts +19 -7
- package/dist/types/scopes/type.d.ts +20 -12
- package/dist/types/scopes/validation/creditCard.d.ts +2 -0
- package/dist/types/scopes/validation/date.d.ts +23 -0
- package/dist/types/scopes/validation/validation.d.ts +26 -0
- package/dist/types/traverse/problems.d.ts +14 -8
- package/dist/types/traverse/traverse.d.ts +13 -3
- package/dist/types/utils/generics.d.ts +5 -5
- package/package.json +1 -1
- package/dist/cjs/scopes/validation.js +0 -79
- package/dist/mjs/scopes/validation.js +0 -66
- package/dist/types/scopes/validation.d.ts +0 -25
package/dist/cjs/scopes/scope.js
CHANGED
|
@@ -9,13 +9,18 @@ function _export(target, all) {
|
|
|
9
9
|
});
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
|
+
compileScopeOptions: ()=>compileScopeOptions,
|
|
13
|
+
isConfigTuple: ()=>isConfigTuple,
|
|
12
14
|
Scope: ()=>Scope,
|
|
13
15
|
scope: ()=>scope,
|
|
16
|
+
rootScope: ()=>rootScope,
|
|
17
|
+
rootType: ()=>rootType,
|
|
14
18
|
writeShallowCycleErrorMessage: ()=>writeShallowCycleErrorMessage,
|
|
15
19
|
writeDuplicateAliasesMessage: ()=>writeDuplicateAliasesMessage
|
|
16
20
|
});
|
|
17
21
|
const _nodeTs = require("../nodes/node.js");
|
|
18
22
|
const _definitionTs = require("../parse/definition.js");
|
|
23
|
+
const _problemsTs = require("../traverse/problems.js");
|
|
19
24
|
const _chainableNoOpProxyTs = require("../utils/chainableNoOpProxy.js");
|
|
20
25
|
const _errorsTs = require("../utils/errors.js");
|
|
21
26
|
const _freezeTs = require("../utils/freeze.js");
|
|
@@ -43,29 +48,6 @@ function _classApplyDescriptorSet(receiver, descriptor, value) {
|
|
|
43
48
|
descriptor.value = value;
|
|
44
49
|
}
|
|
45
50
|
}
|
|
46
|
-
function _classApplyDescriptorUpdate(receiver, descriptor) {
|
|
47
|
-
if (descriptor.set) {
|
|
48
|
-
if (!descriptor.get) {
|
|
49
|
-
throw new TypeError("attempted to read set only private field");
|
|
50
|
-
}
|
|
51
|
-
if (!("__destrWrapper" in descriptor)) {
|
|
52
|
-
descriptor.__destrWrapper = {
|
|
53
|
-
set value (v){
|
|
54
|
-
descriptor.set.call(receiver, v);
|
|
55
|
-
},
|
|
56
|
-
get value () {
|
|
57
|
-
return descriptor.get.call(receiver);
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
return descriptor.__destrWrapper;
|
|
62
|
-
} else {
|
|
63
|
-
if (!descriptor.writable) {
|
|
64
|
-
throw new TypeError("attempted to set read only private field");
|
|
65
|
-
}
|
|
66
|
-
return descriptor;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
51
|
function _classExtractFieldDescriptor(receiver, privateMap, action) {
|
|
70
52
|
if (!privateMap.has(receiver)) {
|
|
71
53
|
throw new TypeError("attempted to " + action + " private field on non-instance");
|
|
@@ -85,10 +67,6 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
|
85
67
|
_classApplyDescriptorSet(receiver, descriptor, value);
|
|
86
68
|
return value;
|
|
87
69
|
}
|
|
88
|
-
function _classPrivateFieldUpdate(receiver, privateMap) {
|
|
89
|
-
var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "update");
|
|
90
|
-
return _classApplyDescriptorUpdate(receiver, descriptor);
|
|
91
|
-
}
|
|
92
70
|
function _classPrivateMethodGet(receiver, privateSet, fn) {
|
|
93
71
|
if (!privateSet.has(receiver)) {
|
|
94
72
|
throw new TypeError("attempted to get private field on non-instance");
|
|
@@ -99,14 +77,45 @@ function _classPrivateMethodInit(obj, privateSet) {
|
|
|
99
77
|
_checkPrivateRedeclaration(obj, privateSet);
|
|
100
78
|
privateSet.add(obj);
|
|
101
79
|
}
|
|
102
|
-
var
|
|
80
|
+
var _ctx_type;
|
|
81
|
+
const compileScopeOptions = (opts)=>({
|
|
82
|
+
codes: (0, _problemsTs.compileProblemWriters)(opts.codes),
|
|
83
|
+
keys: opts.keys ?? "loose"
|
|
84
|
+
});
|
|
103
85
|
let anonymousScopeCount = 0;
|
|
104
86
|
const scopeRegistry = {};
|
|
105
87
|
const spaceRegistry = {};
|
|
106
|
-
|
|
88
|
+
const isConfigTuple = (def)=>Array.isArray(def) && def[1] === ":";
|
|
89
|
+
var _resolutions = /*#__PURE__*/ new WeakMap(), _exports = /*#__PURE__*/ new WeakMap(), _register = /*#__PURE__*/ new WeakSet(), _cacheSpaces = /*#__PURE__*/ new WeakSet(), _initializeContext = /*#__PURE__*/ new WeakSet(), _resolveRecurse = /*#__PURE__*/ new WeakSet();
|
|
107
90
|
class Scope {
|
|
108
|
-
|
|
109
|
-
|
|
91
|
+
getAnonymousTypeName(ctx) {
|
|
92
|
+
let base = ctx ? ctx.path.length ? `${ctx.path}` : ctx.type.name : "type";
|
|
93
|
+
if (base[0] !== "λ") {
|
|
94
|
+
base = `λ${base}`;
|
|
95
|
+
}
|
|
96
|
+
let increment = 1;
|
|
97
|
+
let name = base;
|
|
98
|
+
while(this.isInternallyResolvable(name)){
|
|
99
|
+
name = `${base}${++increment}`;
|
|
100
|
+
}
|
|
101
|
+
return name;
|
|
102
|
+
}
|
|
103
|
+
addAnonymous(type, ctx) {
|
|
104
|
+
if (this.isInternallyResolvable(type.name)) {
|
|
105
|
+
if (type === this.resolve(type.name)) {
|
|
106
|
+
return type.name;
|
|
107
|
+
}
|
|
108
|
+
const name = this.getAnonymousTypeName(ctx);
|
|
109
|
+
this.type([
|
|
110
|
+
"node",
|
|
111
|
+
type.node
|
|
112
|
+
], {
|
|
113
|
+
name
|
|
114
|
+
});
|
|
115
|
+
return name;
|
|
116
|
+
}
|
|
117
|
+
_classPrivateFieldGet(this, _resolutions).set(type.name, type);
|
|
118
|
+
return type.name;
|
|
110
119
|
}
|
|
111
120
|
get infer() {
|
|
112
121
|
return _chainableNoOpProxyTs.chainableNoOpProxy;
|
|
@@ -125,19 +134,19 @@ class Scope {
|
|
|
125
134
|
if (!resolution) {
|
|
126
135
|
return false;
|
|
127
136
|
}
|
|
128
|
-
(
|
|
137
|
+
(_ctx_type = ctx.type).includesMorph || (_ctx_type.includesMorph = resolution.includesMorph);
|
|
129
138
|
return true;
|
|
130
139
|
}
|
|
131
140
|
resolve(name) {
|
|
132
141
|
return _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, name, "throw", []);
|
|
133
142
|
}
|
|
134
143
|
resolveNode(node) {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
return
|
|
144
|
+
return typeof node === "object" ? node : this.resolveNode(this.resolve(node).node);
|
|
145
|
+
}
|
|
146
|
+
isInternallyResolvable(name) {
|
|
147
|
+
return _classPrivateFieldGet(this, _resolutions).has(name) || this.aliases[name];
|
|
139
148
|
}
|
|
140
|
-
constructor(aliases, opts){
|
|
149
|
+
constructor(aliases, opts = {}){
|
|
141
150
|
_classPrivateMethodInit(this, _register);
|
|
142
151
|
_classPrivateMethodInit(this, _cacheSpaces);
|
|
143
152
|
_classPrivateMethodInit(this, _initializeContext);
|
|
@@ -150,16 +159,10 @@ class Scope {
|
|
|
150
159
|
writable: true,
|
|
151
160
|
value: void 0
|
|
152
161
|
});
|
|
153
|
-
_classPrivateFieldInit(this, _anonymousTypeCount, {
|
|
154
|
-
writable: true,
|
|
155
|
-
value: void 0
|
|
156
|
-
});
|
|
157
162
|
this.aliases = aliases;
|
|
158
|
-
this.opts = opts;
|
|
159
163
|
this.parseCache = new _cacheTs.FreezingCache();
|
|
160
164
|
_classPrivateFieldSet(this, _resolutions, new _cacheTs.Cache());
|
|
161
165
|
_classPrivateFieldSet(this, _exports, new _cacheTs.Cache());
|
|
162
|
-
_classPrivateFieldSet(this, _anonymousTypeCount, 0);
|
|
163
166
|
this.expressions = {
|
|
164
167
|
intersection: (l, r, opts)=>this.type([
|
|
165
168
|
l,
|
|
@@ -179,7 +182,7 @@ class Scope {
|
|
|
179
182
|
"keyof",
|
|
180
183
|
def
|
|
181
184
|
], opts),
|
|
182
|
-
|
|
185
|
+
node: (def, opts)=>this.type([
|
|
183
186
|
"node",
|
|
184
187
|
def
|
|
185
188
|
], opts),
|
|
@@ -210,17 +213,19 @@ class Scope {
|
|
|
210
213
|
this.instanceOf = this.expressions.instanceOf;
|
|
211
214
|
this.narrow = this.expressions.narrow;
|
|
212
215
|
this.morph = this.expressions.morph;
|
|
213
|
-
this.type = Object.assign((def, opts
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
const
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
216
|
+
this.type = Object.assign((def, opts)=>{
|
|
217
|
+
const name = opts?.name ? this.isInternallyResolvable(opts.name) ? (0, _errorsTs.throwParseError)(writeDuplicateAliasesMessage(opts.name)) : opts.name : this.getAnonymousTypeName();
|
|
218
|
+
// remove name from opts since we've already used it and otherwise it will
|
|
219
|
+
// interfere with traversal keys being ProblemOptions
|
|
220
|
+
delete opts?.name;
|
|
221
|
+
const t = (0, _typeTs.initializeType)(name, def, opts, this);
|
|
222
|
+
const ctx = _classPrivateMethodGet(this, _initializeContext, initializeContext).call(this, t);
|
|
223
|
+
t.node = (0, _freezeTs.deepFreeze)((0, _definitionTs.parseDefinition)(def, ctx));
|
|
224
|
+
t.flat = (0, _freezeTs.deepFreeze)((0, _nodeTs.flattenType)(t));
|
|
225
|
+
_classPrivateFieldGet(this, _resolutions).set(t.name, t);
|
|
226
|
+
return t;
|
|
222
227
|
}, {
|
|
223
|
-
from: this.expressions.
|
|
228
|
+
from: this.expressions.node
|
|
224
229
|
});
|
|
225
230
|
this.name = _classPrivateMethodGet(this, _register, register).call(this, opts);
|
|
226
231
|
if (opts.standard !== false) {
|
|
@@ -234,9 +239,7 @@ class Scope {
|
|
|
234
239
|
if (opts.includes) {
|
|
235
240
|
_classPrivateMethodGet(this, _cacheSpaces, cacheSpaces).call(this, opts.includes, "includes");
|
|
236
241
|
}
|
|
237
|
-
|
|
238
|
-
this.config = opts.config;
|
|
239
|
-
}
|
|
242
|
+
this.config = compileScopeOptions(opts);
|
|
240
243
|
}
|
|
241
244
|
}
|
|
242
245
|
function register(opts) {
|
|
@@ -268,28 +271,35 @@ function resolveRecurse(name, onUnresolvable, seen) {
|
|
|
268
271
|
if (maybeCacheResult) {
|
|
269
272
|
return maybeCacheResult;
|
|
270
273
|
}
|
|
271
|
-
|
|
274
|
+
const aliasValue = this.aliases[name];
|
|
275
|
+
if (!aliasValue) {
|
|
272
276
|
return onUnresolvable === "throw" ? (0, _errorsTs.throwInternalError)(`Unexpectedly failed to resolve alias '${name}'`) : undefined;
|
|
273
277
|
}
|
|
274
|
-
const type = (0, _typeTs.initializeType)(
|
|
275
|
-
name
|
|
276
|
-
}, this);
|
|
278
|
+
const type = (0, _typeTs.initializeType)(name, aliasValue, undefined, this);
|
|
277
279
|
_classPrivateFieldGet(this, _resolutions).set(name, type);
|
|
278
280
|
_classPrivateFieldGet(this, _exports).set(name, type);
|
|
279
281
|
const ctx = _classPrivateMethodGet(this, _initializeContext, initializeContext).call(this, type);
|
|
280
|
-
|
|
282
|
+
const resolution = (0, _definitionTs.parseDefinition)(type.definition, ctx);
|
|
281
283
|
if (typeof resolution === "string") {
|
|
282
284
|
if (seen.includes(resolution)) {
|
|
283
285
|
return (0, _errorsTs.throwParseError)(writeShallowCycleErrorMessage(name, seen));
|
|
284
286
|
}
|
|
285
287
|
seen.push(resolution);
|
|
286
|
-
|
|
288
|
+
const resolvedType = _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, resolution, "throw", seen);
|
|
289
|
+
type.node = resolvedType.node;
|
|
290
|
+
type.flat = resolvedType.flat;
|
|
291
|
+
return type;
|
|
287
292
|
}
|
|
288
293
|
type.node = (0, _freezeTs.deepFreeze)(resolution);
|
|
289
294
|
type.flat = (0, _freezeTs.deepFreeze)((0, _nodeTs.flattenType)(type));
|
|
290
295
|
return type;
|
|
291
296
|
}
|
|
292
297
|
const scope = (aliases, opts = {})=>new Scope(aliases, opts);
|
|
298
|
+
const rootScope = scope({}, {
|
|
299
|
+
name: "empty",
|
|
300
|
+
standard: false
|
|
301
|
+
});
|
|
302
|
+
const rootType = rootScope.type;
|
|
293
303
|
const writeShallowCycleErrorMessage = (name, seen)=>`Alias '${name}' has a shallow resolution cycle: ${[
|
|
294
304
|
...seen,
|
|
295
305
|
name
|
package/dist/cjs/scopes/type.js
CHANGED
|
@@ -14,27 +14,7 @@ _export(exports, {
|
|
|
14
14
|
});
|
|
15
15
|
const _traverseTs = require("../traverse/traverse.js");
|
|
16
16
|
const _chainableNoOpProxyTs = require("../utils/chainableNoOpProxy.js");
|
|
17
|
-
const
|
|
18
|
-
const compileTypeConfig = (opts)=>{
|
|
19
|
-
if (opts === undefined) {
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
const { name , ...config } = opts;
|
|
23
|
-
if ((0, _genericsTs.hasKeys)(config)) {
|
|
24
|
-
return config;
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
const initializeType = (definition, opts, scope)=>{
|
|
28
|
-
const name = opts?.name ?? "type";
|
|
29
|
-
const config = compileTypeConfig(opts);
|
|
30
|
-
const meta = {
|
|
31
|
-
name,
|
|
32
|
-
id: `${scope.name}.${opts?.name ? name : `type${scope.createAnonymousTypeSuffix()}`}`,
|
|
33
|
-
definition,
|
|
34
|
-
scope,
|
|
35
|
-
config,
|
|
36
|
-
includesMorph: false
|
|
37
|
-
};
|
|
17
|
+
const initializeType = (name, definition, config, scope)=>{
|
|
38
18
|
const root = {
|
|
39
19
|
// temporarily initialize node/flat to aliases that will be included in
|
|
40
20
|
// the final type in case of cyclic resolutions
|
|
@@ -45,23 +25,24 @@ const initializeType = (definition, opts, scope)=>{
|
|
|
45
25
|
name
|
|
46
26
|
]
|
|
47
27
|
],
|
|
48
|
-
|
|
49
|
-
|
|
28
|
+
infer: _chainableNoOpProxyTs.chainableNoOpProxy,
|
|
29
|
+
qualifiedName: `${scope.name}.${name}`,
|
|
30
|
+
definition,
|
|
31
|
+
scope,
|
|
32
|
+
includesMorph: false,
|
|
33
|
+
config
|
|
50
34
|
};
|
|
51
35
|
// dynamically assign a name to the primary traversal function
|
|
52
36
|
const namedTraverse = {
|
|
53
37
|
[name]: (data)=>{
|
|
54
|
-
const state = new _traverseTs.TraversalState(data,
|
|
55
|
-
return (0, _traverseTs.traverse)(
|
|
38
|
+
const state = new _traverseTs.TraversalState(data, namedTraverse);
|
|
39
|
+
return (0, _traverseTs.traverse)(namedTraverse.flat, state) ? {
|
|
56
40
|
data: state.data
|
|
57
41
|
} : {
|
|
58
42
|
problems: state.problems
|
|
59
43
|
};
|
|
60
44
|
}
|
|
61
45
|
}[name];
|
|
62
|
-
|
|
63
|
-
// it in namedTraverse
|
|
64
|
-
const type = Object.assign(namedTraverse, root);
|
|
65
|
-
return type;
|
|
46
|
+
return Object.assign(namedTraverse, root);
|
|
66
47
|
};
|
|
67
48
|
const isType = (value)=>value?.infer === _chainableNoOpProxyTs.chainableNoOpProxy;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
isLuhnValid: ()=>isLuhnValid,
|
|
13
|
+
creditCard: ()=>creditCard
|
|
14
|
+
});
|
|
15
|
+
const _scopeTs = require("../scope.js");
|
|
16
|
+
const isLuhnValid = (creditCardInput)=>{
|
|
17
|
+
const sanitized = creditCardInput.replace(/[- ]+/g, "");
|
|
18
|
+
let sum = 0;
|
|
19
|
+
let digit;
|
|
20
|
+
let tmpNum;
|
|
21
|
+
let shouldDouble;
|
|
22
|
+
for(let i = sanitized.length - 1; i >= 0; i--){
|
|
23
|
+
digit = sanitized.substring(i, i + 1);
|
|
24
|
+
tmpNum = parseInt(digit, 10);
|
|
25
|
+
if (shouldDouble) {
|
|
26
|
+
tmpNum *= 2;
|
|
27
|
+
if (tmpNum >= 10) {
|
|
28
|
+
sum += tmpNum % 10 + 1;
|
|
29
|
+
} else {
|
|
30
|
+
sum += tmpNum;
|
|
31
|
+
}
|
|
32
|
+
} else {
|
|
33
|
+
sum += tmpNum;
|
|
34
|
+
}
|
|
35
|
+
shouldDouble = !shouldDouble;
|
|
36
|
+
}
|
|
37
|
+
return !!(sum % 10 === 0 ? sanitized : false);
|
|
38
|
+
};
|
|
39
|
+
// https://github.com/validatorjs/validator.js/blob/master/src/lib/isCreditCard.js
|
|
40
|
+
const creditCardMatcher = /^(?: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}))$/;
|
|
41
|
+
const creditCard = (0, _scopeTs.rootType)([
|
|
42
|
+
creditCardMatcher,
|
|
43
|
+
"=>",
|
|
44
|
+
(s, problems)=>isLuhnValid(s) || !problems.mustBe("a valid credit card number")
|
|
45
|
+
], {
|
|
46
|
+
mustBe: "a valid credit card number"
|
|
47
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
tryParseDate: ()=>tryParseDate,
|
|
13
|
+
parsedDate: ()=>parsedDate
|
|
14
|
+
});
|
|
15
|
+
const _scopeTs = require("../scope.js");
|
|
16
|
+
const dayDelimiterMatcher = /^[./-]$/;
|
|
17
|
+
// ISO 8601 date/time modernized from https://github.com/validatorjs/validator.js/blob/master/src/lib/isISO8601.js
|
|
18
|
+
// Based on https://tc39.es/ecma262/#sec-date-time-string-format, the T
|
|
19
|
+
// delimiter for date/time is mandatory. Regex from validator.js strict matcher:
|
|
20
|
+
const iso8601Matcher = /^([+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([.,]\d+(?!:))?)?(\17[0-5]\d([.,]\d+)?)?([zZ]|([+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
|
|
21
|
+
const isValidDateInstance = (date)=>!isNaN(date);
|
|
22
|
+
const writeFormattedMustBe = (format)=>`a ${format}-formatted date`;
|
|
23
|
+
const tryParseDate = (data, opts)=>{
|
|
24
|
+
if (!opts?.format) {
|
|
25
|
+
const result = new Date(data);
|
|
26
|
+
return isValidDateInstance(result) ? result : "a valid date";
|
|
27
|
+
}
|
|
28
|
+
if (opts.format === "iso8601") {
|
|
29
|
+
return iso8601Matcher.test(data) ? new Date(data) : writeFormattedMustBe("iso8601");
|
|
30
|
+
}
|
|
31
|
+
const dataParts = data.split(dayDelimiterMatcher);
|
|
32
|
+
// will be the first delimiter matched, if there is one
|
|
33
|
+
const delimiter = data[dataParts[0].length];
|
|
34
|
+
const formatParts = delimiter ? opts.format.split(delimiter) : [
|
|
35
|
+
opts.format
|
|
36
|
+
];
|
|
37
|
+
if (dataParts.length !== formatParts.length) {
|
|
38
|
+
return writeFormattedMustBe(opts.format);
|
|
39
|
+
}
|
|
40
|
+
const parsedParts = {};
|
|
41
|
+
for(let i = 0; i < formatParts.length; i++){
|
|
42
|
+
if (dataParts[i].length !== formatParts[i].length && // if format is "m" or "d", data is allowed to be 1 or 2 characters
|
|
43
|
+
!(formatParts[i].length === 1 && dataParts[i].length === 2)) {
|
|
44
|
+
return writeFormattedMustBe(opts.format);
|
|
45
|
+
}
|
|
46
|
+
parsedParts[formatParts[i][0]] = dataParts[i];
|
|
47
|
+
}
|
|
48
|
+
const date = new Date(`${parsedParts.m}/${parsedParts.d}/${parsedParts.y}`);
|
|
49
|
+
if (`${date.getDate()}` === parsedParts.d) {
|
|
50
|
+
return date;
|
|
51
|
+
}
|
|
52
|
+
return writeFormattedMustBe(opts.format);
|
|
53
|
+
};
|
|
54
|
+
const parsedDate = (0, _scopeTs.rootType)([
|
|
55
|
+
_scopeTs.rootType.from({
|
|
56
|
+
string: true
|
|
57
|
+
}),
|
|
58
|
+
"|>",
|
|
59
|
+
(s, problems)=>{
|
|
60
|
+
const result = tryParseDate(s);
|
|
61
|
+
return typeof result === "string" ? problems.mustBe(result) : result;
|
|
62
|
+
}
|
|
63
|
+
]);
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
validationScope: ()=>validationScope,
|
|
13
|
+
validation: ()=>validation
|
|
14
|
+
});
|
|
15
|
+
const _numericLiteralsTs = require("../../utils/numericLiterals.js");
|
|
16
|
+
const _scopeTs = require("../scope.js");
|
|
17
|
+
const _creditCardTs = require("./creditCard.js");
|
|
18
|
+
const _dateTs = require("./date.js");
|
|
19
|
+
// Non-trivial expressions should have an explanation or attribution
|
|
20
|
+
const parsedNumber = (0, _scopeTs.rootType)([
|
|
21
|
+
_numericLiteralsTs.wellFormedNumberMatcher,
|
|
22
|
+
"|>",
|
|
23
|
+
(s)=>parseFloat(s)
|
|
24
|
+
], {
|
|
25
|
+
mustBe: "a well-formed numeric string"
|
|
26
|
+
});
|
|
27
|
+
const parsedInteger = (0, _scopeTs.rootType)([
|
|
28
|
+
_numericLiteralsTs.wellFormedIntegerMatcher,
|
|
29
|
+
"|>",
|
|
30
|
+
(s)=>parseInt(s)
|
|
31
|
+
], {
|
|
32
|
+
mustBe: "a well-formed integer string"
|
|
33
|
+
});
|
|
34
|
+
// https://www.regular-expressions.info/email.html
|
|
35
|
+
const email = (0, _scopeTs.rootType)(/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/, {
|
|
36
|
+
mustBe: "a valid email"
|
|
37
|
+
});
|
|
38
|
+
// https://github.com/validatorjs/validator.js/blob/master/src/lib/isUUID.js
|
|
39
|
+
const uuid = (0, _scopeTs.rootType)(/^[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}$/, {
|
|
40
|
+
mustBe: "a valid UUID"
|
|
41
|
+
});
|
|
42
|
+
const validationScope = (0, _scopeTs.scope)({
|
|
43
|
+
// Character sets
|
|
44
|
+
alpha: [
|
|
45
|
+
/^[A-Za-z]*$/,
|
|
46
|
+
":",
|
|
47
|
+
{
|
|
48
|
+
mustBe: "only letters"
|
|
49
|
+
}
|
|
50
|
+
],
|
|
51
|
+
alphanumeric: [
|
|
52
|
+
/^[A-Za-z\d]*$/,
|
|
53
|
+
":",
|
|
54
|
+
{
|
|
55
|
+
mustBe: "only letters and digits"
|
|
56
|
+
}
|
|
57
|
+
],
|
|
58
|
+
lowercase: [
|
|
59
|
+
/^[a-z]*$/,
|
|
60
|
+
":",
|
|
61
|
+
{
|
|
62
|
+
mustBe: "only lowercase letters"
|
|
63
|
+
}
|
|
64
|
+
],
|
|
65
|
+
uppercase: [
|
|
66
|
+
/^[A-Z]*$/,
|
|
67
|
+
":",
|
|
68
|
+
{
|
|
69
|
+
mustBe: "only uppercase letters"
|
|
70
|
+
}
|
|
71
|
+
],
|
|
72
|
+
creditCard: _creditCardTs.creditCard,
|
|
73
|
+
email,
|
|
74
|
+
uuid,
|
|
75
|
+
parsedNumber,
|
|
76
|
+
parsedInteger,
|
|
77
|
+
parsedDate: _dateTs.parsedDate,
|
|
78
|
+
integer: [
|
|
79
|
+
"node",
|
|
80
|
+
{
|
|
81
|
+
number: {
|
|
82
|
+
divisor: 1
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
]
|
|
86
|
+
}, {
|
|
87
|
+
name: "validation",
|
|
88
|
+
standard: false
|
|
89
|
+
});
|
|
90
|
+
const validation = validationScope.compile();
|
|
@@ -15,11 +15,14 @@ _export(exports, {
|
|
|
15
15
|
domainsToDescriptions: ()=>domainsToDescriptions,
|
|
16
16
|
objectKindsToDescriptions: ()=>objectKindsToDescriptions,
|
|
17
17
|
describeBranches: ()=>describeBranches,
|
|
18
|
-
|
|
18
|
+
problemCodes: ()=>problemCodes,
|
|
19
|
+
defaultProblemWriters: ()=>defaultProblemWriters,
|
|
20
|
+
compileProblemWriters: ()=>compileProblemWriters
|
|
19
21
|
});
|
|
20
22
|
const _scannerTs = require("../parse/string/shift/scanner.js");
|
|
21
23
|
const _dataTs = require("../utils/data.js");
|
|
22
24
|
const _domainsTs = require("../utils/domains.js");
|
|
25
|
+
const _genericsTs = require("../utils/generics.js");
|
|
23
26
|
const _numericLiteralsTs = require("../utils/numericLiterals.js");
|
|
24
27
|
const _objectKindsTs = require("../utils/objectKinds.js");
|
|
25
28
|
const _pathsTs = require("../utils/paths.js");
|
|
@@ -64,7 +67,6 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
|
64
67
|
_classApplyDescriptorSet(receiver, descriptor, value);
|
|
65
68
|
return value;
|
|
66
69
|
}
|
|
67
|
-
var _definitions_code, _definitions_code1;
|
|
68
70
|
class ArkTypeError extends TypeError {
|
|
69
71
|
constructor(problems){
|
|
70
72
|
super(problems.summary);
|
|
@@ -82,7 +84,7 @@ class Problem {
|
|
|
82
84
|
return this.writers.writeReason(this.mustBe, new _dataTs.DataWrapper(this.data));
|
|
83
85
|
}
|
|
84
86
|
get mustBe() {
|
|
85
|
-
return this.writers.mustBe(this.source);
|
|
87
|
+
return typeof this.writers.mustBe === "string" ? this.writers.mustBe : this.writers.mustBe(this.source);
|
|
86
88
|
}
|
|
87
89
|
constructor(code, path, data, source, writers){
|
|
88
90
|
this.code = code;
|
|
@@ -97,13 +99,16 @@ class Problem {
|
|
|
97
99
|
}
|
|
98
100
|
var _state = /*#__PURE__*/ new WeakMap();
|
|
99
101
|
class ProblemArray extends Array {
|
|
102
|
+
mustBe(description, opts) {
|
|
103
|
+
return this.add("custom", description, opts);
|
|
104
|
+
}
|
|
100
105
|
add(code, source, opts) {
|
|
101
106
|
// copy the path to avoid future mutations affecting it
|
|
102
107
|
const path = opts?.path ?? _pathsTs.Path.from(_classPrivateFieldGet(this, _state).path);
|
|
103
108
|
const data = // we have to check for the presence of the key explicitly since the
|
|
104
109
|
// data could be undefined or null
|
|
105
110
|
opts && "data" in opts ? opts.data : _classPrivateFieldGet(this, _state).data;
|
|
106
|
-
const problem = new Problem(code, path, data, source, _classPrivateFieldGet(this, _state).
|
|
111
|
+
const problem = new Problem(code, path, data, source, _classPrivateFieldGet(this, _state).getProblemConfig(code));
|
|
107
112
|
this.addProblem(problem);
|
|
108
113
|
return problem;
|
|
109
114
|
}
|
|
@@ -117,7 +122,7 @@ class ProblemArray extends Array {
|
|
|
117
122
|
const problemIntersection = new Problem("multi", existing.path, existing.data, [
|
|
118
123
|
existing,
|
|
119
124
|
problem
|
|
120
|
-
], _classPrivateFieldGet(this, _state).
|
|
125
|
+
], _classPrivateFieldGet(this, _state).getProblemConfig("multi"));
|
|
121
126
|
const existingIndex = this.indexOf(existing);
|
|
122
127
|
// If existing is found (which it always should be unless this was externally mutated),
|
|
123
128
|
// replace it with the new problem intersection. In case it isn't for whatever reason,
|
|
@@ -174,15 +179,7 @@ const describeBranches = (descriptions)=>{
|
|
|
174
179
|
};
|
|
175
180
|
const writeDefaultReason = (mustBe, was)=>`must be ${mustBe}${was && ` (was ${was})`}`;
|
|
176
181
|
const addDefaultContext = (reason, path)=>path.length === 0 ? capitalize(reason) : path.length === 1 && (0, _numericLiteralsTs.isWellFormedInteger)(path[0]) ? `Item at index ${path[0]} ${reason}` : `${path} ${reason}`;
|
|
177
|
-
const
|
|
178
|
-
let code;
|
|
179
|
-
for(code in definitions){
|
|
180
|
-
(_definitions_code = definitions[code]).writeReason ?? (_definitions_code.writeReason = writeDefaultReason);
|
|
181
|
-
(_definitions_code1 = definitions[code]).addContext ?? (_definitions_code1.addContext = addDefaultContext);
|
|
182
|
-
}
|
|
183
|
-
return definitions;
|
|
184
|
-
};
|
|
185
|
-
const defaultProblemWriters = compileDefaultProblemWriters({
|
|
182
|
+
const defaultProblemConfig = {
|
|
186
183
|
divisor: {
|
|
187
184
|
mustBe: (divisor)=>divisor === 1 ? `an integer` : `a multiple of ${divisor}`
|
|
188
185
|
},
|
|
@@ -198,6 +195,10 @@ const defaultProblemWriters = compileDefaultProblemWriters({
|
|
|
198
195
|
mustBe: ()=>"defined",
|
|
199
196
|
writeReason: (mustBe)=>writeDefaultReason(mustBe, "")
|
|
200
197
|
},
|
|
198
|
+
extraneous: {
|
|
199
|
+
mustBe: ()=>"removed",
|
|
200
|
+
writeReason: (mustBe)=>writeDefaultReason(mustBe, "")
|
|
201
|
+
},
|
|
201
202
|
bound: {
|
|
202
203
|
mustBe: (bound)=>`${_scannerTs.Scanner.comparatorDescriptions[bound.comparator]} ${bound.limit}${bound.units ? ` ${bound.units}` : ""}`,
|
|
203
204
|
writeReason: (mustBe, data)=>writeDefaultReason(mustBe, `${data.size}`)
|
|
@@ -218,10 +219,38 @@ const defaultProblemWriters = compileDefaultProblemWriters({
|
|
|
218
219
|
writeReason: (mustBe, data)=>`${data} must be...\n${mustBe}`,
|
|
219
220
|
addContext: (reason, path)=>path.length ? `At ${path}, ${reason}` : reason
|
|
220
221
|
},
|
|
221
|
-
|
|
222
|
+
custom: {
|
|
222
223
|
mustBe: (mustBe)=>mustBe
|
|
223
224
|
},
|
|
224
225
|
cases: {
|
|
225
226
|
mustBe: (cases)=>describeBranches(cases)
|
|
226
227
|
}
|
|
227
|
-
}
|
|
228
|
+
};
|
|
229
|
+
const problemCodes = (0, _genericsTs.objectKeysOf)(defaultProblemConfig);
|
|
230
|
+
const compileDefaultProblemWriters = ()=>{
|
|
231
|
+
const result = {};
|
|
232
|
+
let code;
|
|
233
|
+
for (code of problemCodes){
|
|
234
|
+
result[code] = {
|
|
235
|
+
mustBe: defaultProblemConfig[code].mustBe,
|
|
236
|
+
writeReason: defaultProblemConfig[code].writeReason ?? writeDefaultReason,
|
|
237
|
+
addContext: defaultProblemConfig[code].addContext ?? addDefaultContext
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
return result;
|
|
241
|
+
};
|
|
242
|
+
const defaultProblemWriters = compileDefaultProblemWriters();
|
|
243
|
+
const compileProblemWriters = (input)=>{
|
|
244
|
+
if (!input) {
|
|
245
|
+
return defaultProblemWriters;
|
|
246
|
+
}
|
|
247
|
+
const result = {};
|
|
248
|
+
for (const code of problemCodes){
|
|
249
|
+
result[code] = {
|
|
250
|
+
mustBe: input[code]?.mustBe ?? defaultProblemConfig[code].mustBe,
|
|
251
|
+
writeReason: input[code]?.writeReason ?? defaultProblemConfig[code].writeReason ?? input.writeReason ?? writeDefaultReason,
|
|
252
|
+
addContext: input[code]?.addContext ?? defaultProblemConfig[code].addContext ?? input.addContext ?? addDefaultContext
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
return result;
|
|
256
|
+
};
|