@alexify/metaschema 3.4.0 → 4.0.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/CHANGELOG.md +227 -1
  2. package/README.md +29 -11
  3. package/SECURITY.md +5 -4
  4. package/index.d.ts +912 -480
  5. package/package.json +8 -6
  6. package/src/checks.js +72 -107
  7. package/src/codec.js +67 -58
  8. package/src/coerce/index.d.ts +31 -21
  9. package/src/coerce/index.js +64 -47
  10. package/src/compile/code.js +2 -2
  11. package/src/compile/emit.js +20 -31
  12. package/src/compile/index.d.ts +41 -25
  13. package/src/compile/index.js +45 -32
  14. package/src/context.js +10 -10
  15. package/src/definition/index.d.ts +10 -7
  16. package/src/definition/index.js +2 -1
  17. package/src/derive/index.d.ts +25 -17
  18. package/src/derive/index.js +21 -15
  19. package/src/errors.js +84 -59
  20. package/src/formats/index.d.ts +27 -21
  21. package/src/formats/index.js +15 -13
  22. package/src/generators/{fromjsonschema.js → from-json-schema.js} +170 -127
  23. package/src/generators/index.d.ts +23 -15
  24. package/src/generators/index.js +2 -2
  25. package/src/generators/{jsonschema.js → json-schema.js} +37 -43
  26. package/src/generators/typescript.js +9 -6
  27. package/src/index.js +2 -1
  28. package/src/issues.js +41 -42
  29. package/src/kinds.js +31 -10
  30. package/src/lint.js +64 -19
  31. package/src/locales/en.js +7 -7
  32. package/src/locales/uk.js +6 -6
  33. package/src/metadata.js +2 -2
  34. package/src/model/data.js +34 -20
  35. package/src/model/diff.js +29 -24
  36. package/src/model/index.d.ts +51 -35
  37. package/src/model/infer.js +70 -61
  38. package/src/model/merge.js +29 -18
  39. package/src/model/pattern.js +105 -102
  40. package/src/model/restore.js +33 -21
  41. package/src/model/sample.js +90 -100
  42. package/src/model.js +4 -2
  43. package/src/mongodb/bson.js +12 -12
  44. package/src/mongodb/document.js +7 -6
  45. package/src/mongodb/index.d.ts +54 -31
  46. package/src/mongodb/update.js +12 -9
  47. package/src/preprocessor.js +25 -15
  48. package/src/prototypes/abstract.js +8 -8
  49. package/src/prototypes/collections.js +7 -7
  50. package/src/prototypes/flags.js +1 -1
  51. package/src/prototypes/reference.js +3 -4
  52. package/src/prototypes/scalars.js +1 -1
  53. package/src/prototypes/schema.js +5 -6
  54. package/src/prototypes/tuple.js +11 -10
  55. package/src/prototypes/union.js +5 -4
  56. package/src/rules.js +23 -20
  57. package/src/runtime/browser.js +6 -1
  58. package/src/runtime/node.js +19 -2
  59. package/src/schema.js +19 -14
  60. package/src/snapshot.js +14 -12
  61. package/src/stream/index.d.ts +33 -21
  62. package/src/stream/index.js +37 -20
  63. package/src/struct.js +6 -6
  64. package/src/types.js +35 -26
  65. package/src/util.js +70 -64
  66. package/src/validation.js +10 -13
package/src/util.js CHANGED
@@ -7,26 +7,6 @@ const BRAND = Symbol.for('alexify.metaschema.brand');
7
7
  // browser build free of any Node import.
8
8
  const INSPECT = Symbol.for('nodejs.util.inspect.custom');
9
9
 
10
- const hasBrand = (value, name) => value?.[BRAND] === name;
11
-
12
- // A built-in is recognised by the name of its constructor, which nothing
13
- // renames, and by its shape, which a value parsed from JSON cannot fake: an
14
- // own key called `constructor` holding `{ name: 'Map' }` is data, and
15
- // iterating such a value, or calling `getTime` on it, would throw out of a
16
- // check. The name is tested first, since most values fail it at once.
17
- const iterableNamed = (name) => (value) =>
18
- value?.constructor?.name === name && typeof value[Symbol.iterator] === 'function';
19
-
20
- const isMap = iterableNamed('Map');
21
-
22
- const isSet = iterableNamed('Set');
23
-
24
- const isDate = (value) =>
25
- value?.constructor?.name === 'Date' && typeof value.getTime === 'function';
26
-
27
- const isRegExp = (value) =>
28
- value?.constructor?.name === 'RegExp' && typeof value.exec === 'function';
29
-
30
10
  // The bytes of a Uint8Array of any realm, a Buffer included, by the name the
