arktype 0.1.3 → 0.2.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 (74) hide show
  1. package/README.md +1 -1
  2. package/dist/api.cjs +4 -3
  3. package/dist/api.d.ts +3 -2
  4. package/dist/api.mjs +2 -1
  5. package/dist/cjs/nodes/node.js +20 -2
  6. package/dist/cjs/nodes/predicate.js +2 -0
  7. package/dist/cjs/nodes/rules/regex.js +4 -42
  8. package/dist/cjs/nodes/rules/rules.js +16 -12
  9. package/dist/cjs/parse/definition.js +2 -2
  10. package/dist/cjs/parse/string/ast.js +1 -1
  11. package/dist/cjs/parse/tuple/keyof.js +6 -7
  12. package/dist/cjs/parse/tuple/morph.js +7 -6
  13. package/dist/cjs/scopes/cache.js +39 -0
  14. package/dist/cjs/scopes/jsObjects.js +133 -0
  15. package/dist/cjs/scopes/scope.js +244 -0
  16. package/dist/cjs/scopes/standard.js +44 -0
  17. package/dist/cjs/scopes/tsKeywords.js +113 -0
  18. package/dist/cjs/scopes/type.js +70 -0
  19. package/dist/cjs/scopes/validation.js +64 -0
  20. package/dist/cjs/traverse/problems.js +50 -74
  21. package/dist/cjs/traverse/traverse.js +25 -3
  22. package/dist/cjs/utils/numericLiterals.js +1 -0
  23. package/dist/mjs/nodes/node.js +20 -2
  24. package/dist/mjs/nodes/predicate.js +1 -0
  25. package/dist/mjs/nodes/rules/regex.js +3 -41
  26. package/dist/mjs/nodes/rules/rules.js +17 -13
  27. package/dist/mjs/parse/definition.js +1 -1
  28. package/dist/mjs/parse/string/ast.js +1 -1
  29. package/dist/mjs/parse/tuple/keyof.js +6 -7
  30. package/dist/mjs/parse/tuple/morph.js +7 -6
  31. package/dist/mjs/scopes/cache.js +25 -0
  32. package/dist/mjs/scopes/jsObjects.js +119 -0
  33. package/dist/mjs/{main.js → scopes/scope.js} +23 -349
  34. package/dist/mjs/scopes/standard.js +27 -0
  35. package/dist/mjs/scopes/tsKeywords.js +99 -0
  36. package/dist/mjs/scopes/type.js +56 -0
  37. package/dist/mjs/scopes/validation.js +51 -0
  38. package/dist/mjs/traverse/problems.js +49 -73
  39. package/dist/mjs/traverse/traverse.js +26 -4
  40. package/dist/mjs/utils/numericLiterals.js +1 -1
  41. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  42. package/dist/types/nodes/compose.d.ts +1 -1
  43. package/dist/types/nodes/infer.d.ts +4 -4
  44. package/dist/types/nodes/node.d.ts +11 -7
  45. package/dist/types/nodes/predicate.d.ts +43 -54
  46. package/dist/types/nodes/rules/range.d.ts +1 -1
  47. package/dist/types/nodes/rules/regex.d.ts +1 -11
  48. package/dist/types/nodes/rules/rules.d.ts +2 -10
  49. package/dist/types/parse/definition.d.ts +1 -1
  50. package/dist/types/parse/record.d.ts +2 -2
  51. package/dist/types/parse/string/ast.d.ts +5 -4
  52. package/dist/types/parse/string/reduce/dynamic.d.ts +1 -1
  53. package/dist/types/parse/string/reduce/shared.d.ts +1 -1
  54. package/dist/types/parse/string/reduce/static.d.ts +1 -1
  55. package/dist/types/parse/string/shift/operand/unenclosed.d.ts +1 -1
  56. package/dist/types/parse/string/shift/operator/bounds.d.ts +1 -1
  57. package/dist/types/parse/tuple/keyof.d.ts +8 -0
  58. package/dist/types/parse/tuple/morph.d.ts +1 -1
  59. package/dist/types/parse/tuple/narrow.d.ts +1 -1
  60. package/dist/types/parse/tuple/tuple.d.ts +7 -13
  61. package/dist/types/scopes/cache.d.ts +14 -0
  62. package/dist/types/scopes/jsObjects.d.ts +36 -0
  63. package/dist/types/scopes/scope.d.ts +76 -0
  64. package/dist/types/scopes/standard.d.ts +280 -0
  65. package/dist/types/scopes/tsKeywords.d.ts +32 -0
  66. package/dist/types/scopes/type.d.ts +53 -0
  67. package/dist/types/scopes/validation.d.ts +21 -0
  68. package/dist/types/traverse/problems.d.ts +29 -32
  69. package/dist/types/traverse/traverse.d.ts +10 -8
  70. package/dist/types/utils/generics.d.ts +2 -1
  71. package/dist/types/utils/numericLiterals.d.ts +1 -0
  72. package/package.json +2 -2
  73. package/dist/cjs/main.js +0 -574
  74. package/dist/types/main.d.ts +0 -321
