Json 属性 的架构验证只能在数组内的对象上设置一次

Json schema validation for property that can only be set once on an object inside an array

是否可以验证下面的json数据,使得"info"只能在"name"为"a"时填写,否则必须为空或null ?

[
  {
    "name": "a",
    "info": "this is mandatory"
  },
  {
    "name": "b",
    "info": "validation must fail"
  }
]

JSONSchema

{
 "title": "Array of things",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "name": {
        "type": "string",
        "enum": [
          "a",
          "b"
        ]
      },
      "info": {
        "type": "string"
      }
    }
  }
}

online editor

中的json

试试这个:

{
 "title": "Array of things",
  "type": "array",
  "items": {
   "type": "object",
   "properties":  {
      "name": {
         "type": "string",
        "enum": ["a", "b"]
      },
      "info" : {
         "type": ["string", "null"]         
      }
   },
   "required": ["name"],

   "oneOf": [
      {
         "properties": {
             "name": {"enum": ["a"] }
         },
         "required": ["info"]
      },
     {
         "properties": {
             "name": {"enum": ["b"] },
"info": {"enum": [null]}
         }         
      }
   ]
}
}