JSON Schema: validate a number-or-null value

In draft-04, you would use the anyOf directive:

{
  "anyOf": [
    {
      "type": "number",
      "minimum": 0,
      "maximum": 360,
      "exclusiveMaximum": true
    },
    {
      "type": "null"
    }
  ]
}

You could also use “type”: [“number”, “null”] as Adam suggests, but I think anyOf is cleaner (as long as you use a draft-04 implementation), and ties the minimum and maximum declaration to the number explicitly.

Disclaimer: I don’t know anything about the python implementation, my answer is about json schema.

Leave a Comment