一些与自定义json架构相关的问题

Kas*_*yap 5 json jsonschema

我对json说道.在定义我的RESTful API结果的格式(即JSON)时,我觉得将它作为我自己的JSON模式进行记录会更容易.写一篇文章的时候我几乎没有问题:

  1. 在我的结果JSON中,如何指定它确认的架构的URI?--edit--是否使用$schema属性?
  2. 是否有任何JSON模式版本控制的约定/指南?我应该/可以在我的架构中定义一些属性作为属性吗?我看到JSON模式本身没有定义版本,除非它的URI指定为key的值$schema.
  3. 我可以将我的一个BIG JSON模式分解为多个较小的模式并包含在另一个中吗?与C++中的#include一样,然后引用我发送给用户的JSON中的多个模式作为结果.
  4. 我可以为键"类型"定义自定义值吗?例如,我想重用"date"的定义,如下所示:

[忽略这一行,这是为了让格式化为后面的json ..]

{
    "date":{
        "type":"object",
        "properties":{
            "month":{
                "type":"integer",
                "minimum":1,
                "maximum":12
            },
            "year":{
                "type":"integer",
                "minimum":0
            }
        }
    },
    "personInfo":{
        "type":"object",
        "properties":{
            "name":{
                "type":"string"
            },
            "dateOfBirth":{
                "type":"date"
            }
        }
    },
    "student":{
        "type":"object",
        "properties":{
            "id":{
                "type":"personInfo"
            },
            "pass_out_year":{
                "type":"date"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

而不是在多个地方提供"日期"的属性,如下所示:

{
    "personInfo":{
        "type":"object",
        "properties":{
            "name":{
                "type":"string"
            },
            "dateOfBirth":{
                "type":"object",
                "properties":{
                    "month":{
                        "type":"integer",
                        "minimum":1,
                        "maximum":12
                    },
                    "year":{
                        "type":"integer",
                        "minimum":0
                    }
                }
            }
        }
    },
    "student":{
        "type":"object",
        "properties":{
            "id":{
                "type":"personInfo"
            },
            "pass_out_year":{
                "type":"object",
                "properties":{
                    "month":{
                        "type":"integer",
                        "minimum":1,
                        "maximum":12
                    },
                    "year":{
                        "type":"integer",
                        "minimum":0
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

根据规范中的 5.1类型,它是不可能的,但它似乎是这样一个基本的用例!

Mih*_*bar 4

  1. 正如您正确地理解的那样,$schema可用于指定它符合的模式。
  2. 实际上,我在谷歌搜索 JSON Schema 版本控制时发现了这个主题,并且使用 URI 进行版本控制的想法听起来很合乎逻辑。
  3. 您可以使用$ref链接到随后拉入的另一个架构。
  4. 同样,您可以使用JSON 指针$ref从其他模式导入定义。

您始终可以通过验证架构来测试,看看是否犯了任何错误。