arktype 1.0.16-alpha → 1.0.17-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.
Files changed (247) hide show
  1. package/README.md +3 -3
  2. package/dist/cjs/src/main.js +38 -0
  3. package/dist/cjs/src/nodes/branch.js +140 -0
  4. package/dist/cjs/src/nodes/compose.js +157 -0
  5. package/dist/{nodes → cjs/src/nodes}/discriminate.js +92 -100
  6. package/dist/cjs/src/nodes/node.js +139 -0
  7. package/dist/cjs/src/nodes/predicate.js +86 -0
  8. package/dist/cjs/src/nodes/rules/class.js +25 -0
  9. package/dist/cjs/src/nodes/rules/collapsibleSet.js +48 -0
  10. package/dist/cjs/src/nodes/rules/divisor.js +29 -0
  11. package/dist/cjs/src/nodes/rules/props.js +160 -0
  12. package/dist/cjs/src/nodes/rules/range.js +105 -0
  13. package/dist/cjs/src/nodes/rules/regex.js +26 -0
  14. package/dist/cjs/src/nodes/rules/rules.js +123 -0
  15. package/dist/cjs/src/parse/ast/ast.js +4 -0
  16. package/dist/cjs/src/parse/ast/bound.js +16 -0
  17. package/dist/cjs/src/parse/ast/config.js +13 -0
  18. package/dist/cjs/src/parse/ast/distributableFunction.js +48 -0
  19. package/dist/cjs/src/parse/ast/divisor.js +9 -0
  20. package/dist/cjs/src/parse/ast/intersection.js +30 -0
  21. package/dist/cjs/src/parse/ast/keyof.js +111 -0
  22. package/dist/cjs/src/parse/ast/morph.js +58 -0
  23. package/dist/cjs/src/parse/ast/narrow.js +22 -0
  24. package/dist/cjs/src/parse/ast/node.js +4 -0
  25. package/dist/cjs/src/parse/ast/tuple.js +94 -0
  26. package/dist/cjs/src/parse/ast/union.js +9 -0
  27. package/dist/cjs/src/parse/ast/utils.js +4 -0
  28. package/dist/cjs/src/parse/definition.js +61 -0
  29. package/dist/cjs/src/parse/record.js +37 -0
  30. package/dist/cjs/src/parse/string/reduce/dynamic.js +167 -0
  31. package/dist/cjs/src/parse/string/reduce/shared.js +23 -0
  32. package/dist/cjs/src/parse/string/reduce/static.js +4 -0
  33. package/dist/cjs/src/parse/string/shift/operand/enclosed.js +54 -0
  34. package/dist/cjs/src/parse/string/shift/operand/operand.js +11 -0
  35. package/dist/cjs/src/parse/string/shift/operand/unenclosed.js +53 -0
  36. package/dist/cjs/src/parse/string/shift/operator/bounds.js +87 -0
  37. package/dist/cjs/src/parse/string/shift/operator/divisor.js +35 -0
  38. package/dist/cjs/src/parse/string/shift/operator/operator.js +26 -0
  39. package/dist/cjs/src/parse/string/shift/scanner.js +114 -0
  40. package/dist/cjs/src/parse/string/string.js +43 -0
  41. package/dist/cjs/src/scopes/ark.js +45 -0
  42. package/dist/cjs/src/scopes/cache.js +52 -0
  43. package/dist/cjs/src/scopes/expressions.js +29 -0
  44. package/dist/cjs/src/scopes/jsObjects.js +93 -0
  45. package/dist/cjs/src/scopes/scope.js +323 -0
  46. package/dist/cjs/src/scopes/tsKeywords.js +113 -0
  47. package/dist/cjs/src/scopes/type.js +50 -0
  48. package/dist/cjs/src/scopes/validation/creditCard.js +47 -0
  49. package/dist/cjs/src/scopes/validation/date.js +62 -0
  50. package/dist/cjs/src/scopes/validation/validation.js +108 -0
  51. package/dist/cjs/src/traverse/problems.js +281 -0
  52. package/dist/cjs/src/traverse/traverse.js +390 -0
  53. package/dist/cjs/src/utils/chainableNoOpProxy.js +23 -0
  54. package/dist/cjs/src/utils/data.js +53 -0
  55. package/dist/cjs/src/utils/domains.js +30 -0
  56. package/dist/cjs/src/utils/errors.js +26 -0
  57. package/dist/cjs/src/utils/freeze.js +15 -0
  58. package/dist/cjs/src/utils/generics.js +63 -0
  59. package/dist/cjs/src/utils/numericLiterals.js +75 -0
  60. package/dist/cjs/src/utils/objectKinds.js +69 -0
  61. package/dist/cjs/src/utils/paths.js +32 -0
  62. package/dist/cjs/src/utils/serialize.js +74 -0
  63. package/dist/cjs/src/utils/unionToTuple.js +4 -0
  64. package/dist/mjs/package.json +3 -0
  65. package/dist/mjs/src/main.js +8 -0
  66. package/dist/mjs/src/nodes/branch.js +123 -0
  67. package/dist/mjs/src/nodes/compose.js +135 -0
  68. package/dist/mjs/src/nodes/discriminate.js +247 -0
  69. package/dist/mjs/src/nodes/node.js +117 -0
  70. package/dist/mjs/src/nodes/predicate.js +69 -0
  71. package/dist/mjs/src/nodes/rules/class.js +11 -0
  72. package/dist/mjs/src/nodes/rules/collapsibleSet.js +34 -0
  73. package/dist/mjs/src/nodes/rules/divisor.js +15 -0
  74. package/dist/mjs/src/nodes/rules/props.js +142 -0
  75. package/dist/mjs/src/nodes/rules/range.js +86 -0
  76. package/dist/mjs/src/nodes/rules/regex.js +11 -0
  77. package/dist/mjs/src/nodes/rules/rules.js +106 -0
  78. package/dist/mjs/src/parse/ast/ast.js +1 -0
  79. package/dist/mjs/src/parse/ast/bound.js +2 -0
  80. package/dist/mjs/src/parse/ast/config.js +9 -0
  81. package/dist/mjs/src/parse/ast/distributableFunction.js +34 -0
  82. package/dist/mjs/src/parse/ast/divisor.js +1 -0
  83. package/dist/mjs/src/parse/ast/intersection.js +16 -0
  84. package/dist/mjs/src/parse/ast/keyof.js +103 -0
  85. package/dist/mjs/src/parse/ast/morph.js +44 -0
  86. package/dist/mjs/src/parse/ast/narrow.js +14 -0
  87. package/dist/mjs/src/parse/ast/node.js +1 -0
  88. package/dist/mjs/src/parse/ast/tuple.js +80 -0
  89. package/dist/mjs/src/parse/ast/union.js +1 -0
  90. package/dist/mjs/src/parse/ast/utils.js +1 -0
  91. package/dist/mjs/src/parse/definition.js +46 -0
  92. package/dist/mjs/src/parse/record.js +29 -0
  93. package/dist/mjs/src/parse/string/reduce/dynamic.js +159 -0
  94. package/dist/mjs/src/parse/string/reduce/shared.js +6 -0
  95. package/dist/mjs/src/parse/string/reduce/static.js +1 -0
  96. package/dist/mjs/src/parse/string/shift/operand/enclosed.js +39 -0
  97. package/dist/mjs/src/parse/string/shift/operand/operand.js +3 -0
  98. package/dist/mjs/src/parse/string/shift/operand/unenclosed.js +36 -0
  99. package/dist/mjs/src/parse/string/shift/operator/bounds.js +70 -0
  100. package/dist/mjs/src/parse/string/shift/operator/divisor.js +21 -0
  101. package/dist/mjs/src/parse/string/shift/operator/operator.js +13 -0
  102. package/dist/mjs/src/parse/string/shift/scanner.js +106 -0
  103. package/dist/mjs/src/parse/string/string.js +28 -0
  104. package/dist/mjs/src/scopes/ark.js +28 -0
  105. package/dist/mjs/src/scopes/cache.js +38 -0
  106. package/dist/{scopes → mjs/src/scopes}/expressions.js +9 -20
  107. package/dist/mjs/src/scopes/jsObjects.js +82 -0
  108. package/dist/mjs/src/scopes/scope.js +303 -0
  109. package/dist/mjs/src/scopes/tsKeywords.js +116 -0
  110. package/dist/mjs/src/scopes/type.js +35 -0
  111. package/dist/{scopes → mjs/src/scopes}/validation/creditCard.js +11 -15
  112. package/dist/{scopes → mjs/src/scopes}/validation/date.js +16 -21
  113. package/dist/mjs/src/scopes/validation/validation.js +112 -0
  114. package/dist/mjs/src/traverse/problems.js +260 -0
  115. package/dist/mjs/src/traverse/traverse.js +373 -0
  116. package/dist/{utils → mjs/src/utils}/chainableNoOpProxy.js +2 -6
  117. package/dist/mjs/src/utils/data.js +38 -0
  118. package/dist/mjs/src/utils/domains.js +15 -0
  119. package/dist/mjs/src/utils/errors.js +10 -0
  120. package/dist/mjs/src/utils/freeze.js +7 -0
  121. package/dist/mjs/src/utils/generics.js +38 -0
  122. package/dist/mjs/src/utils/numericLiterals.js +72 -0
  123. package/dist/mjs/src/utils/objectKinds.js +55 -0
  124. package/dist/{utils → mjs/src/utils}/paths.js +3 -8
  125. package/dist/mjs/src/utils/serialize.js +58 -0
  126. package/dist/mjs/src/utils/unionToTuple.js +1 -0
  127. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  128. package/package.json +13 -5
  129. package/dist/main.js +0 -29
  130. package/dist/nodes/branch.js +0 -135
  131. package/dist/nodes/compose.js +0 -99
  132. package/dist/nodes/node.js +0 -134
  133. package/dist/nodes/predicate.js +0 -88
  134. package/dist/nodes/rules/class.js +0 -23
  135. package/dist/nodes/rules/collapsibleSet.js +0 -34
  136. package/dist/nodes/rules/divisor.js +0 -19
  137. package/dist/nodes/rules/props.js +0 -130
  138. package/dist/nodes/rules/range.js +0 -108
  139. package/dist/nodes/rules/regex.js +0 -17
  140. package/dist/nodes/rules/rules.js +0 -102
  141. package/dist/parse/ast/ast.js +0 -2
  142. package/dist/parse/ast/bound.js +0 -7
  143. package/dist/parse/ast/config.js +0 -14
  144. package/dist/parse/ast/distributableFunction.js +0 -40
  145. package/dist/parse/ast/divisor.js +0 -5
  146. package/dist/parse/ast/intersection.js +0 -21
  147. package/dist/parse/ast/keyof.js +0 -107
  148. package/dist/parse/ast/morph.js +0 -50
  149. package/dist/parse/ast/narrow.js +0 -17
  150. package/dist/parse/ast/node.js +0 -2
  151. package/dist/parse/ast/tuple.js +0 -72
  152. package/dist/parse/ast/union.js +0 -5
  153. package/dist/parse/ast/utils.js +0 -2
  154. package/dist/parse/definition.js +0 -47
  155. package/dist/parse/record.js +0 -31
  156. package/dist/parse/string/reduce/dynamic.js +0 -159
  157. package/dist/parse/string/reduce/shared.js +0 -13
  158. package/dist/parse/string/reduce/static.js +0 -2
  159. package/dist/parse/string/shift/operand/enclosed.js +0 -37
  160. package/dist/parse/string/shift/operand/operand.js +0 -15
  161. package/dist/parse/string/shift/operand/unenclosed.js +0 -45
  162. package/dist/parse/string/shift/operator/bounds.js +0 -83
  163. package/dist/parse/string/shift/operator/divisor.js +0 -23
  164. package/dist/parse/string/shift/operator/operator.js +0 -34
  165. package/dist/parse/string/shift/scanner.js +0 -97
  166. package/dist/parse/string/string.js +0 -35
  167. package/dist/scopes/ark.js +0 -27
  168. package/dist/scopes/cache.js +0 -28
  169. package/dist/scopes/jsObjects.js +0 -20
  170. package/dist/scopes/scope.js +0 -184
  171. package/dist/scopes/tsKeywords.js +0 -49
  172. package/dist/scopes/type.js +0 -40
  173. package/dist/scopes/validation/validation.js +0 -74
  174. package/dist/traverse/problems.js +0 -234
  175. package/dist/traverse/traverse.js +0 -330
  176. package/dist/utils/data.js +0 -39
  177. package/dist/utils/domains.js +0 -27
  178. package/dist/utils/errors.js +0 -17
  179. package/dist/utils/freeze.js +0 -16
  180. package/dist/utils/generics.js +0 -53
  181. package/dist/utils/numericLiterals.js +0 -88
  182. package/dist/utils/objectKinds.js +0 -69
  183. package/dist/utils/serialize.js +0 -71
  184. package/dist/utils/unionToTuple.js +0 -2
  185. /package/dist/{package.json → cjs/package.json} +0 -0
  186. /package/dist/{main.d.ts → types/main.d.ts} +0 -0
  187. /package/dist/{nodes → types/nodes}/branch.d.ts +0 -0
  188. /package/dist/{nodes → types/nodes}/compose.d.ts +0 -0
  189. /package/dist/{nodes → types/nodes}/discriminate.d.ts +0 -0
  190. /package/dist/{nodes → types/nodes}/node.d.ts +0 -0
  191. /package/dist/{nodes → types/nodes}/predicate.d.ts +0 -0
  192. /package/dist/{nodes → types/nodes}/rules/class.d.ts +0 -0
  193. /package/dist/{nodes → types/nodes}/rules/collapsibleSet.d.ts +0 -0
  194. /package/dist/{nodes → types/nodes}/rules/divisor.d.ts +0 -0
  195. /package/dist/{nodes → types/nodes}/rules/props.d.ts +0 -0
  196. /package/dist/{nodes → types/nodes}/rules/range.d.ts +0 -0
  197. /package/dist/{nodes → types/nodes}/rules/regex.d.ts +0 -0
  198. /package/dist/{nodes → types/nodes}/rules/rules.d.ts +0 -0
  199. /package/dist/{parse → types/parse}/ast/ast.d.ts +0 -0
  200. /package/dist/{parse → types/parse}/ast/bound.d.ts +0 -0
  201. /package/dist/{parse → types/parse}/ast/config.d.ts +0 -0
  202. /package/dist/{parse → types/parse}/ast/distributableFunction.d.ts +0 -0
  203. /package/dist/{parse → types/parse}/ast/divisor.d.ts +0 -0
  204. /package/dist/{parse → types/parse}/ast/intersection.d.ts +0 -0
  205. /package/dist/{parse → types/parse}/ast/keyof.d.ts +0 -0
  206. /package/dist/{parse → types/parse}/ast/morph.d.ts +0 -0
  207. /package/dist/{parse → types/parse}/ast/narrow.d.ts +0 -0
  208. /package/dist/{parse → types/parse}/ast/node.d.ts +0 -0
  209. /package/dist/{parse → types/parse}/ast/tuple.d.ts +0 -0
  210. /package/dist/{parse → types/parse}/ast/union.d.ts +0 -0
  211. /package/dist/{parse → types/parse}/ast/utils.d.ts +0 -0
  212. /package/dist/{parse → types/parse}/definition.d.ts +0 -0
  213. /package/dist/{parse → types/parse}/record.d.ts +0 -0
  214. /package/dist/{parse → types/parse}/string/reduce/dynamic.d.ts +0 -0
  215. /package/dist/{parse → types/parse}/string/reduce/shared.d.ts +0 -0
  216. /package/dist/{parse → types/parse}/string/reduce/static.d.ts +0 -0
  217. /package/dist/{parse → types/parse}/string/shift/operand/enclosed.d.ts +0 -0
  218. /package/dist/{parse → types/parse}/string/shift/operand/operand.d.ts +0 -0
  219. /package/dist/{parse → types/parse}/string/shift/operand/unenclosed.d.ts +0 -0
  220. /package/dist/{parse → types/parse}/string/shift/operator/bounds.d.ts +0 -0
  221. /package/dist/{parse → types/parse}/string/shift/operator/divisor.d.ts +0 -0
  222. /package/dist/{parse → types/parse}/string/shift/operator/operator.d.ts +0 -0
  223. /package/dist/{parse → types/parse}/string/shift/scanner.d.ts +0 -0
  224. /package/dist/{parse → types/parse}/string/string.d.ts +0 -0
  225. /package/dist/{scopes → types/scopes}/ark.d.ts +0 -0
  226. /package/dist/{scopes → types/scopes}/cache.d.ts +0 -0
  227. /package/dist/{scopes → types/scopes}/expressions.d.ts +0 -0
  228. /package/dist/{scopes → types/scopes}/jsObjects.d.ts +0 -0
  229. /package/dist/{scopes → types/scopes}/scope.d.ts +0 -0
  230. /package/dist/{scopes → types/scopes}/tsKeywords.d.ts +0 -0
  231. /package/dist/{scopes → types/scopes}/type.d.ts +0 -0
  232. /package/dist/{scopes → types/scopes}/validation/creditCard.d.ts +0 -0
  233. /package/dist/{scopes → types/scopes}/validation/date.d.ts +0 -0
  234. /package/dist/{scopes → types/scopes}/validation/validation.d.ts +0 -0
  235. /package/dist/{traverse → types/traverse}/problems.d.ts +0 -0
  236. /package/dist/{traverse → types/traverse}/traverse.d.ts +0 -0
  237. /package/dist/{utils → types/utils}/chainableNoOpProxy.d.ts +0 -0
  238. /package/dist/{utils → types/utils}/data.d.ts +0 -0
  239. /package/dist/{utils → types/utils}/domains.d.ts +0 -0
  240. /package/dist/{utils → types/utils}/errors.d.ts +0 -0
  241. /package/dist/{utils → types/utils}/freeze.d.ts +0 -0
  242. /package/dist/{utils → types/utils}/generics.d.ts +0 -0
  243. /package/dist/{utils → types/utils}/numericLiterals.d.ts +0 -0
  244. /package/dist/{utils → types/utils}/objectKinds.d.ts +0 -0
  245. /package/dist/{utils → types/utils}/paths.d.ts +0 -0
  246. /package/dist/{utils → types/utils}/serialize.d.ts +0 -0
  247. /package/dist/{utils → types/utils}/unionToTuple.d.ts +0 -0
