Skip to content

Commit

Permalink
support Symbol in props type validation (close #5396)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 10, 2017
1 parent 9fe26a6 commit 682141f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
23 changes: 8 additions & 15 deletions src/core/util/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,16 @@ function assertProp (
}
}

/**
* Assert the type of a value
*/
const simpleCheckRE = /^(String|Number|Boolean|Function|Symbol)$/

function assertType (value: any, type: Function): {
valid: boolean,
expectedType: ?string
valid: boolean;
expectedType: string;
} {
let valid
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')
const expectedType = getType(type)
if (simpleCheckRE.test(expectedType)) {
valid = typeof value === expectedType.toLowerCase()
} else if (expectedType === 'Object') {
valid = isPlainObject(value)
} else if (expectedType === 'Array') {
Expand All @@ -166,7 +159,7 @@ function assertType (value: any, type: Function): {
*/
function getType (fn) {
const match = fn && fn.toString().match(/^\s*function (\w+)/)
return match && match[1]
return match ? match[1] : ''
}

function isType (type, fn) {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/features/options/props.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Vue from 'vue'
import { hasSymbol } from 'core/util/env'

describe('Options props', () => {
it('array syntax', done => {
Expand Down Expand Up @@ -205,6 +206,15 @@ describe('Options props', () => {
expect('Expected Array').toHaveBeenWarned()
})

if (hasSymbol) {
it('symbol', () => {
makeInstance(Symbol('foo'), Symbol)
expect(console.error.calls.count()).toBe(0)
makeInstance({}, Symbol)
expect('Expected Symbol').toHaveBeenWarned()
})
}

it('custom constructor', () => {
function Class () {}
makeInstance(new Class(), Class)
Expand Down

0 comments on commit 682141f

Please sign in to comment.