1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | var assign = require('object-assign');
|
11 |
|
12 | var ReactPropTypesSecret = require('./lib/ReactPropTypesSecret');
|
13 | var checkPropTypes = require('./checkPropTypes');
|
14 |
|
15 | var printWarning = function() {};
|
16 |
|
17 | if (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 |
|
25 |
|
26 |
|
27 | throw new Error(message);
|
28 | } catch (x) {}
|
29 | };
|
30 | }
|
31 |
|
32 | function emptyFunctionThatReturnsNull() {
|
33 | return null;
|
34 | }
|
35 |
|
36 | module.exports = function(isValidElement, throwOnDirectAccess) {
|
37 |
|
38 | var ITERATOR_SYMBOL = typeof Symbol === 'function' && Symbol.iterator;
|
39 | var FAUX_ITERATOR_SYMBOL = '@@iterator';
|
40 |
|
41 | |
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
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 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | var ANONYMOUS = '<<anonymous>>';
|
110 |
|
111 |
|
112 |
|
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 |
|
136 |
|
137 |
|
138 |
|
139 | function is(x, y) {
|
140 |
|
141 | if (x === y) {
|
142 |
|
143 |
|
144 | return x !== 0 || 1 / x === 1 / y;
|
145 | } else {
|
146 |
|
147 | return x !== x && y !== y;
|
148 | }
|
149 | }
|
150 |
|
151 |
|
152 | |
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | function PropTypeError(message) {
|
160 | this.message = message;
|
161 | this.stack = '';
|
162 | }
|
163 |
|
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 |
|
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 |
|
187 | var cacheKey = componentName + ':' + propName;
|
188 | if (
|
189 | !manualPropTypeCallCache[cacheKey] &&
|
190 |
|
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 |
|
230 |
|
231 |
|
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 |
|
403 |
|
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 |
|
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 |
|
474 | if (propType === 'symbol') {
|
475 | return true;
|
476 | }
|
477 |
|
478 |
|
479 | if (propValue['@@toStringTag'] === 'Symbol') {
|
480 | return true;
|
481 | }
|
482 |
|
483 |
|
484 | if (typeof Symbol === 'function' && propValue instanceof Symbol) {
|
485 | return true;
|
486 | }
|
487 |
|
488 | return false;
|
489 | }
|
490 |
|
491 |
|
492 | function getPropType(propValue) {
|
493 | var propType = typeof propValue;
|
494 | if (Array.isArray(propValue)) {
|
495 | return 'array';
|
496 | }
|
497 | if (propValue instanceof RegExp) {
|
498 |
|
499 |
|
500 |
|
501 | return 'object';
|
502 | }
|
503 | if (isSymbol(propType, propValue)) {
|
504 | return 'symbol';
|
505 | }
|
506 | return propType;
|
507 | }
|
508 |
|
509 |
|
510 |
|
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 |
|
527 |
|
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 |
|
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 | };
|