[英]Descriptive Hapi/Joi validation error
我一直在嘗試在我們的節點應用程序中實現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()
}
非常感謝你的幫助!
我認為有幾個問題。 首先,確保您要驗證的object
確實是具有workers
鍵的object
。 驗證似乎表明你沒有提供這個基值的對象(也許是一個數組)?
同樣在少數情況下,我認為你錯誤地使用了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);
提供很好的詳細輸出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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.