31
11
  // engine keeps in the view itself: the tag getter of the typed arrays reads
32
12
  // that slot and gives undefined for anything else, which a key or a symbol
@@ -36,31 +16,10 @@ const TYPED_TAG = Object.getOwnPropertyDescriptor(
36
16
  Symbol.toStringTag,
37
17
  ).get;
38
18
 
39
- const isBytes = (value) => TYPED_TAG.call(value) === 'Uint8Array';
40
-
41
- // The type of a value as a message names it: an array and null by name.
42
- const typeOf = (value) => {
43
- if (value === null) return 'null';
44
- if (Array.isArray(value)) return 'array';
45
- return typeof value;
46
- };
47
-
48
19
  // A JavaScript identifier: what generated code may write as `object.key` and
49
20
  // a TypeScript declaration as a bare name.
50
21
  const IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
51
22
 
52
- // Defines an own data property, so a key such as `__proto__` taken from a
53
- // path or a definition lands on the object instead of changing its prototype.
54
- const define = (target, key, value) => {
55
- Object.defineProperty(target, key, {
56
- value,
57
- enumerable: true,
58
- writable: true,
59
- configurable: true,
60
- });
61
- return value;
62
- };
63
-
64
23
  // The method that validates a value inside an existing context, shared by
65
24
  // Schema and the reference type that checks its target through it. A global
66
25
  // symbol like BRAND, so a schema from another copy of the package has it too.
@@ -82,6 +41,10 @@ const TRACKED = Symbol('tracked');
82
41
  const COERCES = Symbol('coerces');
83
42
  const CODEC = Symbol('codec');
84
43
  const STANDARD = Symbol('standard');
44
+ // The kinds of the program's own a definition named, at any depth, as
45
+ // [key, metadata] pairs (null when there is none): the lint of the
46
+ // validation half reads them for `ambiguous-kind`.
47
+ const KINDS = Symbol('kinds');
85
48
  // The recursive dependencies a model met while ordering its entities, as
86
49
  // [entity, reference] pairs: the validation half writes the warnings.
87
50
  const CYCLES = Symbol('cycles');
@@ -92,20 +55,7 @@ const ENUM_SET_SIZE = 8;
92
55
 
93
56
  // Key names taken from the value under check go into messages; a huge key
94
57
  // must not become a huge message.
95
- const KEY_LIMIT = 100;
96
-
97
- const shorten = (key) => {
98
- const name = String(key);
99
- return name.length > KEY_LIMIT ? `${name.slice(0, KEY_LIMIT)}...` : name;
100
- };
101
-
102
- // The text of a value that may have none (a symbol, an object without a
103
- // prototype), for a message: what a validator threw or returned is anything.
104
- const safeString = (value) => {
105
- if (typeof value === 'symbol') return value.toString();
106
- const bare = value !== null && typeof value === 'object' && typeof value.toString !== 'function';
107
- return bare ? Object.prototype.toString.call(value) : String(value);
108
- };
58
+ const KEY_MAX_CHARS = 100;
109
59
 
