Thomas Hobbes
Thomas Hobbes

Reputation: 53

jsonschema for array type validates incorrect data, how to fix?

I have following jsonschema:

{
        "$schema": "http://json-schema.org/schema#",
        "type": "object",
        "properties": {
            "abc": {
                "type": "array",
                "item": {
                   "type": "object",
                   "minItems": 1,
                   "properties": {
                        "a" : {"type": "string"},
                        "b" : {"type": "string"}
                    },
                    "required": [ "a", "b" ]
                }
            }
        },
        "required": [ "abc" ]
}

If I pass to validator following data:

{
    "abc":  [
        {

        },
        {

        }
    ]
}

validator will output no error, but such data incorrect.

Upvotes: 1

Views: 79

Answers (1)

Relequestual
Relequestual

Reputation: 12315

You used item rather than items.

Additionally, "minItems": 1 needs to be moved up to the parent object.

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "abc": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "properties": {
          "a": {
            "type": "string"
          },
          "b": {
            "type": "string"
          }
        },
        "required": [
          "a",
          "b"
        ]
      }
    }
  },
  "required": [
    "abc"
  ]
}

Checked and validated using https://jsonschema.dev

Upvotes: 1

Related Questions