Vivek
Vivek

Reputation: 1461

JSON schema validation with 'required' property

I am using https://www.jsonschemavalidator.net/ and trying to validate the schema I have written.

Schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "required": [
    "accounts"
  ],
  "accounts": {
    "required": "account",
    "properties": {
      "account": {
        "type": "array",
        "minItems": 1,
        "maxItems": 999,
        "required": [
          "scheme",
          "accountType",
          "accountSubType"
        ],
        "items": {
          "type": "object",
          "properties": {
            "scheme": {
              "description": "scheme",
              "type": "object",
              "required": [
                "schemeName",
                "identification"
              ],
              "properties": {
                "schemeName": {
                  "type": "string",
                  "maxLength": 40,
                },
                "identification": {
                  "type": "string",
                  "maxLength": 256
                },
                "name": {
                  "type": "string",
                  "maxLength": 70
                },
                "secondaryIdentification": {
                  "type": "string",
                  "maxLength": 35
                }
              }
            },
            "currency": {
              "type": "string",
              "format": "iso-4217",
              "pattern": "^[A-Z]{3,3}$",
              "maxLength": 3,
              "example": "EUR"
            },
            "accountType": {
              "type": "string"
            },
            "accountSubType": {
              "type": "string",
              "maxLength": 35
            }
          }
        }
      }
    }
  }
}

When I use the online link to validate like

{}

I get an error

Message:
Required properties are missing from object: accounts.
Schema path:
#/required

Which is correct, but when I do

{
  "accounts": {

  }
}

I don't get any errors although I should get an error for saying "account" is required. It seems like none of the inner 'required' fields are validated.

How can I fix this?

Upvotes: 1

Views: 3664

Answers (1)

Vivek
Vivek

Reputation: 1461

I found the problem.

  1. The accounts needed to be within a 'properties'
  2. The 'required' need to be inside the 'items'

Now the schema looks like

    {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "accounts": {
            "required": "account",
            "properties": {
                "account": {
                    "type": "array",
                    "minItems": 1,
                    "maxItems": 999,
                    "items": {
                        "type": "object",
                        "required": [
                            "scheme",
                            "accountType",
                            "accountSubType"
                        ],
                        "properties": {
                            "scheme": {
                                "description": "scheme",
                                "type": "object",
                                "required": [
                                    "schemeName",
                                    "identification"
                                ],
                                "properties": {
                                    "schemeName": {
                                        "type": "string",
                                        "maxLength": 40
                                    },
                                    "identification": {
                                        "type": "string",
                                        "maxLength": 256
                                    },
                                    "name": {
                                        "type": "string",
                                        "maxLength": 70
                                    },
                                    "secondaryIdentification": {
                                        "type": "string",
                                        "maxLength": 35
                                    }
                                }
                            },
                            "currency": {
                                "type": "string",
                                "format": "iso-4217",
                                "pattern": "^[A-Z]{3,3}$",
                                "maxLength": 3,
                                "example": "EUR"
                            },
                            "accountType": {
                                "type": "string"
                            },
                            "accountSubType": {
                                "type": "string",
                                "maxLength": 35
                            }
                        }
                    }
                }
            }
        }
    }
}

Upvotes: 1

Related Questions