JAVASCRIPT 23
ValidateOptions.js Guest on 4th May 2021 08:03:55 AM
  1. /* eslint-disable
  2.   strict,
  3.   no-param-reassign
  4. */
  5.  
  6. 'use strict';
  7.  
  8. const fs = require('fs');
  9. const path = require('path');
  10.  
  11. const Ajv = require('ajv');
  12. const ajvKeywords = require('ajv-keywords');
  13.  
  14. const ValidationError = require('./ValidationError');
  15.  
  16. const ajv = new Ajv({
  17.   allErrors: true,
  18.   useDefaults: true,
  19.   errorDataPath: 'property',
  20. });
  21.  
  22. ajvKeywords(ajv, ['instanceof', 'typeof']);
  23.  
  24. const validateOptions = (schema, options, name) => {
  25.   if (typeof schema === 'string') {
  26.     schema = fs.readFileSync(path.resolve(schema), 'utf8');
  27.     schema = JSON.parse(schema);
  28.   }
  29.  
  30.   if (!ajv.validate(schema, options)) {
  31.     throw new ValidationError(ajv.errors, name);
  32.   }
  33.  
  34.   return true;
  35. };
  36.  
  37. module.exports = validateOptions;

Paste-bin is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.