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."); } }); })
body("vehicle", "If you have a vehicle please provide a its manufacture year") .custom(a => { if (a && "year" in a) { const currentYear = new Date().getFullYear(); // comparing year with age of the oldest and the most recent vehicule if (a.year === parseInt(a.year, 10) && (a.year >= 1885) && (a.year <= (currentYear + 1))) { return true; } else return false; } else return true; })
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 );
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"); } }); })
router.post('/', [ body('name').exists().withMessage('name is required.'), ], (req, res, next) => { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(400).json({error: errors.mapped() }); } const data = { chaincodeId: CHAINCODE_NAME, fcn: 'createAccount', args: [req.body.name], }; invoke(data, req, res, next); });
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); } }
body("risk_questions", "The Risk answers must be an array with 3 booleans (only 0 and 1 are allowed))").isArray({min: 3, max: 3}) .custom(a => { return a.every((e) => { if (0 >= Number(e) || Number(e) <= 1) { return true ; } else return false; }); })
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 }) })
body('email') .isEmail() .withMessage('Enter valid email') .custom(async (value, req) => { try { const user = await User.findOne({email: value}) if (user) { return Promise.reject('User is also exist') } } catch (e) { console.log(e); } }) .normalizeEmail()
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); });
body("house", "The attribute ownership status should can be \"owned\" or \"mortgaged\"") .custom(a => { if (a && "ownership_status" in a) { if (a.ownership_status === "owned" || a.ownership_status === "mortgaged") { return true; } else return false; } else return true; })
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); });
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."); } }); })
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); });