arktype 0.5.1 → 1.0.0-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 (93) hide show
  1. package/README.md +1 -1
  2. package/dist/api.cjs +4 -2
  3. package/dist/api.d.ts +1 -1
  4. package/dist/api.mjs +1 -1
  5. package/dist/cjs/nodes/branch.js +3 -3
  6. package/dist/cjs/nodes/compose.js +4 -4
  7. package/dist/cjs/nodes/discriminate.js +2 -2
  8. package/dist/cjs/nodes/node.js +33 -30
  9. package/dist/cjs/nodes/predicate.js +1 -7
  10. package/dist/cjs/nodes/rules/props.js +39 -0
  11. package/dist/cjs/nodes/rules/rules.js +5 -2
  12. package/dist/cjs/parse/ast/config.js +13 -0
  13. package/dist/cjs/parse/ast/distributableFunction.js +2 -2
  14. package/dist/cjs/parse/ast/keyof.js +1 -1
  15. package/dist/cjs/parse/ast/morph.js +4 -3
  16. package/dist/cjs/parse/ast/narrow.js +8 -1
  17. package/dist/cjs/{nodes/infer.js → parse/ast/node.js} +0 -0
  18. package/dist/cjs/parse/ast/tuple.js +2 -1
  19. package/dist/cjs/parse/definition.js +2 -2
  20. package/dist/cjs/parse/string/reduce/dynamic.js +2 -2
  21. package/dist/cjs/parse/string/shift/operand/unenclosed.js +1 -1
  22. package/dist/cjs/parse/string/string.js +3 -3
  23. package/dist/cjs/scopes/{standard.js → ark.js} +9 -8
  24. package/dist/cjs/scopes/expressions.js +9 -9
  25. package/dist/cjs/scopes/scope.js +73 -75
  26. package/dist/cjs/scopes/type.js +24 -38
  27. package/dist/cjs/scopes/validation/creditCard.js +47 -0
  28. package/dist/cjs/scopes/validation/date.js +63 -0
  29. package/dist/cjs/scopes/validation/validation.js +95 -0
  30. package/dist/cjs/traverse/problems.js +48 -19
  31. package/dist/cjs/traverse/traverse.js +80 -25
  32. package/dist/cjs/utils/generics.js +16 -0
  33. package/dist/cjs/utils/serialize.js +29 -53
  34. package/dist/mjs/nodes/branch.js +2 -2
  35. package/dist/mjs/nodes/compose.js +5 -5
  36. package/dist/mjs/nodes/discriminate.js +2 -2
  37. package/dist/mjs/nodes/node.js +31 -31
  38. package/dist/mjs/nodes/predicate.js +1 -6
  39. package/dist/mjs/nodes/rules/props.js +39 -0
  40. package/dist/mjs/nodes/rules/rules.js +5 -2
  41. package/dist/mjs/parse/ast/config.js +5 -0
  42. package/dist/mjs/parse/ast/distributableFunction.js +2 -2
  43. package/dist/mjs/parse/ast/keyof.js +1 -1
  44. package/dist/mjs/parse/ast/morph.js +5 -4
  45. package/dist/mjs/parse/ast/narrow.js +9 -2
  46. package/dist/mjs/{nodes/infer.js → parse/ast/node.js} +0 -0
  47. package/dist/mjs/parse/ast/tuple.js +2 -1
  48. package/dist/mjs/parse/definition.js +2 -2
  49. package/dist/mjs/parse/string/reduce/dynamic.js +2 -2
  50. package/dist/mjs/parse/string/shift/operand/unenclosed.js +1 -1
  51. package/dist/mjs/parse/string/string.js +3 -3
  52. package/dist/mjs/scopes/{standard.js → ark.js} +8 -7
  53. package/dist/mjs/scopes/expressions.js +9 -9
  54. package/dist/mjs/scopes/scope.js +70 -76
  55. package/dist/mjs/scopes/type.js +22 -38
  56. package/dist/mjs/scopes/validation/creditCard.js +34 -0
  57. package/dist/mjs/scopes/validation/date.js +49 -0
  58. package/dist/mjs/scopes/validation/validation.js +81 -0
  59. package/dist/mjs/traverse/problems.js +45 -18
  60. package/dist/mjs/traverse/traverse.js +82 -27
  61. package/dist/mjs/utils/generics.js +11 -0
  62. package/dist/mjs/utils/serialize.js +29 -52
  63. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  64. package/dist/types/nodes/branch.d.ts +8 -6
  65. package/dist/types/nodes/node.d.ts +24 -12
  66. package/dist/types/nodes/predicate.d.ts +4 -8
  67. package/dist/types/nodes/rules/props.d.ts +23 -8
  68. package/dist/types/nodes/rules/rules.d.ts +2 -2
  69. package/dist/types/parse/ast/ast.d.ts +1 -1
  70. package/dist/types/parse/ast/config.d.ts +10 -0
  71. package/dist/types/parse/ast/distributableFunction.d.ts +1 -1
  72. package/dist/types/parse/ast/morph.d.ts +2 -2
  73. package/dist/types/parse/ast/narrow.d.ts +1 -1
  74. package/dist/types/{nodes/infer.d.ts → parse/ast/node.d.ts} +14 -14
  75. package/dist/types/parse/ast/tuple.d.ts +8 -7
  76. package/dist/types/parse/definition.d.ts +2 -2
  77. package/dist/types/parse/string/reduce/dynamic.d.ts +5 -5
  78. package/dist/types/parse/string/string.d.ts +3 -3
  79. package/dist/types/scopes/{standard.d.ts → ark.d.ts} +15 -86
  80. package/dist/types/scopes/expressions.d.ts +3 -3
  81. package/dist/types/scopes/scope.d.ts +24 -11
  82. package/dist/types/scopes/type.d.ts +37 -20
  83. package/dist/types/scopes/validation/creditCard.d.ts +2 -0
  84. package/dist/types/scopes/validation/date.d.ts +23 -0
  85. package/dist/types/scopes/validation/validation.d.ts +28 -0
  86. package/dist/types/traverse/problems.d.ts +14 -8
  87. package/dist/types/traverse/traverse.d.ts +18 -3
  88. package/dist/types/utils/generics.d.ts +10 -5
  89. package/dist/types/utils/serialize.d.ts +0 -1
  90. package/package.json +2 -2
  91. package/dist/cjs/scopes/validation.js +0 -79
  92. package/dist/mjs/scopes/validation.js +0 -66
  93. package/dist/types/scopes/validation.d.ts +0 -25
