forked from maasglobal/maas-schemas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (41 loc) · 1020 Bytes
/
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
'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() {
ajv = new AJV({
addUsedSchema: false,
allErrors: true,
coerceTypes: true,
errorDataPath: 'property',
inlineRefs: false,
meta: true,
multipleOfPrecision: 6,
removeAdditional: true,
sanitize: false,
sourceCode: false,
useDefaults: true,
validateSchema: false,
verbose: true,
});
require('ajv-merge-patch')(ajv);
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();
return validator.validate(ajv, schema, object, options);
}
module.exports = {
init,
validate,
};