arktype 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. package/dist/api.cjs +9 -0
  2. package/dist/api.d.ts +10 -0
  3. package/dist/api.mjs +1 -0
  4. package/dist/cjs/nodes/{branches.js → branch.js} +65 -10
  5. package/dist/cjs/nodes/compose.js +2 -3
  6. package/dist/cjs/nodes/discriminate.js +9 -15
  7. package/dist/cjs/nodes/node.js +21 -11
  8. package/dist/cjs/nodes/predicate.js +8 -10
  9. package/dist/cjs/nodes/rules/class.js +10 -4
  10. package/dist/cjs/nodes/rules/divisor.js +1 -5
  11. package/dist/cjs/nodes/rules/props.js +75 -25
  12. package/dist/cjs/nodes/rules/range.js +2 -6
  13. package/dist/cjs/nodes/rules/regex.js +1 -5
  14. package/dist/cjs/nodes/rules/rules.js +23 -63
  15. package/dist/cjs/parse/ast/ast.js +4 -0
  16. package/dist/cjs/parse/ast/bound.js +9 -0
  17. package/dist/cjs/parse/{tuple/utils.js → ast/distributableFunction.js} +1 -1
  18. package/dist/cjs/parse/ast/divisor.js +9 -0
  19. package/dist/cjs/parse/{string/ast.js → ast/intersection.js} +2 -8
  20. package/dist/cjs/parse/ast/keyof.js +109 -0
  21. package/dist/cjs/parse/{tuple → ast}/morph.js +2 -2
  22. package/dist/cjs/parse/{tuple → ast}/narrow.js +2 -2
  23. package/dist/cjs/parse/{tuple → ast}/tuple.js +23 -16
  24. package/dist/cjs/parse/ast/union.js +9 -0
  25. package/dist/cjs/parse/ast/utils.js +4 -0
  26. package/dist/cjs/parse/definition.js +12 -2
  27. package/dist/cjs/parse/string/reduce/dynamic.js +7 -8
  28. package/dist/cjs/parse/string/shift/operator/bounds.js +4 -4
  29. package/dist/cjs/parse/string/shift/operator/divisor.js +3 -3
  30. package/dist/cjs/parse/string/string.js +2 -2
  31. package/dist/cjs/scopes/expressions.js +29 -0
  32. package/dist/cjs/scopes/jsObjects.js +14 -14
  33. package/dist/cjs/scopes/scope.js +56 -4
  34. package/dist/cjs/scopes/type.js +4 -7
  35. package/dist/cjs/traverse/problems.js +67 -53
  36. package/dist/cjs/traverse/traverse.js +103 -135
  37. package/dist/cjs/utils/data.js +39 -0
  38. package/dist/cjs/utils/generics.js +20 -2
  39. package/dist/cjs/utils/numericLiterals.js +2 -0
  40. package/dist/cjs/utils/objectKinds.js +1 -2
  41. package/dist/mjs/nodes/{branches.js → branch.js} +54 -2
  42. package/dist/mjs/nodes/compose.js +3 -4
  43. package/dist/mjs/nodes/discriminate.js +9 -15
  44. package/dist/mjs/nodes/node.js +17 -8
  45. package/dist/mjs/nodes/predicate.js +4 -5
  46. package/dist/mjs/nodes/rules/class.js +10 -4
  47. package/dist/mjs/nodes/rules/divisor.js +1 -5
  48. package/dist/mjs/nodes/rules/props.js +74 -27
  49. package/dist/mjs/nodes/rules/range.js +2 -6
  50. package/dist/mjs/nodes/rules/regex.js +1 -5
  51. package/dist/mjs/nodes/rules/rules.js +23 -62
  52. package/dist/mjs/parse/ast/ast.js +1 -0
  53. package/dist/mjs/parse/ast/bound.js +1 -0
  54. package/dist/mjs/parse/{tuple/utils.js → ast/distributableFunction.js} +2 -2
  55. package/dist/mjs/parse/ast/divisor.js +1 -0
  56. package/dist/mjs/parse/{string/ast.js → ast/intersection.js} +2 -5
  57. package/dist/mjs/parse/ast/keyof.js +101 -0
  58. package/dist/mjs/parse/{tuple → ast}/morph.js +2 -2
  59. package/dist/mjs/parse/ast/narrow.js +7 -0
  60. package/dist/mjs/parse/{tuple → ast}/tuple.js +24 -17
  61. package/dist/mjs/parse/ast/union.js +1 -0
  62. package/dist/mjs/parse/ast/utils.js +1 -0
  63. package/dist/mjs/parse/definition.js +12 -2
  64. package/dist/mjs/parse/string/reduce/dynamic.js +8 -9
  65. package/dist/mjs/parse/string/shift/operator/bounds.js +4 -4
  66. package/dist/mjs/parse/string/shift/operator/divisor.js +3 -3
  67. package/dist/mjs/parse/string/string.js +2 -2
  68. package/dist/mjs/scopes/expressions.js +9 -0
  69. package/dist/mjs/scopes/jsObjects.js +14 -14
  70. package/dist/mjs/scopes/scope.js +55 -3
  71. package/dist/mjs/scopes/type.js +4 -7
  72. package/dist/mjs/traverse/problems.js +68 -53
  73. package/dist/mjs/traverse/traverse.js +107 -139
  74. package/dist/mjs/utils/data.js +24 -0
  75. package/dist/mjs/utils/generics.js +18 -1
  76. package/dist/mjs/utils/numericLiterals.js +1 -0
  77. package/dist/mjs/utils/objectKinds.js +1 -1
  78. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  79. package/dist/types/nodes/branch.d.ts +27 -0
  80. package/dist/types/nodes/compose.d.ts +5 -10
  81. package/dist/types/nodes/discriminate.d.ts +3 -3
  82. package/dist/types/nodes/infer.d.ts +21 -20
  83. package/dist/types/nodes/node.d.ts +12 -16
  84. package/dist/types/nodes/predicate.d.ts +4 -59
  85. package/dist/types/nodes/rules/class.d.ts +3 -2
  86. package/dist/types/nodes/rules/divisor.d.ts +2 -1
  87. package/dist/types/nodes/rules/props.d.ts +19 -11
  88. package/dist/types/nodes/rules/range.d.ts +3 -2
  89. package/dist/types/nodes/rules/regex.d.ts +2 -1
  90. package/dist/types/nodes/rules/rules.d.ts +12 -14
  91. package/dist/types/parse/ast/ast.d.ts +24 -0
  92. package/dist/types/parse/ast/bound.d.ts +10 -0
  93. package/dist/types/parse/{tuple/utils.d.ts → ast/distributableFunction.d.ts} +2 -3
  94. package/dist/types/parse/ast/divisor.d.ts +8 -0
  95. package/dist/types/parse/ast/intersection.d.ts +17 -0
  96. package/dist/types/parse/{tuple → ast}/keyof.d.ts +4 -4
  97. package/dist/types/parse/{tuple → ast}/morph.d.ts +3 -3
  98. package/dist/types/parse/ast/narrow.d.ts +21 -0
  99. package/dist/types/parse/ast/tuple.d.ts +40 -0
  100. package/dist/types/parse/ast/union.d.ts +12 -0
  101. package/dist/types/parse/ast/utils.d.ts +7 -0
  102. package/dist/types/parse/definition.d.ts +9 -7
  103. package/dist/types/parse/record.d.ts +2 -2
  104. package/dist/types/parse/string/reduce/dynamic.d.ts +1 -1
  105. package/dist/types/parse/string/reduce/shared.d.ts +6 -5
  106. package/dist/types/parse/string/reduce/static.d.ts +11 -6
  107. package/dist/types/parse/string/shift/operand/unenclosed.d.ts +2 -2
  108. package/dist/types/parse/string/shift/operator/bounds.d.ts +4 -3
  109. package/dist/types/parse/string/shift/operator/divisor.d.ts +5 -1
  110. package/dist/types/parse/string/string.d.ts +2 -3
  111. package/dist/types/scopes/expressions.d.ts +48 -0
  112. package/dist/types/scopes/scope.d.ts +17 -7
  113. package/dist/types/scopes/type.d.ts +13 -15
  114. package/dist/types/traverse/problems.d.ts +17 -41
  115. package/dist/types/traverse/traverse.d.ts +85 -52
  116. package/dist/types/utils/data.d.ts +13 -0
  117. package/dist/types/utils/domains.d.ts +1 -1
  118. package/dist/types/utils/generics.d.ts +13 -10
  119. package/dist/types/utils/numericLiterals.d.ts +1 -0
  120. package/dist/types/utils/objectKinds.d.ts +43 -43
  121. package/dist/types/utils/paths.d.ts +2 -0
  122. package/package.json +1 -2
  123. package/dist/cjs/nodes/functors.js +0 -35
  124. package/dist/cjs/nodes/rules/objectKind.js +0 -78
  125. package/dist/cjs/parse/tuple/keyof.js +0 -45
  126. package/dist/cjs/utils/size.js +0 -16
  127. package/dist/mjs/nodes/functors.js +0 -27
  128. package/dist/mjs/nodes/rules/objectKind.js +0 -63
  129. package/dist/mjs/parse/tuple/keyof.js +0 -37
  130. package/dist/mjs/parse/tuple/narrow.js +0 -7
  131. package/dist/mjs/utils/size.js +0 -2
  132. package/dist/types/nodes/branches.d.ts +0 -14
  133. package/dist/types/nodes/functors.d.ts +0 -6
  134. package/dist/types/nodes/rules/objectKind.d.ts +0 -15
  135. package/dist/types/parse/string/ast.d.ts +0 -45
  136. package/dist/types/parse/tuple/narrow.d.ts +0 -13
  137. package/dist/types/parse/tuple/tuple.d.ts +0 -31
  138. package/dist/types/utils/size.d.ts +0 -3
