Skip to content

Commit

Permalink
Fix validation bug in Twitter, Pinterest, and Twilio API examples
Browse files Browse the repository at this point in the history
  • Loading branch information
YasharF committed Aug 5, 2019
1 parent 2ff3ede commit 64a390c
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions controllers/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const ig = require('instagram-node').instagram();
const axios = require('axios');
const { google } = require('googleapis');
const Quickbooks = require('node-quickbooks');
const validator = require('validator');

Quickbooks.setOauthVersion('2.0');

Expand Down Expand Up @@ -281,12 +282,11 @@ exports.getTwitter = async (req, res, next) => {
* Post a tweet.
*/
exports.postTwitter = (req, res, next) => {
req.assert('tweet', 'Tweet cannot be empty').notEmpty();
const validationErrors = [];
if (validator.isEmpty(req.body.tweet)) validationErrors.push({ msg: 'Tweet cannot be empty' });

const errors = req.validationErrors();

if (errors) {
req.flash('errors', errors);
if (validationErrors.length) {
req.flash('errors', validationErrors);
return res.redirect('/api/twitter');
}

Expand Down Expand Up @@ -430,13 +430,12 @@ exports.getTwilio = (req, res) => {
* Send a text message using Twilio.
*/
exports.postTwilio = (req, res, next) => {
req.assert('number', 'Phone number is required.').notEmpty();
req.assert('message', 'Message cannot be blank.').notEmpty();

const errors = req.validationErrors();
const validationErrors = [];
if (validator.isEmpty(req.body.number)) validationErrors.push({ msg: 'Phone number is required.' });
if (validator.isEmpty(req.body.message)) validationErrors.push({ msg: 'Message cannot be blank.' });

if (errors) {
req.flash('errors', errors);
if (validationErrors.length) {
req.flash('errors', validationErrors);
return res.redirect('/api/twilio');
}

Expand Down Expand Up @@ -693,14 +692,13 @@ exports.getPinterest = (req, res, next) => {
* Create a pin.
*/
exports.postPinterest = (req, res, next) => {
req.assert('board', 'Board is required.').notEmpty();
req.assert('note', 'Note cannot be blank.').notEmpty();
req.assert('image_url', 'Image URL cannot be blank.').notEmpty();

const errors = req.validationErrors();
const validationErrors = [];
if (validator.isEmpty(req.body.board)) validationErrors.push({ msg: 'Board is required.' });
if (validator.isEmpty(req.body.note)) validationErrors.push({ msg: 'Note cannot be blank.' });
if (validator.isEmpty(req.body.image_url)) validationErrors.push({ msg: 'Image URL cannot be blank.' });

if (errors) {
req.flash('errors', errors);
if (validationErrors.length) {
req.flash('errors', validationErrors);
return res.redirect('/api/pinterest');
}

Expand Down

0 comments on commit 64a390c

Please sign in to comment.