@alexify/metaschema 3.0.0 → 3.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 (47) hide show
  1. package/CHANGELOG.md +312 -1
  2. package/README.md +463 -17
  3. package/SECURITY.md +26 -7
  4. package/index.d.ts +338 -3
  5. package/package.json +42 -5
  6. package/src/codec.js +161 -73
  7. package/src/compile/code.js +1 -1
  8. package/src/compile/emit.js +60 -7
  9. package/src/compile/index.d.ts +6 -0
  10. package/src/compile/index.js +14 -3
  11. package/src/context.js +8 -9
  12. package/src/errors.js +18 -1
  13. package/src/formats/index.d.ts +19 -0
  14. package/src/formats/index.js +146 -0
  15. package/src/generators/fromjsonschema.js +517 -0
  16. package/src/generators/index.d.ts +19 -2
  17. package/src/generators/index.js +16 -10
  18. package/src/generators/jsonschema.js +2 -0
  19. package/src/issues.js +37 -20
  20. package/src/lint.js +12 -3
  21. package/src/locales/en.js +11 -2
  22. package/src/locales/uk.js +10 -2
  23. package/src/metadata.js +3 -2
  24. package/src/model/data.js +122 -0
  25. package/src/model/diff.js +439 -0
  26. package/src/model/index.d.ts +68 -0
  27. package/src/model/index.js +45 -0
  28. package/src/model/infer.js +483 -0
  29. package/src/model/merge.js +255 -0
  30. package/src/model/pattern.js +322 -0
  31. package/src/model/restore.js +226 -0
  32. package/src/model/sample.js +398 -0
  33. package/src/model.js +13 -1
  34. package/src/preprocessor.js +5 -1
  35. package/src/prototypes/collections.js +4 -3
  36. package/src/prototypes/scalars.js +4 -4
  37. package/src/prototypes/schema.js +6 -5
  38. package/src/prototypes/union.js +2 -2
  39. package/src/result.js +6 -0
  40. package/src/rules.js +53 -4
  41. package/src/schema.js +28 -7
  42. package/src/snapshot.js +111 -0
  43. package/src/stream/index.d.ts +68 -0
  44. package/src/stream/index.js +247 -0
  45. package/src/struct.js +64 -18
  46. package/src/types.js +14 -1
  47. package/src/util.js +18 -0
package/src/errors.js CHANGED
@@ -34,4 +34,21 @@ const asyncReturn = (what, where, method) => {
34
34
  return error;
35
35
  };
36
36
 
37
- module.exports = { SchemaDefinitionError, asyncReturn };
37
+ // A function of a subpath was given something other than what it takes (a
38
+ // Schema, a Model, a snapshot, a list of changes): the call is wrong, not a
39
+ // definition, so a plain TypeError with a code of its own, like asyncReturn.
40
+ const invalidTarget = (message) => {
41
+ const error = new TypeError(message);
42
+ error.code = 'ERR_INVALID_TARGET';
43
+ return error;
44
+ };
45
+
46
+ const shown = (value) =>
47
+ typeof value === 'bigint' ? `${value}n` : (JSON.stringify(value) ?? String(value));
48
+
49
+ // An option of a call that is not what it must be (`maxErrors`, the options
50
+ // of the stream subpath): a TypeError naming it, as invalidTarget is.
51
+ const invalidOption = (name, expected, value) =>
52
+ new TypeError(`${name} must be ${expected}, got ${shown(value)}`);
53
+
54
+ module.exports = { SchemaDefinitionError, asyncReturn, invalidTarget, invalidOption };
@@ -0,0 +1,19 @@
1
+ // A format a string field may name: a test over the string, the examples
2
+ // `sample` draws, and whether `infer` may read the format out of data (true
3
+ // unless said otherwise; a format every word passes says false).
4
+ export interface FormatDefinition {
5
+ test: (value: string) => boolean;
6
+ examples: readonly string[];
7
+ infer?: boolean;
8
+ }
9
+
10
+ // The formats registered so far, by name: empty until `register()` ran. Read
11
+ // it; `register()` writes it.
12
+ export const FORMATS: { readonly [name: string]: Readonly<FormatDefinition> | undefined };
13
+
14
+ // Registers the built-in formats (`email`, `uuid`, `uri`, `hostname`, `ipv4`,
15
+ // `ipv6`, `date`, `time`, `date-time`) and the program's own, once per name
16
+ // and process-wide, like the type registry: a name registered again with the
17
+ // same test is nothing, with another test `ERR_FORMAT_REGISTERED`, and an
18
+ // entry that is not a format `ERR_INVALID_FORMAT`.
19
+ export function register(custom?: Record<string, FormatDefinition>): void;
@@ -0,0 +1,146 @@
1
+ // The `./formats` subpath: the formats a string field may name
2
+ // (`{ type: 'string', format: 'email' }`). The core knows the rule and keeps
3
+ // an empty table (`FORMATS` in rules.js); `register()` fills it with the
4
+ // nine formats below and with the program's own, so that a program that
5
+ // validates no email carries no email expression. Each format is a `test`
6
+ // function over a string, a few `examples` for `sample`, and whether `infer`
7
+ // may read it out of data (a format every word passes, like a hostname,
8
+ // says not to).
9
+ //
10
+ // The expressions of email, uuid, uri, hostname, ipv4 and ipv6 are those of
11
+ // the fast mode of ajv-formats 3.0.1 (MIT, Copyright (c) 2020 Evgeny
12
+ // Poberezkin), copied with the attribution the project gives for a helper
13
+ // it carries instead of a dependency; they are anchored and linear, with
14
+ // every repetition bounded. The date and time forms are RFC 3339 with the
15
+ // ranges checked (a 31st of February is not a date).
16
+ const { FORMATS } = require('../rules.js');
17
+ const { SchemaDefinitionError } = require('../errors.js');
18
+
19
+ const invalidFormat = (name, reason) =>
20
+ new SchemaDefinitionError('ERR_INVALID_FORMAT', `Format "${name}" ${reason}`);
21
+
22
+ const EMAIL =
23
+ /^[a-z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$/i;
24
+
25
+ const UUID = /^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i;
26
+
27
+ const URI = /^(?:[a-z][a-z0-9+\-.]*:)(?:\/?\/)?[^\s]*$/i;
28
+
29
+ const HOSTNAME =
30
+ /^(?=.{1,253}\.?$)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[-0-9a-z]{0,61}[0-9a-z])?)*\.?$/i;
31
+
32
+ const IPV4 = /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/;
33
+
34
+ const IPV6 =
35
+ /^((([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}|:))|(([0-9a-f]{1,4}:){6}(:[0-9a-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){5}(((:[0-9a-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){4}(((:[0-9a-f]{1,4}){1,3})|((:[0-9a-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){3}(((:[0-9a-f]{1,4}){1,4})|((:[0-9a-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){2}(((:[0-9a-f]{1,4}){1,5})|((:[0-9a-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){1}(((:[0-9a-f]{1,4}){1,6})|((:[0-9a-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9a-f]{1,4}){1,7})|((:[0-9a-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))$/i;
36
+
37
+ // A full-date of RFC 3339 with the day within its month, and a full-time
38
+ // (an offset is required, as the RFC says; a leap second is allowed).
39
+ const DATE = /^(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;
40
+
41
+ const DAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
42
+
43
+ const isDate = (text) => {
44
+ const match = DATE.exec(text);
45
+ if (match === null) return false;
46
+ const year = Number(match[1]);
47
+ const month = Number(match[2]);
48
+ const leap = month === 2 && year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
49
+ return Number(match[3]) <= DAYS[month] + (leap ? 1 : 0);
50
+ };
51
+
52
+ const TIME =
53
+ /^([01]\d|2[0-3]):([0-5]\d):([0-5]\d|60)(?:\.\d+)?(?:[zZ]|([+-])([01]\d|2[0-3])(?::?([0-5]\d))?)$/;
54
+
55
+ // A leap second is the one of 23:59:60 UTC, whatever the offset says.
56
+ const isTime = (text) => {
57
+ const match = TIME.exec(text);
58
+ if (match === null) return false;
59
+ if (match[3] !== '60') return true;
60
+ const sign = match[4] === '-' ? -1 : 1;
61
+ const offset =
62
+ match[4] === undefined ? 0 : sign * (Number(match[5]) * 60 + Number(match[6] ?? 0));
63
+ const minutes = (Number(match[1]) * 60 + Number(match[2]) - offset + 1440) % 1440;
64
+ return minutes === 23 * 60 + 59;
65
+ };
66
+
67
+ // A date-time is the two joined by `T` (or, as the RFC allows for
68
+ // readability, a space).
69
+ const isDateTime = (text) =>
70
+ text.length > 11 &&
71
+ (text[10] === 'T' || text[10] === 't' || text[10] === ' ') &&
72
+ isDate(text.slice(0, 10)) &&
73
+ isTime(text.slice(11));
74
+
75
+ const tested = (expression) => (text) => expression.test(text);
76
+
77
+ // In the order `infer` tries them: a form that is also a hostname (a uuid,
78
+ // a date, an address) comes before it, and a hostname is never inferred,
79
+ // since every word is one.
80
+ const BUILT_IN = {
81
+ uuid: {
82
+ test: tested(UUID),
83
+ examples: ['550e8400-e29b-41d4-a716-446655440000', '123e4567-e89b-12d3-a456-426614174000'],
84
+ },
85
+ email: { test: tested(EMAIL), examples: ['ann@example.com', 'ann.lee@mail.example.org'] },
86
+ 'date-time': {
87
+ test: isDateTime,
88
+ examples: ['2026-10-11T12:00:00Z', '2026-10-11T12:00:00.000+02:00'],
89
+ },
90
+ date: { test: isDate, examples: ['2026-10-11', '2000-02-29'] },
91
+ time: { test: isTime, examples: ['12:00:00Z', '23:59:59.999+02:00'] },
92
+ ipv4: { test: tested(IPV4), examples: ['192.168.0.1', '10.0.0.255'] },
93
+ ipv6: { test: tested(IPV6), examples: ['::1', '2001:db8::ff00:42:8329'] },
94
+ uri: { test: tested(URI), examples: ['https://example.com/path?q=1', 'mailto:ann@example.com'] },
95
+ hostname: { test: tested(HOSTNAME), examples: ['example.com', 'api.example.org'], infer: false },
96
+ };
97
+
98
+ // An entry as the table keeps it: the test, a copy of the examples, and
99
+ // whether infer may read it, frozen.
100
+ const entryOf = (name, entry) => {
101
+ if (entry === null || typeof entry !== 'object') {
102
+ throw invalidFormat(name, 'needs { test, examples }');
103
+ }
104
+ const { test, examples, infer } = entry;
105
+ if (typeof test !== 'function') throw invalidFormat(name, 'needs a test function');
106
+ const listed = Array.isArray(examples) && examples.length > 0;
107
+ if (!listed) throw invalidFormat(name, 'needs a list of example strings');
108
+ for (let index = 0; index < examples.length; index += 1) {
109
+ if (typeof examples[index] !== 'string') {
110
+ throw invalidFormat(name, 'needs a list of example strings');
111
+ }
112
+ }
113
+ if (infer !== undefined && typeof infer !== 'boolean') {
114
+ throw invalidFormat(name, 'needs a boolean infer');
115
+ }
116
+ return Object.freeze({ test, examples: Object.freeze(examples.slice()), infer: infer !== false });
117
+ };
118
+
119
+ // A name is registered once: the same test again is nothing, another test
120
+ // under a registered name is an error, since the schemas built so far hold
121
+ // the first one.
122
+ const add = (name, entry) => {
123
+ if (name === '') throw invalidFormat(name, 'needs a name');
124
+ const known = FORMATS[name];
125
+ if (known !== undefined) {
126
+ if (known.test === entry.test) return;
127
+ throw new SchemaDefinitionError(
128
+ 'ERR_FORMAT_REGISTERED',
129
+ `Format "${name}" is already registered with another test`,
130
+ );
131
+ }
132
+ FORMATS[name] = entryOf(name, entry);
133
+ };
134
+
135
+ // Registers the built-in formats and the program's own
136
+ // (`{ phone: { test, examples } }`); once per name, process-wide, like the
137
+ // type registry. Importing the subpath registers nothing.
138
+ const register = (custom = {}) => {
139
+ if (custom === null || typeof custom !== 'object') {
140
+ throw new SchemaDefinitionError('ERR_INVALID_FORMAT', 'Formats must be an object of entries');
141
+ }
142
+ for (const name of Object.keys(BUILT_IN)) add(name, BUILT_IN[name]);
143
+ for (const name of Object.keys(custom)) add(name, custom[name]);
144
+ };
145
+
146
+ module.exports = { FORMATS, register };