用Joi验证嵌套对象 [英] Validating nested objects with Joi
问题描述
如何使用joi验证此对象?我在Hapi API中使用Joi.
How to validate this object using joi? Im using Joi with Hapi api.
{
"email":"rambo@gmail.com",
"password":"abc123",
"active":"",
"details": {
"firstName": "Rambo",
"lastName": "Comando",
"phoneNumber": "5554446655",
"billing":{
"firstName": "",
"lastName": "",
"phoneNumber": "",
"address": "",
"adress2": "",
"postalCode": "",
"city": "",
"state": "",
"country": "",
"stripeId": ""
}
}
}
我试图这样做,但是没有用.正确的方法是什么?
I tried doing like this, but it is not working. What is the correct way of doing this?
payload: {
email: Joi.string().email().required(),
password: Joi.string().alphanum().min(8).max(30).required(),
active: Joi.boolean(),
details: Joi.object().keys({
firstName: Joi.string().max(50),
lastName: Joi.string().max(50),
phoneNumber: Joi.number().integer().min(10).max(11),
billing : Joi.object().keys({
firstName: Joi.string().max(50),
lastName: Joi.string().max(50),
phoneNumber: Joi.string().integer().min(10).max(11),
address: Joi.string().alphanum(),
adress2: Joi.string().alphanum(),
postalCode: Joi.string().alphanum(),
city: Joi.string(),
state: Joi.string(),
country: Joi.string(),
stripeId: Joi.string().alphanum()
})
})
}
我不确定我在哪里错过了事情.
Im not sure where im missing things up.
推荐答案
您的代码在此行出现错误:
There is an error in your code, at this line:
phoneNumber: Joi.string().integer().min(10).max(11),
Node.js会因此引发异常:
The Node.js throws exception because of it:
TypeError:Joi.integer不是函数
TypeError: Joi.integer is not a function
如果将其更改为string()
或number()
,则一切都会正常进行:
If you change that to either string()
or number()
everything will work as it should:
phoneNumber: Joi.number().min(10).max(11),
这很明显,所以我只是想知道,您怎么错过了?您的Joi模式中的所有其他内容似乎都很好.
It's quite obvious so I'm just wondering, how you've missed it? Everything else with your Joi schema seems alright.
这篇关于用Joi验证嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!