@samitouri / QOS-React / commits / 6cc037bd0d

Fix for #5468: Validate PropTypes.oneOf(Type) arguments early (#6316)

* Fix for 5468: Validate proptype definitions sooner Added typeCheckWarn() func and updated the oneOf/oneOfType tests Added __DEV__ warning for invalid oneOf/OneOfType args * Suppress redundant error on warn; typeCheckWarn() removed * Return no-op * Using emptyFunction module for consistency * Remove createChainableTypeChecker() call * Adjust test to assert type check passes when warned

Troy DeMonbreun committed Jun 28, 2016 at 19:30 UTC 6cc037bd0d49f1f55cbdd36fbfd395118c64d393
2 files changed +25 -22
src/isomorphic/classic/types/ReactPropTypes.js
+5 -10
@@ -16,6 +16,7 @@ var ReactPropTypeLocationNames = require('ReactPropTypeLocationNames');
16
17 var emptyFunction = require('emptyFunction');
18 var getIteratorFn = require('getIteratorFn');
19 +var warning = require('warning');
20
21 /**
22 * Collection of methods that allow declaration and validation of props that are
@@ -226,11 +227,8 @@ function createInstanceTypeChecker(expectedClass) {
227
228 function createEnumTypeChecker(expectedValues) {
229 if (!Array.isArray(expectedValues)) {
229 - return createChainableTypeChecker(function() {
230 - return new Error(
231 - `Invalid argument supplied to oneOf, expected an instance of array.`
232 - );
233 - });
230 + warning(false, 'Invalid argument supplied to oneOf, expected an instance of array.');
231 + return emptyFunction.thatReturnsNull;
232 }
233
234 function validate(props, propName, componentName, location, propFullName) {
@@ -288,11 +286,8 @@ function createObjectOfTypeChecker(typeChecker) {
286
287 function createUnionTypeChecker(arrayOfTypeCheckers) {
288 if (!Array.isArray(arrayOfTypeCheckers)) {
291 - return createChainableTypeChecker(function() {
292 - return new Error(
293 - `Invalid argument supplied to oneOfType, expected an instance of array.`
294 - );
295 - });
289 + warning(false, 'Invalid argument supplied to oneOfType, expected an instance of array.');
290 + return emptyFunction.thatReturnsNull;
291 }
292
293 function validate(props, propName, componentName, location, propFullName) {
src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
+20 -12
@@ -590,12 +590,16 @@ describe('ReactPropTypes', function() {
590 });
591
592 describe('OneOf Types', function() {
593 - it('should fail for invalid argument', function() {
594 - typeCheckFail(
595 - PropTypes.oneOf('red', 'blue'),
596 - 'red',
597 - 'Invalid argument supplied to oneOf, expected an instance of array.'
598 - );
593 + it('should warn but not error for invalid argument', function() {
594 + spyOn(console, 'error');
595 +
596 + PropTypes.oneOf('red', 'blue');
597 +
598 + expect(console.error).toHaveBeenCalled();
599 + expect(console.error.calls.argsFor(0)[0])
600 + .toContain('Invalid argument supplied to oneOf, expected an instance of array.');
601 +
602 + typeCheckPass(PropTypes.oneOf('red', 'blue'), 'red');
603 });
604
605 it('should warn for invalid values', function() {
@@ -651,12 +655,16 @@ describe('ReactPropTypes', function() {
655 });
656
657 describe('Union Types', function() {
654 - it('should fail for invalid argument', function() {
655 - typeCheckFail(
656 - PropTypes.oneOfType(PropTypes.string, PropTypes.number),
657 - 'red',
658 - 'Invalid argument supplied to oneOfType, expected an instance of array.'
659 - );
658 + it('should warn but not error for invalid argument', function() {
659 + spyOn(console, 'error');
660 +
661 + PropTypes.oneOfType(PropTypes.string, PropTypes.number);
662 +
663 + expect(console.error).toHaveBeenCalled();
664 + expect(console.error.calls.argsFor(0)[0])
665 + .toContain('Invalid argument supplied to oneOfType, expected an instance of array.');
666 +
667 + typeCheckPass(PropTypes.oneOf(PropTypes.string, PropTypes.number), []);
668 });
669
670 it('should warn if none of the types are valid', function() {