From 17ee5bcfc4b9d8058f38c3fc480f78eaec5e8554 Mon Sep 17 00:00:00 2001 From: Mark Moffat Date: Sat, 16 Nov 2019 09:31:57 +1030 Subject: [PATCH] Switching to ajv validation --- lib/schema.js | 11 +++++------ lib/schemas/editProduct.json | 2 +- lib/schemas/newProduct.json | 2 +- package-lock.json | 7 +------ package.json | 2 +- routes/product.js | 4 ++-- 6 files changed, 11 insertions(+), 17 deletions(-) diff --git a/lib/schema.js b/lib/schema.js index 85346ba..8138e07 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -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 = { diff --git a/lib/schemas/editProduct.json b/lib/schemas/editProduct.json index b3ecb74..24b86fd 100644 --- a/lib/schemas/editProduct.json +++ b/lib/schemas/editProduct.json @@ -1,5 +1,5 @@ { - "id": "/editProduct", + "$id": "editProduct", "type": "object", "properties": { "productId": { diff --git a/lib/schemas/newProduct.json b/lib/schemas/newProduct.json index 32677a8..496a399 100644 --- a/lib/schemas/newProduct.json +++ b/lib/schemas/newProduct.json @@ -1,5 +1,5 @@ { - "id": "/newProduct", + "$id": "newProduct", "type": "object", "properties": { "productPermalink": { diff --git a/package-lock.json b/package-lock.json index d289dba..561a470 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 60055ad..d6b0934 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/routes/product.js b/routes/product.js index 7f708c5..90fabae 100644 --- a/routes/product.js +++ b/routes/product.js @@ -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);