Function hasPropertiesWithValue

  • Checks if an object has the specified properties with the given values.

    Remarks

    This function uses the hasPropertyValue function to check if the object has the specified property with the given value as well as the type is the same.

    Example

    Check if person object has name and email properties with the specified values:

    interface Person {
    name: string;
    age: number;
    email: string;
    }

    const person: Person = {
    name: 'John Doe',
    age: 30,
    email: 'johndoe@example.com',
    };

    hasPropertiesWithValue(person, { name: 'John Doe', email: 'johndoe@example.com' });
    // => { ok: true, value: 'true' }

    Example

    Check if person object has name and email properties with different values:

    interface Person {
    name: string;
    age: number;
    email: string;
    }

    const person: Person = {
    name: 'John Doe',
    age: 30,
    email: 'johndoe@example.com',
    };

    hasPropertiesWithValue(person, { name: 'Jane Doe', email: 'janedoe@example.com' });
    // => { ok: true, value: 'false' }

    Example

    Check if person object has surname property with the specified value:

    interface Person {
    name: string;
    age: number;
    email: string;
    }

    const person: Person = {
    name: 'John Doe',
    age: 30,
    email: 'johndoe@example.com',
    };

    hasPropertiesWithValue(person, { surname: 'Doe', age: 40 });
    // => { ok: false, error: Error('[objects.hasPropertyValue] Property surname does not exist') }

    Type Parameters

    • T extends Record<string, any>

      The type of the object to check.

    Parameters

    • obj: T

      The object to check for properties.

    • propValues: Partial<{
          [P in string | number | symbol]: T[P]
      }>

      An object whose keys are property names and values are the values to compare against.

    Returns Result<boolean, Error>

    A Result object with either a boolean indicating if all properties exist in the object with the specified values, or an Error object if any of the properties are missing or have different values.

Generated using TypeDoc v0.24.7