JSON Schema:验证数字或空值

Ada*_*tan 20 python api validation json jsonschema

有没有办法让JSON架构属性成为数字或null

我正在构建一个包含heading属性的API .可以是介于0(含)和360(独占)之间的数字,也可以为null,因此以下输入可以:

{"heading": 5}
{"heading": 0}
{"heading": null}
{"heading": 12}
{"heading": 120}
{"heading": null}
Run Code Online (Sandbox Code Playgroud)

以下输入是错误的:

{"heading": 360}
{"heading": 360.1}
{"heading": -5}
{"heading": false}
{"heading": "X"}
{"heading": 1200}
{"heading": false}
Run Code Online (Sandbox Code Playgroud)

附录:

anyOf显然是正确的答案.为清晰起见,添加完整的架构.

架构

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "additionalProperties": false,
    "properties": {
      "heading": {
        "anyOf": [
          {"type": "number"},
          {"type": "null"}
        ]
      }
    }
}
Run Code Online (Sandbox Code Playgroud)

fid*_*dur 34

在draft-04中,您将使用anyOf指令:

{
  "anyOf": [
    {
      "type": "number",
      "minimum": 0,
      "maximum": 360,
      "exclusiveMaximum": true
    },
    {
      "type": "null"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

您也可以使用"type":["number","null"],如Adam建议的那样,但我认为anyOf更干净(只要你使用draft-04实现),并将最小和最大声明与数字联系起来明确.

免责声明:我对python实现一无所知,我的答案是关于json架构.


Ada*_*tan 29

诀窍是使用类型数组.代替:

"type": "number"
Run Code Online (Sandbox Code Playgroud)

使用:

"type": ["number", "null"]
Run Code Online (Sandbox Code Playgroud)

以下代码强制执行number-or-null策略,如果值为数字,则加上数字限制:

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
Run Code Online (Sandbox Code Playgroud)

这使:

{"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
Run Code Online (Sandbox Code Playgroud)