check('urlGitHub') .optional() .custom((v) => (v === '' ? true : validator.isURL(v))) .withMessage('NOT_A_VALID_URL')
function validationRules() { return [ check('first_name').not().isEmpty().withMessage('First name is required'), check('last_name').not().isEmpty().withMessage('Last name is required'), check('gender').not().isEmpty().withMessage('Gender is required').isIn(['M', 'F']), check('date_of_birth').toDate().optional({ checkFalsy: true }), check('mobile').optional({ checkFalsy: true }).isInt(), check('address').optional({ checkFalsy: true, nullable: true }).isLength({ min: 10 }).withMessage('Please enter minimum 10 characters'), check('email').optional({ checkFalsy: true, nullable: true }).isEmail().withMessage('Please enter valid email') ]; }
router.post('/', [ check('name').isString() ], (req, res) => { // If errors return 422, client didn't provide required values const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(422).json({ errors: errors.array() }); } // Otherwise use the server secret to encode the user's request as a JWT let info = {}; info.token = jwt.encode(req.body, secret); res.json(info); });
router.put('/products/:productId/', [ check('name').not().isEmpty() ] , (req, res, next) => { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(400).json({ errors: errors.array() }); } return ProductController.updateProductById(req, res, next); });
router.post(['/open'], [ check('code').isLength({ min: 4, max: 4 }) ], (req, res) => { let code = passCodes.findIndex(obj => { return obj.code === req.body.code; }); if(code !== -1) { passCodes.splice(code, 1); res.json({ message: 'Pass code is valid, door opened.' }); } else { res.status(403).json({ error: 'Pass code is not valid.' }); } });
// Function to run if user sends a PUT request router.put(['/', '/actions/fade'], [ check('mode').isNumeric().isLength({ min: 0, max: 1 }), check('temperature').isNumeric() ], (req, res) => { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(422).json({ errors: errors.array() }); } let temperature = req.body.temperature; let mode = req.body.mode; let message = `success: temperature set to ${temperature} mode is ${mode}`; res.json({"message": message}); });
router.post('/products', [ check('name').not().isEmpty() ] , (req, res, next) => { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(400).json({ errors: errors.array() }); } return ProductController.createProduct(req, res, next); });
check('urlGitHub') .optional() .custom((v) => (v === '' ? true : validator.isURL(v))) .withMessage('NOT_A_VALID_URL')
check('urlGitHub') .optional() .custom((v) => (v === '' ? true : validator.isURL(v))) .withMessage('NOT_A_VALID_URL')
check('urlTwitter') .optional() .custom((v) => (v === '' ? true : validator.isURL(v))) .withMessage('NOT_A_VALID_URL')
check('urlTwitter') .optional() .custom((v) => (v === '' ? true : validator.isURL(v))) .withMessage('NOT_A_VALID_URL')
check('urlTwitter') .optional() .custom((v) => (v === '' ? true : validator.isURL(v))) .withMessage('NOT_A_VALID_URL')
router.post('/', [ check('name').isString() ], (req, res) => { // If errors return 422, client didn't provide required values const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(422).json({ errors: errors.array() }); } // Otherwise use the server secret to encode the user's request as a JWT let info = {}; info.token = jwt.encode(req.body, secret); res.json(info); });