Skip to content

Commit 682141f

Browse files
committed
support Symbol in props type validation (close vuejs#5396)
1 parent 9fe26a6 commit 682141f

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

src/core/util/props.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -129,23 +129,16 @@ function assertProp (
129129
}
130130
}
131131

132-
/**
133-
* Assert the type of a value
134-
*/
132+
const simpleCheckRE = /^(String|Number|Boolean|Function|Symbol)$/
133+
135134
function assertType (value: any, type: Function): {
136-
valid: boolean,
137-
expectedType: ?string
135+
valid: boolean;
136+
expectedType: string;
138137
} {
139138
let valid
140-
let expectedType = getType(type)
141-
if (expectedType === 'String') {
142-
valid = typeof value === (expectedType = 'string')
143-
} else if (expectedType === 'Number') {
144-
valid = typeof value === (expectedType = 'number')
145-
} else if (expectedType === 'Boolean') {
146-
valid = typeof value === (expectedType = 'boolean')
147-
} else if (expectedType === 'Function') {
148-
valid = typeof value === (expectedType = 'function')
139+
const expectedType = getType(type)
140+
if (simpleCheckRE.test(expectedType)) {
141+
valid = typeof value === expectedType.toLowerCase()
149142
} else if (expectedType === 'Object') {
150143
valid = isPlainObject(value)
151144
} else if (expectedType === 'Array') {
@@ -166,7 +159,7 @@ function assertType (value: any, type: Function): {
166159
*/
167160
function getType (fn) {
168161
const match = fn && fn.toString().match(/^\s*function (\w+)/)
169-
return match && match[1]
162+
return match ? match[1] : ''
170163
}
171164

172165
function isType (type, fn) {

test/unit/features/options/props.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Vue from 'vue'
2+
import { hasSymbol } from 'core/util/env'
23

34
describe('Options props', () => {
45
it('array syntax', done => {
@@ -205,6 +206,15 @@ describe('Options props', () => {
205206
expect('Expected Array').toHaveBeenWarned()
206207
})
207208

209+
if (hasSymbol) {
210+
it('symbol', () => {
211+
makeInstance(Symbol('foo'), Symbol)
212+
expect(console.error.calls.count()).toBe(0)
213+
makeInstance({}, Symbol)
214+
expect('Expected Symbol').toHaveBeenWarned()
215+
})
216+
}
217+
208218
it('custom constructor', () => {
209219
function Class () {}
210220
makeInstance(new Class(), Class)

0 commit comments

Comments
 (0)