arktype 1.0.28-alpha → 1.1.0-dev-1

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