最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get all the keys in a joi schema? - Stack Overflow

programmeradmin3浏览0评论

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"
]

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"
]
Share Improve this question asked Oct 7, 2019 at 12:32 Malik BagwalaMalik Bagwala 3,0198 gold badges27 silver badges40 bronze badges 3
  • Why do you need the keys of the validation schema? – a1300 Commented Oct 7, 2019 at 12:57
  • @a1300 I need to pick only the required fields from the main data..eg. if the main data is {id: "123",username: "abc",email:"email.com"} and my model is {id,name} I could only pick the required keys from the main data using a tool like lodash's pick which accepts an array of keys as its second parameter – Malik Bagwala Commented Oct 7, 2019 at 13:13
  • Ok, please edit your question and add an example how the data looks. Describe in detail what you try to achieve. Which properties should be required? That will make it easier for me to answer. – a1300 Commented Oct 7, 2019 at 13:18
Add a comment  | 

4 Answers 4

Reset to default 8

The reason for the unexpected behaviour is due to the fact the userModel is not an ordinary object, it's a joi object.

A possible solution is to check the userModel._ids._byKey.keys() to get a Map iterator of all of the keys in the schema. The problem with this solution is that you count on the internals of the Joi framework.

I might suggest another approach: Extract the required fields in a separate data structure - array or object and extend the Joi schema based on that.


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)

I've just been struggling with this myself. After some digging in the api I found this

For a Joi.object This returns a simple object with a property called keys

{
  type: 'object',
  keys: {
    nestedItem1: { type: 'any', rules: [Array], allow: [Array] },
    nestedItem2: { type: 'string', flags: [Object], rules: [Array] },
  }
}

It's pretty straightforward from there.

Something like this could help.

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


const keys = [];
for (var i of userModel._ids._byKey.entries()){
  keys.push(i[0])
}

console.log(keys);
发布评论

评论列表(0)

  1. 暂无评论