5

I'm trying to define a schema inside a python script, to be used right away to validate some json data. the schema definition looks like this:

    response_schema = {
    "required": ["identifiers" ],
    "properties": {
        "identifiers": {
          "minProperties": 1,"maxProperties": 1,
          "additionalProperties": {
            "required": [  "name","surname" ],
            "properties": {
              "surname": {
                  "required": ["sur1", "sur2" ],
                  "properties": {
                     "sur1": { },
                     "sur2": { }
              } },
              "name": {},

            "additionalProperties": false
            }
          }
        }
      },
    "additionalProperties": false
}

That schema works fine in any online validator, but when I execute the validation in my script:

validate(response_body_dict, response_schema)

I get the following error:

NameError: name 'false' is not defined

If I remove the lines "additionalProperties" : false from the schema I don't get the error, but of course it doesn't work for me, as it is a much less restrictive validation.

Can anyone please explain why I am getting this error?

  • Hi! Thanks for asking. Can you let us know what library you are using please? (specifically). I suspect this issue is to do with a library that uses an older version of JSON Schema than is current. If that's the case, there may be a simple solution. It's best to always include `$schema` at the root of your schema so people can tell what version of JSON Schema you are using. – Relequestual Sep 28 '18 at 08:27
  • hi! schema draft is version 4, and I get the error with both 2.6.0 and 3.0.0a2 versions of jsonschema library. thanks! – mtnezdilarduya Sep 30 '18 at 09:10
  • In which case, I can't see any issue with the schema. I suggest you file a bug with the library on github. – Relequestual Oct 01 '18 at 08:21

2 Answers2

1

The problem is a difference between Python and JSON. In Python you spell it 'False' and in JSON you spell it 'false'.

If you copy your schema into a text file and load it with the json module it will work correctly - no error.

When you load this snippet of code in a Python program you get the error you provided because Python does not know what 'false' is. The code is creating a dictionary, not a JSON schema.

If you want to prototype in place you could wrap it in """ and then use json.loads.

Like this:

import json
response_schema_str = """{
    "required": ["identifiers" ],
    "properties": {
        "identifiers": {
            "minProperties": 1,
            "maxProperties": 1,
            "additionalProperties": {
                "required": [
                    "name",
                    "surname" ],
                "properties": {
                    "surname": {
                        "required": [
                            "sur1",
                            "sur2" ],
                        "properties": {
                            "sur1": { },
                            "sur2": { }
                        }
                    },
                    "name": {},
                    "additionalProperties": false
                }
            }
        }
    },
    "additionalProperties": false
}"""
response_schema = json.loads(response_schema_str)
print(response_schema)
ChipJust
  • 1,376
  • 12
  • 20
0

I came across the same error while trying to validate against a similar JSON schema in my python script. I changed false to False for the reasons provided by ChipJust, and it worked for me.