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