UNPKG

728 BJavaScriptView Raw
1/* eslint-disable
2 strict,
3 no-param-reassign
4*/
5
6'use strict';
7
8const fs = require('fs');
9const path = require('path');
10
11const Ajv = require('ajv');
12const ajvKeywords = require('ajv-keywords');
13
14const ValidationError = require('./ValidationError');
15
16const ajv = new Ajv({
17 allErrors: true,
18 useDefaults: true,
19 errorDataPath: 'property',
20});
21
22ajvKeywords(ajv, ['instanceof', 'typeof']);
23
24const 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
37module.exports = validateOptions;