0

I want my JSON schema to accept a list but the values in the list are from a set list and can apparear in any order!

I.e. ["GOV","CRD", "CON"] is acceptable, but so is ["CRD", "GOV", "COM"].

My current thinking is something along these lines:

"sources":{"type": "array",
           "uniqueItems": true,
           "emum": ["CRD", "GOV", "COM", "CON", "OTH", "UTL", "PRO", "TEL", "POS", "INS", "CCJ", "POP", "VOT", "MVR", "PPS", "DRV", "PMC"]},

But I'm not entirely sure that's going to do what I want. I've read up on items where you can define the values in the list, but it looks like that would set the order and also the number of items in the list. Although both can be worked around using oneOf combined with definitions.

E.g. (shortened for space saving reasons) Please feel free to correct this if I'm wrong:

{
  "definitions": {
      "source":{"emum": ["CRD", "GOV", "COM", "CON", "OTH", "UTL", "PRO", "TEL", "POS", "INS", "CCJ", "POP", "VOT", "MVR", "PPS", "DRV", "PMC", ""]},
  }
  "sources":{"type": "array",
             "uniqueItems": true,
             "items": {
                     "source": {"$ref": "#/definitions/source"},
                     "source": {"$ref": "#/definitions/source"},
                     "source": {"$ref": "#/definitions/source"},
                    .
                    .
                    .
             }
  }
}

My question is: Is there a nicer way to do this?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Gamora
  • 337
  • 2
  • 18
  • 1
    Possible duplicate of [Correct way to define array of enums in JSON schema](https://stackoverflow.com/questions/30924271/correct-way-to-define-array-of-enums-in-json-schema) – Charlie Sep 06 '19 at 16:02

1 Answers1

1

You don't have to specify every possible order. When the array is made limited by enum, the items can come in any order. However, you have to specify the type of the enumerated values.

"sources":{
           "type": "array",
           "uniqueItems": true,
           "items": {
              "type": "string",
              "emum": ["CRD", "GOV", "COM"]
           }
Charlie
  • 22,886
  • 11
  • 59
  • 90