@alexify/metaschema 2.0.0 → 3.1.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 +443 -1
- package/README.md +124 -12
- package/SECURITY.md +27 -8
- package/index.d.ts +537 -84
- package/package.json +25 -6
- package/src/codec.js +544 -0
- package/src/compile/code.js +92 -0
- package/src/compile/emit.js +540 -0
- package/src/compile/index.d.ts +38 -0
- package/src/compile/index.js +176 -0
- package/src/context.js +68 -19
- package/src/errors.js +19 -1
- package/src/generators/fromjsonschema.js +520 -0
- package/src/generators/index.d.ts +46 -0
- package/src/generators/index.js +95 -0
- package/src/generators/jsonschema.js +535 -0
- package/src/generators/typescript.js +237 -0
- package/src/index.js +0 -2
- package/src/issues.js +117 -22
- package/src/kinds.js +9 -12
- package/src/lint.js +31 -14
- package/src/locales/en.js +10 -2
- package/src/locales/uk.js +7 -2
- package/src/metadata.js +41 -44
- package/src/metautil.js +8 -1
- package/src/model/data.js +122 -0
- package/src/model/diff.js +438 -0
- package/src/model/index.d.ts +56 -0
- package/src/model/index.js +42 -0
- package/src/model/merge.js +254 -0
- package/src/model/pattern.js +322 -0
- package/src/model/restore.js +226 -0
- package/src/model/sample.js +394 -0
- package/src/model.js +18 -15
- package/src/preprocessor.js +9 -11
- package/src/prototypes/abstract.js +14 -1
- package/src/prototypes/collections.js +20 -3
- package/src/prototypes/reference.js +17 -3
- package/src/prototypes/scalars.js +1 -3
- package/src/prototypes/schema.js +15 -2
- package/src/prototypes/tuple.js +14 -9
- package/src/prototypes/union.js +57 -18
- package/src/result.js +5 -14
- package/src/rules.js +55 -26
- package/src/runtime/browser.js +2 -2
- package/src/runtime/node.js +4 -2
- package/src/schema.js +129 -101
- package/src/snapshot.js +111 -0
- package/src/struct.js +80 -23
- package/src/types.js +5 -11
- package/src/util.js +39 -0
package/src/util.js
CHANGED
|
@@ -9,11 +9,37 @@ const INSPECT = Symbol.for('nodejs.util.inspect.custom');
|
|
|
9
9
|
|
|
10
10
|
const hasBrand = (value, name) => value?.[BRAND] === name;
|
|
11
11
|
|
|
12
|
+
// A JavaScript identifier: what generated code may write as `object.key` and
|
|
13
|
+
// a TypeScript declaration as a bare name.
|
|
14
|
+
const IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
|
|
15
|
+
|
|
16
|
+
// Defines an own data property, so a key such as `__proto__` taken from a
|
|
17
|
+
// path or a definition lands on the object instead of changing its prototype.
|
|
18
|
+
const define = (target, key, value) => {
|
|
19
|
+
Object.defineProperty(target, key, {
|
|
20
|
+
value,
|
|
21
|
+
enumerable: true,
|
|
22
|
+
writable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
});
|
|
25
|
+
return value;
|
|
26
|
+
};
|
|
27
|
+
|
|
12
28
|
// The method that validates a value inside an existing context, shared by
|
|
13
29
|
// Schema and the reference type that checks its target through it. A global
|
|
14
30
|
// symbol like BRAND, so a schema from another copy of the package has it too.
|
|
15
31
|
const RUN = Symbol.for('alexify.metaschema.run');
|
|
16
32
|
|
|
33
|
+
// A slot on the Schema class for a function to call with every schema built
|
|
34
|
+
// (`Schema[HOOK]`): the `./compile` subpath installs its JIT there, so a
|
|
35
|
+
// program that registered it gets every later schema compiled. Nothing in the
|
|
36
|
+
// core sets it, and it is read once per construction, never per check.
|
|
37
|
+
const HOOK = Symbol.for('alexify.metaschema.hook');
|
|
38
|
+
|
|
39
|
+
// Past this many values an enum is checked through a Set instead of a chain
|
|
40
|
+
// of comparisons, on both backends.
|
|
41
|
+
const ENUM_SET_SIZE = 8;
|
|
42
|
+
|
|
17
43
|
// Key names taken from the value under check go into messages; a huge key
|
|
18
44
|
// must not become a huge message.
|
|
19
45
|
const KEY_LIMIT = 100;
|
|
@@ -23,6 +49,14 @@ const shorten = (key) => {
|
|
|
23
49
|
return name.length > KEY_LIMIT ? `${name.slice(0, KEY_LIMIT)}...` : name;
|
|
24
50
|
};
|
|
25
51
|
|
|
52
|
+
// The text of a value that may have none (a symbol, an object without a
|
|
53
|
+
// prototype), for a message: what a validator threw or returned is anything.
|
|
54
|
+
const safeString = (value) => {
|
|
55
|
+
if (typeof value === 'symbol') return value.toString();
|
|
56
|
+
const bare = value !== null && typeof value === 'object' && typeof value.toString !== 'function';
|
|
57
|
+
return bare ? Object.prototype.toString.call(value) : String(value);
|
|
58
|
+
};
|
|
59
|
+
|
|
26
60
|
const formatters = {
|
|
27
61
|
type: (type, req = true) => {
|
|
28
62
|
const required = !type.startsWith('?');
|
|
@@ -43,7 +77,12 @@ module.exports = {
|
|
|
43
77
|
BRAND,
|
|
44
78
|
INSPECT,
|
|
45
79
|
hasBrand,
|
|
80
|
+
IDENTIFIER,
|
|
81
|
+
define,
|
|
46
82
|
RUN,
|
|
83
|
+
HOOK,
|
|
84
|
+
ENUM_SET_SIZE,
|
|
47
85
|
shorten,
|
|
86
|
+
safeString,
|
|
48
87
|
formatters,
|
|
49
88
|
};
|