arktype 1.0.9-alpha → 1.0.11-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 (86) hide show
  1. package/README.md +53 -39
  2. package/dist/cjs/main.js +26 -18
  3. package/dist/cjs/nodes/branch.js +18 -18
  4. package/dist/cjs/nodes/compose.js +28 -11
  5. package/dist/cjs/nodes/discriminate.js +27 -27
  6. package/dist/cjs/nodes/node.js +24 -24
  7. package/dist/cjs/nodes/predicate.js +17 -17
  8. package/dist/cjs/nodes/rules/class.js +5 -5
  9. package/dist/cjs/nodes/rules/collapsibleSet.js +3 -3
  10. package/dist/cjs/nodes/rules/divisor.js +2 -2
  11. package/dist/cjs/nodes/rules/props.js +19 -19
  12. package/dist/cjs/nodes/rules/range.js +6 -6
  13. package/dist/cjs/nodes/rules/regex.js +3 -3
  14. package/dist/cjs/nodes/rules/rules.js +20 -20
  15. package/dist/cjs/parse/ast/config.js +2 -2
  16. package/dist/cjs/parse/ast/distributableFunction.js +9 -9
  17. package/dist/cjs/parse/ast/intersection.js +5 -5
  18. package/dist/cjs/parse/ast/keyof.js +31 -30
  19. package/dist/cjs/parse/ast/morph.js +10 -10
  20. package/dist/cjs/parse/ast/narrow.js +6 -6
  21. package/dist/cjs/parse/ast/tuple.js +21 -21
  22. package/dist/cjs/parse/definition.js +18 -20
  23. package/dist/cjs/parse/record.js +4 -4
  24. package/dist/cjs/parse/string/reduce/dynamic.js +44 -26
  25. package/dist/cjs/parse/string/reduce/shared.js +2 -2
  26. package/dist/cjs/parse/string/shift/operand/enclosed.js +2 -2
  27. package/dist/cjs/parse/string/shift/operand/operand.js +3 -3
  28. package/dist/cjs/parse/string/shift/operand/unenclosed.js +3 -3
  29. package/dist/cjs/parse/string/shift/operator/bounds.js +18 -18
  30. package/dist/cjs/parse/string/shift/operator/divisor.js +6 -6
  31. package/dist/cjs/parse/string/shift/operator/operator.js +6 -6
  32. package/dist/cjs/parse/string/shift/scanner.js +16 -1
  33. package/dist/cjs/parse/string/string.js +8 -8
  34. package/dist/cjs/scopes/ark.js +15 -15
  35. package/dist/cjs/scopes/cache.js +16 -3
  36. package/dist/cjs/scopes/expressions.js +9 -9
  37. package/dist/cjs/scopes/jsObjects.js +2 -42
  38. package/dist/cjs/scopes/scope.js +59 -31
  39. package/dist/cjs/scopes/tsKeywords.js +2 -2
  40. package/dist/cjs/scopes/type.js +6 -5
  41. package/dist/cjs/scopes/validation/creditCard.js +2 -2
  42. package/dist/cjs/scopes/validation/date.js +4 -4
  43. package/dist/cjs/scopes/validation/validation.js +17 -17
  44. package/dist/cjs/traverse/problems.js +41 -21
  45. package/dist/cjs/traverse/traverse.js +56 -31
  46. package/dist/cjs/utils/data.js +18 -4
  47. package/dist/cjs/utils/generics.js +3 -3
  48. package/dist/cjs/utils/numericLiterals.js +4 -4
  49. package/dist/cjs/utils/objectKinds.js +4 -4
  50. package/dist/cjs/utils/serialize.js +6 -6
  51. package/dist/mjs/main.js +5 -1
  52. package/dist/mjs/nodes/compose.js +17 -0
  53. package/dist/mjs/parse/ast/config.js +5 -1
  54. package/dist/mjs/parse/ast/keyof.js +2 -1
  55. package/dist/mjs/parse/definition.js +0 -1
  56. package/dist/mjs/parse/string/reduce/dynamic.js +18 -0
  57. package/dist/mjs/parse/string/shift/scanner.js +16 -1
  58. package/dist/mjs/scopes/cache.js +14 -1
  59. package/dist/mjs/scopes/expressions.js +88 -8
  60. package/dist/mjs/scopes/jsObjects.js +4 -41
  61. package/dist/mjs/scopes/scope.js +30 -2
  62. package/dist/mjs/scopes/tsKeywords.js +18 -1
  63. package/dist/mjs/scopes/type.js +1 -0
  64. package/dist/mjs/scopes/validation/validation.js +19 -1
  65. package/dist/mjs/traverse/problems.js +22 -2
  66. package/dist/mjs/traverse/traverse.js +25 -0
  67. package/dist/mjs/utils/data.js +14 -0
  68. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  69. package/dist/types/main.d.ts +8 -1
  70. package/dist/types/nodes/node.d.ts +6 -0
  71. package/dist/types/parse/ast/bound.d.ts +14 -0
  72. package/dist/types/parse/ast/config.d.ts +5 -0
  73. package/dist/types/parse/ast/divisor.d.ts +5 -0
  74. package/dist/types/parse/definition.d.ts +2 -4
  75. package/dist/types/parse/string/reduce/static.d.ts +12 -2
  76. package/dist/types/parse/string/shift/operand/operand.d.ts +3 -3
  77. package/dist/types/parse/string/shift/operand/unenclosed.d.ts +5 -5
  78. package/dist/types/parse/string/string.d.ts +1 -2
  79. package/dist/types/scopes/ark.d.ts +10 -22
  80. package/dist/types/scopes/expressions.d.ts +88 -0
  81. package/dist/types/scopes/jsObjects.d.ts +4 -10
  82. package/dist/types/scopes/tsKeywords.d.ts +18 -0
  83. package/dist/types/scopes/type.d.ts +4 -2
  84. package/dist/types/scopes/validation/validation.d.ts +19 -0
  85. package/dist/types/utils/generics.d.ts +2 -2
  86. package/package.json +17 -18
