简体   繁体   中英

joi validation for passing key value pair to other schema

I have a object like

{
"a": {
    "name": "name of a",
    "year": "1"
},
"b": {
    "name": "name of b",
    "year": "2"
},
"c": {
    "name": "name of c",
    "year": "123456"
}
}

I'm using the below validation

var mainobject=Joi.object().pattern(/^/,subObject),
var suboject=joi.object({
name:joi.string(),
year:joi.string()
})

With this code, can I be able to iterate through objects?Is there anything wrong with my first code? Here in the main object, I used pattern for unknown keys. If I want to include one more data like details,

{
"a": {
"name": "name of a",
"year": "1"
"details":(should include name and year in "name":'name of a',"year":'1')
},
"b": {
"name": "name of b",
"year": "2"
},
"c": {
"name": "name of c",
"year": "123456"
}
}

How can I achieve the above solution? Please, help me out.

Use .unknown() API to allow unknown key.

var subobject = Joi.object({
  name:Joi.string(),
  year:Joi.string()
}).unknown();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM