forked from mrvautin/expressCart
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Starting to add schema validation to API endpoints
- Loading branch information
Mark Moffat
committed
Jun 17, 2019
1 parent
de5a01c
commit fc1580d
Showing
16 changed files
with
1,119 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const _ = require('lodash'); | ||
const Validator = require('jsonschema').Validator; | ||
const v = new Validator(); | ||
const glob = require('glob-fs')(); | ||
|
||
const addSchemas = () => { | ||
const schemaFiles = glob.readdirSync('./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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"id": "/editProduct", | ||
"type": "object", | ||
"properties": { | ||
"productPermalink": { | ||
"type": "string" | ||
}, | ||
"productTitle": { | ||
"type": "string" | ||
}, | ||
"productPrice": { | ||
"type": "number" | ||
}, | ||
"productDescription": { | ||
"type": "string" | ||
}, | ||
"productPublished": { | ||
"type": "boolean" | ||
}, | ||
"productTags": { | ||
"type": "string" | ||
}, | ||
"productOptions": { | ||
"type": ["object", "string", "null"] | ||
}, | ||
"productComment": { | ||
"type": "boolean" | ||
}, | ||
"productStock": { | ||
"type": ["number", "null"] | ||
} | ||
}, | ||
"required": [ | ||
"productPermalink", | ||
"productTitle", | ||
"productPrice", | ||
"productDescription", | ||
"productPublished" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"id": "/newProduct", | ||
"type": "object", | ||
"properties": { | ||
"productPermalink": { | ||
"type": "string" | ||
}, | ||
"productTitle": { | ||
"type": "string" | ||
}, | ||
"productPrice": { | ||
"type": "number" | ||
}, | ||
"productDescription": { | ||
"type": "string" | ||
}, | ||
"productPublished": { | ||
"type": "boolean" | ||
}, | ||
"productTags": { | ||
"type": "string" | ||
}, | ||
"productOptions": { | ||
"type": ["object", "string", "null"] | ||
}, | ||
"productComment": { | ||
"type": "boolean" | ||
}, | ||
"productStock": { | ||
"type": ["number", "null"] | ||
} | ||
}, | ||
"required": [ | ||
"productPermalink", | ||
"productTitle", | ||
"productPrice", | ||
"productDescription", | ||
"productPublished" | ||
] | ||
} |
Oops, something went wrong.