0

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.

3
  • 1
    Please provide your full schema and some data you expect to pass and fail. See stackoverflow.com/help/minimal-reproducible-example - Without a full schema, and example data, it's impossible to know what you mean by "Doesn't seem to work well". Also, can you explain what you mean by "with array of objects"? Your example shows an array of strings. Commented Aug 31, 2021 at 13:36
  • The example is an array of objects containing string enum values Commented Aug 31, 2021 at 14:57
  • Apologies. Asked this question after a long day at work. I've added the details you requested. Please advise. Commented Sep 1, 2021 at 0:53

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Jason. Such a simple typo and it's been driving me nuts for the past couple of days. Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.