Skip to content
Advertisement

Joi Nested schema

I am trying to create nested schema in joi and it is throwing error

[Error: Object schema cannot be a joi schema]

var nestedSchema = joi.object({
    b: joi.number()
});

var base = joi.object({
    a: joi.string(),
    nestedData:joi.object(nestedSchema)
});

How should i define nested schema in joi?

Advertisement

Answer

You could use object.keys API

var nestedSchema = joi.object().keys({
    b: joi.number()
});

var base = joi.object({
    a: joi.string(),
    nestedData: nestedSchema
});
Advertisement