JSON Schema: check number or null

Is there a way to enable the JSON schema property as a number or null ?

I am creating an API that contains a heading attribute. It can be a number from 0 (inclusive) to 360 (excluding) or zero, so the following inputs are in order:

 {"heading": 5} {"heading": 0} {"heading": null} {"heading": 12} {"heading": 120} {"heading": null} 

And the following inputs are erroneous:

 {"heading": 360} {"heading": 360.1} {"heading": -5} {"heading": false} {"heading": "X"} {"heading": 1200} {"heading": false} 

Addendum:

anyOf is definitely the right answer. Adding a complete outline for clarity.

Scheme

 { "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "additionalProperties": false, "properties": { "heading": { "anyOf": [ {"type": "number"}, {"type": "null"} ] } } } 
+7
json python api validation jsonschema
source share
2 answers

In project-04, you should use the anyOf directive:

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

You can also use "type": ["number", "null"], as Adam suggests, but I think anyOf is cleaner (as long as you use the project-04 implementation) and associates the minimum and maximum declaration to a number explicitly.

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

+17
source share

The trick uses an array of types. Instead:

 "type": "number" 

Using:

 "type": ["number", "null"] 

The following code applies the number-or-zero policy plus numerical restrictions if it is a number:

 from jsonschema import validate from jsonschema.exceptions import ValidationError import json schema=json.loads("""{ "$schema": "http://json-schema.org/schema#", "description": "Schemas for heading: either a number within [0, 360) or null.", "title": "Tester for number-or-null schema", "properties": { "heading": { "type": ["number", "null"], "exclusiveMinimum": false, "exclusiveMaximum": true, "minimum": 0, "maximum": 360 } } }""") inputs = [ {"heading":5}, {"heading":0}, {"heading":360}, {"heading":360.1}, {"heading":-5},{"heading":None},{"heading":False},{"heading":"X"}, json.loads('''{"heading":12}'''),json.loads('''{"heading":120}'''), json.loads('''{"heading":1200}'''),json.loads('''{"heading":false}'''), json.loads('''{"heading":null}''') ] for input in inputs: print "%-30s" % json.dumps(input), try: validate(input, schema) print "OK" except ValidationError as e: print e.message 

What gives:

 {"heading": 5} OK {"heading": 0} OK {"heading": 360} 360.0 is greater than or equal to the maximum of 360 {"heading": 360.1} 360.1 is greater than or equal to the maximum of 360 {"heading": -5} -5.0 is less than the minimum of 0 {"heading": null} OK {"heading": false} False is not of type u'number', u'null' {"heading": "X"} 'X' is not of type u'number', u'null' {"heading": 12} OK {"heading": 120} OK {"heading": 1200} 1200.0 is greater than or equal to the maximum of 360 {"heading": false} False is not of type u'number', u'null' {"heading": null} OK 
+16
source share

All Articles