使用tv4或任何其他验证器进行JSON模式验证

时间:2018-08-13 06:54:00

标签: json jsonschema tv4

我在数组中有某种JS​​ON结构。要求是:

1. All the JSON object within the array can be optional.
2. Each JSON can have its own set of properties and can be complex and nested.
3. Each JSON object will have a set of mandatory attributes.

如何为此类JSON创建架构。 uisng anyOf或定义会有所帮助吗?

已更新:我有一个JSON对象数组,其中每个对象可以具有不同的属性。唯一常见的属性是“类型”,其有效值为:电子产品,家具或金融。所以我的问题是如何推导架构?

示例

{
 "list": [
  {
   "type": "electronics"
  },
  {
   "type": "furniture"
  },
  {
   "accessRights": "readOnly",
   "rules": ['print','copy'],
   "type": "finance"
  }
}

解决方案

{
"properties": {
    "list": {
        "type": "array",
        "items": {
            "type": "object",
            "required": ["type"],
            "properties": {
                "type": {
                    "type": "string",
                    "enum": ["electronics", "furniture", "finance"]
                }
            },
            "anyOf": [{
                "properties": {
                    "type": {
                        "enum": ["electronics"]
                    }
                }
            }, {
                "properties": {
                    "type": {
                        "enum": ["furniture"]
                    }
                }
            }, {
                "properties": {
                    "type": {
                        "enum": ["finance"]
                    },
                    "accessRights": {
                        "type": "string"
                    },
                    "rules": {
                        "type": "array"
                    }
                }
            }]
        }
    }
 }
}

0 个答案:

没有答案