tguard-js
    Preparing search index...

    Function hasShape

    • Determines whether the provided value is an object matching the specified property shape.

      Type Parameters

      • S extends Record<PropertyKey, unknown>

        The object shape inferred from the predicate map.

      Parameters

      • o: unknown

        The value to validate.

      • shape: { [K in string | number | symbol]: (value: unknown) => value is S[K] }

        An object whose values are predicates for each expected property.

      • OptionalincludeHidden: boolean

        Whether to include non-enumerable and symbol keys.

      Returns o is S

      true if the value matches the specified shape.

      This function validates that:

      • the value is an object
      • all keys defined in shape exist as own properties
      • each corresponding property value satisfies its predicate

      Additional properties not defined in shape are allowed.

      Unlike hasKeys, this function also validates the values of each property.

      Unlike isRecordOf, each property may use a different validator.

      The shape descriptor always evaluates all own keys using Reflect.ownKeys(), including symbol and non-enumerable properties.

      The includeHidden option only controls whether the target object being validated includes non-enumerable and symbol keys.

      Arrays are treated as objects and are therefore supported.

      When validating arrays:

      • Object.keys() returns enumerable index keys ('0', '1', ...)
      • Reflect.ownKeys() additionally includes non-enumerable own keys such as 'length'
      const arr = ['foo', null];

      hasShape(arr, {
      '0': v => v === 'foo',
      '1': v => v === null
      }); // true
      • Only own properties are checked.
      • Prototype properties are ignored.
      • Additional properties are allowed.
      • Symbol and non-enumerable properties require includeHidden to be enabled.

      Basic usage:

      hasShape(
      { id: 1, name: 'Alice' },
      {
      id: isNumber,
      name: isString
      }
      ); // true

      Missing property:

      hasShape(
      { id: 1 },
      {
      id: isNumber,
      name: isString
      }
      ); // false

      Invalid property value:

      hasShape(
      { id: '1', name: 'Alice' },
      {
      id: isNumber,
      name: isString
      }
      ); // false

      Symbol keys:

      const token = Symbol('token');

      hasShape(
      { [token]: 'abc' },
      {
      [token]: isString
      },
      true
      ); // true

      1.1