@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/src/util.js
CHANGED
|
@@ -9,20 +9,10 @@ const INSPECT = Symbol.for('nodejs.util.inspect.custom');
|
|
|
9
9
|
|
|
10
10
|
const hasBrand = (value, name) => value?.[BRAND] === name;
|
|
11
11
|
|
|
12
|
-
// The
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
// A result stops collecting once it holds this many messages. Schema#check
|
|
18
|
-
// sets it from its options for the duration of the call (validation is
|
|
19
|
-
// synchronous) and restores it afterwards.
|
|
20
|
-
const limits = { maxErrors: Infinity };
|
|
21
|
-
|
|
22
|
-
// A validation problem with a code, built by the library's own checks. A
|
|
23
|
-
// message from a custom validator gets the code 'custom' (or 'type' from a
|
|
24
|
-
// custom checkType) when the result adds it.
|
|
25
|
-
const issue = (code, path, detail) => ({ code, path, message: `Field "${path}" ${detail}` });
|
|
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');
|
|
26
16
|
|
|
27
17
|
// Key names taken from the value under check go into messages; a huge key
|
|
28
18
|
// must not become a huge message.
|
|
@@ -33,16 +23,6 @@ const shorten = (key) => {
|
|
|
33
23
|
return name.length > KEY_LIMIT ? `${name.slice(0, KEY_LIMIT)}...` : name;
|
|
34
24
|
};
|
|
35
25
|
|
|
36
|
-
const { SchemaDefinitionError } = require('./errors.js');
|
|
37
|
-
|
|
38
|
-
const isBound = (value) => value === undefined || typeof value === 'number';
|
|
39
|
-
|
|
40
|
-
const invalidLength = () =>
|
|
41
|
-
new SchemaDefinitionError(
|
|
42
|
-
'ERR_INVALID_LENGTH',
|
|
43
|
-
'Rule "length" needs a number, [min, max] or { min, max }',
|
|
44
|
-
);
|
|
45
|
-
|
|
46
26
|
const formatters = {
|
|
47
27
|
type: (type, req = true) => {
|
|
48
28
|
const required = !type.startsWith('?');
|
|
@@ -57,41 +37,13 @@ const formatters = {
|
|
|
57
37
|
const field = key.slice(0, -1);
|
|
58
38
|
return { field, required: false };
|
|
59
39
|
},
|
|
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
40
|
};
|
|
86
41
|
|
|
87
42
|
module.exports = {
|
|
88
43
|
BRAND,
|
|
89
44
|
INSPECT,
|
|
90
45
|
hasBrand,
|
|
91
|
-
|
|
92
|
-
limits,
|
|
93
|
-
issue,
|
|
46
|
+
RUN,
|
|
94
47
|
shorten,
|
|
95
48
|
formatters,
|
|
96
|
-
checks,
|
|
97
49
|
};
|