Skip to content

Commit

Permalink
adjust prop validation to work across vms/iframes
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Aug 11, 2016
1 parent 0e75fb9 commit a88e233
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions src/core/util/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { hasOwn, isObject, isPlainObject, capitalize, hyphenate } from 'shared/u
import { observe, observerState } from '../observer/index'
import { warn } from './debug'

type PropOptions = {
type: Function | Array<Function> | null,
default: any,
required: ?boolean,
validator: ?Function
}
// type PropOptions = {
// type: Function | Array<Function> | null,
// default: any,
// required: ?boolean,
// validator: ?Function
// }

export function validateProp (
key: string,
Expand All @@ -23,7 +23,7 @@ export function validateProp (
const absent = !hasOwn(propsData, key)
let value = propsData[key]
// handle boolean props
if (prop.type === Boolean) {
if (getType(prop.type) === 'Boolean') {
if (absent && !hasOwn(prop, 'default')) {
value = false
} else if (value === '' || value === hyphenate(key)) {
Expand Down Expand Up @@ -131,31 +131,34 @@ function assertType (value: any, type: Function): {
expectedType: string
} {
let valid
let expectedType
if (type === String) {
expectedType = 'string'
valid = typeof value === expectedType
} else if (type === Number) {
expectedType = 'number'
valid = typeof value === expectedType
} else if (type === Boolean) {
expectedType = 'boolean'
valid = typeof value === expectedType
} else if (type === Function) {
expectedType = 'function'
valid = typeof value === expectedType
} else if (type === Object) {
expectedType = 'Object'
let expectedType = getType(type)
if (expectedType === 'String') {
valid = typeof value === (expectedType = 'string')
} else if (expectedType === 'Number') {
valid = typeof value === (expectedType = 'number')
} else if (expectedType === 'Boolean') {
valid = typeof value === (expectedType = 'boolean')
} else if (expectedType === 'Function') {
valid = typeof value === (expectedType = 'function')
} else if (expectedType === 'Object') {
valid = isPlainObject(value)
} else if (type === Array) {
expectedType = 'Array'
} else if (expectedType === 'Array') {
valid = Array.isArray(value)
} else {
expectedType = type.name || type.toString()
valid = value instanceof type
}
return {
valid,
expectedType
}
}

/**
* Use function string name to check built-in types,
* because a simple equality check will fail when running
* across different vms / iframes.
*/
function getType (fn) {
const match = fn && fn.toString().match(/^function (\w+)/)
return match && match[1]
}

0 comments on commit a88e233

Please sign in to comment.