我有以下 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 schema documentation声称它符合 JSON Schema draft 4 .
有谁知道我可能做错了什么?
谢谢。
最佳答案
从今天开始,您的问题是 JSON Schema validation spec ,而不是 RapidJSON:
4.3. Default values for missing keywords
Some keywords, if absent, MAY be regarded by implementations as having a default value. In this case, the default value will be mentioned.
结果:处理器被允许忽略缺失值,即使它们提供了默认
值并且仍然符合JSON-schema验证规范
5.4.3.2. Conditions for successful validation An object instance is valid against this keyword if its property set contains all elements in this keyword's array value.
结果:验证器不能忽略缺失但必需的属性。
将两者放在一起,会产生什么? RapidJSON 仍然可以声称符合 JSON 模式验证,即使它在模式验证期间不考虑 required
属性的 default
值。
您仍然可以在 issues page of RapidJson project 中提出增强请求
关于c++ - 如何获取 RapidJSON 模式来处理默认属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40041301/