@alexify/metaschema 1.0.0 → 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 +228 -1
- package/README.md +124 -28
- package/SECURITY.md +6 -4
- package/index.d.ts +558 -19
- package/package.json +11 -2
- package/src/context.js +95 -0
- package/src/index.js +1 -1
- package/src/issues.js +196 -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 +25 -76
- package/src/model.js +23 -7
- package/src/preprocessor.js +42 -19
- package/src/prototypes/abstract.js +86 -49
- package/src/prototypes/collections.js +86 -38
- package/src/prototypes/json.js +9 -5
- package/src/prototypes/reference.js +77 -20
- package/src/prototypes/scalars.js +126 -16
- package/src/prototypes/schema.js +19 -6
- package/src/prototypes/tuple.js +52 -29
- package/src/prototypes/union.js +100 -0
- package/src/result.js +124 -0
- package/src/rules.js +156 -0
- package/src/schema.js +133 -39
- package/src/struct.js +89 -32
- package/src/types.js +9 -3
- package/src/util.js +5 -53
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alexify/metaschema",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Zero-dependency metadata schema and interface definition language (IDL) for Node.js and browsers — data validation, domain models and TypeScript generation",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Alex Dolid <dolid.sasha@gmail.com>",
|
|
@@ -26,6 +26,14 @@
|
|
|
26
26
|
"types": "./index.d.ts",
|
|
27
27
|
"browser": "./browser.js",
|
|
28
28
|
"default": "./index.js"
|
|
29
|
+
},
|
|
30
|
+
"./locales/en": {
|
|
31
|
+
"types": "./src/locales/en.d.ts",
|
|
32
|
+
"default": "./src/locales/en.js"
|
|
33
|
+
},
|
|
34
|
+
"./locales/uk": {
|
|
35
|
+
"types": "./src/locales/uk.d.ts",
|
|
36
|
+
"default": "./src/locales/uk.js"
|
|
29
37
|
}
|
|
30
38
|
},
|
|
31
39
|
"directories": {
|
|
@@ -65,6 +73,7 @@
|
|
|
65
73
|
"metarhia"
|
|
66
74
|
],
|
|
67
75
|
"devDependencies": {
|
|
76
|
+
"@standard-schema/spec": "^1.1.0",
|
|
68
77
|
"@vercel/analytics": "^2.0.1",
|
|
69
78
|
"@vercel/speed-insights": "^2.0.0",
|
|
70
79
|
"c8": "^12.0.0",
|
|
@@ -79,7 +88,7 @@
|
|
|
79
88
|
"test": "node --test tests/unit/*.test.js",
|
|
80
89
|
"test:coverage": "c8 --all --include 'src/**' --check-coverage --lines 98 --statements 98 --branches 90 --functions 100 --reporter text --reporter lcov node --test tests/unit/*.test.js",
|
|
81
90
|
"test:types": "tsd",
|
|
82
|
-
"check:dts": "tsc --noEmit --strict --target es2022 --skipLibCheck false index.d.ts",
|
|
91
|
+
"check:dts": "tsc --noEmit --strict --target es2022 --skipLibCheck false index.d.ts src/locales/en.d.ts src/locales/uk.d.ts",
|
|
83
92
|
"lint": "oxlint src tests scripts bench",
|
|
84
93
|
"format": "oxfmt src tests scripts bench",
|
|
85
94
|
"format:check": "oxfmt --check src tests scripts bench",
|
package/src/context.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// The state of one `check` call. Validation used to keep the current ancestors
|
|
2
|
+
// and the error limit in module globals, which only worked because validation
|
|
3
|
+
// is synchronous; a context per call keeps two checks independent of each
|
|
4
|
+
// other and leaves the door open to async validators.
|
|
5
|
+
const en = require('./locales/en.js');
|
|
6
|
+
|
|
7
|
+
const { NONE } = require('./issues.js');
|
|
8
|
+
|
|
9
|
+
// The limit when there is none: the largest small integer, so the comparison
|
|
10
|
+
// with the count on every field stays an integer comparison.
|
|
11
|
+
const UNLIMITED = 2 ** 30 - 1;
|
|
12
|
+
|
|
13
|
+
const invalid = (name, expected, value) =>
|
|
14
|
+
new TypeError(`${name} must be ${expected}, got ${JSON.stringify(value) ?? String(value)}`);
|
|
15
|
+
|
|
16
|
+
const maxErrorsOf = (maxErrors) => {
|
|
17
|
+
const valid = typeof maxErrors === 'number' && maxErrors >= 1;
|
|
18
|
+
if (!valid) throw invalid('maxErrors', 'a number of at least 1', maxErrors);
|
|
19
|
+
return Math.min(maxErrors, UNLIMITED);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const messagesOf = (messages) => {
|
|
23
|
+
const isTable = messages !== null && typeof messages === 'object';
|
|
24
|
+
if (!isTable && typeof messages !== 'function') {
|
|
25
|
+
throw invalid('messages', 'a locale table or a function', messages);
|
|
26
|
+
}
|
|
27
|
+
return messages;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const unknownOf = (unknown) => {
|
|
31
|
+
if (unknown !== 'reject' && unknown !== 'ignore') {
|
|
32
|
+
throw invalid('unknown', '"reject" or "ignore"', unknown);
|
|
33
|
+
}
|
|
34
|
+
return unknown;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const REFERENCES = ['kind', 'embed', 'id'];
|
|
38
|
+
|
|
39
|
+
const referencesOf = (references) => {
|
|
40
|
+
if (!REFERENCES.includes(references)) {
|
|
41
|
+
throw invalid('references', '"kind", "embed" or "id"', references);
|
|
42
|
+
}
|
|
43
|
+
return references;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const rootOf = (root) => {
|
|
47
|
+
if (typeof root !== 'string') throw invalid('root', 'a string', root);
|
|
48
|
+
return root;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// The 1.x signature was check(value, path, options); a string here is that
|
|
52
|
+
// path, and the message says where it went.
|
|
53
|
+
const optionsOf = (options) => {
|
|
54
|
+
if (options !== null && typeof options === 'object') return options;
|
|
55
|
+
const hint = typeof options === 'string' ? ' (the path is options.root now)' : '';
|
|
56
|
+
throw invalid('check options', `an object${hint}`, options);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// The options of a call, checked and written over the defaults. Kept out of
|
|
60
|
+
// createContext so that the allocation stays small enough for V8 to inline
|
|
61
|
+
// into Schema#check: a check without options is the common call.
|
|
62
|
+
const configure = (context, options) => {
|
|
63
|
+
optionsOf(options);
|
|
64
|
+
if (options.maxErrors !== undefined) context.limit = maxErrorsOf(options.maxErrors);
|
|
65
|
+
if (options.messages !== undefined) context.messages = messagesOf(options.messages);
|
|
66
|
+
if (options.unknown !== undefined) context.unknown = unknownOf(options.unknown);
|
|
67
|
+
if (options.references !== undefined) context.references = referencesOf(options.references);
|
|
68
|
+
if (options.root !== undefined) context.root = rootOf(options.root);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// One object shape for every context: the keys are always present, in this
|
|
72
|
+
// order, so the hot path sees a single hidden class. `unknown` is what the
|
|
73
|
+
// schema being checked says (its unknown-keys policy); an option of the call
|
|
74
|
+
// wins over it.
|
|
75
|
+
const createContext = (options, name = '', unknown = 'reject') => {
|
|
76
|
+
const context = {
|
|
77
|
+
issues: NONE,
|
|
78
|
+
count: 0,
|
|
79
|
+
limit: UNLIMITED,
|
|
80
|
+
path: [],
|
|
81
|
+
// The objects on the current path, created at the first object met so a
|
|
82
|
+
// check over scalars never allocates it.
|
|
83
|
+
seen: null,
|
|
84
|
+
unknown,
|
|
85
|
+
// How a reference is checked: by the kind of its target ('kind'), as
|
|
86
|
+
// the record itself ('embed') or as its id ('id').
|
|
87
|
+
references: 'kind',
|
|
88
|
+
root: name,
|
|
89
|
+
messages: en,
|
|
90
|
+
};
|
|
91
|
+
if (options !== undefined) configure(context, options);
|
|
92
|
+
return context;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
module.exports = { createContext, UNLIMITED };
|
package/src/index.js
CHANGED
|
@@ -3,7 +3,7 @@ const { saveTypes } = require('./runtime/node.js');
|
|
|
3
3
|
const { Schema } = require('./schema.js');
|
|
4
4
|
const { Model } = require('./model.js');
|
|
5
5
|
const { SchemaDefinitionError } = require('./errors.js');
|
|
6
|
-
const { ValidationResult } = require('./
|
|
6
|
+
const { ValidationResult } = require('./result.js');
|
|
7
7
|
|
|
8
8
|
const { KIND, KIND_STORED, KIND_MEMORY, SCOPE, STORE, ALLOW } = constants;
|
|
9
9
|
|
package/src/issues.js
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
// A validation issue is data: { code, path, message, params }. The library's
|
|
2
|
+
// own checks record the code, the path (an array of keys from the root of the
|
|
3
|
+
// value) and the params of the problem, and leave the message empty; messages
|
|
4
|
+
// are rendered once, at the end of the check, through the locale of that check.
|
|
5
|
+
// Every issue has the same keys in the same order, so loops over them see one
|
|
6
|
+
// shape.
|
|
7
|
+
const { hasBrand, shorten } = require('./util.js');
|
|
8
|
+
const en = require('./locales/en.js');
|
|
9
|
+
|
|
10
|
+
// Shared by the issues that carry no params, so none of them is ever undefined.
|
|
11
|
+
const EMPTY = Object.freeze({});
|
|
12
|
+
|
|
13
|
+
// The issues of a context before the first one is recorded: a valid check
|
|
14
|
+
// never allocates a list.
|
|
15
|
+
const NONE = Object.freeze([]);
|
|
16
|
+
|
|
17
|
+
const push = (context, issue) => {
|
|
18
|
+
if (context.issues === NONE) context.issues = [];
|
|
19
|
+
context.issues.push(issue);
|
|
20
|
+
context.count += 1;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// The path of an issue: the current path, plus the key of the field under
|
|
24
|
+
// check when its check was given one instead of pushing it on the path (a
|
|
25
|
+
// leaf check never pushes; a container pushes its key before its children).
|
|
26
|
+
const pathOf = (context, key) => {
|
|
27
|
+
const { path } = context;
|
|
28
|
+
if (key === undefined) return path.slice();
|
|
29
|
+
if (path.length === 0) return [key];
|
|
30
|
+
const keys = path.slice();
|
|
31
|
+
keys.push(key);
|
|
32
|
+
return keys;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const record = (context, code, params, key, message = '') => {
|
|
36
|
+
push(context, { code, path: pathOf(context, key), message, params });
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const typeOf = (value) => {
|
|
40
|
+
if (value === null) return 'null';
|
|
41
|
+
if (Array.isArray(value)) return 'array';
|
|
42
|
+
return typeof value;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const issues = {
|
|
46
|
+
required: (context, key) => record(context, 'required', EMPTY, key),
|
|
47
|
+
type: (context, expected, value, key) =>
|
|
48
|
+
record(context, 'type', { expected, received: typeOf(value) }, key),
|
|
49
|
+
key: (context, expected, name, key) =>
|
|
50
|
+
record(context, 'type', { expected, received: typeof name, key: name }, key),
|
|
51
|
+
unexpected: (context, keys, key) => record(context, 'unexpected', { keys }, key),
|
|
52
|
+
enum: (context, values, key) => record(context, 'enum', { values }, key),
|
|
53
|
+
length: (context, min, max, actual, key) => record(context, 'length', { min, max, actual }, key),
|
|
54
|
+
range: (context, min, max, actual, key) => record(context, 'range', { min, max, actual }, key),
|
|
55
|
+
pattern: (context, pattern, key) => record(context, 'pattern', { pattern }, key),
|
|
56
|
+
union: (context, expected, discriminator, key) =>
|
|
57
|
+
record(context, 'union', { expected, discriminator }, key),
|
|
58
|
+
reference: (context, entity, key) => record(context, 'reference', { entity }, key),
|
|
59
|
+
circular: (context, key) => record(context, 'circular', EMPTY, key),
|
|
60
|
+
exception: (context, error) => record(context, 'exception', { error }),
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// A key of the value that is not an identifier is bracketed and quoted, so
|
|
64
|
+
// `a.b` as one key and `a` then `b` render differently. Keys taken from the
|
|
65
|
+
// value may be huge; a message must not be.
|
|
66
|
+
const IDENTIFIER = /^[\w$]+$/;
|
|
67
|
+
|
|
68
|
+
const toDotPath = (path, root = '') => {
|
|
69
|
+
let text = root;
|
|
70
|
+
for (let index = 0; index < path.length; index += 1) {
|
|
71
|
+
const key = path[index];
|
|
72
|
+
if (typeof key === 'number') {
|
|
73
|
+
text += `[${key}]`;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const name = String(key);
|
|
77
|
+
if (!IDENTIFIER.test(name)) text += `[${JSON.stringify(shorten(name))}]`;
|
|
78
|
+
else text += text === '' ? shorten(name) : `.${shorten(name)}`;
|
|
79
|
+
}
|
|
80
|
+
return text;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// The path of an issue returned by a validator is relative to the field the
|
|
84
|
+
// validator belongs to: an array of keys, or a single key.
|
|
85
|
+
const relative = (context, path) => {
|
|
86
|
+
if (path === undefined) return context.path.slice();
|
|
87
|
+
if (Array.isArray(path)) return context.path.concat(path);
|
|
88
|
+
const keys = context.path.slice();
|
|
89
|
+
keys.push(path);
|
|
90
|
+
return keys;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const isIssue = (output) =>
|
|
94
|
+
output !== null && typeof output === 'object' && typeof output.message === 'string';
|
|
95
|
+
|
|
96
|
+
// Everything a validator or a custom checkType may return, recorded as
|
|
97
|
+
// issues: true, null and undefined are nothing; false is a generic error with
|
|
98
|
+
// `fallback` as its params; a string is a message; an object with a message is
|
|
99
|
+
// an issue of its own; an array holds any of these; a ValidationResult holds
|
|
100
|
+
// issues already.
|
|
101
|
+
const absorb = (context, output, code, fallback) => {
|
|
102
|
+
if (output === true || output === null || output === undefined) return;
|
|
103
|
+
if (output === false) {
|
|
104
|
+
record(context, code, fallback);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (typeof output === 'string') {
|
|
108
|
+
record(context, code, EMPTY, undefined, output);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (Array.isArray(output)) {
|
|
112
|
+
// One by one: a validator may legitimately return more messages than a
|
|
113
|
+
// spread call accepts.
|
|
114
|
+
for (const item of output) {
|
|
115
|
+
if (context.count >= context.limit) return;
|
|
116
|
+
absorb(context, item, code, fallback);
|
|
117
|
+
}
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (hasBrand(output, 'ValidationResult')) {
|
|
121
|
+
for (const issue of output.issues) {
|
|
122
|
+
if (context.count >= context.limit) return;
|
|
123
|
+
const path = context.path.concat(issue.path);
|
|
124
|
+
push(context, { code: issue.code, path, message: issue.message, params: issue.params });
|
|
125
|
+
}
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (isIssue(output)) {
|
|
129
|
+
const path = relative(context, output.path);
|
|
130
|
+
const params = output.params === undefined ? EMPTY : output.params;
|
|
131
|
+
push(context, { code: output.code || code, path, message: output.message, params });
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
record(context, code, EMPTY, undefined, String(output));
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const render = (messages, issue) => {
|
|
138
|
+
if (typeof messages === 'function') return messages(issue);
|
|
139
|
+
const detail = messages[issue.code] || en[issue.code];
|
|
140
|
+
return detail === undefined ? issue.message : detail(issue.params, issue);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// Messages are produced once, after the walk, for the issues that have none
|
|
144
|
+
// (a validator's own message is kept as it is).
|
|
145
|
+
const finalize = (list, messages) => {
|
|
146
|
+
for (let index = 0; index < list.length; index += 1) {
|
|
147
|
+
const issue = list[index];
|
|
148
|
+
if (issue.message === '') issue.message = render(messages, issue);
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
// A message that already names its field (`Field "User.name" is not hex`, the
|
|
153
|
+
// upstream convention for custom types) is kept as it is.
|
|
154
|
+
const PREFIXED = 'Field';
|
|
155
|
+
|
|
156
|
+
const describe = (messages, root, issue) => {
|
|
157
|
+
const { message } = issue;
|
|
158
|
+
if (message.startsWith(PREFIXED)) return message;
|
|
159
|
+
const field = typeof messages === 'function' ? en.field : messages.field || en.field;
|
|
160
|
+
return `${field(toDotPath(issue.path, root))} ${message}`;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
// The two places where user code runs, and the only ones with a try/catch:
|
|
164
|
+
// an exception from a validator or a custom checkType is reported as an
|
|
165
|
+
// issue, not thrown. Both get the dotted path of the field.
|
|
166
|
+
const guarded = (context, code, fallback, call) => {
|
|
167
|
+
let output;
|
|
168
|
+
try {
|
|
169
|
+
output = call(toDotPath(context.path, context.root));
|
|
170
|
+
} catch (error) {
|
|
171
|
+
issues.exception(context, error);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
absorb(context, output, code, fallback);
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const runValidate = (owner, validate, value, context) =>
|
|
178
|
+
guarded(context, 'custom', EMPTY, (path) => validate.call(owner, value, path));
|
|
179
|
+
|
|
180
|
+
const runCheckType = (type, value, context) => {
|
|
181
|
+
const fallback = { expected: type.type, received: typeOf(value) };
|
|
182
|
+
guarded(context, 'type', fallback, (path) => type.checkType(value, path));
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
module.exports = {
|
|
186
|
+
EMPTY,
|
|
187
|
+
NONE,
|
|
188
|
+
issues,
|
|
189
|
+
typeOf,
|
|
190
|
+
toDotPath,
|
|
191
|
+
absorb,
|
|
192
|
+
finalize,
|
|
193
|
+
describe,
|
|
194
|
+
runValidate,
|
|
195
|
+
runCheckType,
|
|
196
|
+
};
|
package/src/lint.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// Lints of a built schema: what is not wrong enough to throw, but is not
|
|
2
|
+
// what the author meant either. A contradiction that can never validate
|
|
3
|
+
// (an enum without values, a min above max) is a SchemaDefinitionError at
|
|
4
|
+
// build time; these are warnings, as `Warning [code]: text` strings.
|
|
5
|
+
const { hasBrand } = require('./util.js');
|
|
6
|
+
|
|
7
|
+
const warning = (code, text) => `Warning [${code}]: ${text}`;
|
|
8
|
+
|
|
9
|
+
// A field lints by its definition keys: the keys every type knows, the
|
|
10
|
+
// annotations that any field may carry for code that reads the schema, and
|
|
11
|
+
// the options of its own type. A custom type with its own construct owns
|
|
12
|
+
// its vocabulary and is not linted.
|
|
13
|
+
const COMMON = ['root', 'type', 'required', 'nullable', 'validate'];
|
|
14
|
+
|
|
15
|
+
const ANNOTATIONS = [
|
|
16
|
+
'default',
|
|
17
|
+
'unique',
|
|
18
|
+
'index',
|
|
19
|
+
'primary',
|
|
20
|
+
'title',
|
|
21
|
+
'description',
|
|
22
|
+
'examples',
|
|
23
|
+
'deprecated',
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
const KNOWN = new Set([...COMMON, ...ANNOTATIONS]);
|
|
27
|
+
|
|
28
|
+
const isCustom = (Type) => Type.source !== undefined && Type.source.js === undefined;
|
|
29
|
+
|
|
30
|
+
const lintField = (field, path, warnings) => {
|
|
31
|
+
const Type = field.constructor;
|
|
32
|
+
if (!isCustom(Type)) {
|
|
33
|
+
for (const key of Object.keys(field)) {
|
|
34
|
+
if (KNOWN.has(key) || Type.options.has(key)) continue;
|
|
35
|
+
const text = `option "${key}" of "${path}" is not known to type "${field.type}"`;
|
|
36
|
+
warnings.push(warning('unknown-option', text));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (field.pattern !== undefined && field.length?.max === undefined) {
|
|
40
|
+
const text = `"${path}" has a pattern without length.max (see the note on ReDoS)`;
|
|
41
|
+
warnings.push(warning('unbounded-pattern', text));
|
|
42
|
+
}
|
|
43
|
+
if (field.schema !== undefined) lintStruct(field.schema, path, warnings);
|
|
44
|
+
if (field.value !== undefined) {
|
|
45
|
+
if (Array.isArray(field.value)) {
|
|
46
|
+
field.value.forEach((element, index) => lintField(element, `${path}[${index}]`, warnings));
|
|
47
|
+
} else {
|
|
48
|
+
lintField(field.value, `${path}[]`, warnings);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (field.union !== undefined) {
|
|
52
|
+
field.union.forEach((branch, index) => lintField(branch, `${path}|${index}`, warnings));
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const lintStruct = (fields, prefix, warnings) => {
|
|
57
|
+
if (!hasBrand(fields, 'Struct')) {
|
|
58
|
+
lintField(fields, prefix, warnings);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
for (const key of Object.keys(fields)) {
|
|
62
|
+
const field = fields[key];
|
|
63
|
+
if (!hasBrand(field, 'Type')) continue;
|
|
64
|
+
lintField(field, prefix ? `${prefix}.${key}` : key, warnings);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const lintIndexes = (schema, warnings) => {
|
|
69
|
+
const { name, indexes, fields } = schema;
|
|
70
|
+
for (const key of Object.keys(indexes)) {
|
|
71
|
+
const index = indexes[key];
|
|
72
|
+
const columns = index.index || index.primary || index.unique;
|
|
73
|
+
if (!Array.isArray(columns)) continue;
|
|
74
|
+
for (const column of columns) {
|
|
75
|
+
if (hasBrand(fields[column], 'Type')) continue;
|
|
76
|
+
const text = `index "${key}" of "${name}" names a field "${column}" it does not have`;
|
|
77
|
+
warnings.push(warning('missing-index-field', text));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// The warnings of one schema on its own: everything that needs no model.
|
|
83
|
+
const lintSchema = (schema) => {
|
|
84
|
+
const warnings = [];
|
|
85
|
+
lintStruct(schema.fields, schema.name, warnings);
|
|
86
|
+
lintIndexes(schema, warnings);
|
|
87
|
+
return warnings;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
module.exports = { warning, lintSchema };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// The English messages. A locale is a table of renderers by issue code: each
|
|
2
|
+
// one turns the params of an issue into the text that follows the field in a
|
|
3
|
+
// message. `field` renders the location prefix of an error line.
|
|
4
|
+
const { shorten } = require('../util.js');
|
|
5
|
+
|
|
6
|
+
const field = (path) => `Field "${path}"`;
|
|
7
|
+
|
|
8
|
+
const required = () => 'is required';
|
|
9
|
+
|
|
10
|
+
const type = ({ expected, key }) =>
|
|
11
|
+
key === undefined ? `not of expected type: ${expected}` : `keys must be of type ${expected}`;
|
|
12
|
+
|
|
13
|
+
const list = (keys) => {
|
|
14
|
+
let text = shorten(keys[0]);
|
|
15
|
+
for (let index = 1; index < keys.length; index += 1) text += `, ${shorten(keys[index])}`;
|
|
16
|
+
return text;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const unexpected = ({ keys }) => `has unexpected keys: ${list(keys)}`;
|
|
20
|
+
|
|
21
|
+
const enumeration = ({ values }) => `value is not of enum: ${values.join(', ')}`;
|
|
22
|
+
|
|
23
|
+
const length = ({ min, actual }) =>
|
|
24
|
+
min !== undefined && actual < min ? 'value is too short' : 'exceeds the maximum length';
|
|
25
|
+
|
|
26
|
+
const range = ({ min, max, actual }) =>
|
|
27
|
+
min !== undefined && actual < min ? `is less than ${min}` : `is greater than ${max}`;
|
|
28
|
+
|
|
29
|
+
const pattern = ({ pattern: source }) => `does not match the pattern ${source}`;
|
|
30
|
+
|
|
31
|
+
const union = ({ expected, discriminator }) =>
|
|
32
|
+
discriminator === undefined
|
|
33
|
+
? `does not match any of: ${expected.join(', ')}`
|
|
34
|
+
: `is not one of: ${expected.join(', ')}`;
|
|
35
|
+
|
|
36
|
+
const reference = ({ entity }) => `Entity "${entity}" is not found`;
|
|
37
|
+
|
|
38
|
+
const circular = () => 'is a circular reference';
|
|
39
|
+
|
|
40
|
+
const exception = ({ error }) => `validation failed ${error}`;
|
|
41
|
+
|
|
42
|
+
const custom = () => 'validation error';
|
|
43
|
+
|
|
44
|
+
module.exports = {
|
|
45
|
+
field,
|
|
46
|
+
required,
|
|
47
|
+
type,
|
|
48
|
+
unexpected,
|
|
49
|
+
enum: enumeration,
|
|
50
|
+
length,
|
|
51
|
+
range,
|
|
52
|
+
pattern,
|
|
53
|
+
union,
|
|
54
|
+
reference,
|
|
55
|
+
circular,
|
|
56
|
+
exception,
|
|
57
|
+
custom,
|
|
58
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// Ukrainian messages, with the same table as ./en.js. Pass the table to a
|
|
2
|
+
// check as `messages`: `schema.check(value, { messages: uk })`.
|
|
3
|
+
const { shorten } = require('../util.js');
|
|
4
|
+
|
|
5
|
+
const field = (path) => `Поле "${path}"`;
|
|
6
|
+
|
|
7
|
+
const required = () => 'є обовʼязковим';
|
|
8
|
+
|
|
9
|
+
const type = ({ expected, key }) =>
|
|
10
|
+
key === undefined ? `не відповідає типу: ${expected}` : `ключі мають бути типу ${expected}`;
|
|
11
|
+
|
|
12
|
+
const list = (keys) => {
|
|
13
|
+
let text = shorten(keys[0]);
|
|
14
|
+
for (let index = 1; index < keys.length; index += 1) text += `, ${shorten(keys[index])}`;
|
|
15
|
+
return text;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const unexpected = ({ keys }) => `має неочікувані ключі: ${list(keys)}`;
|
|
19
|
+
|
|
20
|
+
const enumeration = ({ values }) => `значення не входить до переліку: ${values.join(', ')}`;
|
|
21
|
+
|
|
22
|
+
const length = ({ min, actual }) =>
|
|
23
|
+
min !== undefined && actual < min ? 'значення закоротке' : 'перевищує максимальну довжину';
|
|
24
|
+
|
|
25
|
+
const range = ({ min, max, actual }) =>
|
|
26
|
+
min !== undefined && actual < min ? `менше за ${min}` : `більше за ${max}`;
|
|
27
|
+
|
|
28
|
+
const pattern = ({ pattern: source }) => `не відповідає шаблону ${source}`;
|
|
29
|
+
|
|
30
|
+
const union = ({ expected, discriminator }) =>
|
|
31
|
+
discriminator === undefined
|
|
32
|
+
? `не відповідає жодному з: ${expected.join(', ')}`
|
|
33
|
+
: `не є одним із: ${expected.join(', ')}`;
|
|
34
|
+
|
|
35
|
+
const reference = ({ entity }) => `сутність "${entity}" не знайдено`;
|
|
36
|
+
|
|
37
|
+
const circular = () => 'є циклічним посиланням';
|
|
38
|
+
|
|
39
|
+
const exception = ({ error }) => `перевірка завершилась помилкою ${error}`;
|
|
40
|
+
|
|
41
|
+
const custom = () => 'помилка перевірки';
|
|
42
|
+
|
|
43
|
+
module.exports = {
|
|
44
|
+
field,
|
|
45
|
+
required,
|
|
46
|
+
type,
|
|
47
|
+
unexpected,
|
|
48
|
+
enum: enumeration,
|
|
49
|
+
length,
|
|
50
|
+
range,
|
|
51
|
+
pattern,
|
|
52
|
+
union,
|
|
53
|
+
reference,
|
|
54
|
+
circular,
|
|
55
|
+
exception,
|
|
56
|
+
custom,
|
|
57
|
+
};
|