[英]How to implement Joi validation in hapi.js?
I just want to implement Joi
in Hapi
API.我只想在
Hapi
API 中实现Joi
。
server.route([
{
method: 'POST',
path: '/login',
config: {
tags: ['login', 'auth'],
auth: false,
validate: {
payload: payloadValidator,
failAction: (req, h, source, error) => {
console.log("Error ::: ", source.details[0].message);
return h.response({ code: 0, message: source.details[0].message });
}
}
},
handler: async (request, h) => {
console.log(request.payload.email);
console.log(request.payload.password);
...
}
}
]);
Hear I call payloadValidator
.听说我打电话给
payloadValidator
。
const payloadValidator = Joi.object({
email: Joi.string().required(),
password: Joi.string().required()
}).options({ allowUnknown: true });
Actually I'm new with hapi
and I'm missing something in my code.实际上,我是
hapi
新手,我的代码中缺少某些内容。 Can anyone help me to fix this issue?谁能帮我解决这个问题?
Required output所需的输出
If I do not pass email
then the app must throw an error of Email is required
and it should be the same with the password
field also.如果我不通过
email
那么应用程序必须抛出Email is required
错误,并且password
字段也应该相同。
Error:错误:
Error ::: "email" is required Debug: internal, implementation, error Error: Lifecycle methods called before the handler can only return an error, a takeover response, or a continue signal at Request._lifecycle (/var/www/html/hapi/node_modules/@hapi/hapi/lib/request.js:326:33) at process._tickCallback (internal/process/next_tick.js:68:7)
As an error suggests Lifecycle methods called before the handler can only return an error, a takeover response, or a continue signal you have to return takeover response.由于错误表明在处理程序之前调用的 Lifecycle 方法只能返回错误、接管响应或继续信号,您必须返回接管响应。
return h.response({ code: 0, message: source.details[0].message }).takeover();
For more information you can visit this link : reference link有关更多信息,您可以访问此链接: 参考链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.