validation loop: schema - problem validating

481 views
Skip to first unread message

Alex Shneyderman

unread,
Jul 10, 2014, 4:31:16 PM7/10/14
to jsons...@googlegroups.com
I am having a hard time understanding why an instance would not validate. Here is my schema:

{
    "definitions" : {
      "message_id_type" : {
         "type" : "object",
         "properties" : {
             "msgtype" : { "type" : "string" },
             "msgkey"  : { "type" : "string" }
         }
      },
      "contact" : {
         "type": "object",
         "allOf" : [ 
            { "$ref" :  "#/definitions/message_id_type" },
            { "properties" : {
                "last_name" : { "type" : "string" },
                "first_name" : { "type" : "string" } }
            }], 
         "additionalProperties" : false
      },
      "address" : {
         "type" : "object",
         "allOf" : [ 
            { "$ref" :  "#/definitions/message_id_type" },
            { "properties" : {
               "street" : { "type" : "string" },
               "zip" : { "type" : "string" } }
            }], 
         "additionalProperties" : false
      }
    },
    "oneOf" : [
        { "$ref" : "#/definitions/contact" }, 
        { "$ref" : "#/definitions/address" }
    ]
}

Here is the instance I would like to validate:

{
  "msgtype" : "test",
  "last_name" : "teter",
  "first_name" : "trter"
}

and this is the error I am getting doing so (with heroku app):

[ {
  "level" : "fatal",
  "schema" : {
    "loadingURI" : "#",
    "pointer" : "/definitions/message_id_type/properties/msgtype"
  },
  "instance" : {
    "pointer" : "/msgtype"
  },
  "domain" : "validation",
  "message" : "validation loop: schema \"#/definitions/message_id_type/properties/msgtype\" visited twice for pointer \"/msgtype\" of validated instance",
  "alreadyVisited" : "#/definitions/message_id_type/properties/msgtype",
  "instancePointer" : "/msgtype",
  "validationPath" : [ "#", "#/oneOf/0", "#/definitions/contact/allOf/0", "#/definitions/message_id_type/properties/msgtype", "#/definitions/contact/allOf/1", "#/definitions/contact/allOf/1/properties/first_name", "#/definitions/contact/allOf/1/properties/last_name", "#/oneOf/1", "#/definitions/address/allOf/0" ],
  "info" : "other messages follow (if any)"
} ]

Note if I change the schema to this:

{
    "definitions" : {
      "message_id_type" : {
         "type" : "object",
         "properties" : {
             "msgtype" : { "type" : "string" },
             "msgkey"  : { "type" : "string" }
         }
      },
      "contact" : {
         "type": "object",
         "properties" : {
             "msgtype" : { "type" : "string" },
             "msgkey"  : { "type" : "string" },
             "last_name" : { "type" : "string" },
             "first_name" : { "type" : "string" } }, 
         "additionalProperties" : false
      },
      "address" : {
         "type" : "object",
         "properties" : {
             "msgtype" : { "type" : "string" },
             "msgkey"  : { "type" : "string" },
             "street" : { "type" : "string" },
             "zip" : { "type" : "string" } }, 
         "additionalProperties" : false
      }
    },
    "oneOf" : [
        { "$ref" : "#/definitions/contact" }, 
        { "$ref" : "#/definitions/address" }
    ]
}

the instance above starts to validate. All I have done is to inline a refrenced type. Anyone have any input on this? 

Julian Berman

unread,
Jul 12, 2014, 11:54:37 PM7/12/14
to jsons...@googlegroups.com
Hi, I'm not sure what Heroku app you're referring to, though with our validator, the result of validating that instance is:


Traceback (most recent call last):
 
File "<input>", line 5, in <module>
 
File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/jsonschema/validators.py", line 432, in validate
    cls
(schema, *args, **kwargs).validate(instance)
 
File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/jsonschema/validators.py", line 117, in validate
   
raise error
ValidationError: {'first_name': 'trter', 'last_name': 'teter', 'msgtype': 'test'} is not valid under any of the given schemas
Failed validating 'oneOf' in schema:
   
{'$schema': 'http://json-schema.org/draft-04/schema#',
     
'definitions': {'address': {'additionalProperties': False,
                                 
'allOf': [{'$ref': '#/definitions/message_id_type'},

                                           
{'properties': {'street': {'type': 'string'},
                                                           
'zip': {'type': 'string'}}}],

                                 
'type': 'object'},
                     
'contact': {'additionalProperties': False,
                                 
'allOf': [{'$ref': '#/definitions/message_id_type'},
                                           
{'properties': {'first_name': {'type': 'string'},
                                                           
'last_name': {'type': 'string'}}}],
                                 
'type': 'object'},
                     
'message_id_type': {'properties': {'msgkey': {'type': 'string'},
                                                       
'msgtype': {'type': 'string'}},
                                         
'type': 'object'}},
     
'oneOf': [{'$ref': '#/definitions/contact'},
               
{'$ref': '#/definitions/address'}]}
On instance:
   
{'first_name': 'trter', 'last_name': 'teter', 'msgtype': 'test'}

I haven't sanity checked whether that's correct or not, but it's certainly nothing looking like what you've got there :).

Maybe you were looking for the (confusingly similarly named) general JSON schema mailing list?

Cheers,

Julian

Alex Shneyderman

unread,
Jul 13, 2014, 8:17:52 AM7/13/14
to jsons...@googlegroups.com
your validator does not really say why it is not a valid instance. But results are the same both validators think this instance is invalid. And I am looking at the instance and at the schema and I can not see why it is invalid.

I suspect both validators have assumptions and/or bugs.

WRT heroku app, I was referring to http://json-schema-validator.herokuapp.com/ which as you rightly noticed does not run python validator. So these validators are different, albeit both equally can not deal with the schema/instance.

Julian Berman

unread,
Jul 13, 2014, 1:28:23 PM7/13/14
to jsons...@googlegroups.com
I suspect you haven't read https://python-jsonschema.readthedocs.org/en/latest/errors/#handling-validation-errors :).


>>> print jsonschema.exceptions.best_match(jsonschema.Draft4Validator(schema).iter_errors(instance))
Additional properties are not allowed ('first_name', 'last_name', 'msgtype' were unexpected)


Failed validating 'additionalProperties' in schema[0]:

   
{'additionalProperties': False,
     
'allOf': [{'$ref': '#/definitions/message_id_type'},
               
{'properties': {'first_name': {'type': 'string'},
                               
'last_name': {'type': 'string'}}}],
     
'type': 'object'}


On instance:
   
{'first_name': 'trter', 'last_name': 'teter', 'msgtype': 'test'}


You have disallowed additional properties, and not specified any in your top level schema. This is how additional properties works, it does not recurse into subschemas or referenced schemas. Inlining your schema means you now define some properties, and so your instance passes. You may just want to add
 "properties" : {"first_name" : {}, "last_name ": {}, "msgtype" : {}}
to your top level schema, and to use this validator, which does tell you fairly specifically what's wrong I think :).

-J
Reply all
Reply to author
Forward
0 new messages