2019-06-17 20:21:45 +10:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const Validator = require('jsonschema').Validator;
|
|
|
|
const v = new Validator();
|
2019-07-11 15:35:03 +10:00
|
|
|
const glob = require('glob');
|
2019-06-17 20:21:45 +10:00
|
|
|
|
|
|
|
const addSchemas = () => {
|
2019-07-11 15:35:03 +10:00
|
|
|
const schemaFiles = glob.sync('./lib/**/*.json');
|
2019-06-17 20:21:45 +10:00
|
|
|
_.forEach(schemaFiles, (file) => {
|
|
|
|
const fileData = JSON.parse(fs.readFileSync(file, 'utf-8'));
|
|
|
|
v.addSchema(fileData, path.basename(schemaFiles[0], '.json'));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const validateJson = (schema, json) => {
|
|
|
|
return v.validate(json, schema);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
addSchemas,
|
|
|
|
validateJson
|
|
|
|
};
|