Home > Back-end >  how to apply patterns on array enums in JSON schema
how to apply patterns on array enums in JSON schema

Time:02-28

I have a simple JSON schema that looks like so (and works)

{
    "cols": {
        "type": "array",
        "items": {
            "type": "string",
            "enum": [
                "id",
                "name",
                "age",
                "affiliation",
                ""
            ]
        },
        "additionalProperties": false
    }
}

I would like the enum to be the values prescribed above a decoration so that any of the following would be allowed

"enum" = [
    "id",
    "lower(name)",
    "average(age)",
    "distinct(affiliation)",
    ""
]

In other words, for cols

  • cols=id would be valid but no further decoration would be allowed around id
  • cols=name and cols=lower(name) would be valid
  • cols=age and cols=average(age) would be valid
  • cols=affiliation and cols=distinct(affiliation) would be valid
  • cols='' empty string would be valid

Specifying the decorations as patterns would be great so that they would be case-insensitive. For example, cols=lower(name) and cols=LOWER(name) would both be ok.

CodePudding user response:

You can change your enumerated list in enum to a list of patterns:

"items": [
  "type": "string",
  "anyOf": [
    { "pattern": "^cols\b...the rest of your pattern here...$" },
    { etc... }
  ]
]
  • Related