110
60
  const formatters = {
111
61
  type: (type, req = true) => {
@@ -123,27 +73,83 @@ const formatters = {
123
73
  },
124
74
  };
125
75
 
76
+ const hasBrand = (value, name) => value?.[BRAND] === name;
77
+
78
+ // A built-in is recognised by the name of its constructor, which nothing
79
+ // renames, and by its shape, which a value parsed from JSON cannot fake: an
80
+ // own key called `constructor` holding `{ name: 'Map' }` is data, and
81
+ // iterating such a value, or calling `getTime` on it, would throw out of a
82
+ // check. The name is tested first, since most values fail it at once.
83
+ const iterableNamed = (name) => (value) =>
84
+ value?.constructor?.name === name && typeof value[Symbol.iterator] === 'function';
85
+
86
+ const isMap = iterableNamed('Map');
87
+
88
+ const isSet = iterableNamed('Set');
89
+
90
+ const isDate = (value) =>
91
+ value?.constructor?.name === 'Date' && typeof value.getTime === 'function';
92
+
93
+ const isRegExp = (value) =>
94
+ value?.constructor?.name === 'RegExp' && typeof value.exec === 'function';
95
+
96
+ const isBytes = (value) => TYPED_TAG.call(value) === 'Uint8Array';
97
+
98
+ // The type of a value as a message names it: an array and null by name.
99
+ const typeOf = (value) => {
100
+ if (value === null) return 'null';
101
+ if (Array.isArray(value)) return 'array';
102
+ return typeof value;
103
+ };
104
+
105
+ // Defines an own data property, so a key such as `__proto__` taken from a
106
+ // path or a definition lands on the object instead of changing its prototype.
107
+ const define = (target, key, value) => {
108
+ Object.defineProperty(target, key, {
109
+ value,
110
+ enumerable: true,
111
+ writable: true,
112
+ configurable: true,
113
+ });
114
+ return value;
115
+ };
116
+
117
+ // `String`, not a template literal: a key may be a symbol.
118
+ const shorten = (key) => {
119
+ const name = String(key);
120
+ return name.length > KEY_MAX_CHARS ? `${name.slice(0, KEY_MAX_CHARS)}...` : name;
121
+ };
122
+
123
+ // The text of a value that may have none (a symbol, an object without a
124
+ // prototype), for a message: what a validator threw or returned is anything.
125
+ const safeString = (value) => {
126
+ if (typeof value === 'symbol') return value.toString();
127
+ const bare = value !== null && typeof value === 'object' && typeof value.toString !== 'function';
128
+ return bare ? Object.prototype.toString.call(value) : `${value}`;
129
+ };
130
+
126
131
  module.exports = {
127
132
  BRAND,
128
133
  INSPECT,
129
- hasBrand,
130
- isMap,
131
- isSet,
132
- isDate,
133
- isRegExp,
134
- isBytes,
135
- typeOf,
136
134
  IDENTIFIER,
137
- define,
138
135
  RUN,
139
136
  HOOK,
140
137
  TRACKED,
141
138
  COERCES,
142
139
  CODEC,
143
140
  STANDARD,
141
+ KINDS,
144
142
  CYCLES,
145
143
  ENUM_SET_SIZE,
144
+ formatters,
145
+ hasBrand,
146
+ isMap,
147
+ isSet,
148
+ isDate,
149
+ isRegExp,
150
+ isBytes,
151
+ typeOf,
152
+ define,
146
153
  shorten,
147
154
  safeString,
148
- formatters,
149
155
  };
package/src/validation.js CHANGED
@@ -31,6 +31,10 @@ const {
31
31
  wantOf,
32
32
  } = require('./codec.js');
33
33
 
34
+ // The warnings of a schema and of a model, computed on first use: a schema
35
+ // built in a hot path never pays for the lint.
36
+ const LINTED = new WeakMap();
37
+
34
38
  // The validation of a schema inside an existing context: `check` starts one,
35
39
  // and a reference field checks its target entity within the context of the
36
40
  // outer check, so the error limit and the cycle detection span the whole
@@ -93,10 +97,6 @@ const pass = (schema, phase, source, context, extra) => {
93
97
  return apply(info, schema, source, context, undefined, phase, want);
94
98
  };
95
99
 
96
- // The warnings of a schema and of a model, computed on first use: a schema
97
- // built in a hot path never pays for the lint.
98
- const LINTED = new WeakMap();
99
-
100
100
  const linted = (target, lint) => {
101
101
  let warnings = LINTED.get(target);
102
102
  if (warnings === undefined) {
@@ -217,13 +217,10 @@ const members = {
217
217
  if (this[TRACKED]) context.source = value;
218
218
  walkAsync(this, value, context);
219
219
  if (context.async.length > 0) await settle(context);
220
- let output;
221
- if (context.count === 0) {
222
- // Only a promise of the pass is awaited: a thenable in the value is data.
223
- output = pass(this, FINISH, value, context, 0);
224
- if (output instanceof Promise) output = (await output).value;
225
- }
226
- return parsed(context, output);
220
+ if (context.count !== 0) return parsed(context, undefined);
221
+ // Only a promise of the pass is awaited: a thenable in the value is data.
222
+ const output = pass(this, FINISH, value, context, 0);
223
+ return parsed(context, output instanceof Promise ? (await output).value : output);
227
224
  },
228
225
 
229
226
  // The other direction: the schema's own `serialize`, then the serializers
@@ -250,7 +247,7 @@ const members = {
250
247
  if (isThenable(output)) throw asyncReturn('validator', path, 'checkAsync');
251
248
  return result.add(output);
252
249
  } catch (error) {
253
- if (error?.code === 'ERR_ASYNC_VALIDATOR') throw error;
250
+ if (error?.code === 'ALX_ERR_ASYNC_VALIDATOR') throw error;
254
251
  return result.add({
255
252
  code: 'exception',
256
253
  message: `validation failed ${safeString(error)}`,
@@ -275,7 +272,7 @@ const members = {
275
272
  if (this[STANDARD] === null) {
276
273
  const async = hasAsync(this);
277
274
  const validate = (value, options) => {
278
- const libraryOptions = options == null ? undefined : options.libraryOptions;
275
+ const libraryOptions = options?.libraryOptions;
279
276
  if (async) return this.parseAsync(value, libraryOptions).then(standardize);
280
277
  return standardize(this.parse(value, libraryOptions));
281
278
  };