@@ -18,7 +18,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
18
18
  "node",
19
19
  {
20
20
  object: {
21
- objectKind: "Function"
21
+ class: "Function"
22
22
  }
23
23
  }
24
24
  ],
@@ -26,7 +26,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
26
26
  "node",
27
27
  {
28
28
  object: {
29
- objectKind: "Array"
29
+ class: "Array"
30
30
  }
31
31
  }
32
32
  ],
@@ -34,7 +34,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
34
34
  "node",
35
35
  {
36
36
  object: {
37
- objectKind: "Date"
37
+ class: "Date"
38
38
  }
39
39
  }
40
40
  ],
@@ -42,7 +42,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
42
42
  "node",
43
43
  {
44
44
  object: {
45
- objectKind: "Error"
45
+ class: "Error"
46
46
  }
47
47
  }
48
48
  ],
@@ -50,7 +50,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
50
50
  "node",
51
51
  {
52
52
  object: {
53
- objectKind: "Map"
53
+ class: "Map"
54
54
  }
55
55
  }
56
56
  ],
@@ -58,7 +58,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
58
58
  "node",
59
59
  {
60
60
  object: {
61
- objectKind: "RegExp"
61
+ class: "RegExp"
62
62
  }
63
63
  }
64
64
  ],
@@ -66,7 +66,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
66
66
  "node",
