【发布时间】:2018-06-12 18:32:42
【问题描述】:
我在 hapi 有简单的路线
const handler = async(request, reply) => {
const id = Helpers.extractUserId(request)
const payload = request.payload
if (payload.recipient !== id) {
// my code....
} else {
return reply({ success: false, message: '_id and recipient id should not match' })
//I want to return this from routeConfig itself
}
}
const routeConfig = {
method: 'POST',
path: '/requestfriend',
config: {
auth:'jwt',
validate: {
payload: {
recipient: Joi.string().required().error(new Error('recipient is required'))
}
},
handler
}
}
我为if (payload.recipient !== id) 设置了一个条件,这意味着登录用户的 id 和有效负载接收者的相同,那么它应该抛出错误...
我不想这样做,我想把这个条件放在routeConfig 本身......那么这里有没有可以使用的参数,就像auth、validate、handler?
【问题讨论】:
标签: node.js validation hapijs joi