Skip to content

Commit 90ed482

Browse files
committed
refactor: move prop rule validation to util/options.js
1 parent d02bb37 commit 90ed482

File tree

2 files changed

+21
-27
lines changed

2 files changed

+21
-27
lines changed

src/core/util/options.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ 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'
87

98
import {
109
ASSET_TYPES,
@@ -295,7 +294,7 @@ function normalizeProps (options: Object, vm: ?Component) {
295294
val = props[key]
296295
name = camelize(key)
297296
if (process.env.NODE_ENV !== 'production' && isPlainObject(val)) {
298-
assertPropObject(name, val, vm)
297+
validatePropObject(name, val, vm)
299298
}
300299
res[name] = isPlainObject(val)
301300
? val
@@ -311,6 +310,26 @@ function normalizeProps (options: Object, vm: ?Component) {
311310
options.props = res
312311
}
313312

313+
/**
314+
* Validate whether a prop object keys are valid.
315+
*/
316+
const propOptionsRE = /^(type|default|required|validator)$/
317+
318+
function validatePropObject (
319+
propName: string,
320+
prop: Object,
321+
vm: ?Component
322+
) {
323+
for (const key in prop) {
324+
if (!propOptionsRE.test(key)) {
325+
warn(
326+
`Invalid key "${key}" in validation rules object for prop "${propName}".`,
327+
vm
328+
)
329+
}
330+
}
331+
}
332+
314333
/**
315334
* Normalize all injections into Object-based format
316335
*/

src/core/util/props.js

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

21-
const propOptionsNames = [
22-
'type',
23-
'default',
24-
'required',
25-
'validator'
26-
]
27-
2821
export function validateProp (
2922
key: string,
3023
propOptions: Object,
@@ -91,24 +84,6 @@ function getPropDefaultValue (vm: ?Component, prop: PropOptions, key: string): a
9184
: def
9285
}
9386

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-
11287
/**
11388
* Assert whether a prop is valid.
11489
*/

0 commit comments

Comments
 (0)