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

Support validation when hasOwnProperty is not in prototype #187

Merged
merged 1 commit into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions __tests__/PropTypesDevelopmentReact15.js
Original file line number Diff line number Diff line change
Expand Up @@ -777,10 +777,20 @@ describe('PropTypesDevelopmentReact15', () => {
);
});

it('should not warn when passing an object with no prototype', () => {
typeCheckPass(PropTypes.objectOf(PropTypes.number), Object.create(null));
});

it('should not warn when passing an empty object', () => {
typeCheckPass(PropTypes.objectOf(PropTypes.number), {});
});

it('should not warn when passing an object with a hasOwnProperty property', () => {
typeCheckPass(PropTypes.objectOf(PropTypes.number), {
hasOwnProperty: 3,
});
});

it('should be implicitly optional and not warn without values', () => {
typeCheckPass(PropTypes.objectOf(PropTypes.number), null);
typeCheckPass(PropTypes.objectOf(PropTypes.number), undefined);
Expand Down
10 changes: 10 additions & 0 deletions __tests__/PropTypesDevelopmentStandalone-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -779,10 +779,20 @@ describe('PropTypesDevelopmentStandalone', () => {
);
});

it('should not warn when passing an object with no prototype', () => {
typeCheckPass(PropTypes.objectOf(PropTypes.number), Object.create(null));
});

it('should not warn when passing an empty object', () => {
typeCheckPass(PropTypes.objectOf(PropTypes.number), {});
});

it('should not warn when passing an object with a hasOwnProperty property', () => {
typeCheckPass(PropTypes.objectOf(PropTypes.number), {
hasOwnProperty: 3,
});
});

it('should be implicitly optional and not warn without values', () => {
typeCheckPass(PropTypes.objectOf(PropTypes.number), null);
typeCheckPass(PropTypes.objectOf(PropTypes.number), undefined);
Expand Down
10 changes: 10 additions & 0 deletions __tests__/PropTypesProductionReact15-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,10 +649,20 @@ describe('PropTypesProductionReact15', () => {
);
});

it('should not warn when passing an object with no prototype', () => {
expectNoop(PropTypes.objectOf(PropTypes.number), Object.create(null));
});

it('should not warn when passing an empty object', () => {
expectNoop(PropTypes.objectOf(PropTypes.number), {});
});

it('should not warn when passing an object with a hasOwnProperty property', () => {
expectNoop(PropTypes.objectOf(PropTypes.number), {
hasOwnProperty: 3,
});
});

it('should be implicitly optional and not warn without values', () => {
expectNoop(PropTypes.objectOf(PropTypes.number), null);
expectNoop(PropTypes.objectOf(PropTypes.number), undefined);
Expand Down
3 changes: 2 additions & 1 deletion checkPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var printWarning = function() {};
if (process.env.NODE_ENV !== 'production') {
var ReactPropTypesSecret = require('./lib/ReactPropTypesSecret');
var loggedTypeFailures = {};
var has = Function.call.bind(Object.prototype.hasOwnProperty);

printWarning = function(text) {
var message = 'Warning: ' + text;
Expand Down Expand Up @@ -41,7 +42,7 @@ if (process.env.NODE_ENV !== 'production') {
function checkPropTypes(typeSpecs, values, location, componentName, getStack) {
if (process.env.NODE_ENV !== 'production') {
for (var typeSpecName in typeSpecs) {
if (typeSpecs.hasOwnProperty(typeSpecName)) {
if (has(typeSpecs, typeSpecName)) {
var error;
// Prop type validation may throw. In case they do, we don't want to
// fail the render phase where it didn't fail before. So we log it.
Expand Down