@alexify/metaschema 0.0.0-stage → 2.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 +679 -0
- package/LICENSE +22 -0
- package/README.md +274 -2
- package/SECURITY.md +62 -0
- package/browser.js +4 -0
- package/index.d.ts +765 -0
- package/index.js +1 -0
- package/package.json +99 -3
- package/src/context.js +95 -0
- package/src/errors.js +28 -0
- package/src/index.js +23 -0
- package/src/issues.js +196 -0
- package/src/kinds.js +68 -0
- package/src/lint.js +90 -0
- package/src/locales/en.d.ts +5 -0
- package/src/locales/en.js +58 -0
- package/src/locales/uk.d.ts +5 -0
- package/src/locales/uk.js +57 -0
- package/src/metadata.js +117 -0
- package/src/metautil.js +23 -0
- package/src/model.js +108 -0
- package/src/preprocessor.js +133 -0
- package/src/prototypes/abstract.js +121 -0
- package/src/prototypes/collections.js +146 -0
- package/src/prototypes/json.js +19 -0
- package/src/prototypes/reference.js +99 -0
- package/src/prototypes/scalars.js +154 -0
- package/src/prototypes/schema.js +44 -0
- package/src/prototypes/tuple.js +68 -0
- package/src/prototypes/union.js +100 -0
- package/src/result.js +124 -0
- package/src/rules.js +156 -0
- package/src/runtime/browser.js +6 -0
- package/src/runtime/node.js +5 -0
- package/src/schema.js +281 -0
- package/src/struct.js +120 -0
- package/src/types.js +133 -0
- package/src/util.js +49 -0
package/src/types.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
const { AbstractType } = require('./prototypes/abstract.js');
|
|
2
|
+
const scalars = require('./prototypes/scalars.js');
|
|
3
|
+
const collections = require('./prototypes/collections.js');
|
|
4
|
+
const { reference } = require('./prototypes/reference.js');
|
|
5
|
+
const { schema } = require('./prototypes/schema.js');
|
|
6
|
+
const { tuple } = require('./prototypes/tuple.js');
|
|
7
|
+
const { union } = require('./prototypes/union.js');
|
|
8
|
+
const { json } = require('./prototypes/json.js');
|
|
9
|
+
const { SchemaDefinitionError } = require('./errors.js');
|
|
10
|
+
|
|
11
|
+
const PROTOTYPES = {
|
|
12
|
+
...scalars,
|
|
13
|
+
...collections,
|
|
14
|
+
reference,
|
|
15
|
+
many: reference,
|
|
16
|
+
one: reference,
|
|
17
|
+
schema,
|
|
18
|
+
tuple,
|
|
19
|
+
union,
|
|
20
|
+
json,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const customTypeError = (name, reason) =>
|
|
24
|
+
new SchemaDefinitionError('ERR_INVALID_CUSTOM_TYPE', `Custom type "${name}" ${reason}`);
|
|
25
|
+
|
|
26
|
+
const createType = (name, prototype) => {
|
|
27
|
+
class Type extends AbstractType {
|
|
28
|
+
static metadata = {};
|
|
29
|
+
static type = name;
|
|
30
|
+
static kind = prototype.kind;
|
|
31
|
+
static rules = new Set(prototype.rules);
|
|
32
|
+
// The definition keys the type reads; the lint reports the others.
|
|
33
|
+
static options = new Set(prototype.options);
|
|
34
|
+
|
|
35
|
+
static assign(key, value) {
|
|
36
|
+
this.metadata[key] = value;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
constructor(def, preprocessor) {
|
|
40
|
+
super(def, preprocessor);
|
|
41
|
+
const { type } = this;
|
|
42
|
+
this.type = type;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
Object.assign(Type.prototype, prototype, { type: name });
|
|
46
|
+
return Type;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const createTypes = (prototypes) => {
|
|
50
|
+
const types = Object.create(null);
|
|
51
|
+
for (const pair of Object.entries(prototypes)) {
|
|
52
|
+
const name = pair[0];
|
|
53
|
+
const proto = pair[1];
|
|
54
|
+
types[name] = createType(name, proto);
|
|
55
|
+
}
|
|
56
|
+
return types;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// The shared, process-wide registry. A model may ask for its own instead.
|
|
60
|
+
const TYPES = createTypes(PROTOTYPES);
|
|
61
|
+
|
|
62
|
+
const createRegistry = () => createTypes(PROTOTYPES);
|
|
63
|
+
|
|
64
|
+
const updateTypeMetadata = (Type, metadata = {}) => {
|
|
65
|
+
for (const pair of Object.entries(metadata)) {
|
|
66
|
+
const key = pair[0];
|
|
67
|
+
const value = pair[1];
|
|
68
|
+
Type.assign(key, value);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
// A custom prototype brings `construct` and `checkType`; a built-in one, reused
|
|
73
|
+
// through a `js` alias, compiles its own check instead.
|
|
74
|
+
const checkCustomType = (name, proto) => {
|
|
75
|
+
const { construct } = proto;
|
|
76
|
+
const checkType = proto.checkType || proto.compile;
|
|
77
|
+
if (!checkType || !construct) {
|
|
78
|
+
throw customTypeError(name, 'must contain "construct" and "checkType" methods');
|
|
79
|
+
}
|
|
80
|
+
const checkIsFn = typeof checkType === 'function';
|
|
81
|
+
const constructIsFn = typeof construct === 'function';
|
|
82
|
+
if (!checkIsFn || !constructIsFn) {
|
|
83
|
+
throw customTypeError(name, 'methods "construct" and "checkType" must be functions');
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// The same table is usually registered by every Model in the process, so an
|
|
88
|
+
// entry that repeats the definition a type was created from (same `js`, same
|
|
89
|
+
// `construct` and `checkType` functions) or only adds metadata is accepted; a
|
|
90
|
+
// different definition for a registered name is an error, not a silent no-op.
|
|
91
|
+
const redefines = (Type, { js, construct, checkType }) => {
|
|
92
|
+
const metadataOnly = js === undefined && construct === undefined && checkType === undefined;
|
|
93
|
+
if (metadataOnly) return false;
|
|
94
|
+
const source = Type.source || {};
|
|
95
|
+
return js !== source.js || construct !== source.construct || checkType !== source.checkType;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const typeFactory = (customTypes, registry = TYPES) => {
|
|
99
|
+
for (const pair of Object.entries(customTypes)) {
|
|
100
|
+
const name = pair[0];
|
|
101
|
+
const value = pair[1];
|
|
102
|
+
if (value === null || typeof value !== 'object') {
|
|
103
|
+
throw customTypeError(name, 'must be an object with methods "construct" and "checkType"');
|
|
104
|
+
}
|
|
105
|
+
const { js, metadata, ...rest } = value;
|
|
106
|
+
const registered = registry[name];
|
|
107
|
+
if (registered) {
|
|
108
|
+
if (redefines(registered, value)) {
|
|
109
|
+
const reason = `Type "${name}" is already registered; only { metadata } may be added`;
|
|
110
|
+
throw new SchemaDefinitionError('ERR_TYPE_REGISTERED', reason);
|
|
111
|
+
}
|
|
112
|
+
updateTypeMetadata(registered, metadata);
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
let proto = rest;
|
|
116
|
+
if (js !== undefined) {
|
|
117
|
+
const base = registry[js];
|
|
118
|
+
if (!base) {
|
|
119
|
+
const reason = `Unknown js type "${js}" for custom type "${name}"`;
|
|
120
|
+
throw new SchemaDefinitionError('ERR_UNKNOWN_JS_TYPE', reason);
|
|
121
|
+
}
|
|
122
|
+
proto = base.prototype;
|
|
123
|
+
}
|
|
124
|
+
checkCustomType(name, proto);
|
|
125
|
+
const Type = createType(name, proto);
|
|
126
|
+
Type.source = { js, construct: rest.construct, checkType: rest.checkType };
|
|
127
|
+
updateTypeMetadata(Type, metadata);
|
|
128
|
+
registry[name] = Type;
|
|
129
|
+
}
|
|
130
|
+
return registry;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
module.exports = { TYPES, createRegistry, typeFactory };
|
package/src/util.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// Identity brand for the package's own classes. A global symbol is shared by every copy of the
|
|
2
|
+
// package in a process and by every realm, and it survives bundlers that rename a class
|
|
3
|
+
// (`class _Schema`) and minifiers that mangle it, none of which `constructor.name` does.
|
|
4
|
+
const BRAND = Symbol.for('alexify.metaschema.brand');
|
|
5
|
+
|
|
6
|
+
// Node's util.inspect looks this symbol up; a plain Symbol.for keeps the
|
|
7
|
+
// browser build free of any Node import.
|
|
8
|
+
const INSPECT = Symbol.for('nodejs.util.inspect.custom');
|
|
9
|
+
|
|
10
|
+
const hasBrand = (value, name) => value?.[BRAND] === name;
|
|
11
|
+
|
|
12
|
+
// The method that validates a value inside an existing context, shared by
|
|
13
|
+
// Schema and the reference type that checks its target through it. A global
|
|
14
|
+
// symbol like BRAND, so a schema from another copy of the package has it too.
|
|
15
|
+
const RUN = Symbol.for('alexify.metaschema.run');
|
|
16
|
+
|
|
17
|
+
// Key names taken from the value under check go into messages; a huge key
|
|
18
|
+
// must not become a huge message.
|
|
19
|
+
const KEY_LIMIT = 100;
|
|
20
|
+
|
|
21
|
+
const shorten = (key) => {
|
|
22
|
+
const name = String(key);
|
|
23
|
+
return name.length > KEY_LIMIT ? `${name.slice(0, KEY_LIMIT)}...` : name;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const formatters = {
|
|
27
|
+
type: (type, req = true) => {
|
|
28
|
+
const required = !type.startsWith('?');
|
|
29
|
+
if (required) return { type, required: req };
|
|
30
|
+
const name = type.substring(1);
|
|
31
|
+
return { type: name, required: false };
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
key: (key, req = true) => {
|
|
35
|
+
const required = !key.endsWith('?');
|
|
36
|
+
if (required) return { field: key, required: req };
|
|
37
|
+
const field = key.slice(0, -1);
|
|
38
|
+
return { field, required: false };
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
module.exports = {
|
|
43
|
+
BRAND,
|
|
44
|
+
INSPECT,
|
|
45
|
+
hasBrand,
|
|
46
|
+
RUN,
|
|
47
|
+
shorten,
|
|
48
|
+
formatters,
|
|
49
|
+
};
|