UNPKG

4.19 kBJavaScriptView Raw
1/**
2 * Copyright 2013-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 *
9 */
10
11'use strict';
12
13var _prodInvariant = require('./reactProdInvariant');
14
15var ReactPropTypeLocationNames = require('./ReactPropTypeLocationNames');
16var ReactPropTypesSecret = require('./ReactPropTypesSecret');
17
18var invariant = require('fbjs/lib/invariant');
19var warning = require('fbjs/lib/warning');
20
21var ReactComponentTreeHook;
22
23if (typeof process !== 'undefined' && process.env && process.env.NODE_ENV === 'test') {
24 // Temporary hack.
25 // Inline requires don't work well with Jest:
26 // https://github.com/facebook/react/issues/7240
27 // Remove the inline requires when we don't need them anymore:
28 // https://github.com/facebook/react/pull/7178
29 ReactComponentTreeHook = require('./ReactComponentTreeHook');
30}
31
32var loggedTypeFailures = {};
33
34/**
35 * Assert that the values match with the type specs.
36 * Error messages are memorized and will only be shown once.
37 *
38 * @param {object} typeSpecs Map of name to a ReactPropType
39 * @param {object} values Runtime values that need to be type-checked
40 * @param {string} location e.g. "prop", "context", "child context"
41 * @param {string} componentName Name of the component for error messages.
42 * @param {?object} element The React element that is being type-checked
43 * @param {?number} debugID The React component instance that is being type-checked
44 * @private
45 */
46function checkReactTypeSpec(typeSpecs, values, location, componentName, element, debugID) {
47 for (var typeSpecName in typeSpecs) {
48 if (typeSpecs.hasOwnProperty(typeSpecName)) {
49 var error;
50 // Prop type validation may throw. In case they do, we don't want to
51 // fail the render phase where it didn't fail before. So we log it.
52 // After these have been cleaned up, we'll let them throw.
53 try {
54 // This is intentionally an invariant that gets caught. It's the same
55 // behavior as without this statement except with a better message.
56 !(typeof typeSpecs[typeSpecName] === 'function') ? process.env.NODE_ENV !== 'production' ? invariant(false, '%s: %s type `%s` is invalid; it must be a function, usually from React.PropTypes.', componentName || 'React class', ReactPropTypeLocationNames[location], typeSpecName) : _prodInvariant('84', componentName || 'React class', ReactPropTypeLocationNames[location], typeSpecName) : void 0;
57 error = typeSpecs[typeSpecName](values, typeSpecName, componentName, location, null, ReactPropTypesSecret);
58 } catch (ex) {
59 error = ex;
60 }
61 process.env.NODE_ENV !== 'production' ? warning(!error || error instanceof Error, '%s: type specification of %s `%s` is invalid; the type checker ' + 'function must return `null` or an `Error` but returned a %s. ' + 'You may have forgotten to pass an argument to the type checker ' + 'creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and ' + 'shape all require an argument).', componentName || 'React class', ReactPropTypeLocationNames[location], typeSpecName, typeof error) : void 0;
62 if (error instanceof Error && !(error.message in loggedTypeFailures)) {
63 // Only monitor this failure once because there tends to be a lot of the
64 // same error.
65 loggedTypeFailures[error.message] = true;
66
67 var componentStackInfo = '';
68
69 if (process.env.NODE_ENV !== 'production') {
70 if (!ReactComponentTreeHook) {
71 ReactComponentTreeHook = require('./ReactComponentTreeHook');
72 }
73 if (debugID !== null) {
74 componentStackInfo = ReactComponentTreeHook.getStackAddendumByID(debugID);
75 } else if (element !== null) {
76 componentStackInfo = ReactComponentTreeHook.getCurrentStackAddendum(element);
77 }
78 }
79
80 process.env.NODE_ENV !== 'production' ? warning(false, 'Failed %s type: %s%s', location, error.message, componentStackInfo) : void 0;
81 }
82 }
83 }
84}
85
86module.exports = checkReactTypeSpec;
\No newline at end of file