Skip to content

Commit 853d1c8

Browse files
author
Andrew Kesterson
committed
Closes dpath-maintainers#4: dpath.path.validate chokes on integer keys, need to convert keys to string before comparing them to the separator or the regex
1 parent 5748451 commit 853d1c8

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

dpath/path.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@
77
def validate(path, separator="/", regex=None):
88
validated = []
99
for key in path:
10-
if (separator and (separator in key)):
10+
strkey = str(key)
11+
if (separator and (separator in strkey)):
1112
raise dpath.exceptions.InvalidKeyName("{} at {} contains the separator {}"
12-
"".format(key,
13+
"".format(strkey,
1314
separator.join(validated),
1415
separator))
15-
elif (regex and (not regex.findall(key))):
16+
elif (regex and (not regex.findall(strkey))):
1617
raise dpath.exceptions.InvalidKeyName("{} at {} does not match the expression {}"
17-
"".format(key,
18+
"".format(strkey,
1819
separator.join(validated),
1920
regex.pattern))
20-
validated.append(key)
21+
validated.append(strkey)
2122

2223
def paths(obj, dirs=True, leaves=True, path=[], skip=False, separator="/"):
2324
"""Yield all paths of the object.

tests/test_path_paths.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ def test_path_paths_invalid_keyname():
1010
}
1111
for x in dpath.path.paths(tdict):
1212
pass
13+
14+
def test_path_paths_int_keys():
15+
dpath.path.validate(['I', 'am', 'path', 0, 'of', 2])

0 commit comments

Comments
 (0)