I tried, and tried but can't figure it :(
This is the object I need to validate:
let body = {
greeting:
{
stringValue: 'Hello !',
stringListValues: [],
binaryListValues: [],
dataType: 'String'
},
newsletterId:
{
stringValue: '123456789',
stringListValues: [],
binaryListValues: [],
dataType: 'String'
}
};
I need to validate that there is a greeting, and that is has key stringValue and that is not empty. Other values I don't care.
Also, for the second object newsletterId, and that also has key stringValue and that is not empty. Other values I don't care.
I have come up with checking only root object, with this schema:
const schema = {
greeting: Joi.required(),
newsletterId: Joi.required()
};
I read many examples, but I was unable to find none that has this type of structure.
lets define a schema :
const schema = Joi.object().keys({
greeting: Joi.object({
stringValue: Joi.string().required().empty(['', null]),
stringListValues: Joi.array().items(Joi.string()),
binaryListValues: Joi.array().items(Joi.binary())
}).required(),
newsletterId: // same as above
});
and test it like this :
Joi.validate(myObjectToTest, schema, function(error, cleanObject){
console.log(error, cleanObject);
})
Full reference can be found here https://github.com/hapijs/joi/blob/master/API.md
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With