user1452030
user1452030

Reputation: 1021

JSON Schema: Enum not working in an array of objects

Enums don't seem to work well with array of objects. I'm using the 2020-12 draft and this is the structure of my JSON is as follows:

{
  "main": {
    "items": [
      {
        "attribute1": "Option 1"
      },
      {
        "attribute1": "Option 2",
        "attribute2": "some random string"
      }
    ]
  }
}

attribute 1 is mandatory and it needs to be one of three values - "Option 1", "Option 2" or "Option 3".

I tried a couple of variations of JSON schema but neither seems to address the requirement.

Variation 1: As a $ref

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/schema.json",
  "$defs": {
    "my_shiny_object": {
      "type": "object",
      "required": [
        "attribute1"
      ],
      "properties": {
        "attribute1": {
          "description": "This is mandatory attribute 1 with an enum",
          "type": "string",
          "enum": [
            "Option 1",
            "Option 2",
            "Option 3"
            ]
        },
        "attribute2": {
          "type": "string",
          "description": "This is an optional attribute 2"
        }
      }
    }
  },
  "title": "root",
  "description": "Root Element.",
  "type": "object",
  "required": [
    "main"
  ],
  "properties": {
    "main": {
      "type": "object",
      "title": "main",
      "description": "Main is the top level field.",
      "required": [
        "items"
      ],
      "properties": {
        "date_time": {
          "type": "string",
          "format": "date-time",
          "title": "Date/time optional",
          "description": "Date time"
        }
      },
      "items": {
        "type": "array",
        "description": "Items included in main",
        "items": {
          "$ref": "#/$defs/my_shiny_object"
        }
      }
    }
  }
}

Variation 2: Inline defintion

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/schema.json",
  "title": "root",
  "description": "Root Element.",
  "type": "object",
  "required": [
    "main"
  ],
  "properties": {
    "main": {
      "type": "object",
      "title": "main",
      "description": "Main is the top level field.",
      "required": [
        "items"
      ],
      "properties": {
        "date_time": {
          "type": "string",
          "format": "date-time",
          "title": "Date/time optional",
          "description": "Date time"
        }
      },
      "items": {
        "type": "array",
        "description": "Items included in main",
        "items": {
          "type": "object",
          "required": [
            "attribute1"
          ],
          "properties": {
            "attribute1": {
              "description": "This is mandatory attribute 1 with an enum",
              "type": "string",
              "enum": [
                "Option 1",
                "Option 2",
                "Option 3"
              ]
            },
            "attribute2": {
              "type": "string",
              "description": "This is an optional attribute 2"
            }
          }
        }
      }
    }
  }
}

Supplying incorrect values for the attribute1 field or even excluding it does not trigger any validation errors. I tried using Hyperjump JSV and jschon.dev.

Upvotes: 0

Views: 1021

Answers (1)

Jason Desrosiers
Jason Desrosiers

Reputation: 24399

You are not defining your "items" property in the right place. The schema at /properties/main/items needs to move to /properties/main/properties/items. Then it will evaluate as you expect.

Upvotes: 1

Related Questions