JSON模式验证:验证对象数组


JSON Schema validation: validate array of objects

我得到以下JSON,并想验证它。

[
    {
        "remindAt": "2015-08-23T18:53:00+02:00",
        "comment": "Postman Comment"
    },
    {
        "remindAt": "2015-08-24T18:53:00+02:00",
        "comment": "Postman Comment"
    }
]

我的模式当前看起来如下

{
    "type": "array",
    "required": true,
    "properties": {
        "type": "object",
        "required": false,
        "additionalProperties": false,
        "properties": {
            "remindAt": {
                "required": true,
                "type": "string",
                "format": "date-time"
            },
            "comment": {
                "required": true,
                "type": "string"
            }
        }
    }
}

这是不工作。即使我从JSON数据中删除注释,它也验证为true。我想我的架构文件的结构是错误的。

为了验证,我使用以下库https://packagist.org/packages/justinrainbow/json-schema

请有人向我解释我做错了什么,我如何正确验证给定的JSON数据?

Thanks in advance

模式中有一些错误。首先,对数组对象使用属性properties是对象子句,而不是数组子句,因此它将被忽略。

从json-schema v4, 必需是一个数组

下面的模式需要对数组中的所有项使用remindAt和comment属性:

{
    "type": "array",
    "items": {
        "additionalProperties": false,
        "properties": {
            "remindAt": {
                "type": "string",
                "format": "date-time"
            },
            "comment": {
                "type": "string"
            }
        },
        "required": ["remindAt", "comment"]
    }
}