@@ -1,9 +1,89 @@
1
1
  import { scopes } from "./ark.js";
2
- export const intersection = scopes.ark.intersection;
3
- export const union = scopes.ark.union;
4
- export const arrayOf = scopes.ark.arrayOf;
5
- export const keyOf = scopes.ark.keyOf;
6
- export const instanceOf = scopes.ark.instanceOf;
7
- export const valueOf = scopes.ark.valueOf;
8
- export const narrow = scopes.ark.narrow;
9
- export const morph = scopes.ark.morph;
2
+ /**
3
+ * @operator {@link intersection | &}
4
+ * @docgenTable
5
+ * @string "L&R"
6
+ * @tuple [L, "&", R]
7
+ * @helper intersection(L,R)
8
+ * @example string
9
+ * const intersection = type("/@arktype\.io$/ & email")
10
+ * @example tuple
11
+ * const tupleIntersection = type(["/@arktype\.io$/", "&", "email"])
12
+ * @example helper
13
+ * const helperIntersection = intersection("/@arktype\.io$/","email")
14
+ */ export const intersection = scopes.ark.intersection;
15
+ /**
16
+ * @operator {@link union | |}
17
+ * @docgenTable
18
+ * @string "L|R"
19
+ * @tuple [L, "|" , R]
20
+ * @helper union(L,R)
21
+ * @example string
22
+ * const union = type("string|number")
23
+ * @example tuple
24
+ * const tupleUnion = type(["string", "|", "number"])
25
+ * @example helper
26
+ * const helperUnion = union("string", "number")
27
+ */ export const union = scopes.ark.union;
28
+ /**
29
+ * @operator {@link arrayOf}
30
+ * @docgenTable
31
+ * @string "T[]"
32
+ * @tuple [T, "[]"]
33
+ * @helper arrayOf(T)
34
+ * @example string
35
+ * const numberArray = type("number[]")
36
+ * @example tuple
37
+ * const tupleArray = type(["number", "[]"])
38
+ * @example helper
39
+ * const helperArray = arrayOf("number")
40
+ */ export const arrayOf = scopes.ark.arrayOf;
41
+ /**
42
+ * @operator {@link keyOf}
43
+ * @docgenTable
44
+ * @tuple "["keyOf", T]"
45
+ * @helper keyOf(T)
46
+ * @example tuple
47
+ * const tupleKeyOf = type(["keyOf", {a:"string"}])
48
+ * @example helper
49
+ * const helperKeyOf = keyOf({a:"string"})
50
+ */ export const keyOf = scopes.ark.keyOf;
51
+ /**
52
+ * @operator {@link instanceOf}
53
+ * @docgenTable
54
+ * @tuple ["instanceOf", T]
55
+ * @helper instanceOf(T)
56
+ * @example tuple
57
+ * const tupleInstanceOf = type(["instanceOf", Date])
58
+ * @example helper
59
+ * const helperInstanceOf = instanceOf(Date)
60
+ */ export const instanceOf = scopes.ark.instanceOf;
61
+ /**
62
+ * @operator {@link valueOf | ===}
63
+ * @docgenTable
64
+ * @tuple ["===", T]
65
+ * @helper valueOf(T)
66
+ * @example tuple
67
+ * const tupleValueOf = type(["valueOf", {a:"string"}])
68
+ * @example helper
69
+ * const helperValueOf = valueOf({a:"string"})
70
+ */ export const valueOf = scopes.ark.valueOf;
71
+ /**
72
+ * @operator {@link narrow | =>}
73
+ * @docgenTable
74
+ * @tuple ["type", "=>" , condition]
75
+ * @example tuple
76
+ * const narrow = type( ["number", "=>" , (n) => n % 2 === 0])
77
+ * @example
78
+ * const isEven = (x: unknown): x is number => x % 2 === 0
79
+ */ export const narrow = scopes.ark.narrow;
80
+ /**
81
+ * @operator {@link morph | |>}
82
+ * @docgenTable
83
+ * @tuple [inputType, "|>", (data) => output]
84
+ * @helper morph(inputType, (data) => output)
85
+ * @example tuple
86
+ * const tupleMorph = type( ["string", "|>" , (data) => `morphed ${data}`])
87
+ * @example helper
88
+ * const helperMorph = morph("string", (data) => `morphed ${data}`)
89
+ */ export const morph = scopes.ark.morph;
@@ -1,5 +1,8 @@
1
1
  import { scope } from "./scope.js";
