mutually exclusive dependencies

245 views
Skip to first unread message

Dean Landolt

unread,
May 20, 2011, 2:13:45 PM5/20/11
to json-...@googlegroups.com
I can't seem to figure out how to model mutually exclusive dependencies (either an object contains the "foo" prop or the "bar" prop, not both nor neither). I figure I'm missing something obvious. Any ideas?

Gary Court

unread,
May 20, 2011, 4:56:21 PM5/20/11
to json-...@googlegroups.com
You can use union types to do this:

{
"type" : [{
"properties" : {
"foo" : {}
},
"additionalProperties" : false
}, {
"properties" : {
"bar" : {}
},
"additionalProperties" : false
}]
}

-Gary

> --
> You received this message because you are subscribed to the Google Groups
> "JSON Schema" group.
> To post to this group, send email to json-...@googlegroups.com.
> To unsubscribe from this group, send email to
> json-schema...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/json-schema?hl=en.
>

Dean Landolt

unread,
May 20, 2011, 4:59:10 PM5/20/11
to json-...@googlegroups.com
On Fri, May 20, 2011 at 4:56 PM, Gary Court <gary....@gmail.com> wrote:
You can use union types to do this:

{
       "type" : [{
               "properties" : {
                       "foo" : {}
               },
               "additionalProperties" : false
       }, {
               "properties" : {
                       "bar" : {}
               },
               "additionalProperties" : false
       }]
}


Creative, but what if I have to also define other properties as well? It seems like this would be a relatively common case.

Iain Duncan

unread,
May 21, 2011, 5:08:01 AM5/21/11
to json-...@googlegroups.com
You could always use the extends (although a quick warning, if you look here there used to be a problem with extends and additionalProperties as it would validate both separately rather than combining so the validate assumes it is invalid as there are additional properties that come from the extends.  Apparently this has been fixed now but I'm not sure if the validators have caught up):

{
       "type" : [{
               "properties" : {
                       "foo" : {}
               },
               "extends": {
                       "id" : "common",
                       "properties" : {
                              "commonProp":{}

                       }
               },
               "additionalProperties" : false
       }, {
               "properties" : {
                       "bar" : {}
               },
               "extends" : { "$ref" : "common" },
               "additionalProperties" : false
       }]
}
Reply all
Reply to author
Forward
0 new messages