expressCart/lib/schema.js

24 lines
603 B
JavaScript
Raw Normal View History

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');
const addSchemas = () => {
2019-07-11 15:35:03 +10:00
const schemaFiles = glob.sync('./lib/**/*.json');
_.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
};