doukuipai8544 2015-08-02 09:42
浏览 1197
已采纳

JSON Schema验证:验证对象数组

I get following JSON and would like to validate it.

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

My schema looks currently as following

{
    "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"
            }
        }
    }
}

This is not working. It validates to true even if I remove comment from JSON ddata. I guess structure of my schema file is wrong.

For validating I use following library https://packagist.org/packages/justinrainbow/json-schema

Can please someone explain to me what I do wrong and how I properly validate given JSON data?

Thanks in advance

  • 写回答

1条回答 默认 最新

  • douyujun0152 2015-08-03 06:20
    关注

    There are some errors in your schema. First, you are using properties for an array object. properties is a clause for objects, not arrays, so it will be ignored.

    From json-schema v4, required is an array.

    The following schema will require remindAt and comment properties for all items in the array:

    {
        "type": "array",
        "items": {
            "additionalProperties": false,
            "properties": {
                "remindAt": {
                    "type": "string",
                    "format": "date-time"
                },
                "comment": {
                    "type": "string"
                }
            },
            "required": ["remindAt", "comment"]
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?