@@ -1,37 +1,33 @@
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");
1
+ import { rootType } from "../scope.js";
2
+ import { tsKeywords } from "../tsKeywords.js";
6
3
  const dayDelimiterMatcher = /^[./-]$/;
7
4
  // ISO 8601 date/time modernized from https://github.com/validatorjs/validator.js/blob/master/src/lib/isISO8601.js
8
5
  // Based on https://tc39.es/ecma262/#sec-date-time-string-format, the T
9
6
  // delimiter for date/time is mandatory. Regex from validator.js strict matcher:
10
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)?)?)?)?$/;
11
- const isValidDateInstance = (date) => !isNaN(date);
12
- const writeFormattedMustBe = (format) => `a ${format}-formatted date`;
13
- const tryParseDate = (data, opts) => {
8
+ const isValidDateInstance = (date)=>!isNaN(date);
9
+ const writeFormattedMustBe = (format)=>`a ${format}-formatted date`;
10
+ export const tryParseDate = (data, opts)=>{
14
11
  if (!opts?.format) {
15
12
  const result = new Date(data);
16
13
  return isValidDateInstance(result) ? result : "a valid date";
17
14
  }
18
15
  if (opts.format === "iso8601") {
19
- return iso8601Matcher.test(data)
20
- ? new Date(data)
21
- : writeFormattedMustBe("iso8601");
16
+ return iso8601Matcher.test(data) ? new Date(data) : writeFormattedMustBe("iso8601");
22
17
  }
23
18
  const dataParts = data.split(dayDelimiterMatcher);
24
19
  // will be the first delimiter matched, if there is one
25
20
  const delimiter = data[dataParts[0].length];
26
- const formatParts = delimiter ? opts.format.split(delimiter) : [opts.format];
21
+ const formatParts = delimiter ? opts.format.split(delimiter) : [
22
+ opts.format
23
+ ];
27
24
  if (dataParts.length !== formatParts.length) {
28
25
  return writeFormattedMustBe(opts.format);
29
26
  }
30
27
  const parsedParts = {};
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)) {
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)) {
35
31
  return writeFormattedMustBe(opts.format);
36
32
  }
37
33
  parsedParts[formatParts[i][0]] = dataParts[i];
@@ -42,12 +38,11 @@ const tryParseDate = (data, opts) => {
42
38
  }
43
39
  return writeFormattedMustBe(opts.format);
44
40
  };
45
- exports.tryParseDate = tryParseDate;
46
- exports.parsedDate = (0, scope_js_1.rootType)([
47
- tsKeywords_js_1.tsKeywords.string,
41
+ export const parsedDate = rootType([
42
+ tsKeywords.string,
48
43
  "|>",
49
- (s, problems) => {
50
- const result = (0, exports.tryParseDate)(s);
44
+ (s, problems)=>{
45
+ const result = tryParseDate(s);
51
46
  return typeof result === "string" ? problems.mustBe(result) : result;
52
47
  }
53
48
  ]);
@@ -0,0 +1,112 @@
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();
@@ -0,0 +1,260 @@
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
+ };