@@ -9,16 +9,22 @@ function _export(target, all) {
9
9
  });
10
10
  }
11
11
  _export(exports, {
12
+ compileScopeOptions: ()=>compileScopeOptions,
13
+ isConfigTuple: ()=>isConfigTuple,
12
14
  Scope: ()=>Scope,
13
15
  scope: ()=>scope,
16
+ rootScope: ()=>rootScope,
17
+ rootType: ()=>rootType,
14
18
  writeShallowCycleErrorMessage: ()=>writeShallowCycleErrorMessage,
15
19
  writeDuplicateAliasesMessage: ()=>writeDuplicateAliasesMessage
16
20
  });
17
21
  const _nodeTs = require("../nodes/node.js");
18
22
  const _definitionTs = require("../parse/definition.js");
23
+ const _problemsTs = require("../traverse/problems.js");
19
24
  const _chainableNoOpProxyTs = require("../utils/chainableNoOpProxy.js");
20
25
  const _errorsTs = require("../utils/errors.js");
21
26
  const _freezeTs = require("../utils/freeze.js");
27
+ const _genericsTs = require("../utils/generics.js");
22
28
  const _pathsTs = require("../utils/paths.js");
23
29
  const _cacheTs = require("./cache.js");
24
30
  const _typeTs = require("./type.js");
@@ -43,29 +49,6 @@ function _classApplyDescriptorSet(receiver, descriptor, value) {
43
49
  descriptor.value = value;
44
50
  }
45
51
  }
