Querying schema information at runtime?

40 views
Skip to first unread message

jason.wh...@gmail.com

unread,
Jul 10, 2016, 3:37:04 PM7/10/16
to JSON Schema

There are lots of libraries that will validate a JSON document against a schema but they all seem geared toward returning "yes" or "no". I would like to find one that will let me interact with instances together with their schema. I would like to write code that would basically do this:

var schema = LoadSchema("personSchema.json");
var obj = "{ firstName:Fred; lastName:Flintsone; age:40 }"

foreach(var key in obj.keys){
var itemschema = GetSchemaDefiniton(key);
}

The hypothetical "GetSchemaDefinition" would take some kind of input (JSONPath or something?) and return the section of the schema that would be used to validate the section. So, for example given the example schema below [1] and the example code above I'd like to query "firstName" key and get back { "type" : "string" } as output.

Anyone know a way to do this?



Why do I want to do this
--------------------------------
I want to do attribute-driven metaprogramming a la Microsoft.NET. It appears I can add arbitrary elements to schema properties (or at least the newtonsoft parser [2] will let me). This is a direct analogue to applying custom attributes as type annotations in C#. If I do that then I need a way to accept an arbitrary object and match up the custom attributes to the input document somehow (even if that means transforming the document and adding the attributes as children somehow). This would allow me to have relatively "flat" documents and would allow me to throw compact documents around while still processing them in intelligent ways.


[1] Sample schema from http://json-schema.org/examples.html

{
"title": "Example Schema",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
}
},
"required": ["firstName", "lastName"]
}

[2] JSON validator at at http://www.jsonschemavalidator.net

Henry Andrews

unread,
Sep 7, 2016, 12:10:29 AM9/7/16
to JSON Schema, jason.wh...@gmail.com
It's in Python, but we more or less implemented this at Riverbed:

https://github.com/riverbed/sleepwalker (data side, uses an instance of the schema side)

Note that this was based on JSON schema plus a non-hyperschema approach to HATEOAS, for reasons that aren't worth going into right now :-)  The schema part is of interest and you can just ignore the links/relations stuff.

We used JSON pointers as URI fragments.  Given a DataRep object d for URI "http://foo.com/bar", d['baz'] would return a DataRep attached to the same resource but the URI set to "http://foo.com/bar#/baz", ant the internal schema object similarly adjusted to point to the part of the schema that validates that part of the data.

You can find a much more comprehensive explanation here:  https://support.riverbed.com/apis/steelscript/sleepwalker/datarep.html

I'm not aware of a similar JavaScript library but I haven't looked at such things since leaving Riverbed over a year ago until just the last few weeks.

thanks,
-henry
Reply all
Reply to author
Forward
0 new messages