Skip to content

Commit d02bb37

Browse files
Alex-Sokolovyyx990803
authored andcommitted
feat: warn misspelled keys on prop validation object (#7198)
1 parent 14f7015 commit d02bb37

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/core/util/options.js

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import config from '../config'
44
import { warn } from './debug'
55
import { nativeWatch } from './env'
66
import { set } from '../observer/index'
7+
import { assertPropObject } from './props'
78

89
import {
910
ASSET_TYPES,
@@ -293,6 +294,9 @@ function normalizeProps (options: Object, vm: ?Component) {
293294
for (const key in props) {
294295
val = props[key]
295296
name = camelize(key)
297+
if (process.env.NODE_ENV !== 'production' && isPlainObject(val)) {
298+
assertPropObject(name, val, vm)
299+
}
296300
res[name] = isPlainObject(val)
297301
? val
298302
: { type: val }

src/core/util/props.js

+25
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ type PropOptions = {
1818
validator: ?Function
1919
};
2020

21+
const propOptionsNames = [
22+
'type',
23+
'default',
24+
'required',
25+
'validator'
26+
]
27+
2128
export function validateProp (
2229
key: string,
2330
propOptions: Object,
@@ -84,6 +91,24 @@ function getPropDefaultValue (vm: ?Component, prop: PropOptions, key: string): a
8491
: def
8592
}
8693

94+
/**
95+
* Assert whether a prop object keys are valid.
96+
*/
97+
export function assertPropObject (
98+
propName: string,
99+
prop: Object,
100+
vm: ?Component
101+
) {
102+
for (const key in prop) {
103+
if (propOptionsNames.indexOf(key) === -1) {
104+
warn(
105+
`Invalid key "${key}" in validation rules object for prop "${propName}".`,
106+
vm
107+
)
108+
}
109+
}
110+
}
111+
87112
/**
88113
* Assert whether a prop is valid.
89114
*/

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

+17
Original file line numberDiff line numberDiff line change
@@ -512,4 +512,21 @@ describe('Options props', () => {
512512
expect(`"${attr}" is a reserved attribute`).toHaveBeenWarned()
513513
})
514514
})
515+
516+
it('should warn about misspelled keys in prop validation object', () => {
517+
new Vue({
518+
template: '<test></test>',
519+
components: {
520+
test: {
521+
template: '<div></div>',
522+
props: {
523+
value: { reqquired: true },
524+
count: { deafult: 1 }
525+
}
526+
}
527+
}
528+
}).$mount()
529+
expect(`Invalid key "reqquired" in validation rules object for prop "value".`).toHaveBeenWarned()
530+
expect(`Invalid key "deafult" in validation rules object for prop "count".`).toHaveBeenWarned()
531+
})
515532
})

0 commit comments

Comments
 (0)