router.post('/reset/:token', [ check('password').not().isEmpty().isLength({min: 6}).withMessage('Must be at least 6 chars long'), check('confirmPassword', 'Passwords do not match').custom((value, {req}) => (value === req.body.password)), ], validate, Password.resetPassword);
body("email").isLength({ min: 1 }).trim().withMessage("Email must be specified.") .isEmail().withMessage("Email must be a valid email address.").custom((value) => { return UserModel.findOne({email : value}).then((user) => { if (user) { return Promise.reject("E-mail already in use"); } }); })
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') ]; }
body("isbn", "ISBN must not be empty").isLength({ min: 1 }).trim().custom((value,{req}) => { return Book.findOne({isbn : value,user: req.user._id}).then(book => { if (book) { return Promise.reject("Book already exist with this ISBN no."); } }); })
// 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( '/', [ check('username').isLength({min: 1}), check('domain').isIn([config.email.domain]) ], (req, res) => { const errors = validationResult(req) if (!errors.isEmpty()) { return res.render('login', { title: 'Login', username: req.body.username, domain: config.email.domain, userInputError: true }) } res.redirect(`/${req.body.username}@${req.body.domain}`) } )
const urlOrTextIsValid = (req, res, next) => { if (req.body.type === 'link') { const chain = body('url') .exists() .withMessage('is required') .isURL() .withMessage('is invalid'); chain(req, res, next); } else { const chain = body('text') .exists() .withMessage('is required') .isLength({ min: 4 }) .withMessage('must be at least 4 characters long'); chain(req, res, next); } }
router.route('/signup').post( [ body('email', 'Please enter a valid email.') .isEmail() .custom(async (value, { req }) => { const user = await User.findOne({ email: value }); if (user) throw 'Email address already exists.'; }) .normalizeEmail(), body('password').trim().isLength({ min: 5 }), body('name').trim().not().isEmpty() ], authController.signup );
/** * custom validation: check if email already exists */ router.put('/signup', [ body('email') .isEmail() .withMessage('Please enter a valid email') .custom((value, { req }) => { return User.findOne({email: value}).then(userDoc => { if(userDoc){ return Promise.reject('Email address already exists!'); } }) }) .normalizeEmail(), body('password').trim().isLength({min: 5}), body('name').trim().not().isEmpty() ], authController.signup);
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.' }); } });
router.post(`/${DEPOSIT}/`, [ body('to_account_no').exists().withMessage('to_account_no is required'), body('to_account_no').isLength({min:16, max:16}).matches(/\d/).withMessage('account_no is not 16 digits'), body('amount').exists().withMessage('amount is required'), body('amount').isInt({min:0}).withMessage('amount is not int or amount is less than zero'), ], (req, res, next) => { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(400).json({error: errors.mapped() }); } const data = { chaincodeId: CHAINCODE_NAME, fcn: 'deposit', args: [req.body.to_account_no, req.body.amount.toString()], }; invoke(data, req, res, next); });
router.post('/register', upload.single('profileimage'), [ check('email', 'Email is not valid').isEmail(), check('username', 'Username field is required').not().isEmpty(), check('password', 'Password field is required').not().isEmpty(), check('password').isLength({ min: 3 }).withMessage('Password must be at least 3 chars long'), check('password').matches(/\d/).withMessage('Password must contain a number'), check('password2').custom((value, {req}) => { //custom validator if(value!==req.body.password) { throw new Error('Passwords doesn\'t match'); } return true; }) ], userController.register);
router.post('/register', [ check('email').isEmail().custom(value => { return User.findOne({ email: value }).then(user => { if (user) { return Promise.reject('E-mail already in use'); } }) }), check('password').isLength({ min: 6 }), check('firstName').not().isEmpty(), check('lastName').not().isEmpty() ], (req, res) => { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(400).json({ errors: errors.array() }); } AuthControlller.register(req, res,"USER"); });
body("isbn", "ISBN must not be empty").isLength({ min: 1 }).trim().custom((value,{req}) => { return Book.findOne({isbn : value,user: req.user._id, _id: { "$ne": req.params.id }}).then(book => { if (book) { return Promise.reject("Book already exist with this ISBN no."); } }); })
router.post(`/${WITHDRAW}/`, [ body('from_account_no').exists().withMessage('from_account_no is required'), body('from_account_no').isLength({min:16, max:16}).matches(/\d/).withMessage('account_no is not 16 digits'), body('amount').exists().withMessage('amount is required'), body('amount').isInt({min:0}).withMessage('amount is not int or amount is less than zero'), ], (req, res, next) => { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(400).json({error: errors.mapped() }); } const data = { chaincodeId: CHAINCODE_NAME, fcn: 'withdraw', args: [req.body.from_account_no, req.body.amount.toString()], }; invoke(data, req, res, next); });