forked from maasglobal/maas-schemas
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
51 lines (45 loc) · 1.07 KB
/
index.js
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
47
48
49
50
51
'use strict';
const AJV = require('ajv');
const validator = require('./lib/validator');
const { transform } = require('./utils/transform-unicode-patterns');
const registry = require('./registry.js');
let ajv;
function init(options) {
ajv = new AJV(
Object.assign(
{
verbose: true,
allErrors: true,
validateSchema: false,
addUsedSchema: false,
meta: false,
inlineRefs: false,
sourceCode: false,
errorDataPath: 'property',
multipleOfPrecision: 6,
sanitize: false,
},
options
)
);
Object.keys(registry).forEach(key => {
const schema = registry[key];
ajv.addSchema(transform(schema));
});
}
/**
* Validate the object against given schema
*
* @param {Object} schema - schema json required from /schemas folder
*/
function validate(schema, object, options = {}) {
if (!ajv) init(options);
if (options.sanitize === true) {
object = validator.sanitize(object);
}
return validator.validate(ajv, schema, object, options);
}
module.exports = {
init,
validate,
};