How to validate this object using joi? Im using Joi with Hapi api.
{
"email":"[email protected]",
"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:
The Node.js throws exception because of it:
If you change that to either
string()
ornumber()
everything will work as it should:It's quite obvious so I'm just wondering, how you've missed it? Everything else with your Joi schema seems alright.