Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何让RapidJSON模式处理默认属性_C++_Json_Jsonschema_Rapidjson - Fatal编程技术网

C++ 如何让RapidJSON模式处理默认属性

C++ 如何让RapidJSON模式处理默认属性,c++,json,jsonschema,rapidjson,C++,Json,Jsonschema,Rapidjson,我有以下JSON模式,它需要id和content,但后者默认为空字符串 { "type": "object", "properties": { "id": { "type": "string" }, "content": { "type": "string", "default": "" } }, "required": [ "id", "content" ], "additionalProperties": false }

我有以下JSON模式,它需要
id
content
,但后者默认为空字符串

{
    "type": "object",
    "properties": {
        "id": { "type": "string" },
        "content": { "type": "string", "default": "" }
    },
    "required": [ "id", "content" ],
    "additionalProperties": false
}
我正在尝试验证以下JSON字符串:

{
    "id": "some id"
}
为此,我有以下代码:

rapidjson::Document document;
document.Parse(schemaJson.c_str());

rapidjson::SchemaDocument schemaDocument(document);
rapidjson::SchemaValidator validator(schemaDocument);

rapidjson::Document modelDoc;
modelDoc.Parse(modelJson.c_str());

modelDoc.Accept(validator); // Complains about missing property
即使属性具有默认值,accept调用也无法通过验证

声明说它符合法律

有人知道我可能做错了什么吗


谢谢。

从今天起,您的牛肉是与,而不是与RapidJSON:

一些关键字如果不存在,可能被实现视为具有默认值。在这种情况下,将提到默认值

结果:允许处理器忽略缺少的值,即使它们提供了
默认值,并且仍然符合JSON模式验证规范

如果对象实例的属性集包含该关键字数组值中的所有元素,则该对象实例对该关键字有效

结果:验证器不能忽略缺少但必需的属性


把两者放在一起,会得到什么?RAPIDJSON仍然可以声明JSON模式验证的一致性,即使在模式验证期间不考虑<代码>默认值<代码>要求属性。


您仍然可以在

中提出增强请求。从今天起,您的牛肉是与,而不是与RapidJSON:

一些关键字如果不存在,可能被实现视为具有默认值。在这种情况下,将提到默认值

结果:允许处理器忽略缺少的值,即使它们提供了
默认值,并且仍然符合JSON模式验证规范

如果对象实例的属性集包含该关键字数组值中的所有元素,则该对象实例对该关键字有效

结果:验证器不能忽略缺少但必需的属性


把两者放在一起,会得到什么?RAPIDJSON仍然可以声明JSON模式验证的一致性,即使在模式验证期间不考虑<代码>默认值<代码>要求属性。


您仍然可以在

中提交增强请求。我刚刚尝试了一些在线验证程序。他们也使你的例子无效


如果这是您想要的,我建议您删除
“required”
中的
“content”

我刚刚尝试了一些在线验证器。他们也使你的例子无效


如果这是您想要的,我建议您删除
“required”
中的
“content”

这将是适合您尝试实现的模式

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "definitions": {},
  "id": "example",
  "properties": {
    "content": {
      "default": "1",
      "id": "/properties/content",
      "type": "string"
    },
    "id": {
      "id": "/properties/id",
      "type": "string"
    }
  },
  "required": [
    "content",
    "id"
  ],
  "type": "object",
  "additionalProperties": false
}

另外,我在了解JSON模式时读到,许多JSON模式验证器会直接忽略
default
关键字。也许你最好不要用它

这将是一个适合您尝试实现的模式

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "definitions": {},
  "id": "example",
  "properties": {
    "content": {
      "default": "1",
      "id": "/properties/content",
      "type": "string"
    },
    "id": {
      "id": "/properties/id",
      "type": "string"
    }
  },
  "required": [
    "content",
    "id"
  ],
  "type": "object",
  "additionalProperties": false
}
另外,我在了解JSON模式时读到,许多JSON模式验证器会直接忽略
default
关键字。也许你最好不要用它

如果在架构中指定了
“default”
值(非零长度字符串),使用最新的合并,RapidJSON将不再引发
缺少属性
错误。以此为例

请注意,此更新只会消除错误,并且不会将默认值填充到正在验证的文档中。如果需要这种行为,用户可以从模式中挖掘出值。

对于最新的合并,如果在模式中指定了
“default”
值(非零长度字符串),RapidJSON将不再提出
缺少属性的错误。以此为例

请注意,此更新只会消除错误,并且不会将默认值填充到正在验证的文档中。如果需要这样的行为,用户可以从模式中挖掘出值