arktype 1.0.14-alpha → 1.0.16-alpha
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 +68 -11
- package/dist/main.js +29 -0
- package/dist/nodes/branch.js +135 -0
- package/dist/nodes/compose.js +99 -0
- package/dist/{cjs/nodes → nodes}/discriminate.js +100 -92
- package/dist/nodes/node.js +134 -0
- package/dist/nodes/predicate.js +88 -0
- package/dist/nodes/rules/class.js +23 -0
- package/dist/nodes/rules/collapsibleSet.js +34 -0
- package/dist/nodes/rules/divisor.js +19 -0
- package/dist/nodes/rules/props.js +130 -0
- package/dist/nodes/rules/range.js +108 -0
- package/dist/nodes/rules/regex.js +17 -0
- package/dist/nodes/rules/rules.js +102 -0
- package/dist/parse/ast/ast.js +2 -0
- package/dist/parse/ast/bound.js +7 -0
- package/dist/parse/ast/config.js +14 -0
- package/dist/parse/ast/distributableFunction.js +40 -0
- package/dist/parse/ast/divisor.js +5 -0
- package/dist/parse/ast/intersection.js +21 -0
- package/dist/parse/ast/keyof.js +107 -0
- package/dist/parse/ast/morph.js +50 -0
- package/dist/parse/ast/narrow.js +17 -0
- package/dist/{types/parse → parse}/ast/node.d.ts +3 -1
- package/dist/parse/ast/node.js +2 -0
- package/dist/parse/ast/tuple.js +72 -0
- package/dist/parse/ast/union.js +5 -0
- package/dist/parse/ast/utils.js +2 -0
- package/dist/parse/definition.js +47 -0
- package/dist/parse/record.js +31 -0
- package/dist/parse/string/reduce/dynamic.js +159 -0
- package/dist/parse/string/reduce/shared.js +13 -0
- package/dist/parse/string/reduce/static.js +2 -0
- package/dist/parse/string/shift/operand/enclosed.js +37 -0
- package/dist/parse/string/shift/operand/operand.js +15 -0
- package/dist/parse/string/shift/operand/unenclosed.js +45 -0
- package/dist/parse/string/shift/operator/bounds.js +83 -0
- package/dist/parse/string/shift/operator/divisor.js +23 -0
- package/dist/parse/string/shift/operator/operator.js +34 -0
- package/dist/parse/string/shift/scanner.js +97 -0
- package/dist/parse/string/string.js +35 -0
- package/dist/scopes/ark.js +27 -0
- package/dist/scopes/cache.js +28 -0
- package/dist/{mjs/scopes → scopes}/expressions.js +20 -9
- package/dist/scopes/jsObjects.js +20 -0
- package/dist/scopes/scope.js +184 -0
- package/dist/scopes/tsKeywords.js +49 -0
- package/dist/scopes/type.js +40 -0
- package/dist/{mjs/scopes → scopes}/validation/creditCard.js +15 -11
- package/dist/{mjs/scopes → scopes}/validation/date.js +21 -16
- package/dist/scopes/validation/validation.js +74 -0
- package/dist/traverse/problems.js +234 -0
- package/dist/traverse/traverse.js +330 -0
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/{mjs/utils → utils}/chainableNoOpProxy.js +6 -2
- package/dist/utils/data.js +39 -0
- package/dist/utils/domains.js +27 -0
- package/dist/utils/errors.js +17 -0
- package/dist/utils/freeze.js +16 -0
- package/dist/{types/utils → utils}/generics.d.ts +2 -2
- package/dist/utils/generics.js +53 -0
- package/dist/utils/numericLiterals.js +88 -0
- package/dist/utils/objectKinds.js +69 -0
- package/dist/{mjs/utils → utils}/paths.js +8 -3
- package/dist/utils/serialize.js +71 -0
- package/dist/utils/unionToTuple.js +2 -0
- package/package.json +26 -39
- package/dist/cjs/main.js +0 -38
- package/dist/cjs/nodes/branch.js +0 -140
- package/dist/cjs/nodes/compose.js +0 -157
- package/dist/cjs/nodes/node.js +0 -139
- package/dist/cjs/nodes/predicate.js +0 -86
- package/dist/cjs/nodes/rules/class.js +0 -25
- package/dist/cjs/nodes/rules/collapsibleSet.js +0 -48
- package/dist/cjs/nodes/rules/divisor.js +0 -29
- package/dist/cjs/nodes/rules/props.js +0 -160
- package/dist/cjs/nodes/rules/range.js +0 -105
- package/dist/cjs/nodes/rules/regex.js +0 -26
- package/dist/cjs/nodes/rules/rules.js +0 -123
- package/dist/cjs/parse/ast/ast.js +0 -4
- package/dist/cjs/parse/ast/bound.js +0 -16
- package/dist/cjs/parse/ast/config.js +0 -13
- package/dist/cjs/parse/ast/distributableFunction.js +0 -48
- package/dist/cjs/parse/ast/divisor.js +0 -9
- package/dist/cjs/parse/ast/intersection.js +0 -30
- package/dist/cjs/parse/ast/keyof.js +0 -111
- package/dist/cjs/parse/ast/morph.js +0 -58
- package/dist/cjs/parse/ast/narrow.js +0 -22
- package/dist/cjs/parse/ast/node.js +0 -4
- package/dist/cjs/parse/ast/tuple.js +0 -94
- package/dist/cjs/parse/ast/union.js +0 -9
- package/dist/cjs/parse/ast/utils.js +0 -4
- package/dist/cjs/parse/definition.js +0 -61
- package/dist/cjs/parse/record.js +0 -37
- package/dist/cjs/parse/string/reduce/dynamic.js +0 -167
- package/dist/cjs/parse/string/reduce/shared.js +0 -23
- package/dist/cjs/parse/string/reduce/static.js +0 -4
- package/dist/cjs/parse/string/shift/operand/enclosed.js +0 -54
- package/dist/cjs/parse/string/shift/operand/operand.js +0 -11
- package/dist/cjs/parse/string/shift/operand/unenclosed.js +0 -53
- package/dist/cjs/parse/string/shift/operator/bounds.js +0 -87
- package/dist/cjs/parse/string/shift/operator/divisor.js +0 -35
- package/dist/cjs/parse/string/shift/operator/operator.js +0 -26
- package/dist/cjs/parse/string/shift/scanner.js +0 -114
- package/dist/cjs/parse/string/string.js +0 -43
- package/dist/cjs/scopes/ark.js +0 -45
- package/dist/cjs/scopes/cache.js +0 -52
- package/dist/cjs/scopes/expressions.js +0 -29
- package/dist/cjs/scopes/jsObjects.js +0 -93
- package/dist/cjs/scopes/scope.js +0 -323
- package/dist/cjs/scopes/tsKeywords.js +0 -113
- package/dist/cjs/scopes/type.js +0 -50
- package/dist/cjs/scopes/validation/creditCard.js +0 -47
- package/dist/cjs/scopes/validation/date.js +0 -62
- package/dist/cjs/scopes/validation/validation.js +0 -104
- package/dist/cjs/traverse/problems.js +0 -281
- package/dist/cjs/traverse/traverse.js +0 -390
- package/dist/cjs/utils/chainableNoOpProxy.js +0 -23
- package/dist/cjs/utils/data.js +0 -53
- package/dist/cjs/utils/domains.js +0 -30
- package/dist/cjs/utils/errors.js +0 -26
- package/dist/cjs/utils/freeze.js +0 -15
- package/dist/cjs/utils/generics.js +0 -63
- package/dist/cjs/utils/numericLiterals.js +0 -75
- package/dist/cjs/utils/objectKinds.js +0 -69
- package/dist/cjs/utils/paths.js +0 -32
- package/dist/cjs/utils/serialize.js +0 -74
- package/dist/cjs/utils/unionToTuple.js +0 -4
- package/dist/mjs/main.js +0 -8
- package/dist/mjs/nodes/branch.js +0 -123
- package/dist/mjs/nodes/compose.js +0 -135
- package/dist/mjs/nodes/discriminate.js +0 -247
- package/dist/mjs/nodes/node.js +0 -117
- package/dist/mjs/nodes/predicate.js +0 -69
- package/dist/mjs/nodes/rules/class.js +0 -11
- package/dist/mjs/nodes/rules/collapsibleSet.js +0 -34
- package/dist/mjs/nodes/rules/divisor.js +0 -15
- package/dist/mjs/nodes/rules/props.js +0 -142
- package/dist/mjs/nodes/rules/range.js +0 -86
- package/dist/mjs/nodes/rules/regex.js +0 -11
- package/dist/mjs/nodes/rules/rules.js +0 -106
- package/dist/mjs/package.json +0 -3
- package/dist/mjs/parse/ast/ast.js +0 -1
- package/dist/mjs/parse/ast/bound.js +0 -2
- package/dist/mjs/parse/ast/config.js +0 -9
- package/dist/mjs/parse/ast/distributableFunction.js +0 -34
- package/dist/mjs/parse/ast/divisor.js +0 -1
- package/dist/mjs/parse/ast/intersection.js +0 -16
- package/dist/mjs/parse/ast/keyof.js +0 -103
- package/dist/mjs/parse/ast/morph.js +0 -44
- package/dist/mjs/parse/ast/narrow.js +0 -14
- package/dist/mjs/parse/ast/node.js +0 -1
- package/dist/mjs/parse/ast/tuple.js +0 -80
- package/dist/mjs/parse/ast/union.js +0 -1
- package/dist/mjs/parse/ast/utils.js +0 -1
- package/dist/mjs/parse/definition.js +0 -46
- package/dist/mjs/parse/record.js +0 -29
- package/dist/mjs/parse/string/reduce/dynamic.js +0 -159
- package/dist/mjs/parse/string/reduce/shared.js +0 -6
- package/dist/mjs/parse/string/reduce/static.js +0 -1
- package/dist/mjs/parse/string/shift/operand/enclosed.js +0 -39
- package/dist/mjs/parse/string/shift/operand/operand.js +0 -3
- package/dist/mjs/parse/string/shift/operand/unenclosed.js +0 -36
- package/dist/mjs/parse/string/shift/operator/bounds.js +0 -70
- package/dist/mjs/parse/string/shift/operator/divisor.js +0 -21
- package/dist/mjs/parse/string/shift/operator/operator.js +0 -13
- package/dist/mjs/parse/string/shift/scanner.js +0 -106
- package/dist/mjs/parse/string/string.js +0 -28
- package/dist/mjs/scopes/ark.js +0 -28
- package/dist/mjs/scopes/cache.js +0 -38
- package/dist/mjs/scopes/jsObjects.js +0 -82
- package/dist/mjs/scopes/scope.js +0 -303
- package/dist/mjs/scopes/tsKeywords.js +0 -116
- package/dist/mjs/scopes/type.js +0 -35
- package/dist/mjs/scopes/validation/validation.js +0 -108
- package/dist/mjs/traverse/problems.js +0 -260
- package/dist/mjs/traverse/traverse.js +0 -373
- package/dist/mjs/utils/data.js +0 -38
- package/dist/mjs/utils/domains.js +0 -15
- package/dist/mjs/utils/errors.js +0 -10
- package/dist/mjs/utils/freeze.js +0 -7
- package/dist/mjs/utils/generics.js +0 -38
- package/dist/mjs/utils/numericLiterals.js +0 -72
- package/dist/mjs/utils/objectKinds.js +0 -55
- package/dist/mjs/utils/serialize.js +0 -58
- package/dist/mjs/utils/unionToTuple.js +0 -1
- /package/dist/{types/main.d.ts → main.d.ts} +0 -0
- /package/dist/{types/nodes → nodes}/branch.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/compose.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/discriminate.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/node.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/predicate.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/rules/class.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/rules/collapsibleSet.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/rules/divisor.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/rules/props.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/rules/range.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/rules/regex.d.ts +0 -0
- /package/dist/{types/nodes → nodes}/rules/rules.d.ts +0 -0
- /package/dist/{cjs/package.json → package.json} +0 -0
- /package/dist/{types/parse → parse}/ast/ast.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/bound.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/config.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/distributableFunction.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/divisor.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/intersection.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/keyof.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/morph.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/narrow.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/tuple.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/union.d.ts +0 -0
- /package/dist/{types/parse → parse}/ast/utils.d.ts +0 -0
- /package/dist/{types/parse → parse}/definition.d.ts +0 -0
- /package/dist/{types/parse → parse}/record.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/reduce/dynamic.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/reduce/shared.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/reduce/static.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/shift/operand/enclosed.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/shift/operand/operand.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/shift/operand/unenclosed.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/shift/operator/bounds.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/shift/operator/divisor.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/shift/operator/operator.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/shift/scanner.d.ts +0 -0
- /package/dist/{types/parse → parse}/string/string.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/ark.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/cache.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/expressions.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/jsObjects.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/scope.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/tsKeywords.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/type.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/validation/creditCard.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/validation/date.d.ts +0 -0
- /package/dist/{types/scopes → scopes}/validation/validation.d.ts +0 -0
- /package/dist/{types/traverse → traverse}/problems.d.ts +0 -0
- /package/dist/{types/traverse → traverse}/traverse.d.ts +0 -0
- /package/dist/{types/utils → utils}/chainableNoOpProxy.d.ts +0 -0
- /package/dist/{types/utils → utils}/data.d.ts +0 -0
- /package/dist/{types/utils → utils}/domains.d.ts +0 -0
- /package/dist/{types/utils → utils}/errors.d.ts +0 -0
- /package/dist/{types/utils → utils}/freeze.d.ts +0 -0
- /package/dist/{types/utils → utils}/numericLiterals.d.ts +0 -0
- /package/dist/{types/utils → utils}/objectKinds.d.ts +0 -0
- /package/dist/{types/utils → utils}/paths.d.ts +0 -0
- /package/dist/{types/utils → utils}/serialize.d.ts +0 -0
- /package/dist/{types/utils → utils}/unionToTuple.d.ts +0 -0
|
@@ -1,33 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parsedDate = exports.tryParseDate = void 0;
|
|
4
|
+
const scope_js_1 = require("../scope.js");
|
|
5
|
+
const tsKeywords_js_1 = require("../tsKeywords.js");
|
|
3
6
|
const dayDelimiterMatcher = /^[./-]$/;
|
|
4
7
|
// ISO 8601 date/time modernized from https://github.com/validatorjs/validator.js/blob/master/src/lib/isISO8601.js
|
|
5
8
|
// Based on https://tc39.es/ecma262/#sec-date-time-string-format, the T
|
|
6
9
|
// delimiter for date/time is mandatory. Regex from validator.js strict matcher:
|
|
7
10
|
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)?)?)?)?$/;
|
|
8
|
-
const isValidDateInstance = (date)
|
|
9
|
-
const writeFormattedMustBe = (format)
|
|
10
|
-
|
|
11
|
+
const isValidDateInstance = (date) => !isNaN(date);
|
|
12
|
+
const writeFormattedMustBe = (format) => `a ${format}-formatted date`;
|
|
13
|
+
const tryParseDate = (data, opts) => {
|
|
11
14
|
if (!opts?.format) {
|
|
12
15
|
const result = new Date(data);
|
|
13
16
|
return isValidDateInstance(result) ? result : "a valid date";
|
|
14
17
|
}
|
|
15
18
|
if (opts.format === "iso8601") {
|
|
16
|
-
return iso8601Matcher.test(data)
|
|
19
|
+
return iso8601Matcher.test(data)
|
|
20
|
+
? new Date(data)
|
|
21
|
+
: writeFormattedMustBe("iso8601");
|
|
17
22
|
}
|
|
18
23
|
const dataParts = data.split(dayDelimiterMatcher);
|
|
19
24
|
// will be the first delimiter matched, if there is one
|
|
20
25
|
const delimiter = data[dataParts[0].length];
|
|
21
|
-
const formatParts = delimiter ? opts.format.split(delimiter) : [
|
|
22
|
-
opts.format
|
|
23
|
-
];
|
|
26
|
+
const formatParts = delimiter ? opts.format.split(delimiter) : [opts.format];
|
|
24
27
|
if (dataParts.length !== formatParts.length) {
|
|
25
28
|
return writeFormattedMustBe(opts.format);
|
|
26
29
|
}
|
|
27
30
|
const parsedParts = {};
|
|
28
|
-
for(let i = 0; i < formatParts.length; i++){
|
|
29
|
-
if (dataParts[i].length !== formatParts[i].length &&
|
|
30
|
-
|
|
31
|
+
for (let i = 0; i < formatParts.length; i++) {
|
|
32
|
+
if (dataParts[i].length !== formatParts[i].length &&
|
|
33
|
+
// if format is "m" or "d", data is allowed to be 1 or 2 characters
|
|
34
|
+
!(formatParts[i].length === 1 && dataParts[i].length === 2)) {
|
|
31
35
|
return writeFormattedMustBe(opts.format);
|
|
32
36
|
}
|
|
33
37
|
parsedParts[formatParts[i][0]] = dataParts[i];
|
|
@@ -38,11 +42,12 @@ export const tryParseDate = (data, opts)=>{
|
|
|
38
42
|
}
|
|
39
43
|
return writeFormattedMustBe(opts.format);
|
|
40
44
|
};
|
|
41
|
-
|
|
42
|
-
|
|
45
|
+
exports.tryParseDate = tryParseDate;
|
|
46
|
+
exports.parsedDate = (0, scope_js_1.rootType)([
|
|
47
|
+
tsKeywords_js_1.tsKeywords.string,
|
|
43
48
|
"|>",
|
|
44
|
-
(s, problems)=>{
|
|
45
|
-
const result = tryParseDate(s);
|
|
49
|
+
(s, problems) => {
|
|
50
|
+
const result = (0, exports.tryParseDate)(s);
|
|
46
51
|
return typeof result === "string" ? problems.mustBe(result) : result;
|
|
47
52
|
}
|
|
48
53
|
]);
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validation = exports.validationScope = void 0;
|
|
4
|
+
const numericLiterals_js_1 = require("../../utils/numericLiterals.js");
|
|
5
|
+
const scope_js_1 = require("../scope.js");
|
|
6
|
+
const tsKeywords_js_1 = require("../tsKeywords.js");
|
|
7
|
+
const creditCard_js_1 = require("./creditCard.js");
|
|
8
|
+
const date_js_1 = require("./date.js");
|
|
9
|
+
// Non-trivial expressions should have an explanation or attribution
|
|
10
|
+
const parsedNumber = (0, scope_js_1.rootType)([numericLiterals_js_1.wellFormedNumberMatcher, "|>", (s) => parseFloat(s)], { mustBe: "a well-formed numeric string" });
|
|
11
|
+
const parsedInteger = (0, scope_js_1.rootType)([
|
|
12
|
+
tsKeywords_js_1.tsKeywords.string,
|
|
13
|
+
"|>",
|
|
14
|
+
(s, problems) => {
|
|
15
|
+
if (!(0, numericLiterals_js_1.isWellFormedInteger)(s)) {
|
|
16
|
+
return problems.mustBe("a well-formed integer string");
|
|
17
|
+
}
|
|
18
|
+
const parsed = parseInt(s);
|
|
19
|
+
return Number.isSafeInteger(parsed)
|
|
20
|
+
? parsed
|
|
21
|
+
: problems.mustBe("an integer in the range Number.MIN_SAFE_INTEGER to Number.MAX_SAFE_INTEGER");
|
|
22
|
+
}
|
|
23
|
+
]);
|
|
24
|
+
// https://www.regular-expressions.info/email.html
|
|
25
|
+
const email = (0, scope_js_1.rootType)(/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/, {
|
|
26
|
+
mustBe: "a valid email"
|
|
27
|
+
});
|
|
28
|
+
// https://github.com/validatorjs/validator.js/blob/master/src/lib/isUUID.js
|
|
29
|
+
const uuid = (0, scope_js_1.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}$/, { mustBe: "a valid UUID" });
|
|
30
|
+
// https://semver.org/
|
|
31
|
+
const semver = (0, scope_js_1.rootType)(/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/, { mustBe: "a valid semantic version (see https://semver.org/)" });
|
|
32
|
+
const json = (0, scope_js_1.rootType)([tsKeywords_js_1.tsKeywords.string, "|>", (s) => JSON.parse(s)], {
|
|
33
|
+
mustBe: "a JSON-parsable string"
|
|
34
|
+
});
|
|
35
|
+
/**
|
|
36
|
+
* @keywords keywords: {
|
|
37
|
+
"alpha": "only letters",
|
|
38
|
+
"alphanumeric": "only letters and digits",
|
|
39
|
+
"lowercase": "only lowercase letters",
|
|
40
|
+
"uppercase": "only uppercase letters",
|
|
41
|
+
"creditCard": "a valid credit card number",
|
|
42
|
+
"email": "a valid email",
|
|
43
|
+
"uuid": "a valid UUID",
|
|
44
|
+
"parsedNumber": "a well-formed numeric string",
|
|
45
|
+
"parsedInteger": "a well-formed integer string",
|
|
46
|
+
"parsedDate": "a valid date",
|
|
47
|
+
"semver": "a valid semantic version",
|
|
48
|
+
"json": "a JSON-parsable string",
|
|
49
|
+
"integer": "an integer"
|
|
50
|
+
}
|
|
51
|
+
* @docgenScope
|
|
52
|
+
* @docgenTable
|
|
53
|
+
*/
|
|
54
|
+
exports.validationScope = (0, scope_js_1.scope)({
|
|
55
|
+
// Character sets
|
|
56
|
+
alpha: [/^[A-Za-z]*$/, ":", { mustBe: "only letters" }],
|
|
57
|
+
alphanumeric: [
|
|
58
|
+
/^[A-Za-z\d]*$/,
|
|
59
|
+
":",
|
|
60
|
+
{ mustBe: "only letters and digits" }
|
|
61
|
+
],
|
|
62
|
+
lowercase: [/^[a-z]*$/, ":", { mustBe: "only lowercase letters" }],
|
|
63
|
+
uppercase: [/^[A-Z]*$/, ":", { mustBe: "only uppercase letters" }],
|
|
64
|
+
creditCard: creditCard_js_1.creditCard,
|
|
65
|
+
email,
|
|
66
|
+
uuid,
|
|
67
|
+
parsedNumber,
|
|
68
|
+
parsedInteger,
|
|
69
|
+
parsedDate: date_js_1.parsedDate,
|
|
70
|
+
semver,
|
|
71
|
+
json,
|
|
72
|
+
integer: ["node", { number: { divisor: 1 } }]
|
|
73
|
+
}, { name: "validation", standard: false });
|
|
74
|
+
exports.validation = exports.validationScope.compile();
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.compileProblemWriters = exports.defaultProblemWriters = exports.problemCodes = exports.describeBranches = exports.objectKindsToDescriptions = exports.domainsToDescriptions = exports.Problems = exports.Problem = exports.ArkTypeError = void 0;
|
|
4
|
+
const scanner_js_1 = require("../parse/string/shift/scanner.js");
|
|
5
|
+
const data_js_1 = require("../utils/data.js");
|
|
6
|
+
const domains_js_1 = require("../utils/domains.js");
|
|
7
|
+
const generics_js_1 = require("../utils/generics.js");
|
|
8
|
+
const numericLiterals_js_1 = require("../utils/numericLiterals.js");
|
|
9
|
+
const objectKinds_js_1 = require("../utils/objectKinds.js");
|
|
10
|
+
const paths_js_1 = require("../utils/paths.js");
|
|
11
|
+
const serialize_js_1 = require("../utils/serialize.js");
|
|
12
|
+
class ArkTypeError extends TypeError {
|
|
13
|
+
cause;
|
|
14
|
+
constructor(problems) {
|
|
15
|
+
super(`${problems}`);
|
|
16
|
+
this.cause = problems;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.ArkTypeError = ArkTypeError;
|
|
20
|
+
class Problem {
|
|
21
|
+
code;
|
|
22
|
+
path;
|
|
23
|
+
data;
|
|
24
|
+
source;
|
|
25
|
+
writers;
|
|
26
|
+
parts;
|
|
27
|
+
constructor(code, path, data, source, writers) {
|
|
28
|
+
this.code = code;
|
|
29
|
+
this.path = path;
|
|
30
|
+
this.data = data;
|
|
31
|
+
this.source = source;
|
|
32
|
+
this.writers = writers;
|
|
33
|
+
if (this.code === "multi") {
|
|
34
|
+
this.parts = this.source;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
toString() {
|
|
38
|
+
return this.message;
|
|
39
|
+
}
|
|
40
|
+
get message() {
|
|
41
|
+
return this.writers.addContext(this.reason, this.path);
|
|
42
|
+
}
|
|
43
|
+
get reason() {
|
|
44
|
+
return this.writers.writeReason(this.mustBe, new data_js_1.DataWrapper(this.data));
|
|
45
|
+
}
|
|
46
|
+
get mustBe() {
|
|
47
|
+
return typeof this.writers.mustBe === "string"
|
|
48
|
+
? this.writers.mustBe
|
|
49
|
+
: this.writers.mustBe(this.source);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.Problem = Problem;
|
|
53
|
+
class ProblemArray extends Array {
|
|
54
|
+
byPath = {};
|
|
55
|
+
count = 0;
|
|
56
|
+
#state;
|
|
57
|
+
constructor(state) {
|
|
58
|
+
super();
|
|
59
|
+
this.#state = state;
|
|
60
|
+
}
|
|
61
|
+
mustBe(description, opts) {
|
|
62
|
+
return this.add("custom", description, opts);
|
|
63
|
+
}
|
|
64
|
+
add(code, source, opts) {
|
|
65
|
+
// copy the path to avoid future mutations affecting it
|
|
66
|
+
const path = paths_js_1.Path.from(opts?.path ?? this.#state.path);
|
|
67
|
+
const data =
|
|
68
|
+
// we have to check for the presence of the key explicitly since the
|
|
69
|
+
// data could be undefined or null
|
|
70
|
+
opts && "data" in opts
|
|
71
|
+
? opts.data
|
|
72
|
+
: this.#state.data;
|
|
73
|
+
const problem = new Problem(
|
|
74
|
+
// avoid a bunch of errors from TS trying to discriminate the
|
|
75
|
+
// problem input based on the code
|
|
76
|
+
code, path, data, source, this.#state.getProblemConfig(code));
|
|
77
|
+
this.addProblem(problem);
|
|
78
|
+
return problem;
|
|
79
|
+
}
|
|
80
|
+
addProblem(problem) {
|
|
81
|
+
const pathKey = `${problem.path}`;
|
|
82
|
+
const existing = this.byPath[pathKey];
|
|
83
|
+
if (existing) {
|
|
84
|
+
if (existing.parts) {
|
|
85
|
+
existing.parts.push(problem);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
const problemIntersection = new Problem("multi", existing.path, existing.data, [existing, problem], this.#state.getProblemConfig("multi"));
|
|
89
|
+
const existingIndex = this.indexOf(existing);
|
|
90
|
+
// If existing is found (which it always should be unless this was externally mutated),
|
|
91
|
+
// replace it with the new problem intersection. In case it isn't for whatever reason,
|
|
92
|
+
// just append the intersection.
|
|
93
|
+
this[existingIndex === -1 ? this.length : existingIndex] =
|
|
94
|
+
problemIntersection;
|
|
95
|
+
this.byPath[pathKey] = problemIntersection;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
this.byPath[pathKey] = problem;
|
|
100
|
+
this.push(problem);
|
|
101
|
+
}
|
|
102
|
+
this.count++;
|
|
103
|
+
}
|
|
104
|
+
get summary() {
|
|
105
|
+
return `${this}`;
|
|
106
|
+
}
|
|
107
|
+
toString() {
|
|
108
|
+
return this.join("\n");
|
|
109
|
+
}
|
|
110
|
+
throw() {
|
|
111
|
+
throw new ArkTypeError(this);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
exports.Problems = ProblemArray;
|
|
115
|
+
const capitalize = (s) => s[0].toUpperCase() + s.slice(1);
|
|
116
|
+
const domainsToDescriptions = (domains) => domains.map((objectKind) => domains_js_1.domainDescriptions[objectKind]);
|
|
117
|
+
exports.domainsToDescriptions = domainsToDescriptions;
|
|
118
|
+
const objectKindsToDescriptions = (kinds) => kinds.map((objectKind) => objectKinds_js_1.objectKindDescriptions[objectKind]);
|
|
119
|
+
exports.objectKindsToDescriptions = objectKindsToDescriptions;
|
|
120
|
+
const describeBranches = (descriptions) => {
|
|
121
|
+
if (descriptions.length === 0) {
|
|
122
|
+
return "never";
|
|
123
|
+
}
|
|
124
|
+
if (descriptions.length === 1) {
|
|
125
|
+
return descriptions[0];
|
|
126
|
+
}
|
|
127
|
+
let description = "";
|
|
128
|
+
for (let i = 0; i < descriptions.length - 1; i++) {
|
|
129
|
+
description += descriptions[i];
|
|
130
|
+
if (i < descriptions.length - 2) {
|
|
131
|
+
description += ", ";
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
description += ` or ${descriptions[descriptions.length - 1]}`;
|
|
135
|
+
return description;
|
|
136
|
+
};
|
|
137
|
+
exports.describeBranches = describeBranches;
|
|
138
|
+
const writeDefaultReason = (mustBe, was) => `must be ${mustBe}${was && ` (was ${was})`}`;
|
|
139
|
+
const addDefaultContext = (reason, path) => path.length === 0
|
|
140
|
+
? capitalize(reason)
|
|
141
|
+
: path.length === 1 && (0, numericLiterals_js_1.isWellFormedInteger)(path[0])
|
|
142
|
+
? `Item at index ${path[0]} ${reason}`
|
|
143
|
+
: `${path} ${reason}`;
|
|
144
|
+
const defaultProblemConfig = {
|
|
145
|
+
divisor: {
|
|
146
|
+
mustBe: (divisor) => divisor === 1 ? `an integer` : `a multiple of ${divisor}`
|
|
147
|
+
},
|
|
148
|
+
class: {
|
|
149
|
+
mustBe: (expected) => {
|
|
150
|
+
const possibleObjectKind = (0, objectKinds_js_1.getExactConstructorObjectKind)(expected);
|
|
151
|
+
return possibleObjectKind
|
|
152
|
+
? objectKinds_js_1.objectKindDescriptions[possibleObjectKind]
|
|
153
|
+
: `an instance of ${expected.name}`;
|
|
154
|
+
},
|
|
155
|
+
writeReason: (mustBe, data) => writeDefaultReason(mustBe, data.className)
|
|
156
|
+
},
|
|
157
|
+
domain: {
|
|
158
|
+
mustBe: (domain) => domains_js_1.domainDescriptions[domain],
|
|
159
|
+
writeReason: (mustBe, data) => writeDefaultReason(mustBe, data.domain)
|
|
160
|
+
},
|
|
161
|
+
missing: {
|
|
162
|
+
mustBe: () => "defined",
|
|
163
|
+
writeReason: (mustBe) => writeDefaultReason(mustBe, "")
|
|
164
|
+
},
|
|
165
|
+
extraneous: {
|
|
166
|
+
mustBe: () => "removed",
|
|
167
|
+
writeReason: (mustBe) => writeDefaultReason(mustBe, "")
|
|
168
|
+
},
|
|
169
|
+
bound: {
|
|
170
|
+
mustBe: (bound) => `${scanner_js_1.Scanner.comparatorDescriptions[bound.comparator]} ${bound.limit}${bound.units ? ` ${bound.units}` : ""}`,
|
|
171
|
+
writeReason: (mustBe, data) => writeDefaultReason(mustBe, `${data.size}`)
|
|
172
|
+
},
|
|
173
|
+
regex: {
|
|
174
|
+
mustBe: (expression) => `a string matching ${expression}`
|
|
175
|
+
},
|
|
176
|
+
value: {
|
|
177
|
+
mustBe: serialize_js_1.stringify
|
|
178
|
+
},
|
|
179
|
+
branches: {
|
|
180
|
+
mustBe: (branchProblems) => (0, exports.describeBranches)(branchProblems.map((problem) => `${problem.path} must be ${problem.parts
|
|
181
|
+
? (0, exports.describeBranches)(problem.parts.map((part) => part.mustBe))
|
|
182
|
+
: problem.mustBe}`)),
|
|
183
|
+
writeReason: (mustBe, data) => `${mustBe} (was ${data})`,
|
|
184
|
+
addContext: (reason, path) => path.length ? `At ${path}, ${reason}` : reason
|
|
185
|
+
},
|
|
186
|
+
multi: {
|
|
187
|
+
mustBe: (problems) => "• " + problems.map((_) => _.mustBe).join("\n• "),
|
|
188
|
+
writeReason: (mustBe, data) => `${data} must be...\n${mustBe}`,
|
|
189
|
+
addContext: (reason, path) => path.length ? `At ${path}, ${reason}` : reason
|
|
190
|
+
},
|
|
191
|
+
custom: {
|
|
192
|
+
mustBe: (mustBe) => mustBe
|
|
193
|
+
},
|
|
194
|
+
cases: {
|
|
195
|
+
mustBe: (cases) => (0, exports.describeBranches)(cases)
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
exports.problemCodes = (0, generics_js_1.objectKeysOf)(defaultProblemConfig);
|
|
199
|
+
const compileDefaultProblemWriters = () => {
|
|
200
|
+
const result = {};
|
|
201
|
+
let code;
|
|
202
|
+
for (code of exports.problemCodes) {
|
|
203
|
+
result[code] = {
|
|
204
|
+
mustBe: defaultProblemConfig[code].mustBe,
|
|
205
|
+
writeReason: defaultProblemConfig[code].writeReason ??
|
|
206
|
+
writeDefaultReason,
|
|
207
|
+
addContext: defaultProblemConfig[code].addContext ?? addDefaultContext
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
return result;
|
|
211
|
+
};
|
|
212
|
+
exports.defaultProblemWriters = compileDefaultProblemWriters();
|
|
213
|
+
const compileProblemWriters = (input) => {
|
|
214
|
+
if (!input) {
|
|
215
|
+
return exports.defaultProblemWriters;
|
|
216
|
+
}
|
|
217
|
+
const result = {};
|
|
218
|
+
for (const code of exports.problemCodes) {
|
|
219
|
+
result[code] = {
|
|
220
|
+
mustBe: input[code]?.mustBe ??
|
|
221
|
+
defaultProblemConfig[code].mustBe,
|
|
222
|
+
writeReason: input[code]?.writeReason ??
|
|
223
|
+
defaultProblemConfig[code].writeReason ??
|
|
224
|
+
input.writeReason ??
|
|
225
|
+
writeDefaultReason,
|
|
226
|
+
addContext: input[code]?.addContext ??
|
|
227
|
+
defaultProblemConfig[code].addContext ??
|
|
228
|
+
input.addContext ??
|
|
229
|
+
addDefaultContext
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
return result;
|
|
233
|
+
};
|
|
234
|
+
exports.compileProblemWriters = compileProblemWriters;
|