Skip to content

Commit

Permalink
[Fix] Support validation when hasOwnProperty is not in prototype
Browse files Browse the repository at this point in the history
Closes #183; relates to #112.
  • Loading branch information
dferber90 authored and ljharb committed May 29, 2018
1 parent 11e45c4 commit 33e559c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
10 changes: 10 additions & 0 deletions __tests__/PropTypesDevelopmentReact15.js
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
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
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
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

0 comments on commit 33e559c

Please sign in to comment.