Skip to content

Commit

Permalink
refactor: move prop rule validation to util/options.js
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 14, 2017
1 parent d02bb37 commit 90ed482
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
23 changes: 21 additions & 2 deletions src/core/util/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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 @@ -295,7 +294,7 @@ function normalizeProps (options: Object, vm: ?Component) {
val = props[key]
name = camelize(key)
if (process.env.NODE_ENV !== 'production' && isPlainObject(val)) {
assertPropObject(name, val, vm)
validatePropObject(name, val, vm)
}
res[name] = isPlainObject(val)
? val
Expand All @@ -311,6 +310,26 @@ function normalizeProps (options: Object, vm: ?Component) {
options.props = res
}

/**
* Validate whether a prop object keys are valid.
*/
const propOptionsRE = /^(type|default|required|validator)$/

function validatePropObject (
propName: string,
prop: Object,
vm: ?Component
) {
for (const key in prop) {
if (!propOptionsRE.test(key)) {
warn(
`Invalid key "${key}" in validation rules object for prop "${propName}".`,
vm
)
}
}
}

/**
* Normalize all injections into Object-based format
*/
Expand Down
25 changes: 0 additions & 25 deletions src/core/util/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ type PropOptions = {
validator: ?Function
};

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

export function validateProp (
key: string,
propOptions: Object,
Expand Down Expand Up @@ -91,24 +84,6 @@ 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

0 comments on commit 90ed482

Please sign in to comment.