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