67
67
  {
68
68
  object: {
69
- objectKind: "Set"
69
+ class: "Set"
70
70
  }
71
71
  }
72
72
  ],
@@ -74,7 +74,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
74
74
  "node",
75
75
  {
76
76
  object: {
77
- objectKind: "Object"
77
+ class: "Object"
78
78
  }
79
79
  }
80
80
  ],
@@ -82,7 +82,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
82
82
  "node",
83
83
  {
84
84
  object: {
85
- objectKind: "String"
85
+ class: "String"
86
86
  }
87
87
  }
88
88
  ],
@@ -90,7 +90,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
90
90
  "node",
91
91
  {
92
92
  object: {
93
- objectKind: "Number"
93
+ class: "Number"
94
94
  }
95
95
  }
96
96
  ],
@@ -98,7 +98,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
98
98
  "node",
99
99
  {
100
100
  object: {
101
- objectKind: "Boolean"
101
+ class: "Boolean"
102
102
  }
103
103
  }
104
104
  ],
@@ -106,7 +106,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
106
106
  "node",
107
107
  {
108
108
  object: {
109
- objectKind: "WeakMap"
109
+ class: "WeakMap"
110
110
  }
111
111
  }
112
112
  ],
@@ -114,7 +114,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
114
114
  "node",
