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

fix(props): handle misspelled keys on prop validation object #7198

Merged
merged 5 commits into from
Dec 14, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/core/util/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import config from '../config'
import { warn } from './debug'
import { nativeWatch } from './env'
import { set } from '../observer/index'
import { assertPropObject } from './props'

import {
ASSET_TYPES,
Expand Down Expand Up @@ -282,6 +283,9 @@ function normalizeProps (options: Object, vm: ?Component) {
for (const key in props) {
val = props[key]
name = camelize(key)
if (process.env.NODE_ENV !== 'production' && isPlainObject(val)) {
assertPropObject(name, val, vm)
}
res[name] = isPlainObject(val)
? val
: { type: val }
Expand Down
25 changes: 25 additions & 0 deletions src/core/util/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ type PropOptions = {
validator: ?Function
};

const propOptionsNames = [
'type',
'default',
'required',
'validator'
]

export function validateProp (
key: string,
propOptions: Object,
Expand Down Expand Up @@ -84,6 +91,24 @@ function getPropDefaultValue (vm: ?Component, prop: PropOptions, key: string): a
: def
}

/**
* Assert whether a prop object keys are valid.
*/
export function assertPropObject (
propName: string,
prop: Object,
vm: ?Component
) {
for (const key in prop) {
if (propOptionsNames.indexOf(key) === -1) {
warn(
`Invalid key "${key}" in validation rules object for prop "${propName}".`,
vm
)
}
}
}

/**
* Assert whether a prop is valid.
*/
Expand Down
17 changes: 17 additions & 0 deletions test/unit/features/options/props.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,21 @@ describe('Options props', () => {
expect(`"${attr}" is a reserved attribute`).toHaveBeenWarned()
})
})

it('should warn about misspelled keys in prop validation object', () => {
new Vue({
template: '<test></test>',
components: {
test: {
template: '<div></div>',
props: {
value: { reqquired: true },
count: { deafult: 1 }
}
}
}
}).$mount()
expect(`Invalid key "reqquired" in validation rules object for prop "value".`).toHaveBeenWarned()
expect(`Invalid key "deafult" in validation rules object for prop "count".`).toHaveBeenWarned()
})
})