@samitouri / QOS-React-2 / commits / 5d55bbc1b7

Adding validation for array in ReactPropTypes.

Alan Souza committed May 26, 2015 at 15:54 UTC 5d55bbc1b7fcee7ebee9d2fb481b5e6814b38475
2 files changed +26
src/isomorphic/classic/types/ReactPropTypes.js
+12
@@ -200,6 +200,12 @@ function createInstanceTypeChecker(expectedClass) {
200 }
201
202 function createEnumTypeChecker(expectedValues) {
203 + if (!Array.isArray(expectedValues)) {
204 + return new Error(
205 + `Invalid argument supplied to oneOf, expected an instance of array.`
206 + );
207 + }
208 +
209 function validate(props, propName, componentName, location, propFullName) {
210 var propValue = props[propName];
211 for (var i = 0; i < expectedValues.length; i++) {
@@ -249,6 +255,12 @@ function createObjectOfTypeChecker(typeChecker) {
255 }
256
257 function createUnionTypeChecker(arrayOfTypeCheckers) {
258 + if (!Array.isArray(arrayOfTypeCheckers)) {
259 + return new Error(
260 + `Invalid argument supplied to oneOfType, expected an instance of array.`
261 + );
262 + }
263 +
264 function validate(props, propName, componentName, location, propFullName) {
265 for (var i = 0; i < arrayOfTypeCheckers.length; i++) {
266 var checker = arrayOfTypeCheckers[i];
src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
+14
@@ -520,6 +520,13 @@ describe('ReactPropTypes', function() {
520 });
521
522 describe('OneOf Types', function() {
523 + it("should fail for invalid argument", function() {
524 + var error = PropTypes.oneOf('red', 'blue');
525 + expect(error instanceof Error).toBe(true);
526 + expect(error.message).toBe('Invalid argument supplied to ' +
527 + 'oneOf, expected an instance of array.');
528 + });
529 +
530 it("should warn for invalid strings", function() {
531 typeCheckFail(
532 PropTypes.oneOf(['red', 'blue']),
@@ -572,6 +579,13 @@ describe('ReactPropTypes', function() {
579 });
580
581 describe('Union Types', function() {
582 + it("should fail for invalid argument", function() {
583 + var error = PropTypes.oneOfType('red', 'blue');
584 + expect(error instanceof Error).toBe(true);
585 + expect(error.message).toBe('Invalid argument supplied to ' +
586 + 'oneOfType, expected an instance of array.');
587 + });
588 +
589 it('should warn if none of the types are valid', function() {
590 typeCheckFail(
591 PropTypes.oneOfType([PropTypes.string, PropTypes.number]),