JSON Schemas feature enums, which impose a constraint on the values of a string type:
{
"type": "array",
"items": [
{
"type": "number"
},
{
"type": "string"
},
{
"type": "string",
"enum": ["Street", "Avenue", "Boulevard"]
},
{
"type": "string",
"enum": ["NW", "NE", "SW", "SE"]
}
]
}
This schema validates values such as [1600, "Pennsylvania", "Avenue", "NW"]
.
Is there an elegant way to make the enum
case-insensitive, so that both Avenue
and avenue
would be accepted as the third value in the array?
I can use anyOf
on a list of values, and validate each against a case-insensitive regex - but that's cumbersome, error-prone and inelegant.
I'm afraid you won't find any elegant solution to this. There was a proposal for case-insensitive enums and several issues were commented.
So if you can not avoid the requirement, regex solutions are the only feasible ones. Another brute-force approach would be to have n complete lists of enum values, one with starting capital letters, other all capital letters, etc. and then use anyOf as you stated. You can automate the creation of this json-schema easily. Obviously it won't be very readable.
Anyway I would try to solve this with a pre-processing step before validation. You might convert to lowercase the required properties if they are present, and then validate. I find a bit forced to use json-schema specification to allow 'dirty' data.