@@ -0,0 +1,119 @@
1
+ import { scope } from "./scope.js";
2
+ export const jsObjectsScope = scope({
3
+ Function: [
4
+ "node",
5
+ {
6
+ object: {
7
+ objectKind: "Function"
8
+ }
9
+ }
10
+ ],
11
+ Array: [
12
+ "node",
13
+ {
14
+ object: {
15
+ objectKind: "Array"
16
+ }
17
+ }
18
+ ],
19
+ Date: [
20
+ "node",
21
+ {
22
+ object: {
23
+ objectKind: "Date"
24
+ }
25
+ }
26
+ ],
27
+ Error: [
28
+ "node",
29
+ {
30
+ object: {
31
+ objectKind: "Error"
32
+ }
33
+ }
34
+ ],
35
+ Map: [
36
+ "node",
37
+ {
38
+ object: {
39
+ objectKind: "Map"
40
+ }
41
+ }
42
+ ],
43
+ RegExp: [
44
+ "node",
45
+ {
46
+ object: {
47
+ objectKind: "RegExp"
48
+ }
49
+ }
50
+ ],
51
+ Set: [
52
+ "node",
53
+ {
54
+ object: {
55
+ objectKind: "Set"
56
+ }
57
+ }
58
+ ],
59
+ Object: [
60
+ "node",
61
+ {
62
+ object: {
63
+ objectKind: "Object"
64
+ }
65
+ }
66
+ ],
67
+ String: [
68
+ "node",
69
+ {
70
+ object: {
71
+ objectKind: "String"
72
+ }
73
+ }
74
+ ],
75
+ Number: [
76
+ "node",
77
+ {
78
+ object: {
79
+ objectKind: "Number"
80
+ }
81
+ }
82
+ ],
83
+ Boolean: [
84
+ "node",
85
+ {
86
+ object: {
87
+ objectKind: "Boolean"
88
+ }
89
+ }
90
+ ],
91
+ WeakMap: [
92
+ "node",
93
+ {
94
+ object: {
95
+ objectKind: "WeakMap"
96
+ }
97
+ }
98
+ ],
99
+ WeakSet: [
100
+ "node",
101
+ {
102
+ object: {
103
+ objectKind: "WeakSet"
104
+ }
105
+ }
106
+ ],
107
+ Promise: [
108
+ "node",
109
+ {
110
+ object: {
111
+ objectKind: "Promise"
112
+ }
113
+ }
114
+ ]
115
+ }, {
116
+ name: "jsObjects",
117
+ standard: false
118
+ });
119
+ export const jsObjects = jsObjectsScope.compile();
@@ -76,30 +76,30 @@ function _classPrivateMethodInit(obj, privateSet) {
76
76
  privateSet.add(obj);
77
77
  }
78
78
  var _ctx_type_meta;
