我一直在尝试在我们的节点应用程序中实现Joi(joi是独立的,而不是hapi),它似乎正确验证了架构,但错误始终是相同的
[ValidationError: value must be an object]
name: 'ValidationError',
details:
[ { message: 'value must be an object',
path: 'value',
type: 'object.base',
context: [Object] } ],
_object:.....
我从来没有得到关于它失败的关键字的细节以及它失败原因的描述。
这是我使用的示例模式:
exports.workersSchema =
{
workers: joi.array({
id: joi.string().alphanum(),
wID: joi.object({
idValue: joi.string().alphanum()
}),
person: {
governmentIDs: joi.array({itemID: joi.string().alphanum()}),
legalName: joi.object({
givenName: joi.string(),
middleName: joi.string(),
preferredSalutations: joi.array(
{
salutationCode: {
longName: joi.string()
}
}
),
preferredName: joi.object().keys({
FormattedName: joi.string()
}),
}),
birthDate: joi.string().alphanum()
}
})
}
这是我发送的json对象:
{"workers" : [
{
"id" : "",
"wID" : {
"idValue" : ""
},
"person" : {
"governmentIDs":[{
"itemID": "asd"
}],
"legalName":{
"givenName" : "PA",
"middleName" : "",
"preferredSalutations" : [{
"salutationCode" : {
"longName" : ""
}
}],
"preferredName" : {
"FormattedName" : ""
},
"birthDate" : ""
}]
}
我在这里做错了什么?我甚至尝试在博客上关注某些内容,而示例显示的详细信息除了
之外我什么都没有"value must be an object"
它可以正确验证它,但是当它看到错误值时,它只会给出错误而没有别的。
另外,如果你看一下' wID'部分它有一个“idValue'对象但是当我摆脱idValue并且只是在wID键上放置一个alphanum时,它也会通过验证。
PS。验证作为对象的键时。我是否必须使用
进行验证key: Joi.object({
a:Joi.string()
})
或者我可以这样做吗?:
key: {
a:Joi.string()
}
非常感谢您的帮助!
答案 0 :(得分:1)
我认为有几个问题。首先,确保您要验证的对象确实是带有object
密钥的workers
。验证似乎表明你没有为这个基值提供一个对象(也许是一个数组)?
同样在少数情况下,我认为您错误地使用了API(例如joi.array(...)
无效)。我已经修改了你的架构,以便按我的想法工作。如果没有,发布一个样本对象,我会修改。
var schema = {
workers: Joi.array().required().includes({
id: Joi.string().alphanum(),
wID: {
idValue: Joi.string().alphanum()
},
person: {
governmentIDs: Joi.array().includes(Joi.string().alphanum()),
legalName: {
givenName: Joi.string(),
middleName: Joi.string(),
preferredSalutations: Joi.array().includes(Joi.string()),
preferredName: {
formattedName: Joi.string()
},
},
birthDate: Joi.string().alphanum()
}
})
};
这是该架构的有效对象:
var goodExample = {
workers: [
{
id: 'bhdsf78473',
wID: {
idValue: 'idvalue1'
},
person: {
governmentIDs: ['id1', 'id2'],
legalName: {
givenName: 'Johnny',
middleName: 'Michael',
preferredSalutations: ['sir', 'Dr'],
preferredName: {
formattedName: 'Sir Johnny Michael Smith'
}
},
birthDate: '2411986'
}
}
]
};
这是一个无效的:
var badExample = {
workers: [
{
id: 'bhdsf7^£$%^£$%8473', // Here's the issue
wID: {
},
person: {
governmentIDs: ['id1', 'id2'],
legalName: {
givenName: 'Johnny',
middleName: 'Michael',
preferredSalutations: ['sir', 'Dr'],
preferredName: {
formattedName: 'Sir Johnny Michael Smith'
}
},
birthDate: '2411986'
}
},
],
};
Joi应为Joi.assert(example, schema);
提供详细的输出:
$ node index.js
/.../node_modules/Joi/lib/index.js:121
throw new Error(message + error.annotate());
^
Error: {
"workers": [
{
"wID": {},
"person": {
"governmentIDs": [
"id1",
"id2"
],
"legalName": {
"givenName": "Johnny",
"middleName": "Michael",
"preferredSalutations": [
"sir",
"Dr"
],
"preferredName": {
"formattedName": "Sir Johnny Michael Smith"
}
},
"birthDate": "2411986"
},
"id" [1]: "bhdsf7^£$%^£$%8473"
}
]
}
[1] workers at position 0 fails because id must only contain alpha-numeric characters
at root.assert (/.../node_modules/Joi/lib/index.js:121:19)
at Object.<anonymous> (/.../index.js:57:5)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:929:3
注意:此答案使用的是Joi 5.1.2(API:https://github.com/hapijs/joi/blob/v5.1.0/README.md)。 Joi.array().includes()
将在下一版本中删除,以支持Joi.array().items()
您发布的对象不是有效的JavaScript对象,因为它缺少一些结束}括号。这是有效版本:
var obj = {
"workers" : [{
"id" : "", // <-------- Shouldn't be empty
"wID" : {
"idValue" : ""
},
"person" : {
"governmentIDs":[{
"itemID": "asd"
}],
"legalName":{
"givenName" : "PA",
"middleName" : "",
"preferredSalutations" : [{
"salutationCode" : {
"longName" : ""
}
}],
"preferredName" : {
"FormattedName" : ""
},
},
"birthDate" : ""
}
}]
};
如果我使用我提供的架构验证,我收到以下消息(使用Joi 5.1.0):
[1] workers at position 0 fails because id is not allowed to be empty