From 8e7c7d53848e1c5b2ea388523afbcfecda189ba6 Mon Sep 17 00:00:00 2001 From: Alexander Sokolov Date: Thu, 14 Dec 2017 19:39:32 +0300 Subject: [PATCH] feat: warn misspelled keys on prop validation object (#7198) --- src/core/util/options.js | 4 ++++ src/core/util/props.js | 25 ++++++++++++++++++++++++ test/unit/features/options/props.spec.js | 17 ++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/src/core/util/options.js b/src/core/util/options.js index c5c7fb25954..7a36ee168cb 100644 --- a/src/core/util/options.js +++ b/src/core/util/options.js @@ -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, @@ -293,6 +294,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 } diff --git a/src/core/util/props.js b/src/core/util/props.js index 310514d7df6..dcc2358e46d 100644 --- a/src/core/util/props.js +++ b/src/core/util/props.js @@ -18,6 +18,13 @@ type PropOptions = { validator: ?Function }; +const propOptionsNames = [ + 'type', + 'default', + 'required', + 'validator' +] + export function validateProp ( key: string, propOptions: Object, @@ -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. */ diff --git a/test/unit/features/options/props.spec.js b/test/unit/features/options/props.spec.js index ae2baefff99..42acbe6b88b 100644 --- a/test/unit/features/options/props.spec.js +++ b/test/unit/features/options/props.spec.js @@ -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: '', + components: { + test: { + template: '
', + 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() + }) })