Validating subschemas

1,042 views
Skip to first unread message

jan.j...@gmail.com

unread,
Feb 13, 2014, 10:52:00 AM2/13/14
to jsons...@googlegroups.com
Hello,

I do not know how can I keep my schema definitions DRY. Example situation: I have a schema for person and I need also a schema for a collection of person objects. The problem: I want to have only one place where person is defined. I came up with several ways to solve this, but neither one seems to be a satisfying solution:

1) Put everything into one JSON file with subschemas and then tell my validator which subschema to use by refs. However, I could not find a way how to tell jsonschema implementation it should not use the whole schema, but only its subschema. I could extract the subschema myself from the Python dict, but then references to other subschemas would be lost.

2) Put schemas into several JSON files and use their absolute URLs in refs. However, this is not possible very much - they have no absolute URLs, in fact they are internal files and they have no URLs at all. Even if I publish them in my Flask app and they would have absolute URLs, I don't know those from inside the app, because the app could be deployed anywhere. In fact, I know those URLs from inside the app (Flask has it's url_for(..., _external=True) helper), but as long as I want to keep schemas in pure JSON files separated from the rest of the code, this does not seem to lead anywhere.

3) Do not use JSON files and define schema as Python files. In package "schemas" I can have several Python modules and they can import each other and reuse definitions. This actually gets the job done, but well... circular imports could be an issue and in the end of the day, I am not using JSON schema, I am using Python dicts that look like JSON schema... and Python modules... I do not like this solution, I consider it to be a hack.

Is there a "good" solution for this? Thanks!
Honza

Julian Berman

unread,
Feb 14, 2014, 11:43:44 AM2/14/14
to jan.j...@gmail.com, jsons...@googlegroups.com

I can give this a tiny bit more time next week, but it sounds like what you're asking might just be relative refs?

You can use `definitions` and oneOf to do {"properties": {"people": {"oneOf" : [{"$ref": "#/definitions/person"}, {"items": {"$ref": "#/definitions/person"}}]

and then put your definition in the definitions section of your schema.

You certainly can use multiple files as well if you want. I can elaborate more later. You might want to check out the RefResolver.store if you haven't seen it as well.

Cheers
Julian

--
You received this message because you are subscribed to the Google Groups "jsonschema - An implementation of JSON Schema for Python" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema+...@googlegroups.com.
To post to this group, send email to jsons...@googlegroups.com.

Honza Javorek

unread,
Mar 4, 2014, 7:54:55 AM3/4/14
to jsons...@googlegroups.com
Hi,

I just returned to work on the part of my project which deals with validation. Your answer pointed me to right places - I knew what to search for and then I encountered this http://spacetelescope.github.io/understanding-json-schema/structuring.html, which explains ref concepts quite a lot. I'll experiment with this and hopefully it'll get me where I need to be :-)

Thanks!
Honza

Honza Javorek

unread,
Mar 4, 2014, 9:38:06 AM3/4/14
to jsons...@googlegroups.com
Just got it working actually with local refs and separate files according to this: https://github.com/Julian/jsonschema/issues/98 Works like a charm :-)

Thank you again,
Honza
Reply all
Reply to author
Forward
0 new messages