@alexify/metaschema 1.0.0 → 3.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.
- package/CHANGELOG.md +551 -1
- package/README.md +235 -34
- package/SECURITY.md +32 -11
- package/index.d.ts +798 -23
- package/package.json +30 -6
- package/src/codec.js +490 -0
- package/src/compile/code.js +92 -0
- package/src/compile/emit.js +512 -0
- package/src/compile/index.d.ts +38 -0
- package/src/compile/index.js +176 -0
- package/src/context.js +141 -0
- package/src/errors.js +10 -1
- package/src/generators/index.d.ts +29 -0
- package/src/generators/index.js +90 -0
- package/src/generators/jsonschema.js +535 -0
- package/src/generators/typescript.js +237 -0
- package/src/index.js +1 -3
- package/src/issues.js +291 -0
- package/src/kinds.js +9 -12
- package/src/lint.js +98 -0
- package/src/locales/en.d.ts +5 -0
- package/src/locales/en.js +66 -0
- package/src/locales/uk.d.ts +5 -0
- package/src/locales/uk.js +62 -0
- package/src/metadata.js +65 -119
- package/src/metautil.js +8 -1
- package/src/model.js +31 -20
- package/src/preprocessor.js +45 -27
- package/src/prototypes/abstract.js +101 -51
- package/src/prototypes/collections.js +106 -41
- package/src/prototypes/json.js +9 -5
- package/src/prototypes/reference.js +91 -20
- package/src/prototypes/scalars.js +124 -16
- package/src/prototypes/schema.js +31 -6
- package/src/prototypes/tuple.js +57 -29
- package/src/prototypes/union.js +139 -0
- package/src/result.js +115 -0
- package/src/rules.js +185 -0
- package/src/runtime/browser.js +2 -2
- package/src/runtime/node.js +4 -2
- package/src/schema.js +181 -68
- package/src/struct.js +101 -33
- package/src/types.js +14 -14
- package/src/util.js +41 -50
package/src/struct.js
CHANGED
|
@@ -1,10 +1,78 @@
|
|
|
1
|
-
const {
|
|
2
|
-
const {
|
|
1
|
+
const { BRAND, hasBrand, formatters } = require('./util.js');
|
|
2
|
+
const { issues } = require('./issues.js');
|
|
3
3
|
const { SchemaDefinitionError } = require('./errors.js');
|
|
4
4
|
|
|
5
|
-
//
|
|
6
|
-
// like BRAND, so a struct built by another copy of the package
|
|
7
|
-
|
|
5
|
+
// What a struct computes once at construction, kept on the dictionary under a
|
|
6
|
+
// global symbol like BRAND, so a struct built by another copy of the package
|
|
7
|
+
// exposes it too: { plan, known, calculated, check }, the plan being one
|
|
8
|
+
// frozen entry per validated field, `known` the dictionary of the keys a
|
|
9
|
+
// value may carry, `calculated` the keys of the calculated fields among them,
|
|
10
|
+
// and `check` the compiled check over the plan.
|
|
11
|
+
const STRUCT = Symbol.for('alexify.metaschema.struct');
|
|
12
|
+
|
|
13
|
+
// The functions of a nested definition that are the struct's own validate,
|
|
14
|
+
// decoder and serializer (as they are options at the root): they stay in the
|
|
15
|
+
// fields, where a calculated field lives, but a value has no key by that name.
|
|
16
|
+
const CODEC_KEYS = new Set(['validate', 'parse', 'serialize']);
|
|
17
|
+
|
|
18
|
+
// A field is read with a keyed load, and `Object.hasOwn` is consulted only
|
|
19
|
+
// when the value is undefined, to tell a missing key from one set to
|
|
20
|
+
// undefined. A field named after a member of Object.prototype (`toString`,
|
|
21
|
+
// `constructor`, ...) would read the inherited function from a plain object,
|
|
22
|
+
// so such a field (`own`) always asks `Object.hasOwn` first.
|
|
23
|
+
const scanUnknown = (value, known, context) => {
|
|
24
|
+
let unexpected = null;
|
|
25
|
+
for (const name in value) {
|
|
26
|
+
if (name in known || !Object.hasOwn(value, name)) continue;
|
|
27
|
+
if (unexpected === null) unexpected = [];
|
|
28
|
+
unexpected.push(name);
|
|
29
|
+
}
|
|
30
|
+
if (unexpected !== null) issues.unexpected(context, unexpected);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// The check of a struct, as a closure over its plan: an index loop with no
|
|
34
|
+
// lookups in the raw definition, no brand checks and no path strings. Every
|
|
35
|
+
// plan entry has the same shape and every field check is a function chosen
|
|
36
|
+
// when the field was built, called with the key of the field so that a leaf
|
|
37
|
+
// never touches the path. Unknown keys are looked for only when the value has
|
|
38
|
+
// more keys than the plan found, so a value with exactly the expected keys is
|
|
39
|
+
// never scanned twice.
|
|
40
|
+
const compileStruct = (plan, known) => (value, context, key) => {
|
|
41
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
42
|
+
issues.type(context, 'object', value, key);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const nested = key !== undefined;
|
|
46
|
+
if (nested) context.path.push(key);
|
|
47
|
+
let found = 0;
|
|
48
|
+
for (let index = 0; index < plan.length; index += 1) {
|
|
49
|
+
if (context.count >= context.limit) break;
|
|
50
|
+
const entry = plan[index];
|
|
51
|
+
const name = entry.key;
|
|
52
|
+
const item = value[name];
|
|
53
|
+
if (item === undefined) {
|
|
54
|
+
// Absent, or own and set to undefined: the same for an optional field,
|
|
55
|
+
// a required one asks which (the latter is a type error).
|
|
56
|
+
if (!entry.required) continue;
|
|
57
|
+
if (!Object.hasOwn(value, name)) {
|
|
58
|
+
issues.required(context, name);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
} else if (entry.own && !Object.hasOwn(value, name)) {
|
|
62
|
+
if (entry.required) issues.required(context, name);
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
found += 1;
|
|
66
|
+
entry.check(item, context, name);
|
|
67
|
+
}
|
|
68
|
+
if (context.unknown === 'reject' && context.count < context.limit) {
|
|
69
|
+
let total = 0;
|
|
70
|
+
// oxlint-disable-next-line no-unused-vars
|
|
71
|
+
for (const name in value) total += 1;
|
|
72
|
+
if (total !== found) scanUnknown(value, known, context);
|
|
73
|
+
}
|
|
74
|
+
if (nested) context.path.pop();
|
|
75
|
+
};
|
|
8
76
|
|
|
9
77
|
// A struct is a null-prototype dictionary of field name -> Type (or the
|
|
10
78
|
// function of a calculated field), not a class instance with methods: a field
|
|
@@ -13,51 +81,51 @@ const KEYS = Symbol.for('alexify.metaschema.keys');
|
|
|
13
81
|
// resolving to something on Object.prototype.
|
|
14
82
|
const createStruct = (defs, prep) => {
|
|
15
83
|
const fields = Object.create(null);
|
|
84
|
+
const plan = [];
|
|
85
|
+
const known = Object.create(null);
|
|
86
|
+
const calculated = [];
|
|
16
87
|
for (const key of Object.keys(defs)) {
|
|
17
88
|
const entry = defs[key];
|
|
18
89
|
const { field, required } = formatters.key(key, entry?.required);
|
|
19
90
|
try {
|
|
20
91
|
const { Type, defs: typeDefs } = prep.parse(entry);
|
|
21
92
|
if (!Type) {
|
|
22
|
-
fields[
|
|
93
|
+
fields[field] = entry;
|
|
94
|
+
if (!CODEC_KEYS.has(field)) {
|
|
95
|
+
known[field] = true;
|
|
96
|
+
calculated.push(field);
|
|
97
|
+
}
|
|
23
98
|
continue;
|
|
24
99
|
}
|
|
100
|
+
// The key ('tags?') and the definition ('?string', required: false)
|
|
101
|
+
// both decide whether the field is required, and the field's check is
|
|
102
|
+
// compiled with the answer, so it is settled before the field is built.
|
|
103
|
+
typeDefs.required = (typeDefs.required ?? true) && required;
|
|
25
104
|
const child = new Type(typeDefs, prep);
|
|
26
|
-
child.required &&= required;
|
|
27
105
|
fields[field] = child;
|
|
106
|
+
known[field] = true;
|
|
107
|
+
plan.push({
|
|
108
|
+
key: field,
|
|
109
|
+
type: child,
|
|
110
|
+
required: child.required,
|
|
111
|
+
own: field in Object.prototype,
|
|
112
|
+
check: child.check,
|
|
113
|
+
});
|
|
28
114
|
} catch (error) {
|
|
29
115
|
if (error instanceof SchemaDefinitionError) error.locate(prep.root.name, field);
|
|
30
116
|
throw error;
|
|
31
117
|
}
|
|
32
118
|
}
|
|
119
|
+
Object.freeze(plan);
|
|
120
|
+
Object.freeze(calculated);
|
|
121
|
+
const compiled = Object.freeze({ plan, known, calculated, check: compileStruct(plan, known) });
|
|
33
122
|
Object.defineProperty(fields, BRAND, { value: 'Struct' });
|
|
34
|
-
Object.defineProperty(fields,
|
|
123
|
+
Object.defineProperty(fields, STRUCT, { value: compiled });
|
|
35
124
|
return fields;
|
|
36
125
|
};
|
|
37
126
|
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (!isObject) return result.add(issue('type', path, 'not of expected type: object'));
|
|
42
|
-
const keys = fields[KEYS] || Object.keys(fields);
|
|
43
|
-
for (const name of keys) {
|
|
44
|
-
if (result.full) break;
|
|
45
|
-
const type = fields[name];
|
|
46
|
-
if (!hasBrand(type, 'Type')) continue;
|
|
47
|
-
const nestedPath = path ? `${path}.${name}` : name;
|
|
48
|
-
if (!Object.hasOwn(source, name)) {
|
|
49
|
-
if (type.required) result.add(issue('required', nestedPath, 'is required'));
|
|
50
|
-
continue;
|
|
51
|
-
}
|
|
52
|
-
result.add(type.check(source[name], nestedPath));
|
|
53
|
-
}
|
|
54
|
-
for (const name of Object.keys(source)) {
|
|
55
|
-
if (result.full) break;
|
|
56
|
-
if (name in fields) continue;
|
|
57
|
-
const nestedPath = path ? `${path}.${shorten(name)}` : shorten(name);
|
|
58
|
-
result.add(issue('unexpected', nestedPath, 'is not expected'));
|
|
59
|
-
}
|
|
60
|
-
return result;
|
|
61
|
-
};
|
|
127
|
+
const isStruct = (fields) => hasBrand(fields, 'Struct');
|
|
128
|
+
|
|
129
|
+
const checkOf = (fields) => fields[STRUCT].check;
|
|
62
130
|
|
|
63
|
-
module.exports = { createStruct,
|
|
131
|
+
module.exports = { STRUCT, createStruct, isStruct, checkOf };
|
package/src/types.js
CHANGED
|
@@ -4,6 +4,7 @@ const collections = require('./prototypes/collections.js');
|
|
|
4
4
|
const { reference } = require('./prototypes/reference.js');
|
|
5
5
|
const { schema } = require('./prototypes/schema.js');
|
|
6
6
|
const { tuple } = require('./prototypes/tuple.js');
|
|
7
|
+
const { union } = require('./prototypes/union.js');
|
|
7
8
|
const { json } = require('./prototypes/json.js');
|
|
8
9
|
const { SchemaDefinitionError } = require('./errors.js');
|
|
9
10
|
|
|
@@ -15,6 +16,7 @@ const PROTOTYPES = {
|
|
|
15
16
|
one: reference,
|
|
16
17
|
schema,
|
|
17
18
|
tuple,
|
|
19
|
+
union,
|
|
18
20
|
json,
|
|
19
21
|
};
|
|
20
22
|
|
|
@@ -23,13 +25,13 @@ const customTypeError = (name, reason) =>
|
|
|
23
25
|
|
|
24
26
|
const createType = (name, prototype) => {
|
|
25
27
|
class Type extends AbstractType {
|
|
26
|
-
|
|
28
|
+
// A null-prototype dictionary: a metadata key may be anything.
|
|
29
|
+
static metadata = Object.create(null);
|
|
27
30
|
static type = name;
|
|
28
31
|
static kind = prototype.kind;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
32
|
+
static rules = new Set(prototype.rules);
|
|
33
|
+
// The definition keys the type reads; the lint reports the others.
|
|
34
|
+
static options = new Set(prototype.options);
|
|
33
35
|
|
|
34
36
|
constructor(def, preprocessor) {
|
|
35
37
|
super(def, preprocessor);
|
|
@@ -37,8 +39,6 @@ const createType = (name, prototype) => {
|
|
|
37
39
|
this.type = type;
|
|
38
40
|
}
|
|
39
41
|
}
|
|
40
|
-
const { rules } = prototype;
|
|
41
|
-
if (rules) Type.setRules(rules);
|
|
42
42
|
Object.assign(Type.prototype, prototype, { type: name });
|
|
43
43
|
return Type;
|
|
44
44
|
};
|
|
@@ -58,16 +58,16 @@ const TYPES = createTypes(PROTOTYPES);
|
|
|
58
58
|
|
|
59
59
|
const createRegistry = () => createTypes(PROTOTYPES);
|
|
60
60
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const value = pair[1];
|
|
65
|
-
Type.assign(key, value);
|
|
66
|
-
}
|
|
61
|
+
// The own keys of the metadata given, as data: the table has no prototype.
|
|
62
|
+
const updateTypeMetadata = (Type, metadata) => {
|
|
63
|
+
Object.assign(Type.metadata, metadata);
|
|
67
64
|
};
|
|
68
65
|
|
|
66
|
+
// A custom prototype brings `construct` and `checkType`; a built-in one, reused
|
|
67
|
+
// through a `js` alias, compiles its own check instead.
|
|
69
68
|
const checkCustomType = (name, proto) => {
|
|
70
|
-
const {
|
|
69
|
+
const { construct } = proto;
|
|
70
|
+
const checkType = proto.checkType || proto.compile;
|
|
71
71
|
if (!checkType || !construct) {
|
|
72
72
|
throw customTypeError(name, 'must contain "construct" and "checkType" methods');
|
|
73
73
|
}
|
package/src/util.js
CHANGED
|
@@ -9,20 +9,36 @@ const INSPECT = Symbol.for('nodejs.util.inspect.custom');
|
|
|
9
9
|
|
|
10
10
|
const hasBrand = (value, name) => value?.[BRAND] === name;
|
|
11
11
|
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
|
|
15
|
-
const ancestors = new Set();
|
|
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$]*$/;
|
|
16
15
|
|
|
17
|
-
//
|
|
18
|
-
//
|
|
19
|
-
|
|
20
|
-
|
|
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
|
+
|
|
28
|
+
// The method that validates a value inside an existing context, shared by
|
|
29
|
+
// Schema and the reference type that checks its target through it. A global
|
|
30
|
+
// symbol like BRAND, so a schema from another copy of the package has it too.
|
|
31
|
+
const RUN = Symbol.for('alexify.metaschema.run');
|
|
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');
|
|
21
38
|
|
|
22
|
-
//
|
|
23
|
-
//
|
|
24
|
-
|
|
25
|
-
const issue = (code, path, detail) => ({ code, path, message: `Field "${path}" ${detail}` });
|
|
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;
|
|
26
42
|
|
|
27
43
|
// Key names taken from the value under check go into messages; a huge key
|
|
28
44
|
// must not become a huge message.
|
|
@@ -33,15 +49,13 @@ const shorten = (key) => {
|
|
|
33
49
|
return name.length > KEY_LIMIT ? `${name.slice(0, KEY_LIMIT)}...` : name;
|
|
34
50
|
};
|
|
35
51
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
'Rule "length" needs a number, [min, max] or { min, max }',
|
|
44
|
-
);
|
|
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
|
+
};
|
|
45
59
|
|
|
46
60
|
const formatters = {
|
|
47
61
|
type: (type, req = true) => {
|
|
@@ -57,41 +71,18 @@ const formatters = {
|
|
|
57
71
|
const field = key.slice(0, -1);
|
|
58
72
|
return { field, required: false };
|
|
59
73
|
},
|
|
60
|
-
|
|
61
|
-
length: (length) => {
|
|
62
|
-
if (typeof length === 'number') return { max: length };
|
|
63
|
-
if (length === null || typeof length !== 'object') throw invalidLength();
|
|
64
|
-
const min = Array.isArray(length) ? length[0] : length.min;
|
|
65
|
-
const max = Array.isArray(length) ? length[1] : length.max;
|
|
66
|
-
if (!isBound(min) || !isBound(max)) throw invalidLength();
|
|
67
|
-
return { min, max };
|
|
68
|
-
},
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
const checks = {
|
|
72
|
-
length: (src, type, path) => {
|
|
73
|
-
const { length, entries } = type;
|
|
74
|
-
const value = entries ? entries(src) : src;
|
|
75
|
-
const size = value?.size;
|
|
76
|
-
const count = value?.length;
|
|
77
|
-
let len = Number(value);
|
|
78
|
-
if (typeof size === 'number') len = size;
|
|
79
|
-
else if (typeof count === 'number') len = count;
|
|
80
|
-
const { min, max } = length;
|
|
81
|
-
if (min !== undefined && len < min) return issue('length', path, 'value is too short');
|
|
82
|
-
if (max !== undefined && len > max) return issue('length', path, 'exceeds the maximum length');
|
|
83
|
-
return null;
|
|
84
|
-
},
|
|
85
74
|
};
|
|
86
75
|
|
|
87
76
|
module.exports = {
|
|
88
77
|
BRAND,
|
|
89
78
|
INSPECT,
|
|
90
79
|
hasBrand,
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
80
|
+
IDENTIFIER,
|
|
81
|
+
define,
|
|
82
|
+
RUN,
|
|
83
|
+
HOOK,
|
|
84
|
+
ENUM_SET_SIZE,
|
|
94
85
|
shorten,
|
|
86
|
+
safeString,
|
|
95
87
|
formatters,
|
|
96
|
-
checks,
|
|
97
88
|
};
|