javascript - How can I use Joi to sanitize the query parameter for hapi? -
i wondering if can use joi automatically sanitize query parameter instead of manually doing that?
this have done route.
var querystring = require('querystring'); function datetostring(date) { return moment(new date(querystring.unescape(date))).toisostring(); } function posts(request, reply) { request.query.from = datetostring(request.query.from); }
and joi schema.
var toppostsconfig = { description: 'top posts', plugins: { 'hapi-swagger': { order: 1 } }, validate: { query: { form: joi.date().iso().optional().default(moment().subtract(2, 'day').utc().format()).description('start date query') } } } { method: 'get', path: '/posts', handler: posts, config: toppostsconfig }
just wondering if joi has special method auto sanitize query parameter unescaped characters?
edit: request comes in 2016-02-05t20%3a26%3a34.916z
escaped browser. thinking if joi can unescape automatically don't have manually.
first off have typo on field validating - "form" instead of "from".
joi should receive decoded url query. it's easy check replacing joi query validation object function , inspecting value:
server.route({ method: 'get', path: '/posts', handler: (req, reply) => { reply(req.query); }, config: { description: 'top posts', validate: { query: (value, options, next) => { console.log(value); next(null, value); }, }, }, });
when fix typo in example works expected.
if want additional data processing can use function form of validation , using joi.validate() function body.
Comments
Post a Comment