isvalid 4.1.29 → 4.1.31

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/lib/formalize.js CHANGED
@@ -124,7 +124,8 @@ const formalizeAny = (schema, options = {}) => {
124
124
  'errors': ['object'],
125
125
  'pre': ['function', 'array', 'asyncfunction'],
126
126
  'post': ['function', 'array', 'asyncfunction'],
127
- 'priority': 'number'
127
+ 'priority': 'number',
128
+ '_plugins': 'any'
128
129
  };
129
130
 
130
131
  // Validators specific to type.
@@ -164,7 +165,6 @@ const formalizeAny = (schema, options = {}) => {
164
165
  key = convenienceNames[key] || key;
165
166
 
166
167
  let validator = validators[key];
167
- let test = formalizedSchema[key];
168
168
 
169
169
  if (typeof validator === 'undefined') {
170
170
 
@@ -184,8 +184,13 @@ const formalizeAny = (schema, options = {}) => {
184
184
 
185
185
  if (typeof plugin === 'undefined') throw new SchemaError(schema, `Validator \`${key}\` is unknown in this context.`);
186
186
 
187
- formalizedSchema.plugins = formalizedSchema.plugins || {};
188
- formalizedSchema.plugins[key] = {
187
+ Object.defineProperty(formalizedSchema, '_plugins', {
188
+ value: formalizedSchema._plugins || {},
189
+ enumerable: false,
190
+ writable: true
191
+ });
192
+
193
+ formalizedSchema._plugins[key] = {
189
194
  phase: plugin.phase || 'post',
190
195
  validator: plugin.validate,
191
196
  formalize: plugin.formalize
@@ -193,6 +198,8 @@ const formalizeAny = (schema, options = {}) => {
193
198
 
194
199
  }
195
200
 
201
+ let test = formalizedSchema[key];
202
+
196
203
  // Test for - and transform - errors in validator.
197
204
  if (Array.isArray(test) &&
198
205
 
@@ -218,9 +225,9 @@ const formalizeAny = (schema, options = {}) => {
218
225
  );
219
226
  }
220
227
 
221
- if (typeof (formalizedSchema.plugins || {})[key] !== 'undefined') {
228
+ if (typeof (formalizedSchema._plugins || {})[key] !== 'undefined') {
222
229
  try {
223
- formalizedSchema[key] = formalizedSchema.plugins[key].formalize(formalizedSchema[key], key, type) || formalizedSchema[key];
230
+ formalizedSchema[key] = formalizedSchema._plugins[key].formalize(formalizedSchema[key], key, type) || formalizedSchema[key];
224
231
  } catch (error) {
225
232
  throw new SchemaError(schema, error.message);
226
233
  }
package/lib/plugins.js CHANGED
@@ -6,15 +6,31 @@
6
6
  // See license in LICENSE
7
7
  //
8
8
 
9
-
10
9
  import { isSameType, instanceTypeName, typeName } from './utils.js';
11
10
 
12
- const plugins = [];
11
+ const plugins = {};
12
+
13
+ export function use(identifier, plugin, { conflict = 'fail' } = {}) {
14
+
15
+ if (typeof identifier !== 'string') {
16
+ throw new Error('Identifier must be a string.');
17
+ }
18
+
19
+ if (plugins[identifier]) {
20
+ switch (conflict) {
21
+ case 'replace':
22
+ break;
23
+ case 'ignore':
24
+ return;
25
+ default:
26
+ throw new Error(`Plugin with identifier "${identifier}" already exists.`);
27
+ }
28
+ }
29
+
30
+ plugins[identifier] = plugin({ isSameType, instanceTypeName, typeName });
13
31
 
14
- export function use(plugin) {
15
- plugins.push(plugin({ isSameType, instanceTypeName, typeName }));
16
32
  }
17
33
 
18
34
  export function all() {
19
- return plugins;
35
+ return Object.values(plugins);
20
36
  }
package/lib/validate.js CHANGED
@@ -380,9 +380,9 @@ const validateCustom = async (phase, data, schema, options, keyPath, validatedDa
380
380
 
381
381
  const validatePlugins = async (phase, data, schema, options, keyPath) => {
382
382
 
383
- const plugins = Object.keys(schema.plugins || {})
384
- .filter((key) => schema.plugins[key].phase === phase)
385
- .map((key) => [key, schema.plugins[key].validator]);
383
+ const plugins = Object.keys(schema._plugins || {})
384
+ .filter((key) => schema._plugins[key].phase === phase)
385
+ .map((key) => [key, schema._plugins[key].validator]);
386
386
 
387
387
  for (let idx = 0 ; idx < plugins.length ; idx++) {
388
388
  const [key, validator] = plugins[idx];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "isvalid",
3
- "version": "4.1.29",
3
+ "version": "4.1.31",
4
4
  "description": "Async JSON validation library for node.js.",
5
5
  "main": "./index.js",
6
6
  "type": "module",
package/test/index.js CHANGED
@@ -13,7 +13,7 @@ import { use } from '../lib/plugins.js';
13
13
 
14
14
  chaiUse(chaiAsPromised);
15
15
 
16
- use(function (utils) {
16
+ use('utils', (utils) => {
17
17
  return {
18
18
  supportsType: (type) => utils.isSameType(type, String),
19
19
  validatorsForType: () => { return { ensureCase: String }; },