arktype 0.2.0 → 0.3.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 (80) hide show
  1. package/dist/cjs/nodes/{branches.js → branch.js} +56 -10
  2. package/dist/cjs/nodes/compose.js +1 -2
  3. package/dist/cjs/nodes/discriminate.js +7 -13
  4. package/dist/cjs/nodes/node.js +12 -2
  5. package/dist/cjs/nodes/predicate.js +7 -9
  6. package/dist/cjs/nodes/rules/class.js +10 -4
  7. package/dist/cjs/nodes/rules/divisor.js +1 -5
  8. package/dist/cjs/nodes/rules/props.js +75 -25
  9. package/dist/cjs/nodes/rules/range.js +1 -5
  10. package/dist/cjs/nodes/rules/regex.js +1 -5
  11. package/dist/cjs/nodes/rules/rules.js +19 -63
  12. package/dist/cjs/parse/string/reduce/dynamic.js +1 -2
  13. package/dist/cjs/parse/string/shift/operator/bounds.js +1 -1
  14. package/dist/cjs/parse/string/string.js +2 -2
  15. package/dist/cjs/parse/tuple/keyof.js +37 -18
  16. package/dist/cjs/parse/tuple/morph.js +2 -2
  17. package/dist/cjs/parse/tuple/tuple.js +16 -9
  18. package/dist/cjs/scopes/jsObjects.js +14 -14
  19. package/dist/cjs/scopes/type.js +4 -7
  20. package/dist/cjs/traverse/problems.js +12 -28
  21. package/dist/cjs/traverse/traverse.js +104 -133
  22. package/dist/cjs/utils/numericLiterals.js +2 -0
  23. package/dist/cjs/utils/objectKinds.js +1 -2
  24. package/dist/mjs/nodes/{branches.js → branch.js} +45 -2
  25. package/dist/mjs/nodes/compose.js +1 -2
  26. package/dist/mjs/nodes/discriminate.js +6 -12
  27. package/dist/mjs/nodes/node.js +10 -1
  28. package/dist/mjs/nodes/predicate.js +2 -3
  29. package/dist/mjs/nodes/rules/class.js +10 -4
  30. package/dist/mjs/nodes/rules/divisor.js +1 -5
  31. package/dist/mjs/nodes/rules/props.js +74 -27
  32. package/dist/mjs/nodes/rules/range.js +1 -5
  33. package/dist/mjs/nodes/rules/regex.js +1 -5
  34. package/dist/mjs/nodes/rules/rules.js +19 -62
  35. package/dist/mjs/parse/string/reduce/dynamic.js +2 -3
  36. package/dist/mjs/parse/string/shift/operator/bounds.js +1 -1
  37. package/dist/mjs/parse/string/string.js +2 -2
  38. package/dist/mjs/parse/tuple/keyof.js +38 -19
  39. package/dist/mjs/parse/tuple/morph.js +2 -2
  40. package/dist/mjs/parse/tuple/tuple.js +17 -10
  41. package/dist/mjs/scopes/jsObjects.js +14 -14
  42. package/dist/mjs/scopes/type.js +4 -7
  43. package/dist/mjs/traverse/problems.js +13 -29
  44. package/dist/mjs/traverse/traverse.js +105 -133
  45. package/dist/mjs/utils/numericLiterals.js +1 -0
  46. package/dist/mjs/utils/objectKinds.js +1 -1
  47. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  48. package/dist/types/nodes/branch.d.ts +36 -0
  49. package/dist/types/nodes/compose.d.ts +5 -10
  50. package/dist/types/nodes/discriminate.d.ts +1 -1
  51. package/dist/types/nodes/infer.d.ts +17 -16
  52. package/dist/types/nodes/node.d.ts +10 -5
  53. package/dist/types/nodes/predicate.d.ts +4 -32
  54. package/dist/types/nodes/rules/class.d.ts +3 -2
  55. package/dist/types/nodes/rules/divisor.d.ts +2 -1
  56. package/dist/types/nodes/rules/props.d.ts +19 -11
  57. package/dist/types/nodes/rules/range.d.ts +3 -2
  58. package/dist/types/nodes/rules/regex.d.ts +2 -1
  59. package/dist/types/nodes/rules/rules.d.ts +11 -13
  60. package/dist/types/parse/string/ast.d.ts +2 -1
  61. package/dist/types/parse/string/reduce/dynamic.d.ts +1 -1
  62. package/dist/types/parse/string/reduce/shared.d.ts +1 -1
  63. package/dist/types/parse/string/shift/operand/unenclosed.d.ts +1 -1
  64. package/dist/types/parse/string/shift/operator/bounds.d.ts +1 -1
  65. package/dist/types/parse/string/string.d.ts +1 -1
  66. package/dist/types/parse/tuple/keyof.d.ts +2 -2
  67. package/dist/types/scopes/type.d.ts +5 -11
  68. package/dist/types/traverse/problems.d.ts +8 -23
  69. package/dist/types/traverse/traverse.d.ts +27 -23
  70. package/dist/types/utils/generics.d.ts +0 -1
  71. package/dist/types/utils/numericLiterals.d.ts +1 -0
  72. package/dist/types/utils/objectKinds.d.ts +43 -43
  73. package/package.json +1 -2
  74. package/dist/cjs/nodes/functors.js +0 -35
  75. package/dist/cjs/nodes/rules/objectKind.js +0 -78
  76. package/dist/mjs/nodes/functors.js +0 -27
  77. package/dist/mjs/nodes/rules/objectKind.js +0 -63
  78. package/dist/types/nodes/branches.d.ts +0 -14
  79. package/dist/types/nodes/functors.d.ts +0 -6
  80. package/dist/types/nodes/rules/objectKind.d.ts +0 -15
