数组内对象的json模式

时间:2019-10-11 13:32:43

标签: json postman jsonschema

我正在尝试编写一个json模式,以便可以在 Postman 中使用它来验证响应。 我有快要走的感觉,但是却缺少明显的东西。

我已经在Q&Athis page上检查过stackoverflow,并在搜索数组内的 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
    }
]

}

1 个答案:

答案 0 :(得分:0)

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

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