elysia icon indicating copy to clipboard operation
elysia copied to clipboard

Nullable field for types as documented in OpenAPI 3.0

Open Singha360 opened this issue 8 months ago • 1 comments

What is the problem this feature would solve?

OpenAPI has an optional nullable boolean field for data types which currently the TypeBox interface exposed by Elysia does not implement.

Note that there is no null type; instead, the nullable attribute is used as a modifier of the base type.

# Correct
type: integer
nullable: true

# Incorrect
type: null

# Incorrect as well
type:
  - integer
  - null

Currently this code:

my_field: t.Nullable(
    t.Number({
        examples: 120
    })
)

Produces this:

"my_field": {
    "anyOf": [{
            "type": "null"
        },
        {
            "examples": 120,
            "type": "number"
        }
    ]
}

What is the feature you are proposing to solve the problem?

duration_seconds:
    t.Number({
        examples: 120,
        nullable: true // Add this field
    })

Or use the currently available t.Nullable validator to have it generate the appropriate OpenAPI schema for nullable type.

Personally I am not too opinionated about the implementation, however I do think having both as an option would be best.

What alternatives have you considered?

None, couldn't make it work.

Singha360 avatar Jun 05 '24 07:06 Singha360