Function hasAllTruthyValues

  • Checks whether all properties of an object have truthy values, except for keys that are listed as optional.

    • Keys included in optionalKeys are always considered valid, regardless of their values.
    • For non-optional keys, the value must be truthy, unless it is explicitly false.

    Parameters

    • obj: Record<string, any>

      The object whose values should be validated.

    • optionalKeys: string[] = []

      An array of keys that are exempt from truthy validation. Defaults to an empty array.

    Returns boolean

    true if all required keys have valid (truthy or false) values, otherwise false.

    const result = hasAllTruthyValues({ a: 1, b: "test", c: false });
    console.log(result); // true (all keys are valid, `false` is allowed)
    const result = hasAllTruthyValues(
    { a: "", b: 123, c: null },
    ["a", "c"]
    );
    console.log(result); // true (`a` and `c` are optional)
    const result = hasAllTruthyValues({ a: 0, b: undefined });
    console.log(result); // false (both values are falsy and not optional)