JSON.Net: Schema validates where it shouldn't when using anyOf -
i'm trying detect if user specified boolean string instead of real boolean. i'm testing commentsmodule/enabled see if value false, once quotes , once without.
the online validator: http://json-schema-validator.herokuapp.com/ works correctly, , identifies failure "instance value (\"false\") not found in enum (possible values: [false])".
however, newtonsoft json (latest version) same schema , json defines 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 same result)
json:
{ "_id": { "$oid": "530dfec1e4b0ee95f0f3ce11" }, "pageid": 1234, "pagetype": "show", "name": "my name", "description": "this decription.” ", "channel": "tech", "commentsmodule": { "captionfielddoesntexist": "comments", "enabled": "false" }, "localinstance": "com", "productionyear": "2014", "navbarcaptionlink": "", "logoad": "" }
json.net code (taken 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, "definitions" references ignored.
for example, in case "caption" checked minimal length of 1, , has 0, 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 online tool in case talks mismatches both schemas , refers minimal length requirement:
"message" : "instance failed match 1 schema (matched 0 out of 2)" ... "message" : "string \"\" short (length: 0, required minimum: 1)",
json.net doesn't support json schema v4, v3. that's why "anyof" , "definitions" not recognized , validation passes.
update:
json.net schema has full support draft 4.
Comments
Post a Comment