router.post('/login', [ check('email').isEmail(), check('password').not().isEmpty() ] , (req, res) => { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(400).json({ errors: errors.array() }); } return AuthControlller.login(req, res); });
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.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 );
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.put(`/${ACCOUNT_NO}/`, [ body('name').exists().withMessage('name is required.'), body('model_type').not().exists().withMessage('model_type can not be changed.'), body('no').not().exists().withMessage('no can not be changed.'), body('balance').not().exists().withMessage('balance can not be changed.'), ],(req, res, next) => { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(400).json({error: errors.mapped() }); } const data = { chaincodeId: CHAINCODE_NAME, fcn: 'updateAccountName', args: [req.params.no, req.body.name], }; invoke(data, req, res, next); });
/** * 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('/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);
app.patch( '/user', User.authenticate, body('name') .optional() .not().isEmpty() .isLength({ min: 1, max: 25 }) .trim(), body('skin') .optional() .isBase64(), (req, res) => { const { name, skin } = req.body; if (!name && !skin) { res.status(422).end(); return; } if (name) { req.user.name = name; } if (skin) { req.user.skin = Buffer.from(skin, 'base64'); } req.user.save() .then(() => res.status(200).end()) .catch(() => res.status(400).end()); } );
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);
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"); });
app.post( '/users', body('email') .isEmail() .normalizeEmail(), body('name') .not().isEmpty() .isLength({ min: 1, max: 25 }) .trim(), body('password') .not().isEmpty() .trim(), body('skin') .isBase64(), (req, res) => { if (!validationResult(req).isEmpty()) { res.status(422).end(); return; } const user = new User({ email: req.body.email, name: req.body.name, password: req.body.password, skin: Buffer.from(req.body.skin, 'base64'), }); user.save() .then(() => res.json(user.getNewSession())) .catch(() => res.status(400).end()); } );
router.post('/', [ body('message.toMobile').not().isEmpty(), body('message.content').not().isEmpty(), body('message.tags').isArray() ], function(req, res, next) { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(422).json({ errors: errors.array() }); } const messageParams = req.body.message const message = MessageDao.create(messageParams) res.status(201).send({ message }) websocket.broadcast({ event: 'NewMessage', data: message }) })
router.post('/', [ body('email.toAddress').not().isEmpty(), body('email.fromAddress').not().isEmpty(), body('email.subject').not().isEmpty(), body('email.content').not().isEmpty(), body('email.tags').isArray() ], function(req, res, next) { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(422).json({ errors: errors.array() }); } const emailParams = req.body.email const email = EmailDao.create(emailParams) res.status(201).send({ email }) websocket.broadcast({ event: 'NewEmail', data: email }) })
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); });
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('E-Mail address already exists!'); } }); }) .normalizeEmail(), body('password') .trim() .isLength({ min: 5 }), body('name') .trim() .not() .isEmpty() ], authController.signup );