json schema of object inside array

Viewed 106

I'm trying to write a json schema so I can use it to validate a response in Postman. I have the feeling I am almost there, but missing something obvious.

I've checked Q&A on stackoverflow, this page and everything else that came up on google when searching for json schema object inside array

I link 2 pieces of code:

  1. Is my schema that needs a fix
  2. Is te response I'm trying to validate with the schema.

Note: Postman does accept the schema, but when I intentionally make an error ("type": "number" when it is a string in the response) the test in Postman just passes as if everything is allright. I expect the test to fail.

Schema:

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

};

Response to validate

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

Your "items" schema keyword is not a valid schema. The value of items must either be a schema or an array of schemas. In case of a single schema, all items of the array are validated against this schema. In case of an array, the items are validated according to their positions.

Just added the comment also as answer so that other people can see it quickly.

Related