0

I am using flask-restx for building an API.

My api model is like the following:

myModel = api.model(
    'myModel', 
    {
        'id' : fields.Integer(min=1, required=True),
        'code' : fields.String(enum=["A", "B", "C"], required=False),
    }
)

By doing so, code cannot be null.

But sometimes, the code field is null. If it is not, it must be one of the A, B or C values.

I cannot add None to the enum list because it is not a string.

How to make it possible to be null ?

thomask
  • 743
  • 7
  • 10

1 Answers1

3

add in your script

class NullableString(fields.String):
    __schema_type__ = ['string', 'null']
    __schema_example__ = 'nullable string'

Then

myModel = api.model(
    'myModel', 
    {
        'id' : fields.Integer(min=1, required=True),
        'code' : NullableString(enum=["A", "B", "C"], required=False),
    }
)

This should allow your field to be null

Stefan Cronje
  • 149
  • 2
  • 7