Skip to content

Commit

Permalink
Fix for 5468: Validate proptype definitions sooner
Browse files Browse the repository at this point in the history
Added typeCheckWarn() func and updated the oneOf/oneOfType tests
Added __DEV__ warning for invalid oneOf/OneOfType args
  • Loading branch information
troydemonbreun committed Jun 27, 2016
1 parent a49b7a2 commit 985a484
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/isomorphic/classic/types/ReactPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var ReactPropTypeLocationNames = require('ReactPropTypeLocationNames');

var emptyFunction = require('emptyFunction');
var getIteratorFn = require('getIteratorFn');
var warning = require('warning');

/**
* Collection of methods that allow declaration and validation of props that are
Expand Down Expand Up @@ -226,6 +227,9 @@ function createInstanceTypeChecker(expectedClass) {

function createEnumTypeChecker(expectedValues) {
if (!Array.isArray(expectedValues)) {
if (__DEV__) {
warning(false, 'Invalid argument supplied to oneOf, expected an instance of array.');
}
return createChainableTypeChecker(function() {
return new Error(
`Invalid argument supplied to oneOf, expected an instance of array.`
Expand Down Expand Up @@ -288,6 +292,9 @@ function createObjectOfTypeChecker(typeChecker) {

function createUnionTypeChecker(arrayOfTypeCheckers) {
if (!Array.isArray(arrayOfTypeCheckers)) {
if (__DEV__) {
warning(false, 'Invalid argument supplied to oneOfType, expected an instance of array.');
}
return createChainableTypeChecker(function() {
return new Error(
`Invalid argument supplied to oneOfType, expected an instance of array.`
Expand Down
19 changes: 19 additions & 0 deletions src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ function typeCheckPass(declaration, value) {
expect(error).toBe(null);
}

function typeCheckWarn(propTypeFunc, message) {
spyOn(console, ['error']);
propTypeFunc();
expect(console.error).toHaveBeenCalled();
expect(console.error.argsForCall[0][0]).toContain(message);
}

describe('ReactPropTypes', function() {
beforeEach(function() {
PropTypes = require('ReactPropTypes');
Expand Down Expand Up @@ -591,6 +598,12 @@ describe('ReactPropTypes', function() {

describe('OneOf Types', function() {
it('should fail for invalid argument', function() {
typeCheckWarn(
function() {
PropTypes.oneOf('red', 'blue');
},
'Invalid argument supplied to oneOf, expected an instance of array.'
);
typeCheckFail(
PropTypes.oneOf('red', 'blue'),
'red',
Expand Down Expand Up @@ -652,6 +665,12 @@ describe('ReactPropTypes', function() {

describe('Union Types', function() {
it('should fail for invalid argument', function() {
typeCheckWarn(
function() {
PropTypes.oneOfType(PropTypes.string, PropTypes.number);
},
'Invalid argument supplied to oneOfType, expected an instance of array.'
);
typeCheckFail(
PropTypes.oneOfType(PropTypes.string, PropTypes.number),
'red',
Expand Down

0 comments on commit 985a484

Please sign in to comment.