{"id":32477,"date":"2016-03-22T17:17:29","date_gmt":"2016-03-22T11:47:29","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=32477"},"modified":"2016-03-23T09:57:47","modified_gmt":"2016-03-23T04:27:47","slug":"using-dependencies-in-json-schema-version-draft-v4","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/using-dependencies-in-json-schema-version-draft-v4\/","title":{"rendered":"Using &#8220;dependencies&#8221; in json schema (version : draft-v4)"},"content":{"rendered":"<p>Json Schema has another interesting feature which allows value of some property of json schema to depend upon other fields value. This could be done using &#8220;dependencies&#8221; which allows specifying dependent object \/ property on the basis of value of the field which is using dependencies keyword.<\/p>\n<p>An interesting scenario could be where you have an enumeration and want to choose another fields value based on the provided value.<\/p>\n<p>Suppose we have scenario as below:<\/p>\n<ol>\n<li>we have field minimumEligibilityAge, graduate and courseName.<\/li>\n<li>Field minimumEligibilityAge will be used iff person is not graduate otherwise field courseName is used to provide the graduation course name.<\/li>\n<\/ol>\n<p>Our schema without dependencies could be as below:<\/p>\n<p>[code]<br \/>\n  {<br \/>\n    &quot;title&quot;: &quot;Test dependncies&quot;,<br \/>\n    &quot;readOnly&quot;: false,<br \/>\n    &quot;$schema&quot;: &quot;http:\/\/json-schema.org\/draft-04\/hyper-schema&quot;,<br \/>\n    &quot;description&quot;: &quot;This a test to show how we can use dependencies in json schema&quot;,<br \/>\n    &quot;properties&quot;:{<br \/>\n         &quot;graduate&quot;: {<br \/>\n            &quot;title&quot;:&quot;Graduate?&quot;,<br \/>\n            &quot;type&quot;:&quot;string&quot;,<br \/>\n            &quot;enum&quot;:[&quot;Yes&quot;,&quot;No&quot;]<br \/>\n        },<br \/>\n        &quot;minimumEligibilityAge&quot;:{<br \/>\n            &quot;title&quot;:&quot;Enter Age&quot;,<br \/>\n            &quot;type&quot;:&quot;number&quot;<br \/>\n            },<br \/>\n        &quot;courseName&quot;:{<br \/>\n            &quot;title&quot;:&quot;Enter Graduation Course Name&quot;,<br \/>\n            &quot;type&quot;:&quot;string&quot;<br \/>\n        }<br \/>\n    },<br \/>\n    &quot;type&quot;: &quot;object&quot;,<br \/>\n    &quot;required&quot;:[<br \/>\n        &quot;graduate&quot;<br \/>\n     ]<br \/>\n  }<br \/>\n[\/code]<\/p>\n<p>If we have to see a ui representation of json schema for better understanding, it would be like in image below :<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-32478\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/02\/Screen-Shot-2016-02-09-at-1.20.41-PM.png\" alt=\"Screen Shot 2016-02-09 at 1.20.41 PM\" width=\"688\" height=\"236\" \/><\/p>\n<p>In the form <strong>boolean<\/strong> is represented by <strong>checkbox<\/strong> and <strong>number<\/strong> and string are represented by text box.<\/p>\n<p>Now, let&#8217;s add dependencies to <strong>graduate<\/strong> field for above scenario.<\/p>\n<p>[code]<br \/>\n  {<br \/>\n    &quot;title&quot;: &quot;Test dependncies&quot;,<br \/>\n    &quot;readOnly&quot;: false,<br \/>\n    &quot;$schema&quot;: &quot;http:\/\/json-schema.org\/draft-04\/hyper-schema&quot;,<br \/>\n    &quot;description&quot;: &quot;This a test to show how we can use dependencies in json schema&quot;,<br \/>\n    &quot;properties&quot;:{<br \/>\n        &quot;graduate&quot;: {<br \/>\n            &quot;title&quot;:&quot;Graduate?&quot;,<br \/>\n            &quot;type&quot;:&quot;string&quot;,<br \/>\n            &quot;enum&quot;:[&quot;Yes&quot;,&quot;No&quot;],<br \/>\n             &quot;options&quot;: {<br \/>\n             &quot;dependencies&quot;:[<br \/>\n                {&quot;id&quot;:&quot;minimumEligibilityAge&quot;,&quot;value&quot;:&quot;Yes&quot;},<br \/>\n                {&quot;id&quot;:&quot;courseName&quot;,&quot;value&quot;:&quot;No&quot;}<br \/>\n                ]<br \/>\n             }<br \/>\n        },<br \/>\n        &quot;minimumEligibilityAge&quot;:{<br \/>\n            &quot;id&quot;:&quot;minimumEligibilityAge&quot;,<br \/>\n            &quot;title&quot;:&quot;Enter Age&quot;,<br \/>\n            &quot;type&quot;:&quot;number&quot;,<br \/>\n\t\t\t&quot;options&quot;:{&quot;hide_display&quot;:true}<br \/>\n            },<br \/>\n        &quot;courseName&quot;:{<br \/>\n            &quot;id&quot;:&quot;courseName&quot;,<br \/>\n            &quot;title&quot;:&quot;Enter Graduation Course Name&quot;,<br \/>\n            &quot;type&quot;:&quot;string&quot;<br \/>\n        }<br \/>\n    },<br \/>\n    &quot;type&quot;: &quot;object&quot;<br \/>\n  }<\/p>\n<p>[\/code]<\/p>\n<p>A ui representation for both the cases could be:<\/p>\n<p>Case: when person is graduate:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-32479\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/02\/Screen-Shot-2016-02-09-at-1.39.54-PM.png\" alt=\"Screen Shot 2016-02-09 at 1.39.54 PM\" width=\"514\" height=\"272\" \/><\/p>\n<p>Case: when person is not graduate<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-32480\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/02\/Screen-Shot-2016-02-09-at-1.40.02-PM.png\" alt=\"Screen Shot 2016-02-09 at 1.40.02 PM\" width=\"509\" height=\"220\" \/><\/p>\n<p>The UI representation that we have used to illustrate the scenario is from JDorn. A temporary link for reference is https:\/\/github.com\/jdorn\/json-editor. Alternatively, there are several ui representation frameworks available for json schema to html conversion. Also, as json schema is a standard, you could write a framework of your own and customise it.<\/p>\n<p>Hope it helps!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Json Schema has another interesting feature which allows value of some property of json schema to depend upon other fields value. This could be done using &#8220;dependencies&#8221; which allows specifying dependent object \/ property on the basis of value of the field which is using dependencies keyword. An interesting scenario could be where you have [&hellip;]<\/p>\n","protected":false},"author":119,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":54},"categories":[1],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/32477"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/119"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=32477"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/32477\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=32477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=32477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=32477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}