Search code examples
pythonvalidationcerberus

Detect empty string in numeric field using Cerberus


I am using the python library cerberus (http://docs.python-cerberus.org/en/stable/) and I want to check if a JSON field is a number (integer) or an empty string.

I tried using the condition:

{"empty": True, "type": "intenger"}

But when the field is an empty string, for example: (""), I get the following error.

'must be of integer type'

Is there a way of using the basic validation rules so it detects also an empty string in a numeric field?, I know it can be done by using extended validation functions but I want to avoid that solution for the moment.


Solution

  • Try something like this:

    {"anyof":[
       {"type":"string","allowed":[""]},
       {"anyof_type":["float","integer"]}
    ]},