airr-community / airr-standards

AIRR Community Data Standards
https://docs.airr-community.org
Creative Commons Attribution 4.0 International
35 stars 23 forks source link

nullable flag and enum has validation issues #697

Closed schristley closed 11 months ago

schristley commented 1 year ago

While working on airr-js, I was getting validation errors for fields with nullable: true and enum when the field value was null. I'm using the ajv package for validation, instead of custom written like our python library. Apparently, there has been various discussions about this as OpenAPI schema and JSON schema aren't 100% equivalent. From what I can gather, the conclusion was that null needs to be added to the enum list. When I do that, as shown below, then validation passes.

        sex:
            type: string
            enum:
                - male
                - female
                - pooled
                - hermaphrodite
                - intersex
                - "not collected"
                - "not applicable"
                - null
            nullable: true
            description: Biological sex of subject
            title: Sex
            example: female
            x-airr:
                miairr: important
                adc-query-support: true
                set: 1
                subset: subject
                name: Sex
                format: controlled vocabulary

This really only applies to the OpenAPI V3 spec. This is a simple fix but it might effect UIs that display those enum lists.

bcorrie commented 1 year ago

Personally, I think that is dangerous. How do you differentiate that from me defining a field where "null" as a string is one of the allowed enum values. I think this would be a quite common use case. Similar to having "NA" or "None", "null" would be a relatively common string to find in an enum definition.

schristley commented 1 year ago

If you want the string "null" then put quotes around it in the enum like so, the following allows both the null value and the string "null" as acceptable values.

        sex:
            type: string
            enum:
                - male
                - female
                - pooled
                - hermaphrodite
                - intersex
                - "not collected"
                - "not applicable"
                - "null"
                - null
            nullable: true
            description: Biological sex of subject
            title: Sex
            example: female
            x-airr:
                miairr: important
                adc-query-support: true
                set: 1
                subset: subject
                name: Sex
                format: controlled vocabulary

As OpenAPI is based on JSON schema spec, only null is a special value, stuff like "NA" and "None" are just strings.