diff --git a/helpers/routeHelper.js b/helpers/routeHelper.js new file mode 100644 index 0000000..3404fde --- /dev/null +++ b/helpers/routeHelper.js @@ -0,0 +1,50 @@ +const Joi = require('joi'); +// validation does not work well prevents parameters from being entered into the database +module.exports = { + validateParam: (schema, name) => { + return (req, res, next) => { + const result = Joi.validate({ param: req['params'][name] }, schema); + if (result.error){ + return res.status(400).json(result.error); + } else { + if (!req.value) + req.value = {}; + + if (!req.value['params']) + req.value['params'] = {}; + + req.value['params'][name] = result.value.param; + next(); + } + } + }, + // Work but for any rasons not implements + validateBody: (schema) => { + return (req, res, next) => { + const result = Joi.validate(req.body, schema); + if (result.error) { + return res.status(400).json(result.error); + } else { + if (!req.value) + req.value = {}; + + if (!req.value['body']) + req.value['body'] = {}; + + req.value['body'] = result.value; + next(); + } + } + }, + + schemas: { + serieSchema: Joi.object().keys({ + title: Joi.string().optional(), + synopsis: Joi.string().optional() + }), + + idSchema: Joi.object().keys({ + param: Joi.string().regex(/^[0-9a-fA-F]{24}$/).required() + }) + } +}