JSON模式:引用本地子模式

我想引用父JSON模式中的子模式。 这是名为child.json的子模式

{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "Child", "description": "Child schema", "type": "object", "properties": { "name": {"type": "string"}, "age": {"type": "integer"} }} 

这里是名为parent.json的父模式,并且所有这两个文件都在同一个文件夹中。 我想引用子模式,我这样做:

 { "$schema": "http://json-schema.org/draft-04/schema#", "title": "Parent", "description": "Parent schema", "type": "object", "properties": { "allOf": [ { "$ref": "file://child.json" } ], "adresse": {"type": "string"} }} 

我有一个错误,说没有find文件child.json。 我已经testing了很多,但是任何人都在工作。
谢谢你的帮助

我find了解决我的问题。 这是解决scheme
上下文是我们总是有两个模式:父母和孩子。 父级必须在他的一个属性中包含子模式,例如:

 "myChild": { "$ref": "child" //referencing to child schema } 

而在他开始的孩子模式中,你必须像这样把一个id放在它上面

 { "id": "child", //important thing not to forget "$schema": "http://json-schema.org/draft-04/schema#" //other codes goes here } 

现在用jaySchemavalidation你会这样做

 var js = new JaySchema(); var childSchema = require('./child.json'); var parentSchema = require('./parent.json'); //other codes goes here js.register(childSchema); //important thing not to forget js.validate(req.body, schema, function(err) { if (err) //your codes for err }); 

而这一切。 😀
这是我的解决scheme,但这不是最好的,我希望这会有所帮助。 感谢所有您的答案

$ref值可以是URI引用 – 它们不需要是绝对URI。 所以在这里,你应该能够使用:

 {"$ref": "child.json"} 

它应该适当地解决。

如果父类和子模式位于类path中并处于同一位置,则最好的做法是使用自定义resource URIscheme以绝对URI加载它:

 final JsonSchema schema = factory.getJsonSchema("resource:/path/to/parent.json"); 

然后你可以使用{ "$ref": "child.json" }引用你的子模式(因为JSON引用是相对于当前模式的URIparsing的)