【问题标题】:json schema of object inside array数组内对象的json模式
【发布时间】:2020-02-09 01:10:33
【问题描述】:

我正在尝试编写 json 架构,以便可以使用它来验证 Postman 中的响应。 我感觉我快到了,但缺少一些明显的东西。

我在stackoverflowthis page 上检查了Q&A,以及在搜索 json 架构对象在数组中时出现的所有其他内容

我链接了2段代码:

  1. 我的架构需要修复吗
  2. 我正在尝试使用架构验证响应。

注意:Postman 确实接受模式,但是当我故意出错时(“type”:“number”,当它是响应中的字符串时),Postman 中的测试就好像一切正​​常一样通过了。我预计测试会失败。

架构:

const resultaatSchema = {
"type": "object",
"properties": {
    "InputParameters": {"type": "object"},
    "Resultaat": {"type": "array",
    "items": {
        "Bedrijfsnaam": {"type": "number"},
        "Winkel": {"type": "string"},
        "Kvknummer": {"type": "string"},
        "Accountmanager": {"type": "object"},
        "Eigenaar": {"type": "object",
        "properties": {
            "Naam": {"type": "string"},
            "EmailAdres": {"type": "string"},
            "RegionaleUnive": {"type": "object",
            "properties": {
                "Naam": {"type": "string"},
                "Nummer": {"type": "number"}
                }
            }
           }
        }
      }
     }
    }

};

验证响应

{
"InputParameters": {
    "ZoekWaardes": [
        "Z-000168378"
    ]
},
"Resultaat": [
    {
        "Bedrijfsnaam": "Companyname",
        "Winkel": "City",
        "Kvknummer": "08129882",
        "AccountManager": {
            "Gebruikersnaam": "Somename",
            "EmailAdres": "some.mail@address.nl"
        },
        "Eigenaar": {
            "Naam": "aName",
            "EmailAdres": null,
            "RegionaleUnive": {
                "Naam": "anotherName",
                "Nummer": 1111
            }
        },
        "Website": null,
        "EmailAdressen": [
            {
                "TypeId": 1,
                "Type": "Primair",
                "Adres": "mail@address.nl"
            },
            {
                "TypeId": 2,
                "Type": "Secundair",
                "Adres": "mail@addres2.nl"
            }
        ],
        "Telefoonnummers": [
            {
                "TypeId": 2,
                "Type": "Vast",
                "Nummer": "+31623568744",
                "Geheim": false
            },
            {
                "TypeId": 3,
                "Type": "Mobiel",
                "Nummer": "+31623568744",
                "Geheim": false
            }
        ],
        "Addressen": [
            {
                "TypeId": 2,
                "Type": "Bezoek",
                "Straat": "Streetname",
                "Huisnummer": "1",
                "HuisnummerToevoeging": null,
                "Postcode": "postalcode",
                "Woonplaats": "City",
                "Provincie": "province",
                "LandCode": "NL",
                "Geheim": false
            },
            {
                "TypeId": 3,
                "Type": "Post",
                "Straat": "addresline1",
                "Huisnummer": "addresline2",
                "HuisnummerToevoeging": null,
                "Postcode": "postalcode",
                "Woonplaats": "City",
                "Provincie": "Province",
                "LandCode": "XX",
                "Geheim": false
            }
        ],
        "Id": "9bba2277-8536-e911-8109-0050568803e2",
        "CrmRelatieNummer": "Z-000168378",
        "URN": null
    }
]

}

【问题讨论】:

  • 您的“items”架构关键字不是有效的架构。 items 的值必须是模式或模式数组。在单个模式的情况下,数组的所有项目都根据该模式进行验证。如果是数组,则根据它们的位置验证项目。
  • 谢谢,成功了!在 "items": { 之后添加 "type": "object", "properties": 使其按预期工作。现在模式的一部分看起来像这样: "Resultaat": {"type": "array", "items": { "type": "object", "properties": { "Bedrijfsnaam": {"type": "字符串"},
  • 不客气。

标签: json postman jsonschema


【解决方案1】:

您的“items”架构关键字不是有效的架构。 items 的值必须是模式或模式数组。在单个模式的情况下,数组的所有项目都针对此模式进行验证。如果是数组,则根据它们的位置验证项目。

刚刚添加评论也作为答案,以便其他人可以快速看到它。

【讨论】:

    猜你喜欢
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 2019-06-23
    • 2017-03-27
    • 2020-07-28
    • 1970-01-01
    相关资源
    最近更新 更多