51 lines
1.5 KiB
JavaScript
51 lines
1.5 KiB
JavaScript
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()
|
|
})
|
|
}
|
|
}
|