Validates sorting functionality of pagination APIs.

Tests sorting operations by calling the API with sort parameters and validating that results are correctly ordered. Supports multiple fields, ascending/descending order, and optional filtering. Provides detailed error reporting for sorting failures.

  // Test single field sorting
const sortValidator = TestValidator.sort("article sorting")(
(sortable) => api.getArticles({ sort: sortable })
)("created_at")(
(a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime()
);

await sortValidator("+"); // ascending
await sortValidator("-"); // descending

// Test multi-field sorting with filtering
const userSortValidator = TestValidator.sort("user sorting")(
(sortable) => api.getUsers({ sort: sortable })
)("status", "created_at")(
(a, b) => {
if (a.status !== b.status) return a.status.localeCompare(b.status);
return new Date(a.created_at).getTime() - new Date(b.created_at).getTime();
},
(user) => user.isActive // only test active users
);

await userSortValidator("+", true); // ascending with trace logging

Error when API results are not properly sorted according to specification

  • Parameters

    • title: string

      Descriptive title used in error messages when sorting fails

    Returns <
        T extends object,
        Fields extends string,
        Sortable extends
            (`-${Fields}` | `+${Fields}`)[] = (`-${Fields}` | `+${Fields}`)[],
    >(
        getter: (sortable: Sortable) => Promise<T[]>,
    ) => (
        ...fields: Fields[],
    ) => (
        comp: (x: T, y: T) => number,
        filter?: (elem: T) => boolean,
    ) => (direction: "+" | "-", trace?: boolean) => Promise<void>

    A currying function chain: API getter, field names, comparator, then direction