Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can JSON schema enums be case insensitive?

JSON Schema enums

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"].

The problem

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?

Other possible solutions

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.

like image 564
Adam Matan Avatar asked Oct 28 '14 09:10

Adam Matan


People also ask

Is json schema case sensitive?

JSON is case-sensitive. SQL is case-insensitive, but names in SQL code are implicitly uppercase.

Is enum case-insensitive?

Enum constants are case sensitive.

Is Avro schema case sensitive?

The options are case-insensitive.

Does order matter in JSON schema?

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.


1 Answers

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.

like image 149
jruizaranguren Avatar answered Oct 13 '22 14:10

jruizaranguren