Upload files to 'helpers'

This commit is contained in:
Muhammad Iqbal Rifai 2018-07-04 11:05:37 +02:00
parent a8c710960e
commit a2422d3da8
1 changed files with 50 additions and 0 deletions

50
helpers/routeHelper.js Normal file
View File

@ -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()
})
}
}