Switching to ajv validation
parent
20ad07ee74
commit
17ee5bcfc4
|
@ -1,20 +1,19 @@
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const _ = require('lodash');
|
|
||||||
const Validator = require('jsonschema').Validator;
|
|
||||||
const v = new Validator();
|
|
||||||
const glob = require('glob');
|
const glob = require('glob');
|
||||||
|
const Ajv = require('ajv');
|
||||||
|
const ajv = new Ajv();
|
||||||
|
|
||||||
const addSchemas = () => {
|
const addSchemas = () => {
|
||||||
const schemaFiles = glob.sync('./lib/**/*.json');
|
const schemaFiles = glob.sync('./lib/**/*.json');
|
||||||
_.forEach(schemaFiles, (file) => {
|
schemaFiles.forEach((file) => {
|
||||||
const fileData = JSON.parse(fs.readFileSync(file, 'utf-8'));
|
const fileData = JSON.parse(fs.readFileSync(file, 'utf-8'));
|
||||||
v.addSchema(fileData, path.basename(schemaFiles[0], '.json'));
|
ajv.addSchema(fileData, path.basename(file, '.json'));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const validateJson = (schema, json) => {
|
const validateJson = (schema, json) => {
|
||||||
return v.validate(json, schema);
|
return ajv.validate(schema, json);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"id": "/editProduct",
|
"$id": "editProduct",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"productId": {
|
"productId": {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"id": "/newProduct",
|
"$id": "newProduct",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"productPermalink": {
|
"productPermalink": {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "express-cart",
|
"name": "express-cart",
|
||||||
"version": "1.1.13",
|
"version": "1.1.14",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -5888,11 +5888,6 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"jsonschema": {
|
|
||||||
"version": "1.2.4",
|
|
||||||
"resolved": "https://registry.npmjs.org/jsonschema/-/jsonschema-1.2.4.tgz",
|
|
||||||
"integrity": "sha512-lz1nOH69GbsVHeVgEdvyavc/33oymY1AZwtePMiMj4HZPMbP5OIKK3zT9INMWjwua/V4Z4yq7wSlBbSG+g4AEw=="
|
|
||||||
},
|
|
||||||
"just-debounce": {
|
"just-debounce": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/just-debounce/-/just-debounce-1.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/just-debounce/-/just-debounce-1.0.0.tgz",
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
"files": [
|
"files": [
|
||||||
"./test/specs/*.js"
|
"./test/specs/*.js"
|
||||||
],
|
],
|
||||||
|
"timeout": "10s",
|
||||||
"verbose": true,
|
"verbose": true,
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
"NODE_ENV": "test"
|
"NODE_ENV": "test"
|
||||||
|
@ -42,7 +43,6 @@
|
||||||
"helmet": "^3.21.2",
|
"helmet": "^3.21.2",
|
||||||
"html-entities": "^1.2.0",
|
"html-entities": "^1.2.0",
|
||||||
"i18n": "^0.8.4",
|
"i18n": "^0.8.4",
|
||||||
"jsonschema": "^1.2.4",
|
|
||||||
"lodash": "^4.17.15",
|
"lodash": "^4.17.15",
|
||||||
"lunr": "^2.3.8",
|
"lunr": "^2.3.8",
|
||||||
"mime-type": "^3.0.7",
|
"mime-type": "^3.0.7",
|
||||||
|
|
|
@ -103,7 +103,7 @@ router.post('/admin/product/insert', restrict, checkAccess, async (req, res) =>
|
||||||
|
|
||||||
// Validate the body again schema
|
// Validate the body again schema
|
||||||
const schemaResult = validateJson('newProduct', doc);
|
const schemaResult = validateJson('newProduct', doc);
|
||||||
if(!schemaResult.valid){
|
if(!schemaResult){
|
||||||
// If API request, return json
|
// If API request, return json
|
||||||
if(req.apiAuthenticated){
|
if(req.apiAuthenticated){
|
||||||
res.status(400).json(schemaResult.errors);
|
res.status(400).json(schemaResult.errors);
|
||||||
|
@ -342,7 +342,7 @@ router.post('/admin/product/update', restrict, checkAccess, async (req, res) =>
|
||||||
|
|
||||||
// Validate the body again schema
|
// Validate the body again schema
|
||||||
const schemaResult = validateJson('editProduct', productDoc);
|
const schemaResult = validateJson('editProduct', productDoc);
|
||||||
if(!schemaResult.valid){
|
if(!schemaResult){
|
||||||
// If API request, return json
|
// If API request, return json
|
||||||
if(req.apiAuthenticated){
|
if(req.apiAuthenticated){
|
||||||
res.status(400).json(schemaResult.errors);
|
res.status(400).json(schemaResult.errors);
|
||||||
|
|
Loading…
Reference in New Issue