79
- import { flattenType } from "./nodes/node.js";
80
- import { parseDefinition } from "./parse/definition.js";
81
- import { compileProblemOptions } from "./traverse/problems.js";
82
- import { TraversalState, traverse } from "./traverse/traverse.js";
83
- import { chainableNoOpProxy } from "./utils/chainableNoOpProxy.js";
84
- import { throwInternalError, throwParseError } from "./utils/errors.js";
85
- import { deepFreeze } from "./utils/freeze.js";
86
- import { Path } from "./utils/paths.js";
79
+ import { flattenType } from "../nodes/node.js";
80
+ import { parseDefinition } from "../parse/definition.js";
81
+ import { chainableNoOpProxy } from "../utils/chainableNoOpProxy.js";
82
+ import { throwInternalError, throwParseError } from "../utils/errors.js";
83
+ import { Path } from "../utils/paths.js";
84
+ import { Cache, FreezingCache } from "./cache.js";
85
+ import { initializeType } from "./type.js";
87
86
  let anonymousScopeCount = 0;
88
87
  const scopeRegistry = {};
89
- var _resolutions = /*#__PURE__*/ new WeakMap(), _exports = /*#__PURE__*/ new WeakMap(), _anonymousTypeCount = /*#__PURE__*/ new WeakMap(), _register = /*#__PURE__*/ new WeakSet(), _cacheSpaces = /*#__PURE__*/ new WeakSet(), _initializeContext = /*#__PURE__*/ new WeakSet(), _compiled = /*#__PURE__*/ new WeakMap(), _resolveRecurse = /*#__PURE__*/ new WeakSet();
88
+ const spaceRegistry = {};
89
+ var _resolutions = /*#__PURE__*/ new WeakMap(), _exports = /*#__PURE__*/ new WeakMap(), _anonymousTypeCount = /*#__PURE__*/ new WeakMap(), _register = /*#__PURE__*/ new WeakSet(), _cacheSpaces = /*#__PURE__*/ new WeakSet(), _initializeContext = /*#__PURE__*/ new WeakSet(), _resolveRecurse = /*#__PURE__*/ new WeakSet();
90
90
  export class Scope {
91
- createAnonymousTypeName() {
92
- return `type${++_classPrivateFieldUpdate(this, _anonymousTypeCount).value}`;
91
+ createAnonymousTypeSuffix() {
92
+ return ++_classPrivateFieldUpdate(this, _anonymousTypeCount).value;
93
93
  }
94
94
  get infer() {
95
95
  return chainableNoOpProxy;
96
96
  }
97
97
  compile() {
98
- if (!_classPrivateFieldGet(this, _compiled)) {
98
+ if (!spaceRegistry[this.name]) {
99
99
  for(const name in this.aliases){
100
100
  this.resolve(name);
101
101
  }
102
- _classPrivateFieldSet(this, _compiled, true);
102
+ spaceRegistry[this.name] = _classPrivateFieldGet(this, _exports).root;
103
103
  }
104
104
  return _classPrivateFieldGet(this, _exports).root;
105
105
  }
@@ -137,10 +137,6 @@ export class Scope {
137
137
  writable: true,
138
138
  value: void 0
139
139
  });
140
- _classPrivateFieldInit(this, _compiled, {
141
- writable: true,
142
- value: void 0
143
- });
144
140
  this.aliases = aliases;
145
141
  this.opts = opts;
146
142
  this.parseCache = new FreezingCache();
@@ -148,17 +144,19 @@ export class Scope {
148
144
  _classPrivateFieldSet(this, _exports, new Cache());
149
145
  _classPrivateFieldSet(this, _anonymousTypeCount, 0);
150
146
  this.type = (def, opts = {})=>{
147
+ if (opts.name && this.aliases[opts.name]) {
148
+ return throwParseError(writeDuplicateAliasesMessage(opts.name));
149
+ }
151
150
  const result = initializeType(def, opts, this);
152
151
  const ctx = _classPrivateMethodGet(this, _initializeContext, initializeContext).call(this, result);
153
152
  result.node = this.resolveNode(parseDefinition(def, ctx));
154
153
  result.flat = flattenType(result);
155
154
  return result;
156
155
  };
157
- _classPrivateFieldSet(this, _compiled, false);
158
156
  this.name = _classPrivateMethodGet(this, _register, register).call(this, opts);
159
157
  if (opts.standard !== false) {
160
158
  _classPrivateMethodGet(this, _cacheSpaces, cacheSpaces).call(this, [
161
- standardTypes
159
+ spaceRegistry["standard"]
162
160
  ], "imports");
163
161
  }
164
162
  if (opts.imports) {
@@ -167,10 +165,13 @@ export class Scope {
167
165
  if (opts.includes) {
168
166
  _classPrivateMethodGet(this, _cacheSpaces, cacheSpaces).call(this, opts.includes, "includes");
169
167
  }
168
+ if (opts.config) {
169
+ this.config = opts.config;
170
+ }
170
171
  }
171
172
  }
172
173
  function register(opts) {
173
- const name = opts.name ? scopeRegistry[opts.name] ? throwParseError(`A scope named '${opts.name}' already exists`) : opts.name : `anonymousScope${++anonymousScopeCount}`;
174
+ const name = opts.name ? scopeRegistry[opts.name] ? throwParseError(`A scope named '${opts.name}' already exists`) : opts.name : `scope${++anonymousScopeCount}`;
174
175
  scopeRegistry[name] = this;
175
176
  return name;
176
177
  }
@@ -178,7 +179,7 @@ function cacheSpaces(spaces, kind) {
178
179
  for (const space of spaces){
179
180
  for(const name in space){
180
181
  if (_classPrivateFieldGet(this, _resolutions).has(name) || name in this.aliases) {
181
- throwParseError(writeDuplicateAliasMessage(name));
182
+ throwParseError(writeDuplicateAliasesMessage(name));
182
183
  }
183
184
  _classPrivateFieldGet(this, _resolutions).set(name, space[name]);
184
185
  if (kind === "includes") {
@@ -201,7 +202,6 @@ function resolveRecurse(name, onUnresolvable, seen) {
201
202
  if (!this.aliases[name]) {
202
203
  return onUnresolvable === "throw" ? throwInternalError(`Unexpectedly failed to resolve alias '${name}'`) : undefined;
203
204
  }
204
- // TODO: opts?
205
205
  const type = initializeType(this.aliases[name], {
206
206
  name
207
207
  }, this);
@@ -220,335 +220,9 @@ function resolveRecurse(name, onUnresolvable, seen) {
220
220
  type.flat = flattenType(type);
221
221
  return type;
222
222
  }
223
- // TODO: merge scope options
224
- const initializeType = (definition, opts, scope)=>{
225
- const name = opts.name ?? "type";
226
- const meta = {
227
- name,
228
- id: `${scope.name}.${opts.name ? name : scope.createAnonymousTypeName()}`,
229
- definition,
230
- scope,
231
- problems: compileProblemOptions(opts.problems),
232
- includesMorph: false
233
- };
234
- const root = {
235
- // temporarily initialize node/flat to aliases that will be included in
236
- // the final type in case of cyclic resolutions
237
- node: name,
238
- flat: [
239
- [
240
- "alias",
241
- name
242
- ]
243
- ],
244
- meta,
245
- infer: chainableNoOpProxy
246
- };
247
- // dynamically assign a name to the primary traversal function
248
- const namedTraverse = {
249
- [name]: (data)=>{
250
- const state = new TraversalState(type);
251
- const out = traverse(data, type.flat, state);
252
- return state.problems.length ? {
253
- data,
254
- problems: state.problems
255
- } : {
256
- data,
257
- out
258
- };
259
- }
260
- }[name];
261
- // we need to assign this to a variable before returning so we can reference
262
- // it in namedTraverse
263
- const type = Object.assign(namedTraverse, root);
264
- return type;
265
- };
266
- class Cache {
267
- get root() {
268
- return this.cache;
269
- }
270
- has(name) {
271
- return name in this.cache;
272
- }
273
- get(name) {
274
- return this.cache[name];
275
- }
276
- set(name, item) {
277
- this.cache[name] = item;
278
- return item;
279
- }
280
- constructor(){
281
- this.cache = {};
282
- }
283
- }
284
- class FreezingCache extends Cache {
285
- set(name, item) {
286
- this.cache[name] = deepFreeze(item);
287
- return item;
288
- }
289
- }
290
223
  export const writeShallowCycleErrorMessage = (name, seen)=>`Alias '${name}' has a shallow resolution cycle: ${[
291
224
  ...seen,
292
225
  name
293
226
  ].join("=>")}`;
294
- const always = {
295
- bigint: true,
296
- boolean: true,
297
- null: true,
298
- number: true,
299
- object: true,
300
- string: true,
301
- symbol: true,
302
- undefined: true
303
- };
304
227
  export const scope = (aliases, opts = {})=>new Scope(aliases, opts);
305
- const tsKeywords = scope({
306
- any: [
307
- "node",
308
- always
309
- ],
310
- bigint: [
311
- "node",
312
- {
313
- bigint: true
314
- }
315
- ],
316
- boolean: [
317
- "node",
318
- {
319
- boolean: true
320
- }
321
- ],
322
- false: [
323
- "node",
324
- {
325
- boolean: {
326
- value: false
327
- }
328
- }
329
- ],
330
- never: [
331
- "node",
332
- {}
333
- ],
334
- null: [
335
- "node",
336
- {
337
- null: true
338
- }
339
- ],
340
- number: [
341
- "node",
342
- {
343
- number: true
344
- }
345
- ],
346
- object: [
347
- "node",
348
- {
349
- object: true
350
- }
351
- ],
352
- string: [
353
- "node",
354
- {
355
- string: true
356
- }
357
- ],
358
- symbol: [
359
- "node",
360
- {
361
- symbol: true
362
- }
363
- ],
364
- true: [
365
- "node",
366
- {
367
- boolean: {
368
- value: true
369
- }
370
- }
371
- ],
372
- unknown: [
373
- "node",
374
- always
375
- ],
376
- void: [
377
- "node",
378
- {
379
- undefined: true
380
- }
381
- ],
382
- undefined: [
383
- "node",
384
- {
385
- undefined: true
386
- }
387
- ]
388
- }, {
389
- name: "ts",
390
- standard: false
391
- });
392
- const tsKeywordsSpace = tsKeywords.compile();
393
- const jsObjects = scope({
394
- Function: [
395
- "node",
396
- {
397
- object: {
398
- objectKind: "Function"
399
- }
400
- }
401
- ],
402
- Array: [
403
- "node",
404
- {
405
- object: {
406
- objectKind: "Array"
407
- }
408
- }
409
- ],
410
- Date: [
411
- "node",
412
- {
413
- object: {
414
- objectKind: "Date"
415
- }
416
- }
417
- ],
418
- Error: [
419
- "node",
420
- {
421
- object: {
422
- objectKind: "Error"
423
- }
424
- }
425
- ],
426
- Map: [
427
- "node",
428
- {
429
- object: {
430
- objectKind: "Map"
431
- }
432
- }
433
- ],
434
- RegExp: [
435
- "node",
436
- {
437
- object: {
438
- objectKind: "RegExp"
439
- }
440
- }
441
- ],
442
- Set: [
443
- "node",
444
- {
445
- object: {
446
- objectKind: "Set"
447
- }
448
- }
449
- ],
450
- Object: [
451
- "node",
452
- {
453
- object: {
454
- objectKind: "Object"
455
- }
456
- }
457
- ],
458
- String: [
459
- "node",
460
- {
461
- object: {
462
- objectKind: "String"
463
- }
464
- }
465
- ],
466
- Number: [
467
- "node",
468
- {
469
- object: {
470
- objectKind: "Number"
471
- }
472
- }
473
- ],
474
- Boolean: [
475
- "node",
476
- {
477
- object: {
478
- objectKind: "Boolean"
479
- }
480
- }
481
- ],
482
- WeakMap: [
483
- "node",
484
- {
485
- object: {
486
- objectKind: "WeakMap"
487
- }
488
- }
489
- ],
490
- WeakSet: [
491
- "node",
492
- {
493
- object: {
494
- objectKind: "WeakSet"
495
- }
496
- }
497
- ],
498
- Promise: [
499
- "node",
500
- {
501
- object: {
502
- objectKind: "Promise"
503
- }
504
- }
505
- ]
506
- }, {
507
- name: "jsObjects",
508
- standard: false
509
- });
510
- const jsObjectsSpace = jsObjects.compile();
511
- const validation = scope({
512
- email: /^(.+)@(.+)\.(.+)$/,
513
- alphanumeric: /^[dA-Za-z]+$/,
514
- alpha: /^[A-Za-z]+$/,
515
- lowercase: /^[a-z]*$/,
516
- uppercase: /^[A-Z]*$/,
517
- integer: [
518
- "node",
519
- {
520
- number: {
521
- divisor: 1
522
- }
523
- }
524
- ]
525
- }, {
526
- name: "validation",
527
- standard: false
528
- });
529
- const validationSpace = validation.compile();
530
- const standard = scope({}, {
531
- name: "standard",
532
- includes: [
533
- tsKeywordsSpace,
534
- jsObjectsSpace,
535
- validationSpace
536
- ],
537
- standard: false
538
- });
539
- const standardTypes = standard.compile();
540
- export const scopes = {
541
- tsKeywords,
542
- jsObjects,
543
- validation,
544
- standard
545
- };
546
- export const spaces = {
547
- tsKeywords: tsKeywordsSpace,
548
- jsObjects: jsObjectsSpace,
549
- validation: validationSpace,
550
- standard: standardTypes
551
- };
552
- export const type = scopes.standard.type;
553
- export const writeDuplicateAliasMessage = (name)=>`Alias '${name}' is already defined`;
554
- export const isType = (value)=>value?.infer === chainableNoOpProxy;
228
+ export const writeDuplicateAliasesMessage = (name)=>`Alias '${name}' is already defined`;
@@ -0,0 +1,27 @@
1
+ import { jsObjects, jsObjectsScope } from "./jsObjects.js";
2
+ import { scope } from "./scope.js";
3
+ import { tsKeywords, tsKeywordsScope } from "./tsKeywords.js";
4
+ import { validation, validationScope } from "./validation.js";
5
+ export const standardScope = scope({}, {
6
+ name: "standard",
7
+ includes: [
8
+ tsKeywords,
9
+ jsObjects,
10
+ validation
11
+ ],
12
+ standard: false
13
+ });
14
+ export const standard = standardScope.compile();
15
+ export const scopes = {
16
+ tsKeywords: tsKeywordsScope,
17
+ jsObjects: jsObjectsScope,
18
+ validation: validationScope,
19
+ standard: standardScope
20
+ };
21
+ export const spaces = {
22
+ tsKeywords,
23
+ jsObjects,
24
+ validation,
25
+ standard
26
+ };
27
+ export const type = scopes.standard.type;
@@ -0,0 +1,99 @@
1
+ import { scope } from "./scope.js";
2
+ const always = {
3
+ bigint: true,
4
+ boolean: true,
5
+ null: true,
6
+ number: true,
7
+ object: true,
8
+ string: true,
9
+ symbol: true,
10
+ undefined: true
11
+ };
12
+ export const tsKeywordsScope = scope({
13
+ any: [
14
+ "node",
15
+ always
16
+ ],
17
+ bigint: [
18
+ "node",
19
+ {
20
+ bigint: true
21
+ }
22
+ ],
23
+ boolean: [
24
+ "node",
25
+ {
26
+ boolean: true
27
+ }
28
+ ],
29
+ false: [
30
+ "node",
31
+ {
32
+ boolean: {
33
+ value: false
34
+ }
35
+ }
36
+ ],
37
+ never: [
38
+ "node",
39
+ {}
40
+ ],
41
+ null: [
42
+ "node",
43
+ {
44
+ null: true
45
+ }
46
+ ],
47
+ number: [
48
+ "node",
49
+ {
50
+ number: true
51
+ }
52
+ ],
53
+ object: [
54
+ "node",
55
+ {
56
+ object: true
57
+ }
58
+ ],
59
+ string: [
60
+ "node",
61
+ {
62
+ string: true
63
+ }
64
+ ],
65
+ symbol: [
66
+ "node",
67
+ {
68
+ symbol: true
69
+ }
70
+ ],
71
+ true: [
72
+ "node",
73
+ {
74
+ boolean: {
75
+ value: true
76
+ }
77
+ }
78
+ ],
79
+ unknown: [
80
+ "node",
81
+ always
82
+ ],
83
+ void: [
84
+ "node",
85
+ {
86
+ undefined: true
87
+ }
88
+ ],
89
+ undefined: [
90
+ "node",
91
+ {
92
+ undefined: true
93
+ }
94
+ ]
95
+ }, {
96
+ name: "ts",
97
+ standard: false
98
+ });
99
+ export const tsKeywords = tsKeywordsScope.compile();