def _reverse_elastic_sort(current_sort, valid_field_names):
    """
        Reverse each item in current_sort, which is an elasticsearch sort order list.
        (See https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html for format.)
        This currently strips all sorting options except for "order". Otherwise we'd need an allowlist or signature on the cursor, since it's user-submitted data.

        Example: 'asc' becomes 'desc', 'desc' becomes 'asc', plain 'id' becomes {'id': {'order': 'desc'}}:
        >>> assert _reverse_elastic_sort([{'_score': {'order': 'desc'}}, {'date': {'order': 'asc'}}, 'id'], ['_score', 'date', 'id']) == \
                [{'_score': {'order': 'asc'}}, {'date': {'order': 'desc'}}, {'id': {'order': 'desc'}}]

        valid_field_names makes sure that invalid fields aren't passed in:
        >>> assert _reverse_elastic_sort([{'date': {'order': 'asc'}}, {'bad': {'order': 'asc'}}], ['date']) == \
                [{'date': {'order': 'desc'}}]
    """
    new_sort = []
    for item in current_sort:
        if isinstance(item, str):
            # inflate str-only fields like 'id' to dicts like {'id': {'order': 'asc'}}
            item = {item: {"order": "asc"}}
        try:
            # get 'id' from {'id': {'order': 'asc'}}
            field_name = next(iter(item))
            # this would only happen if cursor was manually edited
            if field_name not in valid_field_names:
                continue
            # update order
            new_order = "desc" if item[field_name]["order"] == "asc" else "asc"
            item = {field_name: {"order": new_order}}
        except Exception:
            raise TypeError("Unrecognized sort order: %s" % item)
        new_sort.append(item)
    return new_sort