@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/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 objects on the current validation path. Validation is synchronous, so
13
- // one set per process is enough; a value met again while it is still being
14
- // checked is a cycle and is reported instead of recursed into.
15
- const ancestors = new Set();
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
- ancestors,
92
- limits,
93
- issue,
46
+ RUN,
94
47
  shorten,
95
48
  formatters,
96
- checks,
97
49
  };