数组未在 JSON 架构中验证

Arrays are not validated in JSON Schema

我有一个没有进行正确验证的架构定义。基本上,它不检查数组内的任何内容并接受其中的任何 properties/values 。我是 JSON 验证的新手,所以我可能遗漏了一些东西。

这是架构:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "http://json-schema.org/draft-07/schema#",
    "title": "JSON Validator",
    "type": "object",
    "additionalProperties": {
        "type": "array",
        "items": {
            "type": "object",
            "properties": {
                "hash": {
                    "type": "string"
                },
                "date": {
                    "type": "string"
                },
                "uuid": {
                    "type": "string"
                },
                "task": {
                    "type": "object",
                    "properties": {
                        "name": {
                            "type": "string"
                        },
                        "order": {
                            "type": "integer"
                        },
                        "step": {
                            "type": "integer"
                        }
                    }
                },
                "meta": {
                    "type": "string"
                },
                "additionalProperties": false,
            }
        },
        "required": [
          "hash"
        ]
    }
}

测试 JSON 是这样的:

{
  "task_N": [
    {
      "uuid": "asdfsdafa",
      "author": {
        "id": 1,
        "email": "asdfasdd",
        "name": "dfasd"
      },
      "ip": "245245",
      "message": "asdfasd",
      "step": "",
      "is_archived": false,
      "creation_date": "34332423",
      "related_field": ""
    },
    {
      "uuid": "asdfsdafa",
      "author": {
        "id": 1,
        "email": "asdfasdd",
        "name": "dfasd"
      },
      "ip": "245245",
      "message": "asdfasd",
      "step": "",
      "is_archived": false,
      "creation_date": "34332423",
      "related_field": ""
    }
  ]
}

如您所见,数组属性中没有单个匹配项,但 python 库 jsonschema 和 http://jsonschemavalidator.net 都给出了 JSON 对模式有效。我已经摸不着头脑几个小时了,有人知道吗?

发生这种情况有几个原因,JSON 架构要求您的整个示例与架构相匹配,因此您需要将 "task_N" 作为架构的一部分。

此外,您的架构在 additionalProperties 中定义,应该是 properties

试试这个:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "http://json-schema.org/draft-07/schema#",
    "title": "JSON Validator",
    "type": "object",
    "properties": {
        "task_N": {
            "type": "array",
            "items": {
                "type": "object",
                "required": [
                    "hash"
                ],
                "properties": {
                    "hash": {
                        "type": "string"
                    },
                    "date": {
                        "type": "string"
                    },
                    "uuid": {
                        "type": "string"
                    },
                    "task": {
                        "type": "object",
                        "properties": {
                            "name": {
                                "type": "string"
                            },
                            "order": {
                                "type": "integer"
                            },
                            "step": {
                                "type": "integer"
                            }
                        }
                    },
                    "meta": {
                        "type": "string"
                    },
                    "additionalProperties": false
                }
            }
        }
    }
}

找到解决方案。看来我不仅需要将数组属性设置为 false,还需要将父级设置为 false。另外,我需要使用正确的正则表达式将其设置为 "patternProperties"。完成的架构是这样的:

 {
    '$schema': 'http://json-schema.org/draft-07/schema#',
    '$id': 'http://json-schema.org/draft-07/schema#',
    'type': 'object',
    'patternProperties': {
        '^[a-zA-Z0-9]*$': {
            'type': 'array',
            'items': {
                'type': 'object',
                'required': [
                    'hash',
                    'date',
                    'uuid',
                    'task',
                    'author'
                ],
                'properties': {
                    'hash': {
                        'type': 'string'
                    },
                    'date': {
                        'type': 'string'
                    },
                    'uuid': {
                        'type': 'string'
                    },
                    'task': {
                        'type': 'object',
                        'properties': {
                            'name': {
                                'type': 'string'
                            },
                            'order': {
                                'type': 'integer'
                            },
                            'step': {
                                'type': 'integer'
                            }
                        }
                    },
                    'author': {
                        'type': 'object',
                        'properties': {
                            'id': {
                                'type': 'integer'
                            },
                            'name': {
                                'type': 'string'
                            },
                            'email': {
                                'type': 'string'
                            }
                        }
                    },
                    'meta': {
                        'type': 'string'
                    }
                },
                'additionalProperties': false
            }
        }
    },
    'additionalProperties': false
}