def validate(path, regex=None):
validated = []<EOL>for elem in path:<EOL><INDENT>key = elem[<NUM_LIT:0>]<EOL>strkey = str(key)<EOL>if (regex and (not regex.findall(strkey))):<EOL><INDENT>raise dpath.exceptions.InvalidKeyName("<STR_LIT>"<EOL>"<STR_LIT>".format(strkey,<EOL>validated,<EOL>regex.pattern))<EOL><DEDENT>validated.append(strkey)<EOL><DEDENT>
Validate that all the keys in the given list of path components are valid, given that they do not contain the separator, and match any optional regex given.