繁体   English   中英

JSON模式级联

[英]JSON Schema OneOf Cascading

我想问自己的问题是,我是否可以级联多个“ oneOf”,或者有更好的方法来使我的案例有效。

我正在尝试验证以下内容:

使用ObjectA或ObjectB的定义作为单个对象或它们的数组

情况1:

仅使用ObjectA的定义

{
 "X": "test"
}

情况2:

仅使用ObjectB的定义

{
 "Y": "test"
}

情况3:

在数组中使用ObjectA或ObjectB的定义

[
 {
  "X": "test"
 },
 {
  "Y": "test"
 }
]

情况4:

在数组中两次使用ObjectA的定义

[
 {
  "X": "test"
 },
 {
  "X": "test"
 }
]

架构:

我尝试使用此架构,但MonacoEditor的IntelliSense运作良好,但我仍然收到错误/警告:“只有一个必须验证时,才匹配多个架构。”

{
 "definitions": {
  "objectA": {
  "type": "object",
  "properties": {
   "X": {
    type: "string"
   }
  }
 },
  "objectB": {
   "type": "object",
   "properties": {
    "Y": {
     type: "string"
    }
   }
  }
 },
 "oneOf":
  [
   {
    "oneOf":
     [
      {
       "$ref": "#definitions/objectA"
      },
      {
       "$ref": "#definitions/objectB"
      }
     ]
    },
    {
     "type": "array",
     "items": 
      {
       "oneOf":
        [
         {
          "$ref": "#definitions/objectA"
         },
         {
          "$ref": "#definitions/objectB"
         }
        ]        
      }
    }
  ]
}

错误/警告:

“只有一个必须验证时,才匹配多个模式。”

问题在于您不需要objectA中的X属性和objectB中的Y属性,因此,一个空对象(即{)可以同时验证这两个对象。

另外,如果希望具有objectA和objectY的数组有效,则需要使用anyOf而不是oneOf。

{
 "definitions": {
   "objectA": {
     "type": "object",
     "properties": {
       "X": {
         "type": "string"
       }
     },
     "required": ["X"]
   },
   "objectB": {
     "type": "object",
     "properties": {
       "Y": {
         "type": "string"
       }
     },
     "required": ["Y"]
   }
 },
 "oneOf":
  [
   {"$ref": "#/definitions/objectA"},
   {"$ref": "#/definitions/objectB"},
   {
     "type": "array",
     "minItems": 1,
     "items":
     {
       "anyOf":
       [
         {"$ref": "#/definitions/objectA"},
         {"$ref": "#/definitions/objectB"}
       ]        
     }
   }
  ]
}

如果您不想验证空数组,则添加了minItems。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM