验证具有不同属性的 json 个对象

validate json object with varying properties

我有 json 个对象,其 属性 值是唯一的,可以是任何值;

{
    "cat1": {
        "name": "kitty",
        "type": "animal",
        "color": "ginger"
    },
    "dog2": {
        "name": "ripple",
        "type": "animal",
        "color": "black"
    },
    "book10": {
        "name": "myBook",
        "type": "book",
        "color": "NA"
    },
    "orange6": {
        "name": "NA",
        "type": "fruit",
        "color": "orange"
    },
    "pig1":{
        "name": "spring",
        "type": "animal",
        "color": "pink"
    }
}

现在我很困惑如何编写它的验证模式。有人知道怎么做吗?

var mySchema = {
    "type": "object",
    "properties": {
         // no idea how to check varying properties like cat1, dog2, etc. which might change next time
    }
}

你可以试试这个

var mySchema = {
  "type": "object",
  "additionalProperties": {
    "type": "object",
    "properties": {
      "name": { "type": "string"},
      "type": { "type": "string"},
      "color": { "type": "string"},
    }
  }
}

参考: