How To Validate Nested Object Whose Keys Should Match With Outer Objects Another Key Whose Value Is Array Using Joi?
I have object which I want to validate. // valid object because all values of keys are present in details object var object = { details: { key1: 'stringValue1',
Solution 1:
Using object.pattern
and array.length
var schema = Joi.object({
details: Joi.object().pattern(Joi.in('keys'), Joi.string()),
keys: Joi.array().length(Joi.ref('details', {
adjust: (value) => Object.keys(value).length
}))
});
Solution 2:
You can validate the array(if you want) then make a dynamic schema and validate that.
const arrSchema = Joi.object({
keys: Joi.array()
});
then,
const newSchema = Joi.object({
details: Joi.object().keys(data.keys.reduce((p, k) => {
p[k] = Joi.string().required();
return p;
},{})),
keys: Joi.array()
})
This should probably do it.
You have to set allowUnknown: true
in validate()
option.
Post a Comment for "How To Validate Nested Object Whose Keys Should Match With Outer Objects Another Key Whose Value Is Array Using Joi?"