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