JSON Reference as value of 'oneOf' slot

849 views
Skip to first unread message

William Fisher

unread,
May 26, 2014, 12:09:35 AM5/26/14
to jsons...@googlegroups.com
I am new to JSON Schemas and JSON References, and I have run into an issue in combining them. 

Can a JSON Schema contain a JSON reference in any slot?  

For example, here are two schemas. The first schema works fine, but the second schema is rejected as invalid. Is schema2 really an invalid schema, or is this failure just a limitation of the implementation? Thanks.


import jsonschema

schema1 = {
  "type" : "object",
  "oneOf" : [
    { "properties" : { "a" : { "type" : "string" } }, "required" : ["a"] },
    { "properties" : { "b" : { "type" : "integer" } }, "required" : ["b"] },
  ],
}

# The following lines work as expected.

jsonschema.validate({"a" : "s"}, schema1)
jsonschema.validate({"b" : 1}, schema1)


schema2 = {
  "type" : "object",
  "oneOf" : {"$ref" : "#/objectChoice"},
     
  "objectChoice" : [
    { "properties" : { "a" : { "type" : "string" } }, "required" : ["a"] },
    { "properties" : { "b" : { "type" : "integer" } }, "required" : ["b"] },
  ]
}

# The following lines throw an exception:
#
# jsonschema.exceptions.SchemaError: {'$ref': '#/objectChoice'} is not of type u'array'

jsonschema.validate({"a" : "s"}, schema2)
jsonschema.validate({"b" : 1}, schema2)

Julian Berman

unread,
May 27, 2014, 7:47:06 AM5/27/14
to jsons...@googlegroups.com
Hey.

This is an invalid schema yeah. "$ref" is a schema property, you can basically stick {"$ref" ...} in places where you could have a schema. oneOf takes an array, not a schema, so you can't stick an object with a $ref in there.

You could still obviously make each of the schemas *in* oneOf be a ref if you'd like.

Cheers,

Julian
Reply all
Reply to author
Forward
0 new messages