Search code examples
jsonschemawhitelist

JsonSchema - Defining a white-listed set of values


I feel like there's no chance my question was not asked before, but I couldn't find an answer anywhere.

As the title suggests, I'm trying to create a JSON schema that will validate an array if, and only if, All of the values in it are defined in a pre-defined set.

For example: If the predefined set contains {"One", "Two", "Three"}, and the array I'm trying to validate is ["One", "Two"], then the array should be valid. But if the array is ["One", "Two", "Four"], then the array would be invalid.

So I know of enum but it doesn't accomplish my goal since it validates as long as ONE of the values is defined in the predefined set.

I also know of min/max-contain combinations, tho they may interfere with the requirement that none of the values in the predefined set MUST appear. They all may, or may not appear.

Does anyone have an idea how to accomplish such requirements?


Solution

  • Okay, let's break that down:

    Putting it together:

    {
      "type": "array",
      "items": {
        "enum": [ "one", "two", "three" ]
      }
    }