How to make conditional schemas using JSONSchema?

3,766 views
Skip to first unread message

deb...@gmail.com

unread,
Jul 25, 2015, 3:12:42 AM7/25/15
to json-...@googlegroups.com
Hi all,

Is there a way to define conditional schema using JSONSchema. For example,

{"name":"A1",parmas":{"paramA":value1,"paramB":value2}

is valid and the following:

{"name":"A2",parmas":{"paramD":value3,"paramE":value4,"paramF":value5}

is also valid. The schema of params is dependent on what the value of the key "name" is. Is there a way to represent this using JSONSchema?

In fact, we are trying to autogenerate documentation of JSON schema using something likehttp://lbovet.github.io/typson-demo/ Any other tool to generate documentation from such a schema for a JSON object will also do. Is there any other tool to do this? We are using Voluptuous to do the validation in whihc handle this case. so the main problem is not in validation but in generating the documentation from a given JSON schema.

Thanks in advance,
Debi

Joe McIntyre

unread,
Jul 25, 2015, 1:22:13 PM7/25/15
to JSON Schema, deb...@gmail.com
Debi,

On the first question. Yes, conditional content can be defined using enumerations for the list of selectors ("name" in your example), and different sub-schemas can be associated with each of the enum values.

An example using international addresses can be seen at jsonvalidate.com using the Import menu to load example "4C Selector Driven Content: Address (Valid)". The schema shows a schema for a North American address where "country" is the selector (CAN, MEX, USA) and different sub-schemas are provided for each to match the specific postal addressing for each country.

Regards,
Joe

Dave Cunningham

unread,
Aug 2, 2015, 11:20:59 AM8/2/15
to JSON Schema, deb...@gmail.com
I tried using oneOf and enum with a single value, but at least with the Python jsonschema library, the error messages are very bad when there is a schema violation.  It simply says:

"$MASSIVE_TEXT" is not valid under any of the given schemas

Whereas what it should give an error message that is specific to the kind of object the user was trying to write.

Is there a workaround for that?

thanks

Joe McIntyre

unread,
Aug 7, 2015, 11:52:28 AM8/7/15
to JSON Schema, deb...@gmail.com
Dave,

  The example referenced in my prior reply works with the jsonschema Python library, but I am not sure of the difference between that example and the case you are describing. Can you provide a schema and JSON data example that causes this error?

Thanks.
Joe

neothe...@googlemail.com

unread,
Aug 18, 2015, 3:31:23 AM8/18/15
to JSON Schema, deb...@gmail.com
Hi Dave,

I face the same problem. In your example, just change "CAN" to "USA" and you see the error:

Invalid: Data does not match any schemas from "oneOf"
JSON Schema element: /properties/address/properties/national/oneOf
JSON Content path: /address/national


This is a problem of JSON Schema. It is not powerful enough. Ideally, you want to use "country" as discriminator/selector for a specific schema. The possible errors would then either be:

1.  "'country' did not match CAN, USA, or MEX"
2. (if country is CAN) "'province' field is missing"
etc.

I think this is a major sore point. There are many JSON schemas which have this structure. Take the popular GeoJSON. It is at the root either a Feature, FeatureCollection, or any of the geometries. And all this is discriminated by the "type" field which is one of "Point", "MultiPoint", "LineString", "MultiLineString", "Polygon", "MultiPolygon", "GeometryCollection", "Feature", or "FeatureCollection". If you would write a JSON Schema for GeoJSON, you could not generate any useful error message because any error would be dismissed as "sorry, no schema matched" as you currently would be forced to use the less than optimal anyOf.

I cannot use JSON Schema due to that issue. I really hope this gets solved at some point.

Cheers
Maik
Reply all
Reply to author
Forward
0 new messages