Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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