我正在尝试使用joi包在Hapi节点服务器中验证我的POST和PUT请求。
这是正确的方法吗?
'use strict';
var Joi = require('joi'),
Id = Joi.number().positive().required(),
Firstname = Joi.string().min(3).max(15),
Lastname = Joi.string().min(3).max(15),
Email = Joi.string().email(),
Username = Joi.string().min(3).max(15);
exports.create = {
payload: {
firstname: Firstname.required(),
lastname: Lastname.required(),
email: Email.required(),
username: Username.required()
}
};
exports.findById = {
params: {
userId: Id
}
};
exports.update = {
params: {
userId: Id
},
payload: Joi.object().keys({
firstname: Firstname,
lastname: Lastname,
email: Email,
username: Username
}).or('firstname','lastname','email','username')
};
答案 0 :(得分:0)
当然,您可以模块化验证检查。
使用您的配置,您应该可以像这样使用它:
server.route({
method: 'POST',
path: '/find/{userId}',
config: {
validate: {
findById // this is defined by your code => exports.findById
},
handler: function (req, reply) {
reply({
receivedUserId: req.params.userId
})
}
}
});