javascript - writing more complex json schemas that have dependencies upon other keys -
i've been writing simple json schemas ran api input call bit more complex. have 1 restful end route can take 3 different types of json:
localhost/foo
can take:
{ "type" : "ice_cream", "cone" : "waffle" ...}
or
{"type" : "hot_dog", "bun" : "wheat" ...}
if "type" key contains "ice_cream", ever want see key "cone" , not key "bun". similiarly if "type" contains "hot_dog" want see "bun" , not "cone". know can pattern match make sure ever see type "ice_cream" or type "hot_dog", don't know how force requirement of other fields if key set value. see there json schema field called "dependency" haven't found examples on how use it.
btw, i'm not sure if input json form (overloading type of json structure takes, effectively), don't have option of changing api.
i got information - turns out can make union of several different objects valid so:
{ "description" : "food", "type" : [ { "type" : "object", "additionalproperties" : false, "properties" : { "type" : { "type" : "string", "required" : true, "enum": [ "hot_dog" ] }, "bun" : { "type" : "string", "required" : true }, "ketchup" : { "type" : "string", "required" : true } } }, { "type" : "object", "additionalproperties" : false, "properties" : { "type" : { "type" : "string", "required" : true, "enum": [ "ice_cream" ] }, "cone" : { "type" : "string", "required" : true }, "chocolate_sauce" : { "type" : "string", "required" : true } } } ] }
i'm still not sure if valid json, since schemavalidator dies on invalid input, accepts valid input expected.
Comments
Post a Comment