@alexify/metaschema 3.1.0 → 3.3.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.
- package/CHANGELOG.md +314 -1
- package/README.md +535 -20
- package/SECURITY.md +26 -7
- package/index.d.ts +496 -2
- package/package.json +53 -5
- package/src/codec.js +239 -74
- package/src/coerce/index.d.ts +34 -0
- package/src/coerce/index.js +352 -0
- package/src/compile/code.js +1 -1
- package/src/compile/emit.js +32 -7
- package/src/compile/index.d.ts +6 -0
- package/src/compile/index.js +12 -1
- package/src/context.js +11 -10
- package/src/derive/index.d.ts +86 -0
- package/src/derive/index.js +266 -0
- package/src/errors.js +28 -1
- package/src/formats/index.d.ts +31 -0
- package/src/formats/index.js +557 -0
- package/src/generators/fromjsonschema.js +16 -14
- package/src/generators/index.js +2 -1
- package/src/generators/jsonschema.js +47 -13
- package/src/issues.js +37 -20
- package/src/kinds.js +11 -2
- package/src/lint.js +24 -0
- package/src/locales/en.js +11 -2
- package/src/locales/uk.js +10 -2
- package/src/metadata.js +7 -2
- package/src/model/data.js +17 -0
- package/src/model/diff.js +1 -0
- package/src/model/index.d.ts +14 -2
- package/src/model/index.js +13 -10
- package/src/model/infer.js +483 -0
- package/src/model/merge.js +10 -6
- package/src/model/restore.js +3 -3
- package/src/model/sample.js +8 -5
- package/src/model.js +34 -4
- package/src/mongodb/bson.js +118 -0
- package/src/mongodb/document.js +209 -0
- package/src/mongodb/index.d.ts +133 -0
- package/src/mongodb/index.js +24 -0
- package/src/mongodb/indexes.js +79 -0
- package/src/mongodb/update.js +258 -0
- package/src/preprocessor.js +1 -0
- package/src/prototypes/abstract.js +9 -3
- package/src/prototypes/collections.js +4 -3
- package/src/prototypes/scalars.js +4 -4
- package/src/prototypes/union.js +2 -2
- package/src/result.js +6 -0
- package/src/rules.js +53 -4
- package/src/schema.js +53 -28
- package/src/snapshot.js +1 -1
- package/src/stream/index.d.ts +68 -0
- package/src/stream/index.js +247 -0
- package/src/struct.js +1 -1
- package/src/types.js +33 -1
- package/src/util.js +18 -0
|
@@ -0,0 +1,557 @@
|
|
|
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
|
+
// 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`, whether `infer` may
|
|
7
|
+
// read it out of data (a format ordinary words pass, like a hostname or a
|
|
8
|
+
// slug, says not to), and for some a `pattern` the JSON Schema export
|
|
9
|
+
// writes beside the name, a shape that never rejects what the test accepts.
|
|
10
|
+
//
|
|
11
|
+
// The expressions are copied, with the attribution the project gives for a
|
|
12
|
+
// helper it carries instead of a dependency, from:
|
|
13
|
+
// - ajv-formats 3.0.1 (MIT, Copyright (c) 2020 Evgeny Poberezkin): email,
|
|
14
|
+
// uuid, uri, hostname, ipv4 and ipv6 of its fast mode, duration,
|
|
15
|
+
// uri-template, json-pointer and relative-json-pointer of its full mode,
|
|
16
|
+
// uri-reference of its fast mode, the shape of base64 (`byte`);
|
|
17
|
+
// - zod 4.6 (MIT, Copyright (c) 2025 Colin McDonnell): ulid, nanoid, e164,
|
|
18
|
+
// hex, emoji, the cidr prefixes, the shapes of a card number and an IBAN,
|
|
19
|
+
// the reading of base64, base64url and a JWT (without `atob` on the
|
|
20
|
+
// payload, and an `alg` that is a string);
|
|
21
|
+
// - valibot 1.5 (MIT, Copyright (c) Fabian Hiller): cuid2, slug, hex-color
|
|
22
|
+
// and the MAC-48 forms;
|
|
23
|
+
// - semver.org (CC BY 3.0): the expression of a semantic version.
|
|
24
|
+
// They are anchored and linear, every repetition bounded or separated by a
|
|
25
|
+
// character its class excludes. A repeated group with an alternative in it
|
|
26
|
+
// (`(?:a|bc)*`) is linear in time but not in the stack of the engine, which
|
|
27
|
+
// overflows on a string of megabytes: the forms that had one (an email's
|
|
28
|
+
// domain, a JSON pointer, a URI template, a slug, a semantic version) are
|
|
29
|
+
// read by a scanner over the string instead, with the same verdicts, and
|
|
30
|
+
// the expression stays as the `pattern` of the export where there is one.
|
|
31
|
+
// The date and time forms are RFC 3339 with the ranges checked (a 31st of
|
|
32
|
+
// February is not a date), and `url` is an http or https address whose host
|
|
33
|
+
// is a hostname or an address.
|
|
34
|
+
const { FORMATS } = require('../rules.js');
|
|
35
|
+
const { SchemaDefinitionError } = require('../errors.js');
|
|
36
|
+
|
|
37
|
+
const invalidFormat = (name, reason) =>
|
|
38
|
+
new SchemaDefinitionError('ERR_INVALID_FORMAT', `Format "${name}" ${reason}`);
|
|
39
|
+
|
|
40
|
+
// ajv-formats' fast email: a local part of its characters, an `@`, and
|
|
41
|
+
// labels of at most 63 characters joined by dots.
|
|
42
|
+
const LOCAL = /^[a-z0-9.!#$%&'*+/=?^_`{|}~-]+$/i;
|
|
43
|
+
|
|
44
|
+
const LABEL = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/i;
|
|
45
|
+
|
|
46
|
+
const isEmail = (text) => {
|
|
47
|
+
const at = text.indexOf('@');
|
|
48
|
+
if (at < 1 || !LOCAL.test(text.slice(0, at))) return false;
|
|
49
|
+
const labels = text.slice(at + 1).split('.');
|
|
50
|
+
for (let index = 0; index < labels.length; index += 1) {
|
|
51
|
+
if (!LABEL.test(labels[index])) return false;
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const UUID = /^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i;
|
|
57
|
+
|
|
58
|
+
const URI = /^(?:[a-z][a-z0-9+\-.]*:)(?:\/?\/)?[^\s]*$/i;
|
|
59
|
+
|
|
60
|
+
const HOSTNAME =
|
|
61
|
+
/^(?=.{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;
|
|
62
|
+
|
|
63
|
+
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)$/;
|
|
64
|
+
|
|
65
|
+
const IPV6 =
|
|
66
|
+
/^((([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;
|
|
67
|
+
|
|
68
|
+
// A full-date of RFC 3339 with the day within its month, and a full-time
|
|
69
|
+
// (an offset is required, as the RFC says; a leap second is allowed).
|
|
70
|
+
const DATE = /^(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;
|
|
71
|
+
|
|
72
|
+
const DAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
73
|
+
|
|
74
|
+
const isDate = (text) => {
|
|
75
|
+
const match = DATE.exec(text);
|
|
76
|
+
if (match === null) return false;
|
|
77
|
+
const year = Number(match[1]);
|
|
78
|
+
const month = Number(match[2]);
|
|
79
|
+
const leap = month === 2 && year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
80
|
+
return Number(match[3]) <= DAYS[month] + (leap ? 1 : 0);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// A partial-time with or without its offset: `time` needs one, as the RFC
|
|
84
|
+
// says, `iso-time` does not (ajv-formats' names); a leap second is allowed.
|
|
85
|
+
const TIME =
|
|
86
|
+
/^([01]\d|2[0-3]):([0-5]\d):([0-5]\d|60)(?:\.\d+)?(?:([zZ])|([+-])([01]\d|2[0-3])(?::?([0-5]\d))?)?$/;
|
|
87
|
+
|
|
88
|
+
// A leap second is the one of 23:59:60 UTC, whatever the offset says.
|
|
89
|
+
const timeOf = (zoned) => (text) => {
|
|
90
|
+
const match = TIME.exec(text);
|
|
91
|
+
if (match === null) return false;
|
|
92
|
+
if (zoned && match[4] === undefined && match[5] === undefined) return false;
|
|
93
|
+
if (match[3] !== '60') return true;
|
|
94
|
+
const sign = match[5] === '-' ? -1 : 1;
|
|
95
|
+
const offset =
|
|
96
|
+
match[5] === undefined ? 0 : sign * (Number(match[6]) * 60 + Number(match[7] ?? 0));
|
|
97
|
+
const minutes = (Number(match[1]) * 60 + Number(match[2]) - offset + 1440) % 1440;
|
|
98
|
+
return minutes === 23 * 60 + 59;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const isTime = timeOf(true);
|
|
102
|
+
|
|
103
|
+
const isIsoTime = timeOf(false);
|
|
104
|
+
|
|
105
|
+
// A date-time is the two joined by `T` (or, as the RFC allows for
|
|
106
|
+
// readability, a space).
|
|
107
|
+
const dateTimeOf = (time) => (text) =>
|
|
108
|
+
text.length > 11 &&
|
|
109
|
+
(text[10] === 'T' || text[10] === 't' || text[10] === ' ') &&
|
|
110
|
+
isDate(text.slice(0, 10)) &&
|
|
111
|
+
time(text.slice(11));
|
|
112
|
+
|
|
113
|
+
const DURATION = /^P(?!$)((\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+S)?)?|(\d+W)?)$/;
|
|
114
|
+
|
|
115
|
+
const URI_REFERENCE = /^(?:(?:[a-z][a-z0-9+\-.]*:)?\/?\/)?(?:[^\\\s#][^\s#]*)?(?:#[^\\\s]*)?$/i;
|
|
116
|
+
|
|
117
|
+
// Every character from `from` to `to` is one `allowed` accepts or a
|
|
118
|
+
// percent-encoded byte.
|
|
119
|
+
const HEX2 = /^[0-9a-f]{2}$/i;
|
|
120
|
+
|
|
121
|
+
const encoded = (text, from, to, allowed) => {
|
|
122
|
+
let index = from;
|
|
123
|
+
while (index < to) {
|
|
124
|
+
if (text[index] === '%') {
|
|
125
|
+
if (index + 3 > to || !HEX2.test(text.slice(index + 1, index + 3))) return false;
|
|
126
|
+
index += 3;
|
|
127
|
+
} else {
|
|
128
|
+
if (!allowed.test(text[index])) return false;
|
|
129
|
+
index += 1;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return true;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
// A URI template of RFC 6570 as ajv-formats reads it: literals outside the
|
|
136
|
+
// braces, and in each pair of braces an optional operator and a list of
|
|
137
|
+
// variable names, each with an optional prefix (`:1` to `:9999`) or `*`.
|
|
138
|
+
// oxlint-disable-next-line no-control-regex
|
|
139
|
+
const LITERAL = /[^\x00-\x20"'<>%\\^`{|}]/;
|
|
140
|
+
|
|
141
|
+
const VARCHAR = /[a-z0-9_]/i;
|
|
142
|
+
|
|
143
|
+
const OPERATOR = /[+#./;?&=,!@|]/;
|
|
144
|
+
|
|
145
|
+
const MODIFIER = /^:[1-9][0-9]{0,3}$/;
|
|
146
|
+
|
|
147
|
+
const isVariable = (spec) => {
|
|
148
|
+
let name = spec;
|
|
149
|
+
if (spec.endsWith('*')) name = spec.slice(0, -1);
|
|
150
|
+
else {
|
|
151
|
+
const colon = spec.indexOf(':');
|
|
152
|
+
if (colon !== -1) {
|
|
153
|
+
if (!MODIFIER.test(spec.slice(colon))) return false;
|
|
154
|
+
name = spec.slice(0, colon);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return name !== '' && encoded(name, 0, name.length, VARCHAR);
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const isExpression = (expression) => {
|
|
161
|
+
const body = OPERATOR.test(expression[0] ?? '') ? expression.slice(1) : expression;
|
|
162
|
+
if (body === '') return false;
|
|
163
|
+
const specs = body.split(',');
|
|
164
|
+
for (let index = 0; index < specs.length; index += 1) {
|
|
165
|
+
if (!isVariable(specs[index])) return false;
|
|
166
|
+
}
|
|
167
|
+
return true;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const isTemplate = (text) => {
|
|
171
|
+
let index = 0;
|
|
172
|
+
for (;;) {
|
|
173
|
+
const open = text.indexOf('{', index);
|
|
174
|
+
if (!encoded(text, index, open === -1 ? text.length : open, LITERAL)) return false;
|
|
175
|
+
if (open === -1) return true;
|
|
176
|
+
const close = text.indexOf('}', open + 1);
|
|
177
|
+
if (close === -1 || !isExpression(text.slice(open + 1, close))) return false;
|
|
178
|
+
index = close + 1;
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
// A JSON pointer of RFC 6901: empty, or segments led by `/` in which a `~`
|
|
183
|
+
// escapes a `0` or a `1` and nothing else.
|
|
184
|
+
const isPointer = (text) => {
|
|
185
|
+
if (text === '') return true;
|
|
186
|
+
if (text[0] !== '/') return false;
|
|
187
|
+
for (let at = text.indexOf('~'); at !== -1; at = text.indexOf('~', at + 2)) {
|
|
188
|
+
const next = text[at + 1];
|
|
189
|
+
if (next !== '0' && next !== '1') return false;
|
|
190
|
+
}
|
|
191
|
+
return true;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
const RELATIVE = /^(?:0|[1-9][0-9]*)/;
|
|
195
|
+
|
|
196
|
+
const isRelativePointer = (text) => {
|
|
197
|
+
const match = RELATIVE.exec(text);
|
|
198
|
+
if (match === null) return false;
|
|
199
|
+
const rest = text.slice(match[0].length);
|
|
200
|
+
return rest === '#' || isPointer(rest);
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
// A pattern of JSON Schema 2020-12 is an ECMA-262 expression in unicode
|
|
204
|
+
// mode. Compiling one costs memory in proportion to its text (a property
|
|
205
|
+
// escape expands as it is parsed), so a text longer than any pattern a
|
|
206
|
+
// program writes is not one; `length.max` on the field bounds it further.
|
|
207
|
+
const REGEX_MAX = 4096;
|
|
208
|
+
|
|
209
|
+
const isRegex = (text) => {
|
|
210
|
+
if (text.length > REGEX_MAX) return false;
|
|
211
|
+
try {
|
|
212
|
+
return RegExp(text, 'u') instanceof RegExp;
|
|
213
|
+
} catch {
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const ULID = /^[0-7][0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{25}$/;
|
|
219
|
+
|
|
220
|
+
const NANOID = /^[\w-]{21}$/;
|
|
221
|
+
|
|
222
|
+
const CUID2 = /^[a-z][0-9a-z]{1,31}$/;
|
|
223
|
+
|
|
224
|
+
const E164 = /^\+[1-9]\d{6,14}$/;
|
|
225
|
+
|
|
226
|
+
const HEX = /^[0-9a-fA-F]*$/;
|
|
227
|
+
|
|
228
|
+
const HEX_COLOR = /^#(?:[\da-fA-F]{3,4}|[\da-fA-F]{6}|[\da-fA-F]{8})$/;
|
|
229
|
+
|
|
230
|
+
// Words of digits and lowercase letters joined by single dashes or
|
|
231
|
+
// underscores (valibot's slug); the expression is the pattern of the export.
|
|
232
|
+
const SLUG = /^[\da-z]+(?:[-_][\da-z]+)*$/;
|
|
233
|
+
|
|
234
|
+
const SLUG_CHARS = /^[\da-z](?:[\da-z_-]*[\da-z])?$/;
|
|
235
|
+
|
|
236
|
+
const SEPARATORS = /[-_]{2}/;
|
|
237
|
+
|
|
238
|
+
const isSlug = (text) => SLUG_CHARS.test(text) && !SEPARATORS.test(text);
|
|
239
|
+
|
|
240
|
+
const MAC =
|
|
241
|
+
/^(?:[\da-fA-F]{2}:){5}[\da-fA-F]{2}$|^(?:[\da-fA-F]{2}-){5}[\da-fA-F]{2}$|^(?:[\da-fA-F]{4}\.){2}[\da-fA-F]{4}$/;
|
|
242
|
+
|
|
243
|
+
// semver.org's expression, the pattern of the export; the test reads the
|
|
244
|
+
// three parts (the core, the pre-release after the first `-`, the build
|
|
245
|
+
// after the `+`) and their dot-separated identifiers one by one.
|
|
246
|
+
const SEMVER =
|
|
247
|
+
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[A-Za-z-][\dA-Za-z-]*)(?:\.(?:0|[1-9]\d*|\d*[A-Za-z-][\dA-Za-z-]*))*))?(?:\+([\dA-Za-z-]+(?:\.[\dA-Za-z-]+)*))?$/;
|
|
248
|
+
|
|
249
|
+
const CORE = /^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)$/;
|
|
250
|
+
|
|
251
|
+
const PRERELEASE = /^(?:0|[1-9]\d*|\d*[A-Za-z-][\dA-Za-z-]*)$/;
|
|
252
|
+
|
|
253
|
+
const BUILD = /^[\dA-Za-z-]+$/;
|
|
254
|
+
|
|
255
|
+
const identifiers = (text, form) => {
|
|
256
|
+
const parts = text.split('.');
|
|
257
|
+
for (let index = 0; index < parts.length; index += 1) {
|
|
258
|
+
if (!form.test(parts[index])) return false;
|
|
259
|
+
}
|
|
260
|
+
return true;
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
const isSemver = (text) => {
|
|
264
|
+
const plus = text.indexOf('+');
|
|
265
|
+
const version = plus === -1 ? text : text.slice(0, plus);
|
|
266
|
+
if (plus !== -1 && !identifiers(text.slice(plus + 1), BUILD)) return false;
|
|
267
|
+
const dash = version.indexOf('-');
|
|
268
|
+
if (dash === -1) return CORE.test(version);
|
|
269
|
+
return CORE.test(version.slice(0, dash)) && identifiers(version.slice(dash + 1), PRERELEASE);
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
const EMOJI =
|
|
273
|
+
/^(?=[\s\S]*[\p{Extended_Pictographic}\p{Regional_Indicator}\u20E3])[\p{Extended_Pictographic}\p{Emoji_Component}]+$/u;
|
|
274
|
+
|
|
275
|
+
// A charset and a length instead of the quantified forms, which overflow the
|
|
276
|
+
// stack of the engine on a string of megabytes.
|
|
277
|
+
const BASE64 = /^[A-Za-z0-9+/]*={0,2}$/;
|
|
278
|
+
|
|
279
|
+
const BYTE = '^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$';
|
|
280
|
+
|
|
281
|
+
const isBase64 = (text) => text.length % 4 === 0 && BASE64.test(text);
|
|
282
|
+
|
|
283
|
+
const BASE64URL = /^[\w-]*$/;
|
|
284
|
+
|
|
285
|
+
const isBase64url = (text) => text.length % 4 !== 1 && BASE64URL.test(text);
|
|
286
|
+
|
|
287
|
+
// A JWT is three parts of base64url, the first a JSON object that names its
|
|
288
|
+
// algorithm; the signature is not verified, and the payload not read.
|
|
289
|
+
const JWT = /^([\w-]+)\.[\w-]*\.[\w-]*$/;
|
|
290
|
+
|
|
291
|
+
const isJwt = (text) => {
|
|
292
|
+
const match = JWT.exec(text);
|
|
293
|
+
if (match === null || match[1].length % 4 === 1) return false;
|
|
294
|
+
try {
|
|
295
|
+
const header = JSON.parse(atob(match[1].replaceAll('-', '+').replaceAll('_', '/')));
|
|
296
|
+
return header !== null && typeof header === 'object' && typeof header.alg === 'string';
|
|
297
|
+
} catch {
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
const PREFIX4 = /^(?:3[0-2]|[12]?\d)$/;
|
|
303
|
+
|
|
304
|
+
const PREFIX6 = /^(?:12[0-8]|1[01]\d|[1-9]?\d)$/;
|
|
305
|
+
|
|
306
|
+
const cidrOf = (address, prefix) => (text) => {
|
|
307
|
+
const at = text.indexOf('/');
|
|
308
|
+
return at > 0 && prefix.test(text.slice(at + 1)) && address.test(text.slice(0, at));
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
// An http or https address, stricter than what a browser reads: the host is
|
|
312
|
+
// a hostname, an IPv4 address (a host that ends in a number is one) or an
|
|
313
|
+
// IPv6 address in brackets, and the port fits in 16 bits.
|
|
314
|
+
const URL_FORM =
|
|
315
|
+
/^https?:\/\/(?:[^\s/?#@]*@)?(\[[^\s/?#\]]*\]|[^\s/?#:@[\]]+)(?::(\d{1,5}))?(?:[/?#][^\s\\]*)?$/i;
|
|
316
|
+
|
|
317
|
+
const NUMERIC = /(?:^|\.)\d+\.?$/;
|
|
318
|
+
|
|
319
|
+
const isUrl = (text) => {
|
|
320
|
+
const match = URL_FORM.exec(text);
|
|
321
|
+
if (match === null || Number(match[2] ?? 0) > 65535) return false;
|
|
322
|
+
const host = match[1];
|
|
323
|
+
if (host[0] === '[') return IPV6.test(host.slice(1, -1));
|
|
324
|
+
return NUMERIC.test(host) ? IPV4.test(host) : HOSTNAME.test(host);
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
// A card number of 12 to 19 digits (spaces or dashes between them) whose
|
|
328
|
+
// Luhn sum holds; a valid sum is not a card that exists.
|
|
329
|
+
const CARD = /^\d(?:[ -]?\d){11,18}$/;
|
|
330
|
+
|
|
331
|
+
const isCard = (text) => {
|
|
332
|
+
if (!CARD.test(text)) return false;
|
|
333
|
+
let sum = 0;
|
|
334
|
+
let double = false;
|
|
335
|
+
for (let index = text.length - 1; index >= 0; index -= 1) {
|
|
336
|
+
let digit = text.charCodeAt(index) - 48;
|
|
337
|
+
if (digit < 0 || digit > 9) continue;
|
|
338
|
+
if (double) digit = digit > 4 ? digit * 2 - 9 : digit * 2;
|
|
339
|
+
sum += digit;
|
|
340
|
+
double = !double;
|
|
341
|
+
}
|
|
342
|
+
return sum % 10 === 0;
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
// An IBAN whose check digits hold (ISO 7064 mod 97-10, the letters read as
|
|
346
|
+
// 10 to 35, the remainder kept small instead of a bigint).
|
|
347
|
+
const IBAN = /^[A-Z]{2}(?!00|01|99)\d{2}[A-Z\d]{11,30}$/;
|
|
348
|
+
|
|
349
|
+
const isIban = (text) => {
|
|
350
|
+
if (!IBAN.test(text)) return false;
|
|
351
|
+
const moved = text.slice(4) + text.slice(0, 4);
|
|
352
|
+
let rest = 0;
|
|
353
|
+
for (let index = 0; index < moved.length; index += 1) {
|
|
354
|
+
const code = moved.charCodeAt(index);
|
|
355
|
+
rest = code >= 65 ? (rest * 100 + code - 55) % 97 : (rest * 10 + code - 48) % 97;
|
|
356
|
+
}
|
|
357
|
+
return rest === 1;
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
const tested = (expression) => (text) => expression.test(text);
|
|
361
|
+
|
|
362
|
+
// In the order `infer` tries them: the specific forms first (a uuid, a
|
|
363
|
+
// date, an address are also hostnames), a date-time with its zone before
|
|
364
|
+
// one without, an http address before any URI; the forms ordinary words or
|
|
365
|
+
// numbers pass (a hostname, a slug, a JSON pointer, `1.2.3` as a version, a
|
|
366
|
+
// Luhn sum one number in ten passes) are never inferred.
|
|
367
|
+
const BUILT_IN = {
|
|
368
|
+
uuid: {
|
|
369
|
+
test: tested(UUID),
|
|
370
|
+
examples: ['550e8400-e29b-41d4-a716-446655440000', '123e4567-e89b-12d3-a456-426614174000'],
|
|
371
|
+
},
|
|
372
|
+
ulid: {
|
|
373
|
+
test: tested(ULID),
|
|
374
|
+
examples: ['01ARZ3NDEKTSV4RRFFQ69G5FAV', '01HZX8Q4N0J5W6Y2T9K3M7P1RB'],
|
|
375
|
+
pattern: ULID.source,
|
|
376
|
+
},
|
|
377
|
+
jwt: {
|
|
378
|
+
test: isJwt,
|
|
379
|
+
examples: [
|
|
380
|
+
'eyJhbGciOiJub25lIn0.e30.',
|
|
381
|
+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIn0.c2lnbmF0dXJl',
|
|
382
|
+
],
|
|
383
|
+
pattern: '^[\\w-]+\\.[\\w-]*\\.[\\w-]*$',
|
|
384
|
+
},
|
|
385
|
+
email: { test: isEmail, examples: ['ann@example.com', 'ann.lee@mail.example.org'] },
|
|
386
|
+
'date-time': {
|
|
387
|
+
test: dateTimeOf(isTime),
|
|
388
|
+
examples: ['2026-10-11T12:00:00Z', '2026-10-11T12:00:00.000+02:00'],
|
|
389
|
+
},
|
|
390
|
+
'iso-date-time': {
|
|
391
|
+
test: dateTimeOf(isIsoTime),
|
|
392
|
+
examples: ['2026-10-11T12:00:00', '2026-10-11 12:00:00.5'],
|
|
393
|
+
},
|
|
394
|
+
date: { test: isDate, examples: ['2026-10-11', '2000-02-29'] },
|
|
395
|
+
time: { test: isTime, examples: ['12:00:00Z', '23:59:59.999+02:00'] },
|
|
396
|
+
'iso-time': { test: isIsoTime, examples: ['12:00:00', '23:59:59.999+02:00'] },
|
|
397
|
+
duration: { test: tested(DURATION), examples: ['P1Y2M3DT4H5M6S', 'PT15M'] },
|
|
398
|
+
ipv4: { test: tested(IPV4), examples: ['192.168.0.1', '10.0.0.255'] },
|
|
399
|
+
cidrv4: { test: cidrOf(IPV4, PREFIX4), examples: ['192.168.0.0/24', '10.0.0.0/8'] },
|
|
400
|
+
ipv6: { test: tested(IPV6), examples: ['::1', '2001:db8::ff00:42:8329'] },
|
|
401
|
+
cidrv6: { test: cidrOf(IPV6, PREFIX6), examples: ['2001:db8::/32', 'fe80::/10'] },
|
|
402
|
+
mac: {
|
|
403
|
+
test: tested(MAC),
|
|
404
|
+
examples: ['00:1a:2b:3c:4d:5e', '00-1A-2B-3C-4D-5E'],
|
|
405
|
+
pattern: MAC.source,
|
|
406
|
+
},
|
|
407
|
+
e164: { test: tested(E164), examples: ['+380501234567', '+14155552671'], pattern: E164.source },
|
|
408
|
+
iban: {
|
|
409
|
+
test: isIban,
|
|
410
|
+
examples: ['DE89370400440532013000', 'GB82WEST12345698765432'],
|
|
411
|
+
pattern: IBAN.source,
|
|
412
|
+
},
|
|
413
|
+
'hex-color': {
|
|
414
|
+
test: tested(HEX_COLOR),
|
|
415
|
+
examples: ['#7c5cff', '#fff'],
|
|
416
|
+
pattern: HEX_COLOR.source,
|
|
417
|
+
},
|
|
418
|
+
url: { test: isUrl, examples: ['https://example.com/path?q=1', 'http://localhost:3000/'] },
|
|
419
|
+
uri: { test: tested(URI), examples: ['https://example.com/path?q=1', 'mailto:ann@example.com'] },
|
|
420
|
+
hostname: { test: tested(HOSTNAME), examples: ['example.com', 'api.example.org'], infer: false },
|
|
421
|
+
'uri-reference': {
|
|
422
|
+
test: tested(URI_REFERENCE),
|
|
423
|
+
examples: ['/users/42?tab=info', '../up#top'],
|
|
424
|
+
infer: false,
|
|
425
|
+
},
|
|
426
|
+
'uri-template': {
|
|
427
|
+
test: isTemplate,
|
|
428
|
+
examples: ['https://example.com/users/{id}', '/search{?q,page}'],
|
|
429
|
+
infer: false,
|
|
430
|
+
},
|
|
431
|
+
'json-pointer': {
|
|
432
|
+
test: isPointer,
|
|
433
|
+
examples: ['/users/0/name', '/a~1b'],
|
|
434
|
+
infer: false,
|
|
435
|
+
},
|
|
436
|
+
'relative-json-pointer': {
|
|
437
|
+
test: isRelativePointer,
|
|
438
|
+
examples: ['1/name', '0#'],
|
|
439
|
+
infer: false,
|
|
440
|
+
},
|
|
441
|
+
regex: { test: isRegex, examples: ['^[a-z]+$', '\\d{3}-\\d{4}'], infer: false },
|
|
442
|
+
nanoid: {
|
|
443
|
+
test: tested(NANOID),
|
|
444
|
+
examples: ['V1StGXR8_Z5jdHi6B-myT', 'a3Fk9_LmQz-7xYpR2tWn0'],
|
|
445
|
+
infer: false,
|
|
446
|
+
pattern: NANOID.source,
|
|
447
|
+
},
|
|
448
|
+
cuid2: {
|
|
449
|
+
test: tested(CUID2),
|
|
450
|
+
examples: ['tz4a98xxat96iws9zmbrgj3a', 'pfh0haxfpzowht3oi213cqos'],
|
|
451
|
+
infer: false,
|
|
452
|
+
pattern: CUID2.source,
|
|
453
|
+
},
|
|
454
|
+
base64: { test: isBase64, examples: ['aGVsbG8=', 'TWFu'], infer: false, pattern: BYTE },
|
|
455
|
+
base64url: {
|
|
456
|
+
test: isBase64url,
|
|
457
|
+
examples: ['aGVsbG8', 'PDw_Pz8-'],
|
|
458
|
+
infer: false,
|
|
459
|
+
pattern: '^(?:[\\w-]{4})*(?:[\\w-]{2,3})?$',
|
|
460
|
+
},
|
|
461
|
+
hex: { test: tested(HEX), examples: ['deadbeef', '0A1B2C'], infer: false, pattern: HEX.source },
|
|
462
|
+
emoji: { test: tested(EMOJI), examples: ['👍', '🇺🇦'], infer: false },
|
|
463
|
+
semver: {
|
|
464
|
+
test: isSemver,
|
|
465
|
+
examples: ['1.0.0', '2.1.3-beta.1+build.5'],
|
|
466
|
+
infer: false,
|
|
467
|
+
pattern: SEMVER.source,
|
|
468
|
+
},
|
|
469
|
+
slug: {
|
|
470
|
+
test: isSlug,
|
|
471
|
+
examples: ['hello-world', 'metaschema-3-3'],
|
|
472
|
+
infer: false,
|
|
473
|
+
pattern: SLUG.source,
|
|
474
|
+
},
|
|
475
|
+
'credit-card': {
|
|
476
|
+
test: isCard,
|
|
477
|
+
examples: ['4111111111111111', '5555 5555 5555 4444'],
|
|
478
|
+
infer: false,
|
|
479
|
+
pattern: CARD.source,
|
|
480
|
+
},
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
// An entry as the table keeps it: the test, a copy of the examples, whether
|
|
484
|
+
// infer may read it and the pattern of the export, frozen.
|
|
485
|
+
const entryOf = (name, entry) => {
|
|
486
|
+
if (entry === null || typeof entry !== 'object') {
|
|
487
|
+
throw invalidFormat(name, 'needs { test, examples }');
|
|
488
|
+
}
|
|
489
|
+
const { test, examples, infer, pattern } = entry;
|
|
490
|
+
if (typeof test !== 'function') throw invalidFormat(name, 'needs a test function');
|
|
491
|
+
const listed = Array.isArray(examples) && examples.length > 0;
|
|
492
|
+
if (!listed) throw invalidFormat(name, 'needs a list of example strings');
|
|
493
|
+
for (let index = 0; index < examples.length; index += 1) {
|
|
494
|
+
if (typeof examples[index] !== 'string') {
|
|
495
|
+
throw invalidFormat(name, 'needs a list of example strings');
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
if (infer !== undefined && typeof infer !== 'boolean') {
|
|
499
|
+
throw invalidFormat(name, 'needs a boolean infer');
|
|
500
|
+
}
|
|
501
|
+
if (pattern !== undefined && (typeof pattern !== 'string' || !isRegex(pattern))) {
|
|
502
|
+
throw invalidFormat(name, 'needs a pattern string that compiles under the u flag');
|
|
503
|
+
}
|
|
504
|
+
const copy = { test, examples: Object.freeze(examples.slice()), infer: infer !== false };
|
|
505
|
+
if (pattern !== undefined) copy.pattern = pattern;
|
|
506
|
+
return Object.freeze(copy);
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
const registered = (name) =>
|
|
510
|
+
new SchemaDefinitionError(
|
|
511
|
+
'ERR_FORMAT_REGISTERED',
|
|
512
|
+
`Format "${name}" is already registered with another test`,
|
|
513
|
+
);
|
|
514
|
+
|
|
515
|
+
// A name is registered once: the same test again is nothing, and another
|
|
516
|
+
// test under a registered name is an error, since the schemas built so far
|
|
517
|
+
// hold the first one. The one exception is the program's own format under
|
|
518
|
+
// the name of a built-in: it takes the built-in's place (and its position in
|
|
519
|
+
// the order infer reads them), so that a format a program registered before
|
|
520
|
+
// the name was built in keeps working; the schemas built before it keep the
|
|
521
|
+
// built-in test.
|
|
522
|
+
const add = (name, entry, own) => {
|
|
523
|
+
if (name === '') throw invalidFormat(name, 'needs a name');
|
|
524
|
+
const known = FORMATS[name];
|
|
525
|
+
let replaced = false;
|
|
526
|
+
if (known !== undefined) {
|
|
527
|
+
if (known.test === entry.test) return false;
|
|
528
|
+
if (!own) return false;
|
|
529
|
+
replaced = Object.hasOwn(BUILT_IN, name) && known.test === BUILT_IN[name].test;
|
|
530
|
+
if (!replaced) throw registered(name);
|
|
531
|
+
}
|
|
532
|
+
FORMATS[name] = entryOf(name, entry);
|
|
533
|
+
return replaced;
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
// Registers the built-in formats and the program's own
|
|
537
|
+
// (`{ phone: { test, examples } }`); once per name, process-wide, like the
|
|
538
|
+
// type registry. Importing the subpath registers nothing. Returns the names
|
|
539
|
+
// of the built-in formats the call replaced with the program's own (the
|
|
540
|
+
// schemas built before keep the built-in test), so that a program can log
|
|
541
|
+
// what it overrode; an empty list otherwise.
|
|
542
|
+
const register = (custom = {}) => {
|
|
543
|
+
if (custom === null || typeof custom !== 'object') {
|
|
544
|
+
throw new SchemaDefinitionError('ERR_INVALID_FORMAT', 'Formats must be an object of entries');
|
|
545
|
+
}
|
|
546
|
+
const replaced = [];
|
|
547
|
+
for (const name of Object.keys(BUILT_IN)) {
|
|
548
|
+
const own = Object.hasOwn(custom, name);
|
|
549
|
+
if (add(name, own ? custom[name] : BUILT_IN[name], own)) replaced.push(name);
|
|
550
|
+
}
|
|
551
|
+
for (const name of Object.keys(custom)) {
|
|
552
|
+
if (!Object.hasOwn(BUILT_IN, name)) add(name, custom[name], true);
|
|
553
|
+
}
|
|
554
|
+
return replaced;
|
|
555
|
+
};
|
|
556
|
+
|
|
557
|
+
module.exports = { FORMATS, register };
|
|
@@ -9,9 +9,10 @@
|
|
|
9
9
|
// The document is untrusted input: a pointer reads own keys only, a `$ref` is
|
|
10
10
|
// converted once per pointer, and the nesting is bounded.
|
|
11
11
|
const { isFirstUpper } = require('../metautil.js');
|
|
12
|
-
const { define } = require('../util.js');
|
|
12
|
+
const { define, shorten } = require('../util.js');
|
|
13
13
|
const { Schema } = require('../schema.js');
|
|
14
|
-
const {
|
|
14
|
+
const { readsAsStruct } = require('../types.js');
|
|
15
|
+
const { FORMATS } = require('../rules.js');
|
|
15
16
|
const { SchemaDefinitionError, invalidTarget } = require('../errors.js');
|
|
16
17
|
|
|
17
18
|
const invalidOptions = (reason) => new SchemaDefinitionError('ERR_INVALID_OPTIONS', reason);
|
|
@@ -99,11 +100,12 @@ const createContext = (document, options) => {
|
|
|
99
100
|
};
|
|
100
101
|
};
|
|
101
102
|
|
|
102
|
-
// The place in the document as a JSON pointer (RFC 6901)
|
|
103
|
+
// The place in the document as a JSON pointer (RFC 6901); a key of the
|
|
104
|
+
// document may be huge, a message must not be.
|
|
103
105
|
const pointerOf = (path) => {
|
|
104
106
|
let pointer = '';
|
|
105
107
|
for (const segment of path) {
|
|
106
|
-
pointer += `/${
|
|
108
|
+
pointer += `/${shorten(segment).replace(/~/g, '~0').replace(/\//g, '~1')}`;
|
|
107
109
|
}
|
|
108
110
|
return pointer;
|
|
109
111
|
};
|
|
@@ -134,15 +136,6 @@ const compilesWithU = (pattern) => {
|
|
|
134
136
|
}
|
|
135
137
|
};
|
|
136
138
|
|
|
137
|
-
// A struct key that the first-key rule reads as something else needs the
|
|
138
|
-
// `schema` shorthand around the struct.
|
|
139
|
-
const safeFirstKey = (fields) => {
|
|
140
|
-
const first = Object.keys(fields)[0];
|
|
141
|
-
if (first === undefined) return true;
|
|
142
|
-
const key = first.endsWith('?') ? first.slice(0, -1) : first;
|
|
143
|
-
return key !== 'type' && TYPES[key] === undefined && !isFirstUpper(key);
|
|
144
|
-
};
|
|
145
|
-
|
|
146
139
|
// A definition of a type with no other key is its name.
|
|
147
140
|
const compact = (def, required) => {
|
|
148
141
|
const keys = Object.keys(def);
|
|
@@ -263,6 +256,15 @@ const typed = (context, node, type, used) => {
|
|
|
263
256
|
unsupported(context, `The pattern ${shown} (not a valid pattern under the u flag)`);
|
|
264
257
|
}
|
|
265
258
|
}
|
|
259
|
+
// A format the formats subpath registered is the rule; any other stays
|
|
260
|
+
// the annotation JSON Schema says it is. OpenAPI's `byte` is base64, and
|
|
261
|
+
// the shape the export writes beside a format is the format's own.
|
|
262
|
+
const format = take('format');
|
|
263
|
+
const name = format === 'byte' && FORMATS.byte === undefined ? 'base64' : format;
|
|
264
|
+
if (typeof name === 'string' && FORMATS[name] !== undefined) {
|
|
265
|
+
def.format = name;
|
|
266
|
+
if (def.pattern === FORMATS[name].pattern) delete def.pattern;
|
|
267
|
+
}
|
|
266
268
|
return def;
|
|
267
269
|
}
|
|
268
270
|
if (type === 'number' || type === 'integer') {
|
|
@@ -490,7 +492,7 @@ const compactDef = (def, required) => {
|
|
|
490
492
|
if (def.nullable === true && !required) delete def.nullable;
|
|
491
493
|
if (def.type === 'schema' && Object.keys(def).length === 2) {
|
|
492
494
|
const { schema } = def;
|
|
493
|
-
if (
|
|
495
|
+
if (readsAsStruct(schema)) return required ? schema : { schema, required: false };
|
|
494
496
|
return required ? { schema } : { schema, required: false };
|
|
495
497
|
}
|
|
496
498
|
return compact(def, required);
|
package/src/generators/index.js
CHANGED
|
@@ -60,7 +60,8 @@ const modelMethods = {
|
|
|
60
60
|
};
|
|
61
61
|
|
|
62
62
|
// Installed like class members: not enumerable, so a loop over a schema
|
|
63
|
-
// never sees them.
|
|
63
|
+
// never sees them. A copy per subpath rather than a helper of the core: the
|
|
64
|
+
// core is measured on its own, the subpaths on top of it.
|
|
64
65
|
const install = (prototype, members) => {
|
|
65
66
|
const descriptors = Object.getOwnPropertyDescriptors(members);
|
|
66
67
|
for (const name in descriptors) {
|