115
115
  {
116
116
  object: {
117
- objectKind: "WeakSet"
117
+ class: "WeakSet"
118
118
  }
119
119
  }
120
120
  ],
@@ -122,7 +122,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
122
122
  "node",
123
123
  {
124
124
  object: {
125
- objectKind: "Promise"
125
+ class: "Promise"
126
126
  }
127
127
  }
128
128
  ]
@@ -10,8 +10,8 @@ function _export(target, all) {
10
10
  }
11
11
  _export(exports, {
12
12
  Scope: ()=>Scope,
13
- writeShallowCycleErrorMessage: ()=>writeShallowCycleErrorMessage,
14
13
  scope: ()=>scope,
14
+ writeShallowCycleErrorMessage: ()=>writeShallowCycleErrorMessage,
15
15
  writeDuplicateAliasesMessage: ()=>writeDuplicateAliasesMessage
16
16
  });
17
17
  const _nodeTs = require("../nodes/node.js");
@@ -159,7 +159,57 @@ class Scope {
159
159
  _classPrivateFieldSet(this, _resolutions, new _cacheTs.Cache());
160
160
  _classPrivateFieldSet(this, _exports, new _cacheTs.Cache());
161
161
  _classPrivateFieldSet(this, _anonymousTypeCount, 0);
162
- this.type = (def, opts = {})=>{
162
+ this.expressions = {
163
+ intersection: (l, r, opts)=>this.type([
164
+ l,
165
+ "&",
166
+ r
167
+ ], opts),
168
+ union: (l, r, opts)=>this.type([
169
+ l,
170
+ "|",
171
+ r
172
+ ], opts),
173
+ arrayOf: (def, opts)=>this.type([
174
+ def,
175
+ "[]"
176
+ ], opts),
177
+ keyOf: (def, opts)=>this.type([
178
+ "keyof",
179
+ def
180
+ ], opts),
181
+ fromNode: (def, opts)=>this.type([
182
+ "node",
183
+ def
184
+ ], opts),
185
+ instanceOf: (def, opts)=>this.type([
186
+ "instanceof",
187
+ def
188
+ ], opts),
189
+ valueOf: (def, opts)=>this.type([
190
+ "===",
191
+ def
192
+ ], opts),
193
+ narrow: (def, fn, opts)=>this.type([
194
+ def,
195
+ ":",
196
+ fn
197
+ ], opts),
198
+ morph: (def, fn, opts)=>this.type([
199
+ def,
200
+ "=>",
201
+ fn
202
+ ], opts)
203
+ };
204
+ this.intersection = this.expressions.intersection;
205
+ this.union = this.expressions.union;
206
+ this.arrayOf = this.expressions.arrayOf;
207
+ this.keyOf = this.expressions.keyOf;
208
+ this.valueOf = this.expressions.valueOf;
209
+ this.instanceOf = this.expressions.instanceOf;
210
+ this.narrow = this.expressions.narrow;
211
+ this.morph = this.expressions.morph;
212
+ this.type = Object.assign((def, opts = {})=>{
163
213
  if (opts.name && this.aliases[opts.name]) {
164
214
  return (0, _errorsTs.throwParseError)(writeDuplicateAliasesMessage(opts.name));
165
215
  }
@@ -168,7 +218,9 @@ class Scope {
168
218
  result.node = this.resolveNode((0, _definitionTs.parseDefinition)(def, ctx));
169
219
  result.flat = (0, _nodeTs.flattenType)(result);
170
220
  return result;
171
- };
221
+ }, {
222
+ from: this.expressions.fromNode
223
+ });
172
224
  this.name = _classPrivateMethodGet(this, _register, register).call(this, opts);
173
225
  if (opts.standard !== false) {
174
226
  _classPrivateMethodGet(this, _cacheSpaces, cacheSpaces).call(this, [
@@ -236,9 +288,9 @@ function resolveRecurse(name, onUnresolvable, seen) {
236
288
  type.flat = (0, _nodeTs.flattenType)(type);
237
289
  return type;
238
290
  }
291
+ const scope = (aliases, opts = {})=>new Scope(aliases, opts);
239
292
  const writeShallowCycleErrorMessage = (name, seen)=>`Alias '${name}' has a shallow resolution cycle: ${[
240
293
  ...seen,
241
294
  name
242
295
  ].join("=>")}`;
243
- const scope = (aliases, opts = {})=>new Scope(aliases, opts);
244
296
  const writeDuplicateAliasesMessage = (name)=>`Alias '${name}' is already defined`;
@@ -51,14 +51,11 @@ const initializeType = (definition, opts, scope)=>{
51
51
  // dynamically assign a name to the primary traversal function
52
52
  const namedTraverse = {
53
53
  [name]: (data)=>{
54
- const state = new _traverseTs.TraversalState(type);
55
- const out = (0, _traverseTs.traverse)(data, type.flat, state);
56
- return state.problems.length ? {
57
- data,
58
- problems: state.problems
54
+ const state = new _traverseTs.TraversalState(data, type);
55
+ return (0, _traverseTs.traverse)(type.flat, state) ? {
56
+ data: state.data
59
57
  } : {
60
- data,
61
- out
58
+ problems: state.problems
62
59
  };
63
60
  }
64
61
  }[name];
@@ -14,16 +14,56 @@ _export(exports, {
14
14
  Problems: ()=>Problems,
15
15
  describeDomains: ()=>describeDomains,
16
16
  describeBranches: ()=>describeBranches,
17
- DataWrapper: ()=>DataWrapper,
18
17
  defaultProblemWriters: ()=>defaultProblemWriters
19
18
  });
20
19
  const _scannerTs = require("../parse/string/shift/scanner.js");
20
+ const _dataTs = require("../utils/data.js");
21
21
  const _domainsTs = require("../utils/domains.js");
22
22
  const _numericLiteralsTs = require("../utils/numericLiterals.js");
23
23
  const _objectKindsTs = require("../utils/objectKinds.js");
24
24
  const _pathsTs = require("../utils/paths.js");
25
25
  const _serializeTs = require("../utils/serialize.js");
26
- var _definitions_code, _definitions_code1, _clausesByPath, _pathKey;
26
+ function _checkPrivateRedeclaration(obj, privateCollection) {
27
+ if (privateCollection.has(obj)) {
28
+ throw new TypeError("Cannot initialize the same private elements twice on an object");
29
+ }
30
+ }
31
+ function _classApplyDescriptorGet(receiver, descriptor) {
32
+ if (descriptor.get) {
33
+ return descriptor.get.call(receiver);
34
+ }
35
+ return descriptor.value;
36
+ }
37
+ function _classApplyDescriptorSet(receiver, descriptor, value) {
38
+ if (descriptor.set) {
39
+ descriptor.set.call(receiver, value);
40
+ } else {
41
+ if (!descriptor.writable) {
42
+ throw new TypeError("attempted to set read only private field");
43
+ }
44
+ descriptor.value = value;
45
+ }
46
+ }
47
+ function _classExtractFieldDescriptor(receiver, privateMap, action) {
48
+ if (!privateMap.has(receiver)) {
49
+ throw new TypeError("attempted to " + action + " private field on non-instance");
50
+ }
51
+ return privateMap.get(receiver);
52
+ }
53
+ function _classPrivateFieldGet(receiver, privateMap) {
54
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get");
55
+ return _classApplyDescriptorGet(receiver, descriptor);
56
+ }
57
+ function _classPrivateFieldInit(obj, privateMap, value) {
58
+ _checkPrivateRedeclaration(obj, privateMap);
59
+ privateMap.set(obj, value);
60
+ }
61
+ function _classPrivateFieldSet(receiver, privateMap, value) {
62
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set");
63
+ _classApplyDescriptorSet(receiver, descriptor, value);
64
+ return value;
65
+ }
66
+ var _definitions_code, _definitions_code1;
27
67
  class ArkTypeError extends TypeError {
28
68
  constructor(problems){
29
69
  super(problems.summary);
@@ -38,7 +78,7 @@ class Problem {
38
78
  return this.writers.addContext(this.reason, this.path);
39
79
  }
40
80
  get reason() {
41
- return this.writers.writeReason(this.mustBe, new DataWrapper(this.data));
81
+ return this.writers.writeReason(this.mustBe, new _dataTs.DataWrapper(this.data));
42
82
  }
43
83
  get mustBe() {
44
84
  return this.writers.mustBe(this.source);
@@ -54,20 +94,27 @@ class Problem {
54
94
  }
55
95
  }
56
96
  }
97
+ var _state = /*#__PURE__*/ new WeakMap();
57
98
  class ProblemArray extends Array {
58
- add(code, data, source) {
59
- const problem = new Problem(code, // copy the path to avoid future mutations affecting it
60
- _pathsTs.Path.from(this.state.path), data, source, this.state.getConfigForProblemCode(code));
61
- const pathKey = `${this.state.path}`;
99
+ create(code, source, opts) {
100
+ // copy the path to avoid future mutations affecting it
101
+ const path = opts?.path ?? _pathsTs.Path.from(_classPrivateFieldGet(this, _state).path);
102
+ const data = // we have to check for the presence of the key explicitly since the
103
+ // data could be undefined or null
104
+ opts && "data" in opts ? opts.data : _classPrivateFieldGet(this, _state).data;
105
+ return this.add(new Problem(code, path, data, source, _classPrivateFieldGet(this, _state).getConfigForProblemCode(code)));
106
+ }
107
+ add(problem) {
108
+ const pathKey = `${problem.path}`;
62
109
  const existing = this.byPath[pathKey];
63
110
  if (existing) {
64
111
  if (existing.parts) {
65
112
  existing.parts.push(problem);
66
113
  } else {
67
- const problemIntersection = new Problem("multi", existing.path, data, [
114
+ const problemIntersection = new Problem("multi", existing.path, existing.data, [
68
115
  existing,
69
116
  problem
70
- ], this.state.getConfigForProblemCode("multi"));
117
+ ], _classPrivateFieldGet(this, _state).getConfigForProblemCode("multi"));
71
118
  const existingIndex = this.indexOf(existing);
72
119
  // If existing is found (which it always should be unless this was externally mutated),
73
120
  // replace it with the new problem intersection. In case it isn't for whatever reason,
@@ -79,11 +126,9 @@ class ProblemArray extends Array {
79
126
  this.byPath[pathKey] = problem;
80
127
  this.push(problem);
81
128
  }
129
+ return false;
82
130
  }
83
131
  get summary() {
84
- if (this.length === 1) {
85
- return this[0].message;
86
- }
87
132
  return this.join("\n");
88
133
  }
89
134
  toString() {
@@ -94,8 +139,12 @@ class ProblemArray extends Array {
94
139
  }
95
140
  constructor(state){
96
141
  super();
97
- this.state = state;
98
142
  this.byPath = {};
143
+ _classPrivateFieldInit(this, _state, {
144
+ writable: true,
145
+ value: void 0
146
+ });
147
+ _classPrivateFieldSet(this, _state, state);
99
148
  }
100
149
  }
101
150
  const Problems = ProblemArray;
@@ -120,36 +169,13 @@ const describeBranches = (descriptions)=>{
120
169
  description += ` or ${descriptions[descriptions.length - 1]}`;
121
170
  return description;
122
171
  };
123
- class DataWrapper {
124
- toString() {
125
- return (0, _serializeTs.stringify)(this.value);
126
- }
127
- get domain() {
128
- return (0, _domainsTs.domainOf)(this.value);
129
- }
130
- get objectKind() {
131
- return (0, _objectKindsTs.objectKindOf)(this.value) ?? "non-object";
132
- }
133
- get size() {
134
- return typeof this.value === "string" || Array.isArray(this.value) ? this.value.length : typeof this.value === "number" ? this.value : 0;
135
- }
136
- get units() {
137
- return typeof this.value === "string" ? "characters" : Array.isArray(this.value) ? "items" : "";
138
- }
139
- get className() {
140
- return Object(this.value).constructor.name;
141
- }
142
- constructor(value){
143
- this.value = value;
144
- }
145
- }
146
172
  const writeDefaultReason = (mustBe, was)=>`must be ${mustBe}${was && ` (was ${was})`}`;
147
- const writeDefaultProblemMessage = (reason, path)=>path.length === 0 ? capitalize(reason) : path.length === 1 && (0, _numericLiteralsTs.isWellFormedInteger)(path[0]) ? `Item at index ${path[0]} ${reason}` : `${path} ${reason}`;
173
+ const addDefaultContext = (reason, path)=>path.length === 0 ? capitalize(reason) : path.length === 1 && (0, _numericLiteralsTs.isWellFormedInteger)(path[0]) ? `Item at index ${path[0]} ${reason}` : `${path} ${reason}`;
148
174
  const compileDefaultProblemWriters = (definitions)=>{
149
175
  let code;
150
176
  for(code in definitions){
151
177
  (_definitions_code = definitions[code]).writeReason ?? (_definitions_code.writeReason = writeDefaultReason);
152
- (_definitions_code1 = definitions[code]).addContext ?? (_definitions_code1.addContext = writeDefaultProblemMessage);
178
+ (_definitions_code1 = definitions[code]).addContext ?? (_definitions_code1.addContext = addDefaultContext);
153
179
  }
154
180
  return definitions;
155
181
  };
@@ -158,7 +184,7 @@ const defaultProblemWriters = compileDefaultProblemWriters({
158
184
  mustBe: (divisor)=>divisor === 1 ? `an integer` : `a multiple of ${divisor}`
159
185
  },
160
186
  class: {
161
- mustBe: (constructor)=>`an instance of ${constructor.name}`,
187
+ mustBe: (expected)=>typeof expected === "string" ? _objectKindsTs.objectKindDescriptions[expected] : `an instance of ${expected.name}`,
162
188
  writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.className)
163
189
  },
164
190
  domain: {
@@ -169,10 +195,6 @@ const defaultProblemWriters = compileDefaultProblemWriters({
169
195
  mustBe: (domains)=>describeDomains(domains),
170
196
  writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.domain)
171
197
  },
172
- objectKind: {
173
- mustBe: (kind)=>_objectKindsTs.objectKindDescriptions[kind],
174
- writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.objectKind)
175
- },
176
198
  missing: {
177
199
  mustBe: ()=>"defined",
178
200
  writeReason: (mustBe)=>writeDefaultReason(mustBe, "")
@@ -182,7 +204,7 @@ const defaultProblemWriters = compileDefaultProblemWriters({
182
204
  writeReason: (mustBe, data)=>writeDefaultReason(mustBe, `${data.size}`)
183
205
  },
184
206
  regex: {
185
- mustBe: (expression)=>expression[0] === "/" ? `a string matching ${expression}` : `a valid ${expression}`
207
+ mustBe: (expression)=>`a string matching ${expression}`
186
208
  },
187
209
  value: {
188
210
  mustBe: _serializeTs.stringify
@@ -191,15 +213,7 @@ const defaultProblemWriters = compileDefaultProblemWriters({
191
213
  mustBe: (values)=>describeBranches(values.map(_serializeTs.stringify))
192
214
  },
193
215
  branches: {
194
- mustBe: (branchProblems)=>{
195
- const clausesByPath = {};
196
- for (const problem of branchProblems){
197
- const pathKey = `${problem.path}`;
198
- (_clausesByPath = clausesByPath)[_pathKey = pathKey] ?? (_clausesByPath[_pathKey] = []);
199
- clausesByPath[pathKey].push(problem.mustBe);
200
- }
201
- return describeBranches(Object.entries(clausesByPath).map(([path, clauses])=>`${path} must be ${clauses.length === 1 ? clauses[0] : describeBranches(clauses)}`));
202
- },
216
+ mustBe: (branchProblems)=>describeBranches(branchProblems.map((problem)=>`${problem.path} must be ${problem.parts ? describeBranches(problem.parts.map((part)=>part.mustBe)) : problem.mustBe}`)),
203
217
  writeReason: (mustBe, data)=>`${mustBe} (was ${data})`,
204
218
  addContext: (reason, path)=>path.length ? `At ${path}, ${reason}` : reason
205
219
  },