UNPKG

3.53 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 printWarning = function() {};
11
12if (process.env.NODE_ENV !== 'production') {
13 var ReactPropTypesSecret = require('./lib/ReactPropTypesSecret');
14 var loggedTypeFailures = {};
15
16 printWarning = function(text) {
17 var message = 'Warning: ' + text;
18 if (typeof console !== 'undefined') {
19 console.error(message);
20 }
21 try {
22 // --- Welcome to debugging React ---
23 // This error was thrown as a convenience so that you can use this stack
24 // to find the callsite that caused this warning to fire.
25 throw new Error(message);
26 } catch (x) {}
27 };
28}
29
30/**
31 * Assert that the values match with the type specs.
32 * Error messages are memorized and will only be shown once.
33 *
34 * @param {object} typeSpecs Map of name to a ReactPropType
35 * @param {object} values Runtime values that need to be type-checked
36 * @param {string} location e.g. "prop", "context", "child context"
37 * @param {string} componentName Name of the component for error messages.
38 * @param {?Function} getStack Returns the component stack.
39 * @private
40 */
41function checkPropTypes(typeSpecs, values, location, componentName, getStack) {
42 if (process.env.NODE_ENV !== 'production') {
43 for (var typeSpecName in typeSpecs) {
44 if (typeSpecs.hasOwnProperty(typeSpecName)) {
45 var error;
46 // Prop type validation may throw. In case they do, we don't want to
47 // fail the render phase where it didn't fail before. So we log it.
48 // After these have been cleaned up, we'll let them throw.
49 try {
50 // This is intentionally an invariant that gets caught. It's the same
51 // behavior as without this statement except with a better message.
52 if (typeof typeSpecs[typeSpecName] !== 'function') {
53 var err = Error(
54 (componentName || 'React class') + ': ' + location + ' type `' + typeSpecName + '` is invalid; ' +
55 'it must be a function, usually from the `prop-types` package, but received `' + typeof typeSpecs[typeSpecName] + '`.'
56 );
57 err.name = 'Invariant Violation';
58 throw err;
59 }
60 error = typeSpecs[typeSpecName](values, typeSpecName, componentName, location, null, ReactPropTypesSecret);
61 } catch (ex) {
62 error = ex;
63 }
64 if (error && !(error instanceof Error)) {
65 printWarning(
66 (componentName || 'React class') + ': type specification of ' +
67 location + ' `' + typeSpecName + '` is invalid; the type checker ' +
68 'function must return `null` or an `Error` but returned a ' + typeof error + '. ' +
69 'You may have forgotten to pass an argument to the type checker ' +
70 'creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and ' +
71 'shape all require an argument).'
72 )
73
74 }
75 if (error instanceof Error && !(error.message in loggedTypeFailures)) {
76 // Only monitor this failure once because there tends to be a lot of the
77 // same error.
78 loggedTypeFailures[error.message] = true;
79
80 var stack = getStack ? getStack() : '';
81
82 printWarning(
83 'Failed ' + location + ' type: ' + error.message + (stack != null ? stack : '')
84 );
85 }
86 }
87 }
88 }
89}
90
91module.exports = checkPropTypes;