@alexify/metaschema 0.0.0-stage → 1.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 +452 -0
- package/LICENSE +22 -0
- package/README.md +178 -2
- package/SECURITY.md +60 -0
- package/browser.js +4 -0
- package/index.d.ts +226 -0
- package/index.js +1 -0
- package/package.json +90 -3
- package/src/errors.js +28 -0
- package/src/index.js +23 -0
- package/src/kinds.js +68 -0
- package/src/metadata.js +168 -0
- package/src/metautil.js +23 -0
- package/src/model.js +92 -0
- package/src/preprocessor.js +110 -0
- package/src/prototypes/abstract.js +84 -0
- package/src/prototypes/collections.js +98 -0
- package/src/prototypes/json.js +15 -0
- package/src/prototypes/reference.js +42 -0
- package/src/prototypes/scalars.js +44 -0
- package/src/prototypes/schema.js +31 -0
- package/src/prototypes/tuple.js +45 -0
- package/src/runtime/browser.js +6 -0
- package/src/runtime/node.js +5 -0
- package/src/schema.js +187 -0
- package/src/struct.js +63 -0
- package/src/types.js +127 -0
- package/src/util.js +97 -0
package/src/metautil.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Copied from metautil v5.5.2 (lib/strings.js, lib/objects.js)
|
|
2
|
+
// https://github.com/metarhia/metautil — MIT, Copyright (c) 2017-2026 Metarhia contributors
|
|
3
|
+
|
|
4
|
+
const inRange = (x, min, max) => x >= min && x <= max;
|
|
5
|
+
|
|
6
|
+
const isFirstUpper = (s) => !!s && inRange(s[0], 'A', 'Z');
|
|
7
|
+
|
|
8
|
+
const isFirstLower = (s) => !!s && inRange(s[0], 'a', 'z');
|
|
9
|
+
|
|
10
|
+
const isFirstLetter = (s) => isFirstUpper(s) || isFirstLower(s);
|
|
11
|
+
|
|
12
|
+
const toLowerCamel = (s) => s.charAt(0).toLowerCase() + s.slice(1);
|
|
13
|
+
|
|
14
|
+
const firstKey = (obj) => Object.keys(obj).find(isFirstLetter);
|
|
15
|
+
|
|
16
|
+
module.exports = {
|
|
17
|
+
inRange,
|
|
18
|
+
isFirstUpper,
|
|
19
|
+
isFirstLower,
|
|
20
|
+
isFirstLetter,
|
|
21
|
+
toLowerCamel,
|
|
22
|
+
firstKey,
|
|
23
|
+
};
|
package/src/model.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
const { firstKey } = require('./metautil.js');
|
|
2
|
+
|
|
3
|
+
const { Schema } = require('./schema.js');
|
|
4
|
+
const { TYPES, createRegistry, typeFactory } = require('./types.js');
|
|
5
|
+
const { SchemaDefinitionError } = require('./errors.js');
|
|
6
|
+
|
|
7
|
+
const REGISTRIES = ['shared', 'isolated'];
|
|
8
|
+
|
|
9
|
+
const registryFor = (options) => {
|
|
10
|
+
const { registry = 'shared' } = options;
|
|
11
|
+
if (!REGISTRIES.includes(registry)) {
|
|
12
|
+
const reason = `Model option "registry" must be "shared" or "isolated", got ${JSON.stringify(registry)}`;
|
|
13
|
+
throw new SchemaDefinitionError('ERR_INVALID_OPTIONS', reason);
|
|
14
|
+
}
|
|
15
|
+
return registry === 'isolated' ? createRegistry() : TYPES;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
class Model {
|
|
19
|
+
constructor(types, entities, database = null, options = {}) {
|
|
20
|
+
this.types = typeFactory(types, registryFor(options));
|
|
21
|
+
this.entities = new Map();
|
|
22
|
+
this.database = database;
|
|
23
|
+
this.order = new Set();
|
|
24
|
+
this.warnings = [];
|
|
25
|
+
const projections = [];
|
|
26
|
+
for (const pair of entities) {
|
|
27
|
+
const name = pair[0];
|
|
28
|
+
const entity = pair[1];
|
|
29
|
+
const first = firstKey(entity);
|
|
30
|
+
const isProjection = first === 'Projection';
|
|
31
|
+
if (isProjection) {
|
|
32
|
+
projections.push({ name, entity });
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const schema = new Schema(name, entity, [this]);
|
|
36
|
+
this.entities.set(name, schema);
|
|
37
|
+
}
|
|
38
|
+
for (const projection of projections) {
|
|
39
|
+
const { name, entity } = projection;
|
|
40
|
+
const schema = new Schema(name, entity, [this]);
|
|
41
|
+
this.entities.set(name, schema);
|
|
42
|
+
}
|
|
43
|
+
this.#preprocess();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
#preprocess() {
|
|
47
|
+
const { entities, order } = this;
|
|
48
|
+
for (const pair of entities) {
|
|
49
|
+
const name = pair[0];
|
|
50
|
+
const entity = pair[1];
|
|
51
|
+
if (name.startsWith('.')) continue;
|
|
52
|
+
const warn = entity.checkConsistency();
|
|
53
|
+
this.warnings.push(...warn);
|
|
54
|
+
}
|
|
55
|
+
if (entities.has('Identifier')) order.add('Identifier');
|
|
56
|
+
for (const name of entities.keys()) {
|
|
57
|
+
const isMeta = name.startsWith('.');
|
|
58
|
+
if (!isMeta) this.#reorderEntity(name, new Set());
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Depth-first ordering: an entity is added after everything it references.
|
|
63
|
+
// `visiting` holds the current path, so a reference back into it is a cycle
|
|
64
|
+
// of any length, not only one through the entity the walk started from.
|
|
65
|
+
#reorderEntity(name, visiting) {
|
|
66
|
+
const entity = this.entities.get(name);
|
|
67
|
+
if (!entity || this.order.has(name)) return;
|
|
68
|
+
visiting.add(name);
|
|
69
|
+
for (const ref of entity.references) {
|
|
70
|
+
if (ref === name) continue;
|
|
71
|
+
if (visiting.has(ref)) {
|
|
72
|
+
this.warnings.push(`Warning: "${name}" depends on "${ref}" recursively`);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
this.#reorderEntity(ref, visiting);
|
|
76
|
+
}
|
|
77
|
+
visiting.delete(name);
|
|
78
|
+
this.order.add(name);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
get dts() {
|
|
82
|
+
const { entities, order } = this;
|
|
83
|
+
const dts = [];
|
|
84
|
+
for (const name of order) {
|
|
85
|
+
const schema = entities.get(name);
|
|
86
|
+
dts.push(schema.toInterface());
|
|
87
|
+
}
|
|
88
|
+
return `${dts.join('\n\n')}\n`;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
module.exports = { Model };
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
const { isFirstUpper, toLowerCamel, firstKey } = require('./metautil.js');
|
|
2
|
+
|
|
3
|
+
const { formatters } = require('./util.js');
|
|
4
|
+
const { SchemaDefinitionError } = require('./errors.js');
|
|
5
|
+
|
|
6
|
+
const PARSERS = {
|
|
7
|
+
string: ['stringShorthand'],
|
|
8
|
+
object: ['schemaInstance', 'schemaWithKind', 'typeLongForm', 'typeShorthand', 'kindlessSchema'],
|
|
9
|
+
function: ['functionField'],
|
|
10
|
+
array: ['tupleShorthand'],
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const sourceType = (src) => {
|
|
14
|
+
if (src === null) return 'null';
|
|
15
|
+
if (Array.isArray(src)) return 'array';
|
|
16
|
+
return typeof src;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
class Preprocessor {
|
|
20
|
+
constructor(root) {
|
|
21
|
+
this.Schema = root.constructor;
|
|
22
|
+
this.root = root;
|
|
23
|
+
this.types = root.types;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
parse(source) {
|
|
27
|
+
const srcType = sourceType(source);
|
|
28
|
+
const parsers = PARSERS[srcType];
|
|
29
|
+
if (parsers) {
|
|
30
|
+
for (const name of parsers) {
|
|
31
|
+
const result = this[name](source);
|
|
32
|
+
if (result) return result;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const msg = `Invalid definition: "${source}" of type ${srcType}`;
|
|
36
|
+
throw new SchemaDefinitionError('ERR_INVALID_DEFINITION', msg);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
schemaInstance(source) {
|
|
40
|
+
const { types } = this;
|
|
41
|
+
const schema = this.Schema.extractSchema(source);
|
|
42
|
+
if (!schema) return null;
|
|
43
|
+
this.root.updateFromSchema(schema);
|
|
44
|
+
const { fields } = schema;
|
|
45
|
+
const defs = { schema: fields };
|
|
46
|
+
return { Type: types.schema, defs };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
stringShorthand(source) {
|
|
50
|
+
return this.typeLongForm({ type: source });
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
typeLongForm(source) {
|
|
54
|
+
if (!source.type) return null;
|
|
55
|
+
const { types } = this;
|
|
56
|
+
const parsed = formatters.type(source.type, source.required);
|
|
57
|
+
const { type, required } = parsed;
|
|
58
|
+
if (isFirstUpper(type)) {
|
|
59
|
+
const defs = { one: type, required, ...source };
|
|
60
|
+
return { Type: types.reference, defs };
|
|
61
|
+
}
|
|
62
|
+
if (!types[type]) {
|
|
63
|
+
throw new SchemaDefinitionError('ERR_UNKNOWN_TYPE', `Unknown type "${type}"`);
|
|
64
|
+
}
|
|
65
|
+
const defs = { required, ...source };
|
|
66
|
+
return { Type: types[type], defs };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
typeShorthand(source) {
|
|
70
|
+
const { types } = this;
|
|
71
|
+
const first = firstKey(source);
|
|
72
|
+
const parsed = formatters.key(first, source.required);
|
|
73
|
+
const type = parsed.field;
|
|
74
|
+
const required = parsed.required;
|
|
75
|
+
if (!types[type]) return null;
|
|
76
|
+
const { [first]: def, ...rest } = source;
|
|
77
|
+
const defs = { type, [type]: def, required, ...rest };
|
|
78
|
+
return { Type: types[type], defs };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
schemaWithKind(source) {
|
|
82
|
+
const { types, root } = this;
|
|
83
|
+
const first = firstKey(source);
|
|
84
|
+
if (!isFirstUpper(first)) return null;
|
|
85
|
+
const { [first]: meta, ...fields } = source;
|
|
86
|
+
const kind = toLowerCamel(first);
|
|
87
|
+
const kindMeta = { kind, meta, root };
|
|
88
|
+
const defs = { schema: fields };
|
|
89
|
+
return { Type: types.schema, defs, kindMeta };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
kindlessSchema(source) {
|
|
93
|
+
const { types } = this;
|
|
94
|
+
const kindMeta = { kind: 'struct' };
|
|
95
|
+
const defs = { schema: source };
|
|
96
|
+
return { Type: types.schema, defs, kindMeta };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
tupleShorthand(source) {
|
|
100
|
+
const { types } = this;
|
|
101
|
+
const defs = { value: source, required: true };
|
|
102
|
+
return { Type: types.tuple, defs };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
functionField() {
|
|
106
|
+
return {};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
module.exports = { Preprocessor };
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
const { ValidationResult } = require('../metadata.js');
|
|
2
|
+
const { BRAND, INSPECT, ancestors, issue, formatters, checks } = require('../util.js');
|
|
3
|
+
const { SchemaDefinitionError } = require('../errors.js');
|
|
4
|
+
|
|
5
|
+
// Keys of a field definition become properties of the field, so a key that
|
|
6
|
+
// names one of its methods (or the prototype itself) would replace it.
|
|
7
|
+
const RESERVED = new Set(['__proto__', 'prototype']);
|
|
8
|
+
|
|
9
|
+
const reservedKey = (key) =>
|
|
10
|
+
new SchemaDefinitionError('ERR_RESERVED_KEY', `Key "${key}" is reserved in a field definition`);
|
|
11
|
+
|
|
12
|
+
class AbstractType {
|
|
13
|
+
static checks = Object.create(null);
|
|
14
|
+
static formatters = Object.create(null);
|
|
15
|
+
|
|
16
|
+
// The rule checks that apply to this field, chosen once at construction:
|
|
17
|
+
// `length` only matters to a field that has a length, and check is the hot
|
|
18
|
+
// path.
|
|
19
|
+
#rules;
|
|
20
|
+
|
|
21
|
+
static setRules(rules = []) {
|
|
22
|
+
for (const rule of rules) {
|
|
23
|
+
if (formatters[rule]) AbstractType.formatters[rule] = formatters[rule];
|
|
24
|
+
if (checks[rule]) AbstractType.checks[rule] = checks[rule];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
constructor(def, preprocessor) {
|
|
29
|
+
this.root = preprocessor.root;
|
|
30
|
+
const { formatters: typeFormatters } = AbstractType;
|
|
31
|
+
for (const pair of Object.entries(def)) {
|
|
32
|
+
const key = pair[0];
|
|
33
|
+
const value = pair[1];
|
|
34
|
+
if (key === 'type' || key === this.type) continue;
|
|
35
|
+
if (RESERVED.has(key) || typeof this[key] === 'function') throw reservedKey(key);
|
|
36
|
+
if (typeFormatters[key]) this[key] = typeFormatters[key](value);
|
|
37
|
+
else this[key] = value;
|
|
38
|
+
}
|
|
39
|
+
this.construct(def, preprocessor);
|
|
40
|
+
if (this.type) this.root.references.add(this.type);
|
|
41
|
+
const rules = [];
|
|
42
|
+
for (const name of Object.keys(AbstractType.checks)) {
|
|
43
|
+
if (this[name]) rules.push(AbstractType.checks[name]);
|
|
44
|
+
}
|
|
45
|
+
this.#rules = rules;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
check(value, path) {
|
|
49
|
+
const result = new ValidationResult(path);
|
|
50
|
+
const isEmpty = value === null || value === undefined;
|
|
51
|
+
if (!this.required && isEmpty) return result;
|
|
52
|
+
const isObject = typeof value === 'object' && value !== null;
|
|
53
|
+
if (isObject && ancestors.has(value)) {
|
|
54
|
+
return result.add(issue('circular', path, 'is a circular reference'));
|
|
55
|
+
}
|
|
56
|
+
if (isObject) ancestors.add(value);
|
|
57
|
+
try {
|
|
58
|
+
result.add(this.checkType(value, path), 'type');
|
|
59
|
+
if (this.validate && !result.full) result.add(this.validate(value, path));
|
|
60
|
+
for (const rule of this.#rules) {
|
|
61
|
+
if (result.full) break;
|
|
62
|
+
result.add(rule(value, this, path));
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
} catch (error) {
|
|
66
|
+
return result.add(issue('exception', path, `validation failed ${error}`));
|
|
67
|
+
} finally {
|
|
68
|
+
if (isObject) ancestors.delete(value);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
toJSON() {
|
|
73
|
+
const { root, ...rest } = this;
|
|
74
|
+
return rest;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
[INSPECT](depth, options, inspect) {
|
|
78
|
+
return inspect(this.toJSON(), options);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
AbstractType.prototype[BRAND] = 'Type';
|
|
83
|
+
|
|
84
|
+
module.exports = { AbstractType };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
const { ValidationResult } = require('../metadata.js');
|
|
2
|
+
const { issue, shorten } = require('../util.js');
|
|
3
|
+
|
|
4
|
+
const object = {
|
|
5
|
+
rules: ['length'],
|
|
6
|
+
kind: 'struct',
|
|
7
|
+
|
|
8
|
+
construct(def, prep) {
|
|
9
|
+
const { type } = this;
|
|
10
|
+
const { [type]: short, key, value } = def;
|
|
11
|
+
const pair = short ? Object.entries(short)[0] : [key, value];
|
|
12
|
+
const keyType = pair[0];
|
|
13
|
+
const valueDef = pair[1];
|
|
14
|
+
this.key = keyType;
|
|
15
|
+
const { Type, defs } = prep.parse(valueDef);
|
|
16
|
+
this.value = new Type(defs, prep);
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
checkType(source, path) {
|
|
20
|
+
if (!this.isInstance(source)) {
|
|
21
|
+
return issue('type', path, `not of expected type: ${this.type}`);
|
|
22
|
+
}
|
|
23
|
+
const entries = this.entries(source);
|
|
24
|
+
if (entries.length === 0 && this.required) return issue('required', path, 'is required');
|
|
25
|
+
const result = new ValidationResult(path);
|
|
26
|
+
for (const pair of entries) {
|
|
27
|
+
if (result.full) break;
|
|
28
|
+
const field = pair[0];
|
|
29
|
+
const fieldValue = pair[1];
|
|
30
|
+
// oxlint-disable-next-line valid-typeof
|
|
31
|
+
if (typeof field !== this.key) {
|
|
32
|
+
return result.add(issue('type', path, `keys must be of type ${this.key}`));
|
|
33
|
+
}
|
|
34
|
+
result.add(this.value.check(fieldValue, `${path}.${shorten(field)}`));
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
isInstance(value) {
|
|
40
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
entries(value) {
|
|
44
|
+
return Object.entries(value);
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const map = {
|
|
49
|
+
...object,
|
|
50
|
+
|
|
51
|
+
isInstance(value) {
|
|
52
|
+
return value?.constructor?.name === 'Map';
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
entries(value) {
|
|
56
|
+
return [...value.entries()];
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const array = {
|
|
61
|
+
kind: 'struct',
|
|
62
|
+
rules: ['length'],
|
|
63
|
+
|
|
64
|
+
construct(def, prep) {
|
|
65
|
+
const { type } = this;
|
|
66
|
+
const source = def[type] || def.value;
|
|
67
|
+
const { Type, defs } = prep.parse(source);
|
|
68
|
+
this.value = new Type(defs, prep);
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
checkType(source, path) {
|
|
72
|
+
if (!this.isInstance(source)) {
|
|
73
|
+
return issue('type', path, `not of expected type: ${this.type}`);
|
|
74
|
+
}
|
|
75
|
+
// A Set is copied to index it; an array is walked as it is.
|
|
76
|
+
const value = Array.isArray(source) ? source : [...source];
|
|
77
|
+
const result = new ValidationResult(path);
|
|
78
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
79
|
+
if (result.full) break;
|
|
80
|
+
result.add(this.value.check(value[index], `${path}[${index}]`));
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
isInstance(value) {
|
|
86
|
+
return Array.isArray(value);
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const set = {
|
|
91
|
+
...array,
|
|
92
|
+
|
|
93
|
+
isInstance(value) {
|
|
94
|
+
return value?.constructor?.name === 'Set';
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
module.exports = { object, map, array, set };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const { issue } = require('../util.js');
|
|
2
|
+
|
|
3
|
+
const json = {
|
|
4
|
+
kind: 'struct',
|
|
5
|
+
|
|
6
|
+
construct() {},
|
|
7
|
+
|
|
8
|
+
checkType(value, path) {
|
|
9
|
+
const isObject = value !== null && typeof value === 'object';
|
|
10
|
+
if (!isObject) return issue('type', path, 'not of expected type: object');
|
|
11
|
+
return null;
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
module.exports = { json };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const { SchemaDefinitionError } = require('../errors.js');
|
|
2
|
+
const { ValidationResult } = require('../metadata.js');
|
|
3
|
+
const { issue } = require('../util.js');
|
|
4
|
+
|
|
5
|
+
const reference = {
|
|
6
|
+
kind: 'struct',
|
|
7
|
+
|
|
8
|
+
construct(def) {
|
|
9
|
+
const key = def.many === undefined ? 'one' : 'many';
|
|
10
|
+
const target = def[key];
|
|
11
|
+
if (typeof target !== 'string' || target === '') {
|
|
12
|
+
const reason = `Reference "${key}" needs an entity name, got ${JSON.stringify(target)}`;
|
|
13
|
+
throw new SchemaDefinitionError('ERR_INVALID_REFERENCE', reason);
|
|
14
|
+
}
|
|
15
|
+
const relation = key === 'many' ? 'many-to-one' : 'one-to-many';
|
|
16
|
+
this[key] = target;
|
|
17
|
+
this.type = target;
|
|
18
|
+
this.root.relations.add({ to: target, type: relation });
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
checkType(source, path) {
|
|
22
|
+
const { one, many, root } = this;
|
|
23
|
+
if (one) {
|
|
24
|
+
const schema = root.findReference(one);
|
|
25
|
+
if (!schema) return issue('reference', path, `Entity "${one}" is not found`);
|
|
26
|
+
return schema.check(source, path);
|
|
27
|
+
}
|
|
28
|
+
const schema = root.findReference(many);
|
|
29
|
+
if (!schema) return issue('reference', path, `Entity "${many}" is not found`);
|
|
30
|
+
if (!Array.isArray(source)) {
|
|
31
|
+
return issue('type', path, `not of expected type: array of ${many}`);
|
|
32
|
+
}
|
|
33
|
+
const result = new ValidationResult(path);
|
|
34
|
+
for (let index = 0; index < source.length; index += 1) {
|
|
35
|
+
if (result.full) break;
|
|
36
|
+
result.add(schema.check(source[index], `${path}[${index}]`));
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
module.exports = { reference };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
const { SchemaDefinitionError } = require('../errors.js');
|
|
2
|
+
const { issue } = require('../util.js');
|
|
3
|
+
|
|
4
|
+
const missingEnum = (type) => {
|
|
5
|
+
const hint = `Type "${type}" needs a list of values: { type: '${type}', enum: [...] }`;
|
|
6
|
+
return new SchemaDefinitionError('ERR_INVALID_ENUM', hint);
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const scalar = {
|
|
10
|
+
kind: 'scalar',
|
|
11
|
+
|
|
12
|
+
construct() {},
|
|
13
|
+
|
|
14
|
+
checkType(value, path) {
|
|
15
|
+
// oxlint-disable-next-line valid-typeof
|
|
16
|
+
if (typeof value !== this.scalar) {
|
|
17
|
+
return issue('type', path, `not of expected type: ${this.scalar}`);
|
|
18
|
+
}
|
|
19
|
+
return null;
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const enumerable = {
|
|
24
|
+
kind: 'scalar',
|
|
25
|
+
|
|
26
|
+
construct(def) {
|
|
27
|
+
const values = def.enum;
|
|
28
|
+
if (!Array.isArray(values) || values.length === 0) throw missingEnum(this.type);
|
|
29
|
+
this.enum = values;
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
checkType(value, path) {
|
|
33
|
+
if (this.enum.includes(value)) return null;
|
|
34
|
+
const variants = this.enum.join(', ');
|
|
35
|
+
return issue('enum', path, `value is not of enum: ${variants}`);
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const string = { scalar: 'string', rules: ['length'], ...scalar };
|
|
40
|
+
const number = { scalar: 'number', rules: ['length'], ...scalar };
|
|
41
|
+
const bigint = { scalar: 'bigint', rules: ['length'], ...scalar };
|
|
42
|
+
const boolean = { scalar: 'boolean', ...scalar };
|
|
43
|
+
|
|
44
|
+
module.exports = { string, number, bigint, boolean, enum: enumerable };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const { hasBrand } = require('../util.js');
|
|
2
|
+
const { createStruct, checkStruct } = require('../struct.js');
|
|
3
|
+
const { SchemaDefinitionError } = require('../errors.js');
|
|
4
|
+
|
|
5
|
+
const missingSchema = (type) => {
|
|
6
|
+
const hint = `Type "${type}" needs a schema definition: { type: '${type}', schema: { ... } }`;
|
|
7
|
+
return new SchemaDefinitionError('ERR_MISSING_SCHEMA', hint);
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const isDefinition = (value) =>
|
|
11
|
+
value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
12
|
+
|
|
13
|
+
const schema = {
|
|
14
|
+
kind: 'struct',
|
|
15
|
+
|
|
16
|
+
construct(defs, prep) {
|
|
17
|
+
const { schema: definition, required } = defs;
|
|
18
|
+
if (!isDefinition(definition)) throw missingSchema(this.type);
|
|
19
|
+
this.required = required ?? true;
|
|
20
|
+
const isStruct = hasBrand(definition, 'Struct');
|
|
21
|
+
if (isStruct) this.schema = definition;
|
|
22
|
+
else this.schema = createStruct(definition, prep);
|
|
23
|
+
this.validate = defs.schema.validate || undefined;
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
checkType(source, path = '') {
|
|
27
|
+
return checkStruct(this.schema, source, path);
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
module.exports = { schema };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const { SchemaDefinitionError } = require('../errors.js');
|
|
2
|
+
const { ValidationResult } = require('../metadata.js');
|
|
3
|
+
const { issue } = require('../util.js');
|
|
4
|
+
|
|
5
|
+
const notScalar = (element) => {
|
|
6
|
+
const shown = typeof element === 'string' ? `"${element}"` : JSON.stringify(element);
|
|
7
|
+
const reason = `Tuple element ${shown} is not a scalar type`;
|
|
8
|
+
return new SchemaDefinitionError('ERR_INVALID_TUPLE', reason);
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const tuple = {
|
|
12
|
+
kind: 'struct',
|
|
13
|
+
|
|
14
|
+
construct(def, prep) {
|
|
15
|
+
const elements = def.value || def.tuple;
|
|
16
|
+
this.value = elements.map((element) => {
|
|
17
|
+
const named = typeof element !== 'string';
|
|
18
|
+
const pair = named ? Object.entries(element)[0] : [null, element];
|
|
19
|
+
const name = pair[0];
|
|
20
|
+
const scalar = pair[1];
|
|
21
|
+
const { Type, defs } = prep.parse(scalar);
|
|
22
|
+
if (!Type || Type.kind !== 'scalar') throw notScalar(scalar);
|
|
23
|
+
const type = new Type(defs, prep);
|
|
24
|
+
if (name) type.name = name;
|
|
25
|
+
return type;
|
|
26
|
+
});
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
checkType(source, path) {
|
|
30
|
+
if (!Array.isArray(source)) return issue('type', path, `not of expected type: ${this.type}`);
|
|
31
|
+
if (source.length > this.value.length) {
|
|
32
|
+
return issue('length', path, 'value length is more than expected in tuple');
|
|
33
|
+
}
|
|
34
|
+
const result = new ValidationResult(path);
|
|
35
|
+
for (let index = 0; index < this.value.length; index += 1) {
|
|
36
|
+
if (result.full) break;
|
|
37
|
+
const scalar = this.value[index];
|
|
38
|
+
const itemName = scalar.name || 'item';
|
|
39
|
+
result.add(scalar.check(source[index], `${path}(${itemName}${index})`));
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
module.exports = { tuple };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Browser twin of ./node.js: the same interface, mapped in by the package.json
|
|
2
|
+
// `browser` field. There is no file system to write to, so it rejects.
|
|
3
|
+
|
|
4
|
+
const saveTypes = () => Promise.reject(new Error('saveTypes is not available in the browser'));
|
|
5
|
+
|
|
6
|
+
module.exports = { saveTypes };
|