1 |
|
2 |
|
3 |
|
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;
|