Hello! I've been wondering if there's a clean way to generalize checking if a record exists, checking if a request's value is of a certain type, checking if a string is and email.I have something like this: ``` router.get('/users/create', auth.optional, function(req, res, next){ if(req.payload) { if(!isEmail(req.payload.email)) { // Do something } } // ... });router.get('/users/:id/update', auth.optional, function(req, res, next){ const user = User.findOne({ id: req.payload.id }); if(!user) { return res.sendStatus(401); } if(req.payload) { if(!isEmail(req.payload.email)) { // Do something } } // ... });router.get('/:username', auth.optional, function(req, res, next){ const user = User.findOne({ id: req.payload.id }); if(!user) { return res.sendStatus(401); } // ... });router.post('/', auth.required, function(req, res, next){ const user = User.findOne({ id: req.payload.id }); if(!user) { return res.sendStatus(401); } // ... }); ```You can see I have alot of redundant checks to do everytime I need to use a user. I was wondering if there was a way to abstract those checks somewhere else. I know I could just create a function and call it everytime, but is there a library or a tool that could help with that? Maybe something like this or this in Laravel that uses a validation "schema" for each requests.
Submitted July 16, 2020 at 04:50AM by LordDrakota
No comments:
Post a Comment