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.
JSON is case-sensitive. SQL is case-insensitive, but names in SQL code are implicitly uppercase.
Enum constants are case sensitive.
The options are case-insensitive.
The order of fields in UI is determined by the order of properties in JSON schema generated. As it can be seen that the order of properties in JSON schema does not match the order of fields defined in Java object which is necessary in my case.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With