“当”为空时对JSON模式进行验证;“类型”=&引用;“字符串”;

“当”为空时对JSON模式进行验证;“类型”=&引用;“字符串”;,json,validation,jsonschema,Json,Validation,Jsonschema,我想防止json文件允许null作为它的有效值。 尝试使用关键字而不是,但运气不佳 希望将下面的json验证为false,将字段stats的值验证为null { "stats": "null" } 请在下面找到我的模式:- { "$schema": "http://json-schema.org/draft-04/schema#", "id": "http://jsonschema.net#", "type": "object", "additionalProperties

我想防止json文件允许null作为它的有效值。 尝试使用关键字而不是,但运气不佳

希望将下面的json验证为false,将字段stats的值验证为null

{
  "stats": "null"
}
请在下面找到我的模式:-

{
  "$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"
  ]
}
虽然我给出了“not”:{“type”:“null”},但它仍然成功验证。

您可以使用“enum”关键字而不是“type”。“null”不是有效的json和json架构类型

另外,additionalProperties和maxProperties在stats描述中是无用的

{
    "$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
            "not" : {
                "enum" : ["null"]
            }

        }
    }, 
    "required" : [
        "stats"
    ]
}

首先,null不是字符串。因此,请尝试在您的模式中使用下面的内容--

但是,在示例代码片段中,您提到了如下内容--

{
“stats”:“null”
}

因此,如果您真的希望文件中不允许使用null,那么示例文件应该类似于
{
“stats”:空
}

沿着我提供的模式。

哇。这里太混乱了

问题很简单:

{
  "stats": "null"
}
“null”
是一个字符串,因此它是有效的(因为您允许使用字符串)。您的架构不允许这样做,因为它按照您的预期工作:

{
    stats: null
}
Ashish Patil的回答是错误的:在模式(而不是数据)中,当您指定类型时,类型名称是一个字符串。指定
“not”:{“type”:null}
无效。您可以指定
“not”:{“type”:“null”}
,但这是多余的,因为前面的
“type”:“string”
已经暗示了这一点


jruizaranguren接受的答案是有效的,因为它不允许字符串
“null”
。它没有解决核心混淆问题,即
null
“null”
不同

我尝试使用
{“stats”:“null”}
进行验证,但失败了。您如何验证JSON?您确定它是“null”而不是“null”吗?因为这是一个字符串,而不是一个“null”值。我正在使用java代码验证json,使用jsonSchema。但我正在使用检查验证,以更简单的方式进行测试。感谢jruizaranguren。完美
{
    stats: null
}