46
- function _classApplyDescriptorUpdate(receiver, descriptor) {
47
- if (descriptor.set) {
48
- if (!descriptor.get) {
49
- throw new TypeError("attempted to read set only private field");
50
- }
51
- if (!("__destrWrapper" in descriptor)) {
52
- descriptor.__destrWrapper = {
53
- set value (v){
54
- descriptor.set.call(receiver, v);
55
- },
56
- get value () {
57
- return descriptor.get.call(receiver);
58
- }
59
- };
60
- }
61
- return descriptor.__destrWrapper;
62
- } else {
63
- if (!descriptor.writable) {
64
- throw new TypeError("attempted to set read only private field");
65
- }
66
- return descriptor;
67
- }
68
- }
69
52
  function _classExtractFieldDescriptor(receiver, privateMap, action) {
70
53
  if (!privateMap.has(receiver)) {
71
54
  throw new TypeError("attempted to " + action + " private field on non-instance");
@@ -85,10 +68,6 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
85
68
  _classApplyDescriptorSet(receiver, descriptor, value);
86
69
  return value;
87
70
  }
88
- function _classPrivateFieldUpdate(receiver, privateMap) {
89
- var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "update");
90
- return _classApplyDescriptorUpdate(receiver, descriptor);
91
- }
92
71
  function _classPrivateMethodGet(receiver, privateSet, fn) {
93
72
  if (!privateSet.has(receiver)) {
94
73
  throw new TypeError("attempted to get private field on non-instance");
@@ -99,14 +78,28 @@ function _classPrivateMethodInit(obj, privateSet) {
99
78
  _checkPrivateRedeclaration(obj, privateSet);
100
79
  privateSet.add(obj);
101
80
  }
102
- var _ctx_type_meta;
81
+ var _ctx_type, _ctx_type1;
82
+ const compileScopeOptions = (opts)=>({
83
+ codes: (0, _problemsTs.compileProblemWriters)(opts.codes),
84
+ keys: opts.keys ?? "loose"
85
+ });
103
86
  let anonymousScopeCount = 0;
104
87
  const scopeRegistry = {};
105
88
  const spaceRegistry = {};
106
- 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();
89
+ const isConfigTuple = (def)=>Array.isArray(def) && def[1] === ":";
90
+ var _resolutions = /*#__PURE__*/ new WeakMap(), _exports = /*#__PURE__*/ new WeakMap(), _register = /*#__PURE__*/ new WeakSet(), _cacheSpaces = /*#__PURE__*/ new WeakSet(), _initializeContext = /*#__PURE__*/ new WeakSet(), _resolveRecurse = /*#__PURE__*/ new WeakSet();
107
91
  class Scope {
108
- createAnonymousTypeSuffix() {
109
- return ++_classPrivateFieldUpdate(this, _anonymousTypeCount).value;
92
+ getAnonymousQualifiedName(base) {
93
+ let increment = 0;
94
+ let id = base;
95
+ while(this.isResolvable(id)){
96
+ id = `${base}${increment++}`;
97
+ }
98
+ return `${this.name}.${id}`;
99
+ }
100
+ addAnonymousTypeReference(referencedType, ctx) {
101
+ (_ctx_type = ctx.type).includesMorph || (_ctx_type.includesMorph = referencedType.includesMorph);
102
+ return referencedType.node;
110
103
  }
111
104
  get infer() {
112
105
  return _chainableNoOpProxyTs.chainableNoOpProxy;
@@ -120,24 +113,32 @@ class Scope {
120
113
  }
121
114
  return _classPrivateFieldGet(this, _exports).root;
122
115
  }
123
- addReferenceIfResolvable(name, ctx) {
124
- const resolution = _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, name, "undefined", []);
116
+ addParsedReferenceIfResolvable(name, ctx) {
117
+ const resolution = _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, name, "undefined", [
118
+ name
119
+ ]);
125
120
  if (!resolution) {
126
121
  return false;
127
122
  }
128
- (_ctx_type_meta = ctx.type.meta).includesMorph || (_ctx_type_meta.includesMorph = resolution.meta.includesMorph);
123
+ (_ctx_type1 = ctx.type).includesMorph || (_ctx_type1.includesMorph = resolution.includesMorph);
129
124
  return true;
130
125
  }
131
126
  resolve(name) {
132
- return _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, name, "throw", []);
127
+ return _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, name, "throw", [
128
+ name
129
+ ]);
133
130
  }
134
131
  resolveNode(node) {
135
- if (typeof node === "object") {
136
- return node;
137
- }
138
- return this.resolveNode(this.resolve(node).node);
132
+ return typeof node === "string" ? this.resolveNode(this.resolve(node).node) : node;
133
+ }
134
+ resolveTypeNode(node) {
135
+ const resolution = this.resolveNode(node);
136
+ return (0, _nodeTs.isConfigNode)(resolution) ? resolution.node : resolution;
139
137
  }
140
- constructor(aliases, opts){
138
+ isResolvable(name) {
139
+ return _classPrivateFieldGet(this, _resolutions).has(name) || this.aliases[name];
140
+ }
141
+ constructor(aliases, opts = {}){
141
142
  _classPrivateMethodInit(this, _register);
142
143
  _classPrivateMethodInit(this, _cacheSpaces);
143
144
  _classPrivateMethodInit(this, _initializeContext);
@@ -150,16 +151,10 @@ class Scope {
150
151
  writable: true,
151
152
  value: void 0
152
153
  });
153
- _classPrivateFieldInit(this, _anonymousTypeCount, {
154
- writable: true,
155
- value: void 0
156
- });
157
154
  this.aliases = aliases;
158
- this.opts = opts;
159
155
  this.parseCache = new _cacheTs.FreezingCache();
160
156
  _classPrivateFieldSet(this, _resolutions, new _cacheTs.Cache());
161
157
  _classPrivateFieldSet(this, _exports, new _cacheTs.Cache());
162
- _classPrivateFieldSet(this, _anonymousTypeCount, 0);
163
158
  this.expressions = {
164
159
  intersection: (l, r, opts)=>this.type([
165
160
  l,
@@ -179,7 +174,7 @@ class Scope {
179
174
  "keyof",
180
175
  def
181
176
  ], opts),
182
- fromNode: (def, opts)=>this.type([
177
+ node: (def, opts)=>this.type([
183
178
  "node",
184
179
  def
185
180
  ], opts),
@@ -210,17 +205,18 @@ class Scope {
210
205
  this.instanceOf = this.expressions.instanceOf;
211
206
  this.narrow = this.expressions.narrow;
212
207
  this.morph = this.expressions.morph;
213
- this.type = Object.assign((def, opts = {})=>{
214
- if (opts.name && this.aliases[opts.name]) {
215
- return (0, _errorsTs.throwParseError)(writeDuplicateAliasesMessage(opts.name));
216
- }
217
- const result = (0, _typeTs.initializeType)(def, opts, this);
218
- const ctx = _classPrivateMethodGet(this, _initializeContext, initializeContext).call(this, result);
219
- result.node = this.resolveNode((0, _definitionTs.parseDefinition)(def, ctx));
220
- result.flat = (0, _nodeTs.flattenType)(result);
221
- return result;
208
+ this.type = Object.assign((def, config = {})=>{
209
+ const t = (0, _typeTs.initializeType)("λtype", def, config, this);
210
+ const ctx = _classPrivateMethodGet(this, _initializeContext, initializeContext).call(this, t);
211
+ const root = (0, _definitionTs.parseDefinition)(def, ctx);
212
+ t.node = (0, _freezeTs.deepFreeze)((0, _genericsTs.hasKeys)(config) ? {
213
+ config,
214
+ node: this.resolveTypeNode(root)
215
+ } : root);
216
+ t.flat = (0, _freezeTs.deepFreeze)((0, _nodeTs.flattenType)(t));
217
+ return t;
222
218
  }, {
223
- from: this.expressions.fromNode
219
+ from: this.expressions.node
224
220
  });
225
221
  this.name = _classPrivateMethodGet(this, _register, register).call(this, opts);
226
222
  if (opts.standard !== false) {
@@ -234,9 +230,7 @@ class Scope {
234
230
  if (opts.includes) {
235
231
  _classPrivateMethodGet(this, _cacheSpaces, cacheSpaces).call(this, opts.includes, "includes");
236
232
  }
237
- if (opts.config) {
238
- this.config = opts.config;
239
- }
233
+ this.config = compileScopeOptions(opts);
240
234
  }
241
235
  }
242
236
  function register(opts) {
@@ -268,28 +262,32 @@ function resolveRecurse(name, onUnresolvable, seen) {
268
262
  if (maybeCacheResult) {
269
263
  return maybeCacheResult;
270
264
  }
271
- if (!this.aliases[name]) {
265
+ const aliasDef = this.aliases[name];
266
+ if (!aliasDef) {
272
267
  return onUnresolvable === "throw" ? (0, _errorsTs.throwInternalError)(`Unexpectedly failed to resolve alias '${name}'`) : undefined;
273
268
  }
274
- const type = (0, _typeTs.initializeType)(this.aliases[name], {
275
- name
276
- }, this);
277
- _classPrivateFieldGet(this, _resolutions).set(name, type);
278
- _classPrivateFieldGet(this, _exports).set(name, type);
279
- const ctx = _classPrivateMethodGet(this, _initializeContext, initializeContext).call(this, type);
280
- let resolution = (0, _definitionTs.parseDefinition)(type.meta.definition, ctx);
281
- if (typeof resolution === "string") {
282
- if (seen.includes(resolution)) {
269
+ const t = (0, _typeTs.initializeType)(name, aliasDef, {}, this);
270
+ const ctx = _classPrivateMethodGet(this, _initializeContext, initializeContext).call(this, t);
271
+ _classPrivateFieldGet(this, _resolutions).set(name, t);
272
+ _classPrivateFieldGet(this, _exports).set(name, t);
273
+ let node = (0, _definitionTs.parseDefinition)(aliasDef, ctx);
274
+ if (typeof node === "string") {
275
+ if (seen.includes(node)) {
283
276
  return (0, _errorsTs.throwParseError)(writeShallowCycleErrorMessage(name, seen));
284
277
  }
285
- seen.push(resolution);
286
- resolution = _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, resolution, "throw", seen).node;
278
+ seen.push(node);
279
+ node = _classPrivateMethodGet(this, _resolveRecurse, resolveRecurse).call(this, node, "throw", seen).node;
287
280
  }
288
- type.node = (0, _freezeTs.deepFreeze)(resolution);
289
- type.flat = (0, _freezeTs.deepFreeze)((0, _nodeTs.flattenType)(type));
290
- return type;
281
+ t.node = (0, _freezeTs.deepFreeze)(node);
282
+ t.flat = (0, _freezeTs.deepFreeze)((0, _nodeTs.flattenType)(t));
283
+ return t;
291
284
  }
292
285
  const scope = (aliases, opts = {})=>new Scope(aliases, opts);
286
+ const rootScope = scope({}, {
287
+ name: "root",
288
+ standard: false
289
+ });
290
+ const rootType = rootScope.type;
293
291
  const writeShallowCycleErrorMessage = (name, seen)=>`Alias '${name}' has a shallow resolution cycle: ${[
294
292
  ...seen,
295
293
  name
@@ -10,31 +10,23 @@ function _export(target, all) {
10
10
  }
11
11
  _export(exports, {
12
12
  initializeType: ()=>initializeType,
13
- isType: ()=>isType
13
+ isType: ()=>isType,
14
+ isCheckResult: ()=>isCheckResult,
15
+ isAnonymousName: ()=>isAnonymousName
14
16
  });
15
17
  const _traverseTs = require("../traverse/traverse.js");
16
18
  const _chainableNoOpProxyTs = require("../utils/chainableNoOpProxy.js");
17
19
  const _genericsTs = require("../utils/generics.js");
18
- const compileTypeConfig = (opts)=>{
19
- if (opts === undefined) {
20
- return;
21
- }
22
- const { name , ...config } = opts;
23
- if ((0, _genericsTs.hasKeys)(config)) {
24
- return config;
25
- }
26
- };
27
- const initializeType = (definition, opts, scope)=>{
28
- const name = opts?.name ?? "type";
29
- const config = compileTypeConfig(opts);
30
- const meta = {
31
- name,
32
- id: `${scope.name}.${opts?.name ? name : `type${scope.createAnonymousTypeSuffix()}`}`,
33
- definition,
34
- scope,
35
- config,
36
- includesMorph: false
37
- };
20
+ const initializeType = (name, definition, config, scope)=>{
21
+ const namedTraverse = (0, _genericsTs.composeNamed)((data)=>{
22
+ const state = new _traverseTs.TraversalState(data, t);
23
+ (0, _traverseTs.traverse)(t.flat, state);
24
+ return state.problems.count ? {
25
+ problems: state.problems
26
+ } : {
27
+ data: state.data
28
+ };
29
+ }, name);
38
30
  const root = {
39
31
  // temporarily initialize node/flat to aliases that will be included in
40
32
  // the final type in case of cyclic resolutions
@@ -45,23 +37,17 @@ const initializeType = (definition, opts, scope)=>{
45
37
  name
46
38
  ]
47
39
  ],
48
- meta,
49
- infer: _chainableNoOpProxyTs.chainableNoOpProxy
40
+ allows: (data)=>!namedTraverse(data).problems,
41
+ infer: _chainableNoOpProxyTs.chainableNoOpProxy,
42
+ qualifiedName: isAnonymousName(name) ? scope.getAnonymousQualifiedName(name) : `${scope.name}.${name}`,
43
+ definition,
44
+ scope,
45
+ includesMorph: false,
46
+ config
50
47
  };
51
- // dynamically assign a name to the primary traversal function
52
- const namedTraverse = {
53
- [name]: (data)=>{
54
- const state = new _traverseTs.TraversalState(data, type);
55
- return (0, _traverseTs.traverse)(type.flat, state) ? {
56
- data: state.data
57
- } : {
58
- problems: state.problems
59
- };
60
- }
61
- }[name];
62
- // we need to assign this to a variable before returning so we can reference
63
- // it in namedTraverse
64
- const type = Object.assign(namedTraverse, root);
65
- return type;
48
+ const t = Object.assign(namedTraverse, root);
49
+ return t;
66
50
  };
67
51
  const isType = (value)=>value?.infer === _chainableNoOpProxyTs.chainableNoOpProxy;
52
+ const isCheckResult = (value)=>(0, _genericsTs.getNominalId)(value) !== undefined;
53
+ const isAnonymousName = (name)=>name[0] === "λ";
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ isLuhnValid: ()=>isLuhnValid,
13
+ creditCard: ()=>creditCard
14
+ });
15
+ const _scopeTs = require("../scope.js");
16
+ const isLuhnValid = (creditCardInput)=>{
17
+ const sanitized = creditCardInput.replace(/[- ]+/g, "");
18
+ let sum = 0;
19
+ let digit;
20
+ let tmpNum;
21
+ let shouldDouble;
22
+ for(let i = sanitized.length - 1; i >= 0; i--){
23
+ digit = sanitized.substring(i, i + 1);
24
+ tmpNum = parseInt(digit, 10);
25
+ if (shouldDouble) {
26
+ tmpNum *= 2;
27
+ if (tmpNum >= 10) {
28
+ sum += tmpNum % 10 + 1;
29
+ } else {
30
+ sum += tmpNum;
31
+ }
32
+ } else {
33
+ sum += tmpNum;
34
+ }
35
+ shouldDouble = !shouldDouble;
36
+ }
37
+ return !!(sum % 10 === 0 ? sanitized : false);
38
+ };
39
+ // https://github.com/validatorjs/validator.js/blob/master/src/lib/isCreditCard.js
40
+ const creditCardMatcher = /^(?:4[0-9]{12}(?:[0-9]{3,6})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12,15}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11}|6[27][0-9]{14}|^(81[0-9]{14,17}))$/;
41
+ const creditCard = (0, _scopeTs.rootType)([
42
+ creditCardMatcher,
43
+ "=>",
44
+ (s, problems)=>isLuhnValid(s) || !problems.mustBe("a valid credit card number")
45
+ ], {
46
+ mustBe: "a valid credit card number"
47
+ });
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ tryParseDate: ()=>tryParseDate,
13
+ parsedDate: ()=>parsedDate
14
+ });
15
+ const _scopeTs = require("../scope.js");
16
+ const dayDelimiterMatcher = /^[./-]$/;
17
+ // ISO 8601 date/time modernized from https://github.com/validatorjs/validator.js/blob/master/src/lib/isISO8601.js
18
+ // Based on https://tc39.es/ecma262/#sec-date-time-string-format, the T
19
+ // delimiter for date/time is mandatory. Regex from validator.js strict matcher:
20
+ const iso8601Matcher = /^([+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([.,]\d+(?!:))?)?(\17[0-5]\d([.,]\d+)?)?([zZ]|([+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
21
+ const isValidDateInstance = (date)=>!isNaN(date);
22
+ const writeFormattedMustBe = (format)=>`a ${format}-formatted date`;
23
+ const tryParseDate = (data, opts)=>{
24
+ if (!opts?.format) {
25
+ const result = new Date(data);
26
+ return isValidDateInstance(result) ? result : "a valid date";
27
+ }
28
+ if (opts.format === "iso8601") {
29
+ return iso8601Matcher.test(data) ? new Date(data) : writeFormattedMustBe("iso8601");
30
+ }
31
+ const dataParts = data.split(dayDelimiterMatcher);
32
+ // will be the first delimiter matched, if there is one
33
+ const delimiter = data[dataParts[0].length];
34
+ const formatParts = delimiter ? opts.format.split(delimiter) : [
35
+ opts.format
36
+ ];
37
+ if (dataParts.length !== formatParts.length) {
38
+ return writeFormattedMustBe(opts.format);
39
+ }
40
+ const parsedParts = {};
41
+ for(let i = 0; i < formatParts.length; i++){
42
+ if (dataParts[i].length !== formatParts[i].length && // if format is "m" or "d", data is allowed to be 1 or 2 characters
43
+ !(formatParts[i].length === 1 && dataParts[i].length === 2)) {
44
+ return writeFormattedMustBe(opts.format);
45
+ }
46
+ parsedParts[formatParts[i][0]] = dataParts[i];
47
+ }
48
+ const date = new Date(`${parsedParts.m}/${parsedParts.d}/${parsedParts.y}`);
49
+ if (`${date.getDate()}` === parsedParts.d) {
50
+ return date;
51
+ }
52
+ return writeFormattedMustBe(opts.format);
53
+ };
54
+ const parsedDate = (0, _scopeTs.rootType)([
55
+ _scopeTs.rootType.from({
56
+ string: true
57
+ }),
58
+ "|>",
59
+ (s, problems)=>{
60
+ const result = tryParseDate(s);
61
+ return typeof result === "string" ? problems.mustBe(result) : result;
62
+ }
63
+ ]);
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ validationScope: ()=>validationScope,
13
+ validation: ()=>validation
14
+ });
15
+ const _numericLiteralsTs = require("../../utils/numericLiterals.js");
16
+ const _scopeTs = require("../scope.js");
17
+ const _creditCardTs = require("./creditCard.js");
18
+ const _dateTs = require("./date.js");
19
+ // Non-trivial expressions should have an explanation or attribution
20
+ const parsedNumber = (0, _scopeTs.rootType)([
21
+ _numericLiteralsTs.wellFormedNumberMatcher,
22
+ "|>",
23
+ (s)=>parseFloat(s)
24
+ ], {
25
+ mustBe: "a well-formed numeric string"
26
+ });
27
+ const parsedInteger = (0, _scopeTs.rootType)([
28
+ _numericLiteralsTs.wellFormedIntegerMatcher,
29
+ "|>",
30
+ (s)=>parseInt(s)
31
+ ], {
32
+ mustBe: "a well-formed integer string"
33
+ });
34
+ // https://www.regular-expressions.info/email.html
35
+ const email = (0, _scopeTs.rootType)(/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/, {
36
+ mustBe: "a valid email"
37
+ });
38
+ // https://github.com/validatorjs/validator.js/blob/master/src/lib/isUUID.js
39
+ const uuid = (0, _scopeTs.rootType)(/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/, {
40
+ mustBe: "a valid UUID"
41
+ });
42
+ // https://semver.org/
43
+ const semver = (0, _scopeTs.rootType)(/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/, {
44
+ mustBe: "a valid semantic version (see https://semver.org/)"
45
+ });
46
+ const validationScope = (0, _scopeTs.scope)({
47
+ // Character sets
48
+ alpha: [
49
+ /^[A-Za-z]*$/,
50
+ ":",
51
+ {
52
+ mustBe: "only letters"
53
+ }
54
+ ],
55
+ alphanumeric: [
56
+ /^[A-Za-z\d]*$/,
57
+ ":",
58
+ {
59
+ mustBe: "only letters and digits"
60
+ }
61
+ ],
62
+ lowercase: [
63
+ /^[a-z]*$/,
64
+ ":",
65
+ {
66
+ mustBe: "only lowercase letters"
67
+ }
68
+ ],
69
+ uppercase: [
70
+ /^[A-Z]*$/,
71
+ ":",
72
+ {
73
+ mustBe: "only uppercase letters"
74
+ }
75
+ ],
76
+ creditCard: _creditCardTs.creditCard,
77
+ email,
78
+ uuid,
79
+ parsedNumber,
80
+ parsedInteger,
81
+ parsedDate: _dateTs.parsedDate,
82
+ semver,
83
+ integer: [
84
+ "node",
85
+ {
86
+ number: {
87
+ divisor: 1
88
+ }
89
+ }
90
+ ]
91
+ }, {
92
+ name: "validation",
93
+ standard: false
94
+ });
95
+ const validation = validationScope.compile();