Joi object schema from dot notation

Viewed 191

I am trying to convert a mongoose schema into a basic joi schema using mongoose's Model.schema.paths object. This object exposes paths and types in dot notation e.g. {path: "a.b.c", type: "string"}. Is there any way to simply convert this to a Joi schema?

Joi.object({"a.b.c": Joi.string(), "a.b.d": Joi.number()})

does not produce the desired result of

Joi.object({
    a: Joi.object({
        b: Joi.object({
            c: Joi.string(),
            d: Joi.number()
        })
    })
})

I can achieve the desired result using for example lodash.set(path, obj) to convert path, type pairs into an object and then parsing this and converting each nested object into a Joi.object() call instead, but this feels hacky and I would like to avoid it if possible.

0 Answers
Related