Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn if coerce is not a function #2929

Merged
merged 1 commit into from
May 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/compiler/compile-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ function processPropValue (vm, prop, rawValue, fn) {
if (value === undefined) {
value = getPropDefaultValue(vm, prop)
}
value = coerceProp(prop, value)
value = coerceProp(prop, value, vm)
const coerced = value !== rawValue
if (!assertProp(prop, value, vm)) {
value = undefined
Expand Down Expand Up @@ -374,13 +374,20 @@ function assertProp (prop, value, vm) {
* @return {*}
*/

function coerceProp (prop, value) {
function coerceProp (prop, value, vm) {
var coerce = prop.options.coerce
if (!coerce) {
return value
}
// coerce is a function
return coerce(value)
if (typeof coerce === 'function') {
return coerce(value)
} else {
process.env.NODE_ENV !== 'production' && warn(
'Invalid coerce for prop "' + prop.name + '": expected function, got ' + typeof coerce + '.',
vm
)
return value
}
}

/**
Expand Down
6 changes: 6 additions & 0 deletions test/unit/specs/directives/internal/prop_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,12 @@ describe('prop', function () {
expect(getWarnCount()).toBe(0)
})

it('warn if coerce is not a function', function () {
var coerce = 1
makeInstance('123', String, null, coerce)
expect(getWarnCount()).toBe(1)
})

it('multiple types + custom validator', function () {
makeInstance(123, [String, Boolean, Number], null, String)
expect(getWarnCount()).toBe(0)
Expand Down