【发布时间】:2017-02-19 03:42:36
【问题描述】:
http://hapijs.com/tutorials/validation
我想将一个函数传递给我的验证块,以检查 v 是否存在作为源并确认 account、profile 和 ipAddress 存在。文档说这是可能的,但没有使用函数 var 执行此操作的示例。
当我启动我的 API 时,我得到:Error: Invalid schema content: (account)
如何使用命名函数在 Hapi 中进行验证?
代码:
var validateQueryString;
validateQueryString = function(value, options, next) {
console.dir({
value: value,
options: options
});
// do some validation here
return next(null, value);
};
routes.push({
method: 'POST',
path: '/export/{source}/{start}/{end?}',
config: {
validate: {
query: {
account: validateQueryString,
profile: validateQueryString,
ipAddress: validateQueryString
},
params: {
source: joi.string().valid(['a', 'v', 't']),
start: joi.string().regex(utcDateTimeRegex),
end: joi.string().regex(utcDateTimeRegex)
}
}
},
handler: function(apiRequest, apiReply) {}
});
尝试了其他调用方式,例如:
account: function(value, options, next) {
return validateQueryString(value, options, next); }
没有运气。
【问题讨论】:
标签: validation hapijs