isvalid 3.1.2 → 3.1.3

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/plugins.js CHANGED
@@ -23,9 +23,9 @@ exports.use = function(plugin) {
23
23
  }));
24
24
  };
25
25
 
26
- exports.validatorsForType = function(type) {
26
+ exports.validatorsForType = function(type, phase) {
27
27
  return Object.fromEntries(Object.entries(merge(...this._plugins
28
- .filter((plugin) => plugin.supportsType(type))
28
+ .filter((plugin) => (!phase || (plugin.phase || 'post') === phase) && plugin.supportsType(type))
29
29
  .map((plugin) => plugin._validatorsForType(type)))));
30
30
 
31
31
  };
@@ -36,7 +36,7 @@ exports.formalizeValidator = function(type, identifier, config) {
36
36
  .filter((plugin) => Object.keys(plugin._validatorsForType(type)).includes(identifier))
37
37
  .reduce((config, plugin) => {
38
38
  if (typeof plugin.formalizeValidator !== 'function') return config;
39
- return plugin.formalizeValidator(type, identifier, config);
39
+ return plugin.formalizeValidator(config, identifier, type);
40
40
  }, config);
41
41
  };
42
42
 
@@ -46,7 +46,7 @@ exports.validate = async function(type, identifier, config, data) {
46
46
  .filter((plugin) => plugin.supportsType(type));
47
47
 
48
48
  for (const plugin of plugins) {
49
- data = await plugin.validate(type, identifier, config, data);
49
+ data = await plugin.validate(data, config, identifier, type);
50
50
  }
51
51
 
52
52
  return data;
package/lib/validate.js CHANGED
@@ -299,16 +299,38 @@ const validateOther = async (data, schema, options, keyPath) => {
299
299
 
300
300
  };
301
301
 
302
- const validateCustom = async (type, data, schema, options, keyPath, validatedData) => {
302
+ const validateCustom = async (phase, data, schema, options, keyPath, validatedData) => {
303
303
 
304
- if (!schema[type]) return data;
304
+ if (!schema[phase]) return data;
305
305
 
306
- for (let idx = 0 ; idx < schema[type].length ; idx++) {
306
+ for (let idx = 0 ; idx < schema[phase].length ; idx++) {
307
307
  try {
308
- let result = await Promise.resolve(schema[type][idx](data, schema, { options, keyPath, data: validatedData }));
308
+ let result = await Promise.resolve(schema[phase][idx](data, schema, { options, keyPath, data: validatedData }));
309
309
  if (typeof result !== 'undefined') data = result;
310
310
  } catch (error) {
311
- throw ValidationError.fromError(keyPath, schema._nonFormalizedSchema, type, error);
311
+ throw ValidationError.fromError(keyPath, schema._nonFormalizedSchema, phase, error);
312
+ }
313
+ }
314
+
315
+ return data;
316
+
317
+ };
318
+
319
+ const validatePlugins = async (phase, data, schema, options, keyPath) => {
320
+
321
+ const pluginValidators = Object.keys(plugins.validatorsForType(schema.type, phase))
322
+ .filter((validator) => typeof schema[validator] !== 'undefined');
323
+
324
+ for (const pluginValidator of pluginValidators) {
325
+ try {
326
+ data = await plugins.validate(schema.type, pluginValidator, schema[pluginValidator], data);
327
+ } catch (error) {
328
+ throw new ValidationError(
329
+ keyPath,
330
+ schema._nonFormalizedSchema,
331
+ pluginValidator,
332
+ (schema.errors || {})[pluginValidator] || customErrorMessage((options.errorMessages || {})[pluginValidator] || error.message)
333
+ );
312
334
  }
313
335
  }
314
336
 
@@ -317,10 +339,12 @@ const validateCustom = async (type, data, schema, options, keyPath, validatedDat
317
339
  };
318
340
 
319
341
  const validatePre = async (data, schema, options, keyPath, validatedData) => {
320
- return await validateCustom('pre', data, schema, options, keyPath, validatedData);
342
+ data = await validateCustom('pre', data, schema, options, keyPath, validatedData);
343
+ return await validatePlugins('pre', data, schema, options, keyPath);
321
344
  };
322
345
 
323
346
  const validatePost = async (data, schema, options, keyPath, validatedData) => {
347
+ data = await validatePlugins('post', data, schema, options, keyPath);
324
348
  return await validateCustom('post', data, schema, options, keyPath, validatedData);
325
349
  };
326
350
 
@@ -404,22 +428,6 @@ const validateAny = async (data, schema, options, keyPath, validatedData) => {
404
428
  }
405
429
  }
406
430
 
407
- const pluginValidators = Object.keys(plugins.validatorsForType(schema.type))
408
- .filter((validator) => typeof schema[validator] !== 'undefined');
409
-
410
- for (const pluginValidator of pluginValidators) {
411
- try {
412
- data = await plugins.validate(schema.type, pluginValidator, schema[pluginValidator], data);
413
- } catch (error) {
414
- throw new ValidationError(
415
- keyPath,
416
- schema._nonFormalizedSchema,
417
- pluginValidator,
418
- (schema.errors || {})[pluginValidator] || customErrorMessage((options.errorMessages || {})[pluginValidator] || error.message)
419
- );
420
- }
421
- }
422
-
423
431
  return await validatePost(data, schema, options, keyPath, validatedData);
424
432
 
425
433
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "isvalid",
3
- "version": "3.1.2",
3
+ "version": "3.1.3",
4
4
  "description": "Async JSON validation library for node.js.",
5
5
  "main": "./index.js",
6
6
  "keywords": [
package/test/index.js CHANGED
@@ -11,11 +11,10 @@ isvalid.plugins.use(function (utils) {
11
11
  return {
12
12
  supportsType: (type) => utils.isSameType(type, String),
13
13
  validatorsForType: () => { return { ensureCase: String }; },
14
- formalizeValidator: (_, __, config) => {
15
- if (!config) return;
16
- if (!caseit.supported.includes(config)) throw new Error(`Only case types: ${caseit.supported.map((casing) => `\`${casing}\``).join(', ')} are supported.`);
14
+ formalizeValidator: (config) => {
15
+ if (config && !caseit.supported.includes(config)) throw new Error(`Only case types: ${caseit.supported.map((casing) => `\`${casing}\``).join(', ')} are supported.`);
17
16
  },
18
- validate: (_, __, config, data) => {
17
+ validate: (data, config) => {
19
18
  if (caseit(data, config) !== data) throw new Error(`Is not ${config} case.`);
20
19
  return data;
21
20
  }