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