Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add express validator #17

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions controllers/portfolios.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const router = require('express').Router();
const verifyJWT = require('../middleware/verifyJWT');
const Portfolios = require('../models/portfolios');
const sanitizeInput = require('../middleware/sanitizeUserInput')

const isOid = require('../utils/validateObjectIds');
const isTicker = require('../utils/validateTickers');
Expand Down Expand Up @@ -35,7 +36,6 @@ router.get('/portfolios', (req, res, next) => {

});


/**
* Get a specific portfolio belonging to a user
* Example: GET >> /api/portfolios/65a4sd654asd645asd
Expand Down Expand Up @@ -66,7 +66,7 @@ router.get('/portfolios/:id', (req, res, next) => {
* 2) portfolio name from req body
* Returns: JSON portfolio object
*/
router.post('/portfolios', (req, res, next) => {
router.post('/portfolios', sanitizeInput(['name', 'notes']), (req, res, next) => {

if (!req.body.name) {
return res.status(400).json({ message: 'Portfolio "name" is required.' });
Expand Down Expand Up @@ -97,7 +97,7 @@ router.post('/portfolios', (req, res, next) => {
* 3) updates from req body
* Returns: JSON portfolio object
*/
router.put('/portfolios/:id', (req, res, next) => {
router.put('/portfolios/:id', sanitizeInput(['name', 'notes']), (req, res, next) => {

if (!isOid(req.params.id)) {
return res.status(400).json({ message: badParamMsg });
Expand Down Expand Up @@ -129,7 +129,7 @@ router.put('/portfolios/:id', (req, res, next) => {
* 2) portfolio _id from req params
* Returns: JSON success message
*/
router.delete('/portfolios/:id', (req, res, next) => {
router.delete('/portfolios/:id', sanitizeInput(['id']), (req, res, next) => {

if (!isOid(req.params.id)) {
return res.status(400).json({ message: badParamMsg });
Expand All @@ -152,7 +152,7 @@ router.delete('/portfolios/:id', (req, res, next) => {
* 3) ticker and qty from req body
* Returns: JSON portfolio object
*/
router.post('/portfolios/:id/holdings', async (req, res, next) => {
router.post('/portfolios/:id/holdings', sanitizeInput(['id', 'ticker', 'qty']), async (req, res, next) => {

if (!req.body.ticker || !req.body.qty) {
return res.status(400)
Expand Down Expand Up @@ -204,7 +204,7 @@ router.post('/portfolios/:id/holdings', async (req, res, next) => {
* 4) qty from req body
* Returns: JSON portfolio object
*/
router.put('/portfolios/:pfloId/holdings/:hldgId', (req, res, next) => {
router.put('/portfolios/:pfloId/holdings/:hldgId', sanitizeInput(['qty']), (req, res, next) => {

if (!isOid(req.params.pfloId || !isOid(req.params.hldgId))) {
return res.status(400).json({ message: badParamMsg });
Expand Down
13 changes: 13 additions & 0 deletions middleware/sanitizeUserInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { sanitizeBody, sanitize } = require('express-validator/filter');

const { buildSanitizeFunction } = require('express-validator/filter');
const sanitizeUserInputs = buildSanitizeFunction(['body', 'query', 'params']);


module.exports = ( valuesArray ) => {
return valuesArray.map( val => {
return sanitizeUserInputs(val)
.blacklist( '{}' ) //escapes any chars passed into it
.escape() //escapes HTML chars
} )
}
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"body-parser": "^1.18.2",
"dotenv": "^5.0.0",
"express": "^4.16.2",
"express-validator": "^5.0.1",
"jsonwebtoken": "^8.1.1",
"mongodb": "^3.0.2",
"morgan": "^1.9.0"
Expand Down