2
- export const jsObjectsScope = scope({
2
+ /**
3
+ * @docgenScope
4
+ * @docgenTable
5
+ */ export const jsObjectsScope = scope({
3
6
  Function: [
4
7
  "node",
5
8
  {
@@ -8,14 +11,6 @@ export const jsObjectsScope = scope({
8
11
  }
9
12
  }
10
13
  ],
11
- Array: [
12
- "node",
13
- {
14
- object: {
15
- class: Array
16
- }
17
- }
18
- ],
19
14
  Date: [
20
15
  "node",
21
16
  {
@@ -56,38 +51,6 @@ export const jsObjectsScope = scope({
56
51
  }
57
52
  }
58
53
  ],
59
- Object: [
60
- "node",
61
- {
62
- object: {
63
- class: Object
64
- }
65
- }
66
- ],
67
- String: [
68
- "node",
69
- {
70
- object: {
71
- class: String
72
- }
73
- }
74
- ],
75
- Number: [
76
- "node",
77
- {
78
- object: {
79
- class: Number
80
- }
81
- }
82
- ],
83
- Boolean: [
84
- "node",
85
- {
86
- object: {
87
- class: Boolean
88
- }
89
- }
90
- ],
91
54
  WeakMap: [
92
55
  "node",
93
56
  {
@@ -48,7 +48,19 @@ function _classPrivateMethodInit(obj, privateSet) {
48
48
  _checkPrivateRedeclaration(obj, privateSet);
49
49
  privateSet.add(obj);
50
50
  }
51
- var _ctx_type, _ctx_type1;
51
+ function _defineProperty(obj, key, value) {
52
+ if (key in obj) {
53
+ Object.defineProperty(obj, key, {
54
+ value: value,
55
+ enumerable: true,
56
+ configurable: true,
57
+ writable: true
58
+ });
59
+ } else {
60
+ obj[key] = value;
61
+ }
62
+ return obj;
63
+ }
52
64
  import { flattenType, isConfigNode } from "../nodes/node.js";
53
65
  import { parseDefinition } from "../parse/definition.js";
54
66
  import { compileProblemWriters } from "../traverse/problems.js";
@@ -78,6 +90,7 @@ export class Scope {
78
90
  return `${this.name}.${id}`;
79
91
  }
80
92
  addAnonymousTypeReference(referencedType, ctx) {
93
+ var _ctx_type;
81
94
  (_ctx_type = ctx.type).includesMorph || (_ctx_type.includesMorph = referencedType.includesMorph);
82
95
  return referencedType.node;
83
96
  }
@@ -94,13 +107,14 @@ export class Scope {
94
107
  return _classPrivateFieldGet(this, _exports).root;
95
108
  }
96
109
  addParsedReferenceIfResolvable(name, ctx) {
110
+ var _ctx_type;
97
111
  const resolution = _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, name, "undefined", [
98
112
  name
99
113
  ]);
100
114
  if (!resolution) {
101
115
  return false;
102
116
  }
103
- (_ctx_type1 = ctx.type).includesMorph || (_ctx_type1.includesMorph = resolution.includesMorph);
117
+ (_ctx_type = ctx.type).includesMorph || (_ctx_type.includesMorph = resolution.includesMorph);
104
118
  return true;
105
119
  }
106
120
  resolve(name) {
@@ -123,6 +137,10 @@ export class Scope {
123
137
  _classPrivateMethodInit(this, _cacheSpaces);
124
138
  _classPrivateMethodInit(this, _initializeContext);
125
139
  _classPrivateMethodInit(this, _resolveRecurse);
140
+ _defineProperty(this, "aliases", void 0);
141
+ _defineProperty(this, "name", void 0);
142
+ _defineProperty(this, "config", void 0);
143
+ _defineProperty(this, "parseCache", void 0);
126
144
  _classPrivateFieldInit(this, _resolutions, {
127
145
  writable: true,
128
146
  value: void 0
@@ -131,6 +149,16 @@ export class Scope {
131
149
  writable: true,
132
150
  value: void 0
133
151
  });
152
+ _defineProperty(this, "expressions", void 0);
153
+ _defineProperty(this, "intersection", void 0);
154
+ _defineProperty(this, "union", void 0);
155
+ _defineProperty(this, "arrayOf", void 0);
156
+ _defineProperty(this, "keyOf", void 0);
157
+ _defineProperty(this, "valueOf", void 0);
158
+ _defineProperty(this, "instanceOf", void 0);
159
+ _defineProperty(this, "narrow", void 0);
160
+ _defineProperty(this, "morph", void 0);
161
+ _defineProperty(this, "type", void 0);
134
162
  this.aliases = aliases;
135
163
  this.parseCache = new FreezingCache();
136
164
  _classPrivateFieldSet(this, _resolutions, new Cache());
@@ -9,7 +9,24 @@ const always = {
9
9
  symbol: true,
10
10
  undefined: true
11
11
  };
12
- export const tsKeywordsScope = scope({
12
+ /**
13
+ * @keywords keywords: {"any": "any",
14
+ "bigint": "a bigint",
15
+ "boolean": "a boolean",
16
+ "false": "false",
17
+ "never": "never",
18
+ "null": "null",
19
+ "number": "a number",
20
+ "object": "an object",
21
+ "string": "a string",
22
+ "symbol": "a symbol",
23
+ "true": "true",
24
+ "unknown": "unknown",
25
+ "void": "void",
26
+ "undefined": "undefined"}
27
+ * @docgenScope
28
+ * @docgenTable
29
+ */ export const tsKeywordsScope = scope({
13
30
  any: [
14
31
  "node",
15
32
  always
@@ -17,6 +17,7 @@ export const initializeType = (name, definition, config, scope)=>{
17
17
  return result.problems ? result.problems.throw() : result.data;
18
18
  },
19
19
  infer: chainableNoOpProxy,
20
+ inferIn: chainableNoOpProxy,
20
21
  qualifiedName: isAnonymousName(name) ? scope.getAnonymousQualifiedName(name) : `${scope.name}.${name}`,
21
22
  definition,
22
23
  scope,
@@ -37,7 +37,25 @@ const json = rootType([
37
37
  ], {
38
38
  mustBe: "a JSON-parsable string"
39
39
  });
40
- export const validationScope = scope({
40
+ /**
41
+ * @keywords keywords: {
42
+ "alpha": "only letters",
43
+ "alphanumeric": "only letters and digits",
44
+ "lowercase": "only lowercase letters",
45
+ "uppercase": "only uppercase letters",
46
+ "creditCard": "a valid credit card number",
47
+ "email": "a valid email",
48
+ "uuid": "a valid UUID",
49
+ "parsedNumber": "a well-formed numeric string",
50
+ "parsedInteger": "a well-formed integer string",
51
+ "parsedDate": "a valid date",
52
+ "semver": "a valid semantic version",
53
+ "json": "a JSON-parsable string",
54
+ "integer": "an integer"
55
+ }
56
+ * @docgenScope
57
+ * @docgenTable
58
+ */ export const validationScope = scope({
41
59
  // Character sets
42
60
  alpha: [
43
61
  /^[A-Za-z]*$/,
@@ -38,6 +38,19 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
38
38
  _classApplyDescriptorSet(receiver, descriptor, value);
39
39
  return value;
40
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
+ }
41
54
  import { Scanner } from "../parse/string/shift/scanner.js";
42
55
  import { DataWrapper } from "../utils/data.js";
43
56
  import { domainDescriptions } from "../utils/domains.js";
@@ -49,6 +62,7 @@ import { stringify } from "../utils/serialize.js";
49
62
  export class ArkTypeError extends TypeError {
50
63
  constructor(problems){
51
64
  super(`${problems}`);
65
+ _defineProperty(this, "cause", void 0);
52
66
  this.cause = problems;
53
67
  }
54
68
  }
@@ -66,6 +80,12 @@ export class Problem {
66
80
  return typeof this.writers.mustBe === "string" ? this.writers.mustBe : this.writers.mustBe(this.source);
67
81
  }
68
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);
69
89
  this.code = code;
70
90
  this.path = path;
71
91
  this.data = data;
@@ -128,8 +148,8 @@ class ProblemArray extends Array {
128
148
  }
129
149
  constructor(state){
130
150
  super();
131
- this.byPath = {};
132
- this.count = 0;
151
+ _defineProperty(this, "byPath", {});
152
+ _defineProperty(this, "count", 0);
133
153
  _classPrivateFieldInit(this, _state, {
134
154
  writable: true,
135
155
  value: void 0
@@ -38,6 +38,19 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
38
38
  _classApplyDescriptorSet(receiver, descriptor, value);
39
39
  return value;
40
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
+ }
41
54
  import { serializeCase } from "../nodes/discriminate.js";
42
55
  import { checkClass } from "../nodes/rules/class.js";
43
56
  import { checkDivisor } from "../nodes/rules/divisor.js";
@@ -75,6 +88,10 @@ export const traverseRoot = (t, data)=>{
75
88
  return result;
76
89
  };
77
90
  const CheckResult = class {
91
+ constructor(){
92
+ _defineProperty(this, "data", void 0);
93
+ _defineProperty(this, "problems", void 0);
94
+ }
78
95
  };
79
96
  var _seen = /*#__PURE__*/ new WeakMap();
80
97
  export class TraversalState {
@@ -163,6 +180,14 @@ export class TraversalState {
163
180
  return hasValidBranch || !this.problems.add("branches", branchProblems);
164
181
  }
165
182
  constructor(data, type){
183
+ _defineProperty(this, "data", void 0);
184
+ _defineProperty(this, "type", void 0);
185
+ _defineProperty(this, "path", void 0);
186
+ _defineProperty(this, "problems", void 0);
187
+ _defineProperty(this, "entriesToPrune", void 0);
188
+ _defineProperty(this, "failFast", void 0);
189
+ _defineProperty(this, "traversalConfig", void 0);
190
+ _defineProperty(this, "rootScope", void 0);
166
191
  _classPrivateFieldInit(this, _seen, {
167
192
  writable: true,
168
193
  value: void 0
@@ -1,3 +1,16 @@
1
+ function _defineProperty(obj, key, value) {
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value: value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
8
+ });
9
+ } else {
10
+ obj[key] = value;
11
+ }
12
+ return obj;
13
+ }
1
14
  import { domainOf } from "./domains.js";
2
15
  import { stringify } from "./serialize.js";
3
16
  export const sizeOf = (data)=>typeof data === "string" || Array.isArray(data) ? data.length : typeof data === "number" ? data : 0;
@@ -19,6 +32,7 @@ export class DataWrapper {
19
32
  return Object(this.value).constructor.name;
20
33
  }
21
34
  constructor(value){
35
+ _defineProperty(this, "value", void 0);
22
36
  this.value = value;
23
37
  }
24
38
  }