The object shape inferred from the predicate map.
The value to validate.
An object whose values are predicates for each expected property.
OptionalincludeHidden: booleanWhether to include non-enumerable and symbol keys.
true if the value matches the specified shape.
This function validates that:
shape exist as own propertiesAdditional 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
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
Determines whether the provided value is an object matching the specified property shape.