这个djvi库是否满足您的要求?
提供的示例显示:
var jsonSchema = {"common":{"properties":{"type":{"enum":["common"]}},"required":["type"]}};
var env = new djvi();
env.addSchema('test', jsonSchema);
env.instance('test#/common');
// => { type: 'common' }
我怀疑这是您所追求的解决方案。
现在这不是您所追求的确切解决方案,但我遇到了类似的问题并创建了以下解决方案以将父对象作为函数返回,它可能会有所帮助:
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.