如何获取 RapidJSON 模式来处理默认属性
How to get RapidJSON schema to deal with default properties
我有以下 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 claims it conforms to the 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.
结果:允许处理器 忽略 缺失值,即使它们提供了 default
值 并且仍然符合 JSON-模式验证规范
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
值。
中提出增强请求
我刚刚尝试了一些在线验证器。他们还使您的示例无效。
我建议您删除 "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
}
此外,我阅读了 here 了解 JSON 模式,许多 JSON 模式验证器直接忽略了 default
关键字。也许你最好不要使用它。
以最新的pull request merged, RapidJSON will no longer raise a missing property
error if and when a "default"
value (non-zero length string) is specified in the schema. See this unit test为例。
请注意,此更新只会消除错误,不会将默认值填充到正在验证的文档中。如果需要这样的行为,用户可以从模式中挖掘出值。
我有以下 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 claims it conforms to the 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.
结果:允许处理器 忽略 缺失值,即使它们提供了 default
值 并且仍然符合 JSON-模式验证规范
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
值。
我刚刚尝试了一些在线验证器。他们还使您的示例无效。
我建议您删除 "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
}
此外,我阅读了 here 了解 JSON 模式,许多 JSON 模式验证器直接忽略了 default
关键字。也许你最好不要使用它。
以最新的pull request merged, RapidJSON will no longer raise a missing property
error if and when a "default"
value (non-zero length string) is specified in the schema. See this unit test为例。
请注意,此更新只会消除错误,不会将默认值填充到正在验证的文档中。如果需要这样的行为,用户可以从模式中挖掘出值。