0
votes

I have two different JSON responses, each with different field names, however both validate successfully with the defined schema using jsonschema library.

Here is the defined schema

query_schema = {
    "type": "object",
    "properties" : {
        "pltfrm_nm": {"type" : "string"},
        "srvr_nm": {"type": "string"},
        "db_nm": {"type": "string"},
        "tbl_nm": {"type": "string"},
        "ip_addr_id": {"type": "string"},
        "usr_id": {"type": "string"},
        "sql_txt": {"type": "string"},
        "timestmp": {"type": "string"},
    },
}

and here is two different responses:

input = {'pltfrm_nm': 'p1', 'srvr_nm': 'server', 'db_nm': 'some db', 'tbl_nm': 'some table',
         'ip_addr_id': '999.999.9999', 'usr_id': 'my id', 'sql_txt': "sql text here", 'timestmp': 'aaaa)'}
validate(instance=input, schema=query_schema)

input = {'why': 'p1', 'does': 'server', 'this': 'some db', 'still': 'some table',
         'validate': '999.999.9999', 'i': 'my id', 'do': "sql text here", 'not': 'aaaa',
         'understand': 'hello'}
validate(instance=input, schema=query_schema)

in the second input I named all the fields different and added a new field understand as well. Neither of these throw a ValidationError. Here is the library: https://pypi.org/project/jsonschema/. Why does the second one not throw an error?

2

2 Answers

1
votes

The documentation states:

By default, providing additional properties is valid:

The additionalProperties keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in the properties keyword. By default any additional properties are allowed.

The additionalProperties keyword may be either a boolean or an object. If additionalProperties is a boolean and set to false, no additional properties will be allowed.

Reusing the example above, but this time setting additionalProperties to false.

So try to add that to your query_schema:

query_schema = {
    "type": "object",
    "properties" : {
        "pltfrm_nm": {"type" : "string"},
        "srvr_nm": {"type": "string"},
        "db_nm": {"type": "string"},
        "tbl_nm": {"type": "string"},
        "ip_addr_id": {"type": "string"},
        "usr_id": {"type": "string"},
        "sql_txt": {"type": "string"},
        "timestmp": {"type": "string"},
    },
  "additionalProperties": False
}
0
votes

JSON Schema is constraint based rather than permission based. An empty JSON Schema {} means anything is valid.

Let's take a look at what constraint the keyword properties adds...

Validation succeeds if, for each name that appears in both the
instance and as a name within this keyword's value, the child
instance for that name successfully validates against the
corresponding schema.

Omitting this keyword has the same behavior as an empty object.

https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.5.4

This means, the schema of the value of a key in the properties object, is applicable to the corrosponding value in the JSON instance.

Taking your example:

...    
    "properties" : {
            "pltfrm_nm": {"type" : "string"}
    }
...

pltfrm_nm must be a string.

That's the only constraint the above snippit is implying.

Are you expecting keys not listed in proprties to cause a validation error? If so, you need to specify that fact.

To specify no additional properties beyond those defined in properties, you need to use additionalProperties

https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.5.6

I'd recomend you take a look through our learning resource at http://json-schema.org/understanding-json-schema/ and http://json-schema.org/understanding-json-schema/ for an introduction to JSON Schema.