json - 使用JSON模式来指定带有某些必填字段的“any”类型模式

标签 json jsonschema

假设我有以下JSON模式

{
 "name":"Product",
 "type":"object",
 "properties":{
   "id":{
     "type":"number",
     "required":true
   },
   "name":{
     "description":"Name of the product",
     "required":true
   },
   "price":{
     "required":true,
     "type": "number",
     "minimum":0,
     "required":true
   },
   "tags":{
     "type":"array",
     "items":{
       "type":"any"
     }
   }
 }
}


但是,我希望标签不是数组,而希望成为根架构的一部分。因此,您可以指定任何属性,但是我特别注意“ id”,“ name”和“ price”
下列哪一项是正确的做法,哪些是完全错误的?

{
 "name":"Product",
 "type":"object",
 "properties":{
   "id":{
     "type":"number",
     "required":true
   },
   "name":{
     "description":"Name of the product",
     "required":true
   },
   "price":{
     "required":true,
     "type": "number",
     "minimum":0,
     "required":true
   }
 },
 "additionalProperties": {
     "type":"any"
 }
}

{
 "name":"Product",
 "type":"object",
 "properties":{
   "id":{
     "type":"number",
     "required":true
   },
   "name":{
     "description":"Name of the product",
     "required":true
   },
   "price":{
     "required":true,
     "type": "number",
     "minimum":0,
     "required":true
   }
 },
 "extends": {
     "type":"any"
 }
}

{
 "name":"Product",
 "type":["object","any"],
 "properties":{
   "id":{
     "type":"number",
     "required":true
   },
   "name":{
     "description":"Name of the product",
     "required":true
   },
   "price":{
     "required":true,
     "type": "number",
     "minimum":0,
     "required":true
   }
 }
}


我可以再提出几个例子(例如“ any”和“ object”的倒置角色),但是它们都是这三个例子的派生词。

最佳答案

[免责声明:此处是下一个JSON模式验证规范的作者]

好的,不清楚您要问什么,请参见下文,但是您的示例之一显然没有满足您的要求,这是您编写的示例:

{ "type": [ "object", "any" ] }


这等效于一个空模式,因此可以验证每个实例。

我读到您的问题的一种方式是,您希望JSON数据是标签数组或至少具有名为idnameprice成员的对象。由于您似乎正在使用草稿v3,因此您只有一个解决方案:

{
    "type": [
        {
            "description": "schema for tags array here",
        },
        {
            "description": "schema for the base object here"
        }
    ]
}


这种构造意味着实例必须遵守type内的至少一个模式(您也可以将其与基本类型混合使用)。

但是:当前草稿为v4,您现在应该写:

{
    "anyOf": [
        {
            "description": "schema for tags array here",
        },
        {
            "description": "schema for the base object here"
        }
    ]
}


请注意,并非所有实现都支持草案v4或上述type的构造。实际上,很少。请参见下面的链接,该链接可同时支持两种模式。

我读到您的问题的另一种方式是,您希望允许除idnameprice之外的其他任意属性。然后,这非常简单。只是不要在tags中为properties定义架构:

{
    "type": "object",
    "required": [ "id", "name", "price" ]
    "properties": {
        "id": {
            "type": "number"
        },
        "name": {
            "description": "Name of the product"
        },
        "price": {
            "type": "number",
            "minimum": 0
        }
    }
}


由于未将additionalProperties指定为false,因此对象实例可以具有任意数量的附加成员,并且这些成员可以是任何成员。

链接到可以测试您的模式的在线验证器:here

关于json - 使用JSON模式来指定带有某些必填字段的“any”类型模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14550235/

相关文章:

json:为 main.Time 类型调用 MarshalJSON 时出错:顶级值后的字符 '-' 无效

c# - 为什么我必须在使用之前将 JValue 显式转换为目标类型?

node.js - NodeJS JSON 架构验证不起作用

json - 将 JSON 转换为与 Swagger 2.0 兼容的 JSON Schema 草案 4

c# - 如何从线程内部更新字符串值

python - 你如何解决 'dict' 对象没有属性 'write' ?

ruby - 如何为对象数组编写 JSON 模式?

java - 在 JSON 模式中引用本地相关文件?

java - Gson将长度为1的json数组解析为JsonArray对象而不是JsonObject对象

java - 使用 Jackson 生成 JSON 模式进入无限循环