JSON.Net:Schema验证了使用anyOf时不应该的位置

时间:2014-04-09 10:21:46

标签: json json.net jsonschema

我正在尝试检测用户是否将布尔值指定为字符串而不是实际布尔值。 我正在测试commentsModule / enabled以查看值是否为false,一次是引号而一次是否为。

在线验证器:http://json-schema-validator.herokuapp.com/正常工作,并将失败标识为“在枚举中找不到”实例值(\“false \”)(可能的值:[false])“。

然而,具有完全相同模式的NewtonSoft Json(最新版本)和json将其定义为有效的json。

架构:

{
    "$schema":"http://json-schema.org/draft-04/schema#",
    "description": "pages json",
    "type": "object",
    "properties":
        {
        "name": {"type":"string"},
        "description": {"type":"string"},
        "channel": {"type":"string"},
        "commentsModule":{
            "type": "object",
            "anyOf":[
                 { "$ref": "#/definitions/commentsModuleDisabled" }                                            
              ]
        }
    },
    "definitions":{
        "commentsModuleDisabled":{
            "required": [ "enabled" ],
            "properties": {
                "enabled": { "type": "boolean",  "enum": [ false ] }        
            }
          }
        }                                
}

(使用oneOf给出相同的结果)

JSON:

{
"_id": {
    "$oid": "530dfec1e4b0ee95f0f3ce11"
},
"pageId": 1234,
"pageType": "Show",
"name": "my name",
"description": "this is decription.” ",
"channel": "tech",
"commentsModule": {
    "CaptionFieldDoesntExist": "Comments",
    "enabled": "false"
},    

"localInstance": "com",
"productionYear": "2014",
"navbarCaptionLink": "",
"logoAd": ""                
}

Json.Net代码(取自官方网站):

        JsonSchema schema = JsonSchema.Parse(schemaJson);
        JObject jsonToVerify = JObject.Parse(json);
        IList<string> messages;

        bool valid = jsonToVerify.IsValid(schema, out messages);

谢谢!

编辑: Json.Net不支持Json Schema v4,因此忽略“定义”引用。

例如,在这种情况下,检查“caption”的最小长度为1,并且为0,但Json.net通过验证:

JSON

{
"_id": {
    "$oid": "530dfec1e4b0ee95f0f3ce11"
},
"pageId": 1234,
"pageType": "Show",
"name": "another name",
"description": "description ",
"channel": "tech",
"commentsModule": {
    "caption": "",
    "enabled": true
},    

"localInstance": "com",
"productionYear": "2014",
"navbarCaptionLink": "",
"logoAd": "" }

架构:

{
    "$schema":"http://json-schema.org/draft-04/schema#",
"description": "pages json",
"type": "object",
"properties":
    {
    "name": {"type":"string"},
    "description": {"type":"string"},
    "channel": {"type":"string"},
    "commentsModule":{
        "type": "object",
        "oneOf":[
             { "$ref": "#/definitions/commentsModuleDisabled" },
             { "$ref": "#/definitions/commentsModuleEnabled" } 
          ]
    }
},
"definitions":{
    "commentsModuleDisabled":{
        "required": [ "enabled" ],
        "properties": {
            "enabled": { "type": "boolean",  "enum": [ false ] }        
        }
    },
     "commentsModuleEnabled":{
        "required": [ "enabled", "caption" ],
        "properties": {
            "enabled": { "type": "boolean",  "enum": [ true ] },
            "caption": { "type": "string",  "minLength": 1 }        
        }
    }
}   }

在这种情况下来自在线工具的错误会讨论两种模式的不匹配,并指出最小长度要求:

 "message" : "instance failed to match exactly one schema (matched 0 out of 2)" 
...  "message" : "string \"\" is too short (length: 0, required minimum: 1)",

1 个答案:

答案 0 :(得分:1)

Json.Net不支持Json Schema v4,仅支持v3。这就是为什么“anyOf”和“定义”无法识别并且验证通过的原因。

<强>更新

Json.NET Schema 完全支持草案4。

相关问题