JSON模式:引用本地子模式 [英] JSON schema: referencing a local child schema

查看:88
本文介绍了JSON模式:引用本地子模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to reference a child schema in a parent json schema. Here is the child schema named 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的父模式,所有两个文件都在同一文件夹中.我想引用子模式,我这样做:

and here is the parent schema named parent.json and all the two files are in the same folder. I want to refer to the child schema and i do like this:

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

我遇到一个错误,指出未找到文件child.json.我已经测试了很多点,但是任何人都在工作.
谢谢您的帮助

I've an error saying that the file child.json is not found. I've tested lot of tings but anyone is working.
Thanks for your help

推荐答案

我已经找到了解决问题的方法.这是解决方案
上下文是我们总是有两个模式:父模式和子模式.父级必须在其属性之一中包括子级架构,例如:

I've found a solution for my problem. Here is the solution
The context is that we always have two schemas: the parent and the child. The parent have to include the child schema in one of his properties like this par exemple:

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

在他开始的子模式中,您必须像这样在其上放置一个ID

And in the child schema at the beginning of him, you have to put an id on it like this

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

现在要使用jaySchema进行验证,您将像这样

Now for the validation with jaySchema you will do like this

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
});

仅此而已. :-D
这是我的解决方案,但这不是最好的解决方案,希望对您有所帮助.谢谢大家的回答

And it's all. :-D
This is my solution but it's not the best and i hope that it will help. Thanks to all for your answers

这篇关于JSON模式:引用本地子模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