Is there any library around to generate Javascript typed object (JS functions) from a JSON schema? Basically the equivalent JS version of this http://code.google.com/p/jsonschema2pojo/. Thanks.
EDIT:
Starting from :
{
"description": "An entity",
"type":"object",
"properties": {
"geometries": {"type": "array",
"items": {
"$ref" : "geometry"
}
}
}
}
I'd like some code like this to be generated for me
function Entity {
this.geometries;
}
Obviously the schema could be more complex with $ref's etc I hope this gives the idea.
Does this djvi library cover your requirements?
The provided example shows:
var jsonSchema = {"common":{"properties":{"type":{"enum":["common"]}},"required":["type"]}};
var env = new djvi();
env.addSchema('test', jsonSchema);
env.instance('test#/common');
// => { type: 'common' }
I suspect that is the solution you are after.
Now this is not the exact solution you are after, but I had a similar problem and created the following solution to return a parent object as a function, it may assist:
var dbdict = {
"title": "Entity",
"description": "An entity",
"type":"object",
"properties": {
"geometries": {"type": "array",
"items": {
"$ref" : "geometry"
}
}
}
}
var walkJSONSchema = function (JSONSchema, returnFunction) {
var walkObject = function(PROPS) {
var $this = this,
$child = {}
;
if(returnFunction == true) {
$child = new function() {};
}
//console.log("PROPS");
//console.log(PROPS);
for(var key in PROPS) {
console.log("key:"+key+" type:"+PROPS[key].type+" default:"+PROPS[key].default);
switch(PROPS[key].type) {
case "boolean":
$child[key] = PROPS[key].default || undefined;
break;
case "integer":
case "number":
$child[key] = PROPS[key].default || undefined;
break;
case "array":
$child[key] = [].push($this.walkObject(PROPS[key].properties));
break;
case "object":
$child[key] = $this.walkObject(PROPS[key].properties);
break;
case "string":
$child[key] = PROPS[key].default || undefined;
break;
};
};
return $child;
}
return walkObject(JSONSchema.properties);
}
Entity = walkJSONSchema(dbdict, true);
Of course you could script the retrieval of the "Entity" from the schema doc however you like, but this way at least you get a function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With