Man*_*anu 5 validation json jsonschema
我想阻止json提交允许null作为它的有效值.尝试使用关键字not,但没有运气.
希望下面的json被验证为false,因为字段stats值为null.
{
"stats": "null"
}
Run Code Online (Sandbox Code Playgroud)
请在下面找到我的架构: -
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://jsonschema.net#",
"type": "object",
"additionalProperties": false,
"maxProperties": 1,
"properties": {
"stats": {
"id": "http://jsonschema.net/stats#",
"type": "string",
"maxLength": 5,
"minLength": 2,
"additionalProperties": false,
"maxProperties": 1,
"not": {"type": "null"}
}
},
"required": [
"stats"
]
}
Run Code Online (Sandbox Code Playgroud)
虽然我给了"not":{"type":"null"},但它仍然成功验证.
哇。这里有这么多的混乱。
问题很简单:
{
"stats": "null"
}
Run Code Online (Sandbox Code Playgroud)
"null"是一个字符串,因此它是有效的(因为您允许使用字符串)。您的架构不允许这样做,它按您的预期工作:
{
stats: null
}
Run Code Online (Sandbox Code Playgroud)
Ashish Patil 的答案是错误的:在您的架构(而不是您的数据)中,当您指定类型时,类型名称是一个字符串。指定"not": {"type": null}无效。您可以指定"not": {"type": "null"},但这将是多余的,因为前面"type": "string"已经暗示了这一点。
jruizaranguren 接受的答案有效,因为它不允许string "null"。它没有解决null与"null".