JSON.Net:架构在使用 anyOf 时验证不应验证的位置



我试图检测用户是否将布尔值指定为字符串,而不是真正的布尔值。我正在测试commentsModule/ennabled,看看值是否为false,一次带引号,一次不带引号。

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

然而,具有完全相同模式和Json的NewtonSoft 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不支持JsonSchemav4,因此"定义"引用被忽略。

例如,在这种情况下,"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)",
Json.Net不支持Json Schema v4,只支持v3。这就是为什么"anyOf"one_answers"definitions"不被识别并且验证通过的原因。

更新:

Json.NET架构完全支持Draft 4。

相关内容

  • 没有找到相关文章

最新更新