UNPKG

20.2 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2013-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8'use strict';
9
10var assign = require('object-assign');
11
12var ReactPropTypesSecret = require('./lib/ReactPropTypesSecret');
13var checkPropTypes = require('./checkPropTypes');
14
15var printWarning = function() {};
16
17if (process.env.NODE_ENV !== 'production') {
18 printWarning = function(text) {
19 var message = 'Warning: ' + text;
20 if (typeof console !== 'undefined') {
21 console.error(message);
22 }
23 try {
24 // --- Welcome to debugging React ---
25 // This error was thrown as a convenience so that you can use this stack
26 // to find the callsite that caused this warning to fire.
27 throw new Error(message);
28 } catch (x) {}
29 };
30}
31
32function emptyFunctionThatReturnsNull() {
33 return null;
34}
35
36module.exports = function(isValidElement, throwOnDirectAccess) {
37 /* global Symbol */
38 var ITERATOR_SYMBOL = typeof Symbol === 'function' && Symbol.iterator;
39 var FAUX_ITERATOR_SYMBOL = '@@iterator'; // Before Symbol spec.
40
41 /**
42 * Returns the iterator method function contained on the iterable object.
43 *
44 * Be sure to invoke the function with the iterable as context:
45 *
46 * var iteratorFn = getIteratorFn(myIterable);
47 * if (iteratorFn) {
48 * var iterator = iteratorFn.call(myIterable);
49 * ...
50 * }
51 *
52 * @param {?object} maybeIterable
53 * @return {?function}
54 */
55 function getIteratorFn(maybeIterable) {
56 var iteratorFn = maybeIterable && (ITERATOR_SYMBOL && maybeIterable[ITERATOR_SYMBOL] || maybeIterable[FAUX_ITERATOR_SYMBOL]);
57 if (typeof iteratorFn === 'function') {
58 return iteratorFn;
59 }
60 }
61
62 /**
63 * Collection of methods that allow declaration and validation of props that are
64 * supplied to React components. Example usage:
65 *
66 * var Props = require('ReactPropTypes');
67 * var MyArticle = React.createClass({
68 * propTypes: {
69 * // An optional string prop named "description".
70 * description: Props.string,
71 *
72 * // A required enum prop named "category".
73 * category: Props.oneOf(['News','Photos']).isRequired,
74 *
75 * // A prop named "dialog" that requires an instance of Dialog.
76 * dialog: Props.instanceOf(Dialog).isRequired
77 * },
78 * render: function() { ... }
79 * });
80 *
81 * A more formal specification of how these methods are used:
82 *
83 * type := array|bool|func|object|number|string|oneOf([...])|instanceOf(...)
84 * decl := ReactPropTypes.{type}(.isRequired)?
85 *
86 * Each and every declaration produces a function with the same signature. This
87 * allows the creation of custom validation functions. For example:
88 *
89 * var MyLink = React.createClass({
90 * propTypes: {
91 * // An optional string or URI prop named "href".
92 * href: function(props, propName, componentName) {
93 * var propValue = props[propName];
94 * if (propValue != null && typeof propValue !== 'string' &&
95 * !(propValue instanceof URI)) {
96 * return new Error(
97 * 'Expected a string or an URI for ' + propName + ' in ' +
98 * componentName
99 * );
100 * }
101 * }
102 * },
103 * render: function() {...}
104 * });
105 *
106 * @internal
107 */
108
109 var ANONYMOUS = '<<anonymous>>';
110
111 // Important!
112 // Keep this list in sync with production version in `./factoryWithThrowingShims.js`.
113 var ReactPropTypes = {
114 array: createPrimitiveTypeChecker('array'),
115 bool: createPrimitiveTypeChecker('boolean'),
116 func: createPrimitiveTypeChecker('function'),
117 number: createPrimitiveTypeChecker('number'),
118 object: createPrimitiveTypeChecker('object'),
119 string: createPrimitiveTypeChecker('string'),
120 symbol: createPrimitiveTypeChecker('symbol'),
121
122 any: createAnyTypeChecker(),
123 arrayOf: createArrayOfTypeChecker,
124 element: createElementTypeChecker(),
125 instanceOf: createInstanceTypeChecker,
126 node: createNodeChecker(),
127 objectOf: createObjectOfTypeChecker,
128 oneOf: createEnumTypeChecker,
129 oneOfType: createUnionTypeChecker,
130 shape: createShapeTypeChecker,
131 exact: createStrictShapeTypeChecker,
132 };
133
134 /**
135 * inlined Object.is polyfill to avoid requiring consumers ship their own
136 * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
137 */
138 /*eslint-disable no-self-compare*/
139 function is(x, y) {
140 // SameValue algorithm
141 if (x === y) {
142 // Steps 1-5, 7-10
143 // Steps 6.b-6.e: +0 != -0
144 return x !== 0 || 1 / x === 1 / y;
145 } else {
146 // Step 6.a: NaN == NaN
147 return x !== x && y !== y;
148 }
149 }
150 /*eslint-enable no-self-compare*/
151
152 /**
153 * We use an Error-like object for backward compatibility as people may call
154 * PropTypes directly and inspect their output. However, we don't use real
155 * Errors anymore. We don't inspect their stack anyway, and creating them
156 * is prohibitively expensive if they are created too often, such as what
157 * happens in oneOfType() for any type before the one that matched.
158 */
159 function PropTypeError(message) {
160 this.message = message;
161 this.stack = '';
162 }
163 // Make `instanceof Error` still work for returned errors.
164 PropTypeError.prototype = Error.prototype;
165
166 function createChainableTypeChecker(validate) {
167 if (process.env.NODE_ENV !== 'production') {
168 var manualPropTypeCallCache = {};
169 var manualPropTypeWarningCount = 0;
170 }
171 function checkType(isRequired, props, propName, componentName, location, propFullName, secret) {
172 componentName = componentName || ANONYMOUS;
173 propFullName = propFullName || propName;
174
175 if (secret !== ReactPropTypesSecret) {
176 if (throwOnDirectAccess) {
177 // New behavior only for users of `prop-types` package
178 var err = new Error(
179 'Calling PropTypes validators directly is not supported by the `prop-types` package. ' +
180 'Use `PropTypes.checkPropTypes()` to call them. ' +
181 'Read more at http://fb.me/use-check-prop-types'
182 );
183 err.name = 'Invariant Violation';
184 throw err;
185 } else if (process.env.NODE_ENV !== 'production' && typeof console !== 'undefined') {
186 // Old behavior for people using React.PropTypes
187 var cacheKey = componentName + ':' + propName;
188 if (
189 !manualPropTypeCallCache[cacheKey] &&
190 // Avoid spamming the console because they are often not actionable except for lib authors
191 manualPropTypeWarningCount < 3
192 ) {
193 printWarning(
194 'You are manually calling a React.PropTypes validation ' +
195 'function for the `' + propFullName + '` prop on `' + componentName + '`. This is deprecated ' +
196 'and will throw in the standalone `prop-types` package. ' +
197 'You may be seeing this warning due to a third-party PropTypes ' +
198 'library. See https://fb.me/react-warning-dont-call-proptypes ' + 'for details.'
199 );
200 manualPropTypeCallCache[cacheKey] = true;
201 manualPropTypeWarningCount++;
202 }
203 }
204 }
205 if (props[propName] == null) {
206 if (isRequired) {
207 if (props[propName] === null) {
208 return new PropTypeError('The ' + location + ' `' + propFullName + '` is marked as required ' + ('in `' + componentName + '`, but its value is `null`.'));
209 }
210 return new PropTypeError('The ' + location + ' `' + propFullName + '` is marked as required in ' + ('`' + componentName + '`, but its value is `undefined`.'));
211 }
212 return null;
213 } else {
214 return validate(props, propName, componentName, location, propFullName);
215 }
216 }
217
218 var chainedCheckType = checkType.bind(null, false);
219 chainedCheckType.isRequired = checkType.bind(null, true);
220
221 return chainedCheckType;
222 }
223
224 function createPrimitiveTypeChecker(expectedType) {
225 function validate(props, propName, componentName, location, propFullName, secret) {
226 var propValue = props[propName];
227 var propType = getPropType(propValue);
228 if (propType !== expectedType) {
229 // `propValue` being instance of, say, date/regexp, pass the 'object'
230 // check, but we can offer a more precise error message here rather than
231 // 'of type `object`'.
232 var preciseType = getPreciseType(propValue);
233
234 return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of type ' + ('`' + preciseType + '` supplied to `' + componentName + '`, expected ') + ('`' + expectedType + '`.'));
235 }
236 return null;
237 }
238 return createChainableTypeChecker(validate);
239 }
240
241 function createAnyTypeChecker() {
242 return createChainableTypeChecker(emptyFunctionThatReturnsNull);
243 }
244
245 function createArrayOfTypeChecker(typeChecker) {
246 function validate(props, propName, componentName, location, propFullName) {
247 if (typeof typeChecker !== 'function') {
248 return new PropTypeError('Property `' + propFullName + '` of component `' + componentName + '` has invalid PropType notation inside arrayOf.');
249 }
250 var propValue = props[propName];
251 if (!Array.isArray(propValue)) {
252 var propType = getPropType(propValue);
253 return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of type ' + ('`' + propType + '` supplied to `' + componentName + '`, expected an array.'));
254 }
255 for (var i = 0; i < propValue.length; i++) {
256 var error = typeChecker(propValue, i, componentName, location, propFullName + '[' + i + ']', ReactPropTypesSecret);
257 if (error instanceof Error) {
258 return error;
259 }
260 }
261 return null;
262 }
263 return createChainableTypeChecker(validate);
264 }
265
266 function createElementTypeChecker() {
267 function validate(props, propName, componentName, location, propFullName) {
268 var propValue = props[propName];
269 if (!isValidElement(propValue)) {
270 var propType = getPropType(propValue);
271 return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of type ' + ('`' + propType + '` supplied to `' + componentName + '`, expected a single ReactElement.'));
272 }
273 return null;
274 }
275 return createChainableTypeChecker(validate);
276 }
277
278 function createInstanceTypeChecker(expectedClass) {
279 function validate(props, propName, componentName, location, propFullName) {
280 if (!(props[propName] instanceof expectedClass)) {
281 var expectedClassName = expectedClass.name || ANONYMOUS;
282 var actualClassName = getClassName(props[propName]);
283 return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of type ' + ('`' + actualClassName + '` supplied to `' + componentName + '`, expected ') + ('instance of `' + expectedClassName + '`.'));
284 }
285 return null;
286 }
287 return createChainableTypeChecker(validate);
288 }
289
290 function createEnumTypeChecker(expectedValues) {
291 if (!Array.isArray(expectedValues)) {
292 process.env.NODE_ENV !== 'production' ? printWarning('Invalid argument supplied to oneOf, expected an instance of array.') : void 0;
293 return emptyFunctionThatReturnsNull;
294 }
295
296 function validate(props, propName, componentName, location, propFullName) {
297 var propValue = props[propName];
298 for (var i = 0; i < expectedValues.length; i++) {
299 if (is(propValue, expectedValues[i])) {
300 return null;
301 }
302 }
303
304 var valuesString = JSON.stringify(expectedValues);
305 return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of value `' + propValue + '` ' + ('supplied to `' + componentName + '`, expected one of ' + valuesString + '.'));
306 }
307 return createChainableTypeChecker(validate);
308 }
309
310 function createObjectOfTypeChecker(typeChecker) {
311 function validate(props, propName, componentName, location, propFullName) {
312 if (typeof typeChecker !== 'function') {
313 return new PropTypeError('Property `' + propFullName + '` of component `' + componentName + '` has invalid PropType notation inside objectOf.');
314 }
315 var propValue = props[propName];
316 var propType = getPropType(propValue);
317 if (propType !== 'object') {
318 return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of type ' + ('`' + propType + '` supplied to `' + componentName + '`, expected an object.'));
319 }
320 for (var key in propValue) {
321 if (propValue.hasOwnProperty(key)) {
322 var error = typeChecker(propValue, key, componentName, location, propFullName + '.' + key, ReactPropTypesSecret);
323 if (error instanceof Error) {
324 return error;
325 }
326 }
327 }
328 return null;
329 }
330 return createChainableTypeChecker(validate);
331 }
332
333 function createUnionTypeChecker(arrayOfTypeCheckers) {
334 if (!Array.isArray(arrayOfTypeCheckers)) {
335 process.env.NODE_ENV !== 'production' ? printWarning('Invalid argument supplied to oneOfType, expected an instance of array.') : void 0;
336 return emptyFunctionThatReturnsNull;
337 }
338
339 for (var i = 0; i < arrayOfTypeCheckers.length; i++) {
340 var checker = arrayOfTypeCheckers[i];
341 if (typeof checker !== 'function') {
342 printWarning(
343 'Invalid argument supplied to oneOfType. Expected an array of check functions, but ' +
344 'received ' + getPostfixForTypeWarning(checker) + ' at index ' + i + '.'
345 );
346 return emptyFunctionThatReturnsNull;
347 }
348 }
349
350 function validate(props, propName, componentName, location, propFullName) {
351 for (var i = 0; i < arrayOfTypeCheckers.length; i++) {
352 var checker = arrayOfTypeCheckers[i];
353 if (checker(props, propName, componentName, location, propFullName, ReactPropTypesSecret) == null) {
354 return null;
355 }
356 }
357
358 return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` supplied to ' + ('`' + componentName + '`.'));
359 }
360 return createChainableTypeChecker(validate);
361 }
362
363 function createNodeChecker() {
364 function validate(props, propName, componentName, location, propFullName) {
365 if (!isNode(props[propName])) {
366 return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` supplied to ' + ('`' + componentName + '`, expected a ReactNode.'));
367 }
368 return null;
369 }
370 return createChainableTypeChecker(validate);
371 }
372
373 function createShapeTypeChecker(shapeTypes) {
374 function validate(props, propName, componentName, location, propFullName) {
375 var propValue = props[propName];
376 var propType = getPropType(propValue);
377 if (propType !== 'object') {
378 return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of type `' + propType + '` ' + ('supplied to `' + componentName + '`, expected `object`.'));
379 }
380 for (var key in shapeTypes) {
381 var checker = shapeTypes[key];
382 if (!checker) {
383 continue;
384 }
385 var error = checker(propValue, key, componentName, location, propFullName + '.' + key, ReactPropTypesSecret);
386 if (error) {
387 return error;
388 }
389 }
390 return null;
391 }
392 return createChainableTypeChecker(validate);
393 }
394
395 function createStrictShapeTypeChecker(shapeTypes) {
396 function validate(props, propName, componentName, location, propFullName) {
397 var propValue = props[propName];
398 var propType = getPropType(propValue);
399 if (propType !== 'object') {
400 return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of type `' + propType + '` ' + ('supplied to `' + componentName + '`, expected `object`.'));
401 }
402 // We need to check all keys in case some are required but missing from
403 // props.
404 var allKeys = assign({}, props[propName], shapeTypes);
405 for (var key in allKeys) {
406 var checker = shapeTypes[key];
407 if (!checker) {
408 return new PropTypeError(
409 'Invalid ' + location + ' `' + propFullName + '` key `' + key + '` supplied to `' + componentName + '`.' +
410 '\nBad object: ' + JSON.stringify(props[propName], null, ' ') +
411 '\nValid keys: ' + JSON.stringify(Object.keys(shapeTypes), null, ' ')
412 );
413 }
414 var error = checker(propValue, key, componentName, location, propFullName + '.' + key, ReactPropTypesSecret);
415 if (error) {
416 return error;
417 }
418 }
419 return null;
420 }
421
422 return createChainableTypeChecker(validate);
423 }
424
425 function isNode(propValue) {
426 switch (typeof propValue) {
427 case 'number':
428 case 'string':
429 case 'undefined':
430 return true;
431 case 'boolean':
432 return !propValue;
433 case 'object':
434 if (Array.isArray(propValue)) {
435 return propValue.every(isNode);
436 }
437 if (propValue === null || isValidElement(propValue)) {
438 return true;
439 }
440
441 var iteratorFn = getIteratorFn(propValue);
442 if (iteratorFn) {
443 var iterator = iteratorFn.call(propValue);
444 var step;
445 if (iteratorFn !== propValue.entries) {
446 while (!(step = iterator.next()).done) {
447 if (!isNode(step.value)) {
448 return false;
449 }
450 }
451 } else {
452 // Iterator will provide entry [k,v] tuples rather than values.
453 while (!(step = iterator.next()).done) {
454 var entry = step.value;
455 if (entry) {
456 if (!isNode(entry[1])) {
457 return false;
458 }
459 }
460 }
461 }
462 } else {
463 return false;
464 }
465
466 return true;
467 default:
468 return false;
469 }
470 }
471
472 function isSymbol(propType, propValue) {
473 // Native Symbol.
474 if (propType === 'symbol') {
475 return true;
476 }
477
478 // 19.4.3.5 Symbol.prototype[@@toStringTag] === 'Symbol'
479 if (propValue['@@toStringTag'] === 'Symbol') {
480 return true;
481 }
482
483 // Fallback for non-spec compliant Symbols which are polyfilled.
484 if (typeof Symbol === 'function' && propValue instanceof Symbol) {
485 return true;
486 }
487
488 return false;
489 }
490
491 // Equivalent of `typeof` but with special handling for array and regexp.
492 function getPropType(propValue) {
493 var propType = typeof propValue;
494 if (Array.isArray(propValue)) {
495 return 'array';
496 }
497 if (propValue instanceof RegExp) {
498 // Old webkits (at least until Android 4.0) return 'function' rather than
499 // 'object' for typeof a RegExp. We'll normalize this here so that /bla/
500 // passes PropTypes.object.
501 return 'object';
502 }
503 if (isSymbol(propType, propValue)) {
504 return 'symbol';
505 }
506 return propType;
507 }
508
509 // This handles more types than `getPropType`. Only used for error messages.
510 // See `createPrimitiveTypeChecker`.
511 function getPreciseType(propValue) {
512 if (typeof propValue === 'undefined' || propValue === null) {
513 return '' + propValue;
514 }
515 var propType = getPropType(propValue);
516 if (propType === 'object') {
517 if (propValue instanceof Date) {
518 return 'date';
519 } else if (propValue instanceof RegExp) {
520 return 'regexp';
521 }
522 }
523 return propType;
524 }
525
526 // Returns a string that is postfixed to a warning about an invalid type.
527 // For example, "undefined" or "of type array"
528 function getPostfixForTypeWarning(value) {
529 var type = getPreciseType(value);
530 switch (type) {
531 case 'array':
532 case 'object':
533 return 'an ' + type;
534 case 'boolean':
535 case 'date':
536 case 'regexp':
537 return 'a ' + type;
538 default:
539 return type;
540 }
541 }
542
543 // Returns class name of the object, if any.
544 function getClassName(propValue) {
545 if (!propValue.constructor || !propValue.constructor.name) {
546 return ANONYMOUS;
547 }
548 return propValue.constructor.name;
549 }
550
551 ReactPropTypes.checkPropTypes = checkPropTypes;
552 ReactPropTypes.PropTypes = ReactPropTypes;
553
554 return ReactPropTypes;
555};