From 33e559c540f25c07dcd2b5b6f1d0a8b24092cb69 Mon Sep 17 00:00:00 2001 From: Dominik Ferber Date: Tue, 29 May 2018 12:52:49 +0200 Subject: [PATCH] [Fix] Support validation when hasOwnProperty is not in prototype Closes #183; relates to #112. --- __tests__/PropTypesDevelopmentReact15.js | 10 ++++++++++ __tests__/PropTypesDevelopmentStandalone-test.js | 10 ++++++++++ __tests__/PropTypesProductionReact15-test.js | 10 ++++++++++ checkPropTypes.js | 3 ++- 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/__tests__/PropTypesDevelopmentReact15.js b/__tests__/PropTypesDevelopmentReact15.js index ebc9590..d95dd1c 100644 --- a/__tests__/PropTypesDevelopmentReact15.js +++ b/__tests__/PropTypesDevelopmentReact15.js @@ -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); diff --git a/__tests__/PropTypesDevelopmentStandalone-test.js b/__tests__/PropTypesDevelopmentStandalone-test.js index 4267b6e..6841d9c 100644 --- a/__tests__/PropTypesDevelopmentStandalone-test.js +++ b/__tests__/PropTypesDevelopmentStandalone-test.js @@ -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); diff --git a/__tests__/PropTypesProductionReact15-test.js b/__tests__/PropTypesProductionReact15-test.js index f2afa67..bedd577 100644 --- a/__tests__/PropTypesProductionReact15-test.js +++ b/__tests__/PropTypesProductionReact15-test.js @@ -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); diff --git a/checkPropTypes.js b/checkPropTypes.js index 05403dc..30dc92d 100644 --- a/checkPropTypes.js +++ b/checkPropTypes.js @@ -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; @@ -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.