question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KeyError with nested patternProperties

See original GitHub issue

When an object using patternProperties is further down the tree than another patternProperties field you get a KeyError.

Example schema:-

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "patternProperties": {
        "^foo:": {
            "type": "object",
            "properties": {
                "baz": {
                    "type": "object",
                    "patternProperties": {
                        "^b": {
                            "type": "object"
                        }
                    }
                },
                "bit": {
                    "type": "object"
                }
            },
            "required": [
                "baz",
                "bit"
            ]
        }
    }
}

Example document to validate against it :-

{
    "foo:bar": {
        "baz": {
            "bat": {
            }
        },
        "bit": {
        }
    }
}

This results in :-

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 38, in validate
KeyError: 'bit'

The following schema where the second patternProperties is replaced with a regular properties validates correctly:-

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "patternProperties": {
        "^foo:": {
            "type": "object",
            "properties": {
                "baz": {
                    "type": "object",
                    "properties": {
                        "bat": {
                            "type": "object"
                        }
                    }
                },
                "bit": {
                    "type": "object"
                }
            },
            "required": [
                "baz",
                "bit"
            ]
        }
    }
}

The following where the first patterProperties is replaced also validates correctly:-

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "foo:bar": {
            "type": "object",
            "properties": {
                "baz": {
                    "type": "object",
                    "patternProperties": {
                        "^b": {
                            "type": "object"
                        }
                    }
                },
                "bit": {
                    "type": "object"
                }
            },
            "required": [
                "baz",
                "bit"
            ]
        }
    }
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
horejsekcommented, Sep 26, 2018

Thanks @FrederikP, you found the bug. 😃 The same problem was on more places. It’s fixed in master. I plan to do more stuff tomorrow, do you want to release it today or can you wait till tomorrow, @j616?

0reactions
j616commented, Sep 26, 2018

I can wait until tomorrow. For now it’s useful to know I don’t have to find a work around and to know I’m not doing anything silly on my end! Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Key error when accessing a nested dictionary - Stack Overflow
I have the following list of nested dictionaries: raw_data = [ { "type": "message", "subtype": "bot_message", "text": "This content can't be ...
Read more >
bug in Nested field processing None value · Issue #920 - GitHub
It looks like there are 2 other places in this file that use the try except KeyError pass else pattern that would break...
Read more >
How do I handle key errors on nested dictionaries? - Reddit
The problem is, the keys that I'm extracting from the xml file need NOT always exist. There is a standard pattern for these...
Read more >
Python | Safe access nested dictionary keys - GeeksforGeeks
Sometimes, while working with Python we can have a problem in which we need to get the 2nd degree key of dictionary i.e...
Read more >
What is KeyError in Python? Dictionary and Handling Them
Now let us see what a key error is. KeyError in Python is raised when you attempt to access a key that is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found