Skip to content

Commit 95c5dde

Browse files
committed
should not type check null/undefined for non-required props (fix #2411)
1 parent df01719 commit 95c5dde

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/util/component.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,12 @@ export function initProp (vm, prop, value) {
104104
*/
105105

106106
export function assertProp (prop, value) {
107-
// if a prop is not provided and is not required,
108-
// skip the check.
109-
if (prop.raw === null && !prop.required) {
107+
if (
108+
!prop.options.required && ( // non-required
109+
prop.raw === null || // abscent
110+
value == null // null or undefined
111+
)
112+
) {
110113
return true
111114
}
112115
var options = prop.options

test/unit/specs/directives/internal/prop_spec.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ describe('prop', function () {
293293
})
294294

295295
describe('assertions', function () {
296-
function makeInstance (value, type, validator, coerce) {
296+
function makeInstance (value, type, validator, coerce, required) {
297297
return new Vue({
298298
el: document.createElement('div'),
299299
template: '<test :test="val"></test>',
@@ -306,7 +306,8 @@ describe('prop', function () {
306306
test: {
307307
type: type,
308308
validator: validator,
309-
coerce: coerce
309+
coerce: coerce,
310+
required: required
310311
}
311312
}
312313
}
@@ -415,6 +416,22 @@ describe('prop', function () {
415416
})
416417
expect(hasWarned('Missing required prop')).toBe(true)
417418
})
419+
420+
it('optional with type + null/undefined', function () {
421+
makeInstance(undefined, String)
422+
expect(getWarnCount()).toBe(0)
423+
makeInstance(null, String)
424+
expect(getWarnCount()).toBe(0)
425+
})
426+
427+
it('required with type + null/undefined', function () {
428+
makeInstance(undefined, String, null, null, true)
429+
expect(getWarnCount()).toBe(1)
430+
expect(hasWarned('Expected String')).toBe(true)
431+
makeInstance(null, Boolean, null, null, true)
432+
expect(getWarnCount()).toBe(2)
433+
expect(hasWarned('Expected Boolean')).toBe(true)
434+
})
418435
})
419436

420437
it('alternative syntax', function () {

0 commit comments

Comments
 (0)