code: def validate(path, regex=None):
      
      validated = []
      for elem in path:
          key = elem[0]
          strkey = str(key)
          if (regex and (not regex.findall(strkey))):
              raise dpath.exceptions.InvalidKeyName("{} at {} does not match the expression {}"
                                                    "".format(strkey,
                                                              validated,
                                                              regex.pattern))
          validated.append(strkey)
docs: 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.