diff --git a/litecord/embed/sanitizer.py b/litecord/embed/sanitizer.py index bc9f761..d545a79 100644 --- a/litecord/embed/sanitizer.py +++ b/litecord/embed/sanitizer.py @@ -71,8 +71,9 @@ def path_exists(embed: Embed, components_in: Union[List[str], str]): # (via recursion) try: return path_exists(embed[current], components[1:]) - except KeyError: - # if the current component doesn't exist, return False + except (KeyError, TypeError, ValueError): + # if the current component doesn't exist or we can't do a + # key fetch, return False return False diff --git a/tests/test_embeds.py b/tests/test_embeds.py index 05f597c..cbf161c 100644 --- a/tests/test_embeds.py +++ b/tests/test_embeds.py @@ -19,6 +19,7 @@ along with this program. If not, see . from litecord.schemas import validate from litecord.embed.schemas import EMBED_OBJECT +from litecord.embed.sanitizer import path_exists def validate_embed(embed): return validate(embed, EMBED_OBJECT) @@ -95,8 +96,14 @@ def test_fields(): ] }) - valid({ + assert invalid({ 'fields': [ {'name': 'a'}, ] }) + + +def test_path_exists(): + """Test the path_exists() function for embed sanitization.""" + assert path_exists({'a': {'b': 2}}, 'a.b') + assert not path_exists({'a': 'b'}, 'a.b')