validateOptions.js
1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _ajv = require('ajv');
var _ajv2 = _interopRequireDefault(_ajv);
var _ValidationError = require('./ValidationError');
var _ValidationError2 = _interopRequireDefault(_ValidationError);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// eslint-disable-line
var validateOptions = function validateOptions(schema, options, name) {
var ajv = new _ajv2.default({
useDefaults: true,
allErrors: true,
errorDataPath: 'property'
});
if (typeof schema === 'string') {
schema = _fs2.default.readFileSync(_path2.default.resolve(schema), 'utf8'); // eslint-disable-line
schema = JSON.parse(schema); // eslint-disable-line
}
if (!ajv.validate(schema, options)) {
throw new _ValidationError2.default(ajv.errors, name);
}
return true;
};
exports.default = validateOptions;