Question

I'm trying to detect if the user specified a boolean as a string instead of a real boolean. I'm testing commentsModule/enabled to see if the value is false, once with quotes and once without.

The online validator: http://json-schema-validator.herokuapp.com/ works correctly, and identifies the failure as "instance value (\"false\") not found in enum (possible values: [false])".

However, NewtonSoft Json (latest version) with exactly the same schema and json defines this as a valid json.

Schema:

{
    "$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 ] }        
            }
          }
        }                                
}

(using oneOf gives the same result)

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 code (taken from the official site):

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

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

Thank you!

EDIT: Json.Net doesn't support Json Schema v4, so the "definitions" references are ignored.

For example, in this case the "caption" is checked for minimal length of 1, and has 0, but Json.net passes validation:

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:

{
    "$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 }        
        }
    }
}   }

the error from the online tool in this case talks about mismatches for both schemas and refers to the minimal length requirement:

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

Solution

Json.Net doesn't support Json Schema v4, only v3. That's why "anyOf" and "definitions" are not recognized and the validation passes.

Update:

Json.NET Schema has full support for Draft 4.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top