@@ -7,10 +7,15 @@ Object.defineProperty(exports, "parseKeyOfTuple", {
7
7
  get: ()=>parseKeyOfTuple
8
8
  });
9
9
  const _predicateTs = require("../../nodes/predicate.js");
10
+ const _propsTs = require("../../nodes/rules/props.js");
10
11
  const _errorsTs = require("../../utils/errors.js");
11
12
  const _genericsTs = require("../../utils/generics.js");
13
+ const _numericLiteralsTs = require("../../utils/numericLiterals.js");
12
14
  const _definitionTs = require("../definition.js");
13
15
  const _astTs = require("../string/ast.js");
16
+ const numericIndexKey = {
17
+ regex: _numericLiteralsTs.wellFormedNonNegativeIntegerMatcher.source
18
+ };
14
19
  const parseKeyOfTuple = (def, ctx)=>{
15
20
  const resolution = ctx.type.meta.scope.resolveNode((0, _definitionTs.parseDefinition)(def[1], ctx));
16
21
  if (!(0, _predicateTs.resolutionExtendsDomain)(resolution, "object")) {
@@ -19,27 +24,41 @@ const parseKeyOfTuple = (def, ctx)=>{
19
24
  if (resolution.object === true) {
20
25
  return (0, _astTs.writeImplicitNeverMessage)(ctx.path, "keyof");
21
26
  }
22
- const branchKeys = [];
23
27
  const objectBranches = (0, _genericsTs.listFrom)(resolution.object);
24
- const initialKeys = getPropsFromBranch(objectBranches[0]);
28
+ let sharedKeys = keysOfBranch(objectBranches[0]);
25
29
  for(let i = 1; i < objectBranches.length; i++){
26
- const keys = getPropsFromBranch(objectBranches[i]);
27
- if (!keys.length) {
28
- return (0, _astTs.writeImplicitNeverMessage)(ctx.path, "keyof");
29
- }
30
- branchKeys.push(keys);
30
+ const branchKeys = keysOfBranch(objectBranches[i]);
31
+ // we can filter directly by equality here because the RegExp we're
32
+ // using will always be reference equal to
33
+ // wellFormedNonNegativeIntegerMatcher
34
+ sharedKeys = sharedKeys.filter((k)=>branchKeys.includes(k));
31
35
  }
32
- const result = [];
33
- initialKeys.forEach((key)=>{
34
- const hasKey = branchKeys.every((keySet)=>keySet.includes(key));
35
- if (hasKey) {
36
- result.push({
37
- value: key
38
- });
39
- }
40
- });
36
+ const result = sharedKeys.map((k)=>typeof k === "string" ? {
37
+ value: k
38
+ } : {
39
+ regex: k.source
40
+ });
41
41
  return result.length ? {
42
- string: result
42
+ string: result.length === 1 ? result[0] : result
43
43
  } : (0, _astTs.writeImplicitNeverMessage)(ctx.path, "keyof");
44
44
  };
45
- const getPropsFromBranch = (branch)=>(0, _genericsTs.hasKey)(branch, "props") ? Object.keys(branch.props) : [];
45
+ const keysOfBranch = (branch)=>{
46
+ if (!("props" in branch)) {
47
+ return [];
48
+ }
49
+ if (branch.class !== "Array") {
50
+ return Object.keys(branch.props);
51
+ }
52
+ const arrayProps = [];
53
+ for (const key of Object.keys(branch.props)){
54
+ if (key === "length") {
55
+ continue;
56
+ }
57
+ if (key === _propsTs.mappedKeys.index) {
58
+ arrayProps.push(_numericLiteralsTs.wellFormedNonNegativeIntegerMatcher);
59
+ } else {
60
+ arrayProps.push(key);
61
+ }
62
+ }
63
+ return arrayProps;
64
+ };
@@ -12,7 +12,7 @@ _export(exports, {
12
12
  parseMorphTuple: ()=>parseMorphTuple,
13
13
  writeMalformedMorphExpressionMessage: ()=>writeMalformedMorphExpressionMessage
14
14
  });
15
- const _predicateTs = require("../../nodes/predicate.js");
15
+ const _branchTs = require("../../nodes/branch.js");
16
16
  const _errorsTs = require("../../utils/errors.js");
17
17
  const _objectKindsTs = require("../../utils/objectKinds.js");
18
18
  const _serializeTs = require("../../utils/serialize.js");
@@ -41,7 +41,7 @@ const parseMorphTuple = (def, ctx)=>{
41
41
  }
42
42
  return result;
43
43
  };
44
- const applyMorph = (branch, morph)=>(0, _predicateTs.branchIsTransformation)(branch) ? {
44
+ const applyMorph = (branch, morph)=>(0, _branchTs.isMorphBranch)(branch) ? {
45
45
  ...branch,
46
46
  morph: branch.morph ? Array.isArray(branch.morph) ? [
47
47
  ...branch.morph,
@@ -12,7 +12,6 @@ _export(exports, {
12
12
  parseTuple: ()=>parseTuple,
13
13
  writeMalformedFunctionalExpressionMessage: ()=>writeMalformedFunctionalExpressionMessage
14
14
  });
15
- const _functorsTs = require("../../nodes/functors.js");
16
15
  const _nodeTs = require("../../nodes/node.js");
17
16
  const _domainsTs = require("../../utils/domains.js");
18
17
  const _errorsTs = require("../../utils/errors.js");
@@ -28,7 +27,19 @@ const parseTuple = (def, ctx)=>{
28
27
  if (isPrefixExpression(def)) {
29
28
  return prefixParsers[def[0]](def, ctx);
30
29
  }
31
- const props = {};
30
+ const props = {
31
+ // length is created as a prerequisite prop, ensuring if it is invalid,
32
+ // no other props will be checked, which is usually desirable for tuple
33
+ // definitions.
34
+ length: [
35
+ "!",
36
+ {
37
+ number: {
38
+ value: def.length
39
+ }
40
+ }
41
+ ]
42
+ };
32
43
  for(let i = 0; i < def.length; i++){
33
44
  ctx.path.push(`${i}`);
34
45
  props[i] = (0, _definitionTs.parseDefinition)(def[i], ctx);
@@ -36,12 +47,8 @@ const parseTuple = (def, ctx)=>{
36
47
  }
37
48
  return {
38
49
  object: {
39
- objectKind: "Array",
40
- props,
41
- range: {
42
- comparator: "==",
43
- limit: def.length
44
- }
50
+ class: "Array",
51
+ props
45
52
  }
46
53
  };
47
54
  };
@@ -53,7 +60,7 @@ const parseBranchTuple = (def, ctx)=>{
53
60
  const r = (0, _definitionTs.parseDefinition)(def[2], ctx);
54
61
  return def[1] === "&" ? (0, _nodeTs.intersection)(l, r, ctx.type) : (0, _nodeTs.union)(l, r, ctx.type);
55
62
  };
56
- const parseArrayTuple = (def, scope)=>_functorsTs.functors.Array((0, _definitionTs.parseDefinition)(def[0], scope));
63
+ const parseArrayTuple = (def, scope)=>(0, _nodeTs.arrayOf)((0, _definitionTs.parseDefinition)(def[0], scope));
57
64
  const writeMalformedFunctionalExpressionMessage = (operator, rightDef)=>`Expression requires a function following '${operator}' (was ${typeof rightDef})`;
58
65
  const isPostfixExpression = (def)=>postfixParsers[def[1]] !== undefined;
59
66
  const postfixParsers = {
@@ -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
  ]
@@ -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];
@@ -23,7 +23,7 @@ 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
+ var _definitions_code, _definitions_code1;
27
27
  class ArkTypeError extends TypeError {
28
28
  constructor(problems){
29
29
  super(problems.summary);
@@ -55,10 +55,11 @@ class Problem {
55
55
  }
56
56
  }
57
57
  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}`;
58
+ add(code, data, source, opts) {
59
+ // copy the path to avoid future mutations affecting it
60
+ const path = opts?.path ?? _pathsTs.Path.from(this.state.path);
61
+ const problem = new Problem(code, path, data, source, this.state.getConfigForProblemCode(code));
62
+ const pathKey = `${path}`;
62
63
  const existing = this.byPath[pathKey];
63
64
  if (existing) {
64
65
  if (existing.parts) {
@@ -79,11 +80,9 @@ class ProblemArray extends Array {
79
80
  this.byPath[pathKey] = problem;
80
81
  this.push(problem);
81
82
  }
83
+ return false;
82
84
  }
83
85
  get summary() {
84
- if (this.length === 1) {
85
- return this[0].message;
86
- }
87
86
  return this.join("\n");
88
87
  }
89
88
  toString() {
@@ -127,9 +126,6 @@ class DataWrapper {
127
126
  get domain() {
128
127
  return (0, _domainsTs.domainOf)(this.value);
129
128
  }
130
- get objectKind() {
131
- return (0, _objectKindsTs.objectKindOf)(this.value) ?? "non-object";
132
- }
133
129
  get size() {
134
130
  return typeof this.value === "string" || Array.isArray(this.value) ? this.value.length : typeof this.value === "number" ? this.value : 0;
135
131
  }
@@ -144,12 +140,12 @@ class DataWrapper {
144
140
  }
145
141
  }
146
142
  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}`;
143
+ 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
144
  const compileDefaultProblemWriters = (definitions)=>{
149
145
  let code;
150
146
  for(code in definitions){
151
147
  (_definitions_code = definitions[code]).writeReason ?? (_definitions_code.writeReason = writeDefaultReason);
152
- (_definitions_code1 = definitions[code]).addContext ?? (_definitions_code1.addContext = writeDefaultProblemMessage);
148
+ (_definitions_code1 = definitions[code]).addContext ?? (_definitions_code1.addContext = addDefaultContext);
153
149
  }
154
150
  return definitions;
155
151
  };
@@ -158,7 +154,7 @@ const defaultProblemWriters = compileDefaultProblemWriters({
158
154
  mustBe: (divisor)=>divisor === 1 ? `an integer` : `a multiple of ${divisor}`
159
155
  },
160
156
  class: {
161
- mustBe: (constructor)=>`an instance of ${constructor.name}`,
157
+ mustBe: (expected)=>typeof expected === "string" ? _objectKindsTs.objectKindDescriptions[expected] : `an instance of ${expected.name}`,
162
158
  writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.className)
163
159
  },
164
160
  domain: {
@@ -169,10 +165,6 @@ const defaultProblemWriters = compileDefaultProblemWriters({
169
165
  mustBe: (domains)=>describeDomains(domains),
170
166
  writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.domain)
171
167
  },
172
- objectKind: {
173
- mustBe: (kind)=>_objectKindsTs.objectKindDescriptions[kind],
174
- writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.objectKind)
175
- },
176
168
  missing: {
177
169
  mustBe: ()=>"defined",
178
170
  writeReason: (mustBe)=>writeDefaultReason(mustBe, "")
@@ -182,7 +174,7 @@ const defaultProblemWriters = compileDefaultProblemWriters({
182
174
  writeReason: (mustBe, data)=>writeDefaultReason(mustBe, `${data.size}`)
183
175
  },
184
176
  regex: {
185
- mustBe: (expression)=>expression[0] === "/" ? `a string matching ${expression}` : `a valid ${expression}`
177
+ mustBe: (expression)=>`a string matching ${expression}`
186
178
  },
187
179
  value: {
188
180
  mustBe: _serializeTs.stringify
@@ -191,15 +183,7 @@ const defaultProblemWriters = compileDefaultProblemWriters({
191
183
  mustBe: (values)=>describeBranches(values.map(_serializeTs.stringify))
192
184
  },
193
185
  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
- },
186
+ mustBe: (branchProblems)=>describeBranches(branchProblems.map((problem)=>`${problem.path} must be ${problem.parts ? describeBranches(problem.parts.map((part)=>part.mustBe)) : problem.mustBe}`)),
203
187
  writeReason: (mustBe, data)=>`${mustBe} (was ${data})`,
204
188
  addContext: (reason, path)=>path.length ? `At ${path}, ${reason}` : reason
205
189
  },