objectOf
should not require hasOwnProperty
to be a function #183
Description
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:
prop-types/factoryWithTypeCheckers.js
Line 305 in 01634d9
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.