Search code examples
javascriptobjectjoi

How to get all the keys in a joi schema?


I had a joi schema like this

const userModel = Joi.object({
  id: Joi.string().min(3).max(50),
  username: Joi.string().min(10).max(100)
  ... other 10 properties
})

the thing is I wanted to get the values of all the keys like

["id","username",...]

I tried using Object.keys(userModel), but that is returning an unexpected value like

[
  "isJoi",
  "_currentJoi",
  "_type",
  "_settings",
  "_baseType",
  "_valids",
  "_invalids",
  "_tests",
  "_refs",
  "_flags",
  "_description",
  "_unit",
  "_notes",
  "_tags",
  "_examples",
  "_meta",
  "_inner"
]

Solution

  • const userModel = Joi.object({
        id: Joi.string().min(3).max(50),
        username: Joi.string().min(10).max(100)
    });
    
    const keys = Object.keys(userModel.describe().keys);
    
    console.log(keys)