JSON Schema,用于指定具有某些必填字段的"任何"类型模式

Ber*_*nha 17 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"
     }
   }
 }
}
Run Code Online (Sandbox Code Playgroud)

但是,我不希望标签是一个数组,而是希望它成为根模式的一部分.所以你可以指定任何属性,但我特别注意"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
   }
 }
}
Run Code Online (Sandbox Code Playgroud)

我可以想出更多(比如"任意"和"对象"的反转角色),但它们都是这三个例子的衍生物.

fge*_*fge 35

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

好的,目前还不清楚你的问题,见下文,但你的一个例子显然没有做你想要的,这就是你写的:

{ "type": [ "object", "any" ] }
Run Code Online (Sandbox Code Playgroud)

这相当于一个空模式,因此验证了每个实例.

我阅读你的问题的一种方法是你希望你的JSON数据是一个标签数组或至少有成员名字的对象id,nameprice.由于您似乎使用草案v3,因此您只有一个解决方案:

{
    "type": [
        {
            "description": "schema for tags array here",
        },
        {
            "description": "schema for the base object here"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

这个构造意味着实例必须遵守至少一个模式type(并且你也可以将它与基本类型混合).

但是:现在的草案现在是v4,你现在应该写道:

{
    "anyOf": [
        {
            "description": "schema for tags array here",
        },
        {
            "description": "schema for the base object here"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

请注意,并非所有实现都支持草案v4或type上面的构造.事实上,很少有人这样做.请参阅下面的在线模式验证程序的链接,该验证程序支持两者.

我读到你的问题的另一种方式是你想要允许的属性id,name以及price他们喜欢的任何东西.这很简单.只是不要为tagsin 定义架构properties:

{
    "type": "object",
    "required": [ "id", "name", "price" ]
    "properties": {
        "id": {
            "type": "number"
        },
        "name": {
            "description": "Name of the product"
        },
        "price": {
            "type": "number",
            "minimum": 0
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

由于您未指定additionalProperties为as false,因此对象实例可以包含任意数量的其他成员,并且这些成员可以是任何成员.

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