Skip to content
This repository was archived by the owner on Sep 1, 2024. It is now read-only.
This repository was archived by the owner on Sep 1, 2024. It is now read-only.

objectOf should not require hasOwnProperty to be a function #183

Closed
@tor-ocean

Description

@tor-ocean

When using objects as dictionaries, and to avoid littering code with hasOwnProperty checks, it can be useful to use object = Object.create(null) instead of object = {} when creating the object.

Unfortunately, this causes the PropTypes.objectOf checker to throw:

Warning: Failed prop type: propValue.hasOwnProperty is not a function

as the code assumes that every value will have an hasOwnProperty method in its prototype chain:

if (propValue.hasOwnProperty(key)) {

In my case, I will have to work around it by using a regular object and refactor the rest of my own code to do the same hasOwnProperty check.

I would still argue that this is a bug and needs to be fixed. When checking that a value is an object where every own key maps to a value of a certain type, it is self-contradictory that the code simultaneously requires that one specific key is going to map to a function. What was supposed to check that the value is a dictionary of a specific type now risks crashing the app when that is the case.

Imagine the case where the object is truly being used as a dictionary, even with a regular {} object, what's to stop the key "hasOwnProperty" to map to some other value? Malicious users might even cause a component to crash for other users by exploiting this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions