@@ -278,36 +278,23 @@ export function assertProp (prop, value) {
278
278
}
279
279
var options = prop . options
280
280
var type = options . type
281
- var valid = true
282
- var expectedType
281
+ var valid
282
+ var expectedTypes = [ ]
283
283
if ( type ) {
284
- if ( type === String ) {
285
- expectedType = 'string'
286
- valid = typeof value === expectedType
287
- } else if ( type === Number ) {
288
- expectedType = 'number'
289
- valid = typeof value === 'number'
290
- } else if ( type === Boolean ) {
291
- expectedType = 'boolean'
292
- valid = typeof value === 'boolean'
293
- } else if ( type === Function ) {
294
- expectedType = 'function'
295
- valid = typeof value === 'function'
296
- } else if ( type === Object ) {
297
- expectedType = 'object'
298
- valid = isPlainObject ( value )
299
- } else if ( type === Array ) {
300
- expectedType = 'array'
301
- valid = isArray ( value )
302
- } else {
303
- valid = value instanceof type
284
+ if ( ! isArray ( type ) ) {
285
+ type = [ type ]
286
+ }
287
+ for ( var i = 0 ; i < type . length && ! valid ; i ++ ) {
288
+ var assertedType = assertType ( value , type [ i ] )
289
+ expectedTypes . push ( assertedType . expectedType )
290
+ valid = assertedType . valid
304
291
}
305
292
}
306
- if ( ! valid ) {
293
+ if ( typeof valid !== 'undefined' && ! valid ) {
307
294
process . env . NODE_ENV !== 'production' && warn (
308
295
'Invalid prop: type check failed for ' +
309
296
prop . path + '="' + prop . raw + '".' +
310
- ' Expected ' + formatType ( expectedType ) +
297
+ ' Expected ' + formatTypes ( expectedTypes ) +
311
298
', got ' + formatValue ( value ) + '.'
312
299
)
313
300
return false
@@ -342,9 +329,46 @@ export function coerceProp (prop, value) {
342
329
return coerce ( value )
343
330
}
344
331
345
- function formatType ( val ) {
346
- return val
347
- ? val . charAt ( 0 ) . toUpperCase ( ) + val . slice ( 1 )
332
+ function assertType ( value , type ) {
333
+ var valid
334
+ var expectedType
335
+ if ( type === String ) {
336
+ expectedType = 'string'
337
+ valid = typeof value === expectedType
338
+ } else if ( type === Number ) {
339
+ expectedType = 'number'
340
+ valid = typeof value === expectedType
341
+ } else if ( type === Boolean ) {
342
+ expectedType = 'boolean'
343
+ valid = typeof value === expectedType
344
+ } else if ( type === Function ) {
345
+ expectedType = 'function'
346
+ valid = typeof value === expectedType
347
+ } else if ( type === Object ) {
348
+ expectedType = 'object'
349
+ valid = isPlainObject ( value )
350
+ } else if ( type === Array ) {
351
+ expectedType = 'array'
352
+ valid = isArray ( value )
353
+ } else {
354
+ valid = value instanceof type
355
+ }
356
+ return {
357
+ valid,
358
+ expectedType
359
+ }
360
+ }
361
+
362
+ function formatTypes ( types ) {
363
+ return types . reduce (
364
+ ( p , c , i ) => { return i === 0 ? formatType ( c ) : `${ p } , ${ formatType ( c ) } ` } ,
365
+ ''
366
+ )
367
+ }
368
+
369
+ function formatType ( type ) {
370
+ return type
371
+ ? type . charAt ( 0 ) . toUpperCase ( ) + type . slice ( 1 )
348
372
: 'custom type'
349
373
}
350
374
0 commit comments