Validates deep equality between two values using JSON comparison.

Performs recursive comparison of objects and arrays. Supports an optional exception filter to ignore specific keys during comparison. Useful for validating API responses, data transformations, and object state changes.

Type Safety Notes:

  • The generic type T is inferred from the actual parameter (first in the currying chain)

  • The expected parameter must be assignable to T | null | undefined

  • For objects, expected must have the same or subset of properties as actual

  • For union types like string | null, ensure proper type compatibility:

    const x: string | null;
    TestValidator.equals("works")(x)(null); // ✅ Works: null is assignable to string | null
    TestValidator.equals("error")(null)(x); // ❌ Error: x might be string, but expected is null

  // Basic equality
TestValidator.equals("response should match expected")(expectedUser)(actualUser);

// Ignore timestamps in comparison
TestValidator.equals("user data should match", (key) => key === "updatedAt")(
expectedUser
)(actualUser);

// Validate API response structure
const validateResponse = TestValidator.equals("API response structure");
validateResponse({ id: 1, name: "John" })({ id: 1, name: "John" });

// Type-safe nullable comparisons
const nullableData: { name: string } | null = getData();
TestValidator.equals("nullable check")(nullableData)(null); // ✅ Safe

Error with detailed diff information when values are not equal

  • Parameters

    • title: string

      Descriptive title used in error messages when values differ

    • exception: (key: string) => boolean = ...

      Optional filter function to exclude specific keys from comparison

    Returns <T>(actual: T) => (expected: undefined | null | T) => void

    A currying function chain: first accepts expected value, then actual value