* Assert whether a prop is valid.
( prop, name, value, vm, absent )
| 1332 | * Assert whether a prop is valid. |
| 1333 | */ |
| 1334 | function assertProp ( |
| 1335 | prop, |
| 1336 | name, |
| 1337 | value, |
| 1338 | vm, |
| 1339 | absent |
| 1340 | ) { |
| 1341 | if (prop.required && absent) { |
| 1342 | warn( |
| 1343 | 'Missing required prop: "' + name + '"', |
| 1344 | vm |
| 1345 | ); |
| 1346 | return |
| 1347 | } |
| 1348 | if (value == null && !prop.required) { |
| 1349 | return |
| 1350 | } |
| 1351 | var type = prop.type; |
| 1352 | var valid = !type || type === true; |
| 1353 | var expectedTypes = []; |
| 1354 | if (type) { |
| 1355 | if (!Array.isArray(type)) { |
| 1356 | type = [type]; |
| 1357 | } |
| 1358 | for (var i = 0; i < type.length && !valid; i++) { |
| 1359 | var assertedType = assertType(value, type[i]); |
| 1360 | expectedTypes.push(assertedType.expectedType || ''); |
| 1361 | valid = assertedType.valid; |
| 1362 | } |
| 1363 | } |
| 1364 | if (!valid) { |
| 1365 | warn( |
| 1366 | 'Invalid prop: type check failed for prop "' + name + '".' + |
| 1367 | ' Expected ' + expectedTypes.map(capitalize).join(', ') + |
| 1368 | ', got ' + Object.prototype.toString.call(value).slice(8, -1) + '.', |
| 1369 | vm |
| 1370 | ); |
| 1371 | return |
| 1372 | } |
| 1373 | var validator = prop.validator; |
| 1374 | if (validator) { |
| 1375 | if (!validator(value)) { |
| 1376 | warn( |
| 1377 | 'Invalid prop: custom validator check failed for prop "' + name + '".', |
| 1378 | vm |
| 1379 | ); |
| 1380 | } |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | /** |
| 1385 | * Assert the type of a value |
no test coverage detected