forked from vuejs/create-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
48 lines (47 loc) · 1.19 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
*
* @param obj object that needs to be validated
* @param schema template for validation
* @returns whether missed some keys
*/
export function includeAllKeys(obj: Object, schema: Object) {
for (let key in schema) {
if (!obj.hasOwnProperty(key)) {
console.log(`key '${key}' lost`)
return false
}
if (schema[key] !== null) {
if (typeof schema[key] === 'string') {
if (typeof obj[key] !== schema[key]) {
console.error(`the type of ${key} is incorrect`)
return false
}
} else if (typeof schema[key] === 'object') {
if (!includeAllKeys(obj[key], schema[key])) {
return false
}
}
}
}
return true
}
/**
*
* @param obj object that needs to be validated
* @param schema template for validation
* @returns whether include extra keys
*/
export function excludeKeys(obj: Object, schema: Object) {
for (let key in obj) {
if (!schema.hasOwnProperty(key)) {
console.error(`unexpected key: ${key}`)
return false
}
if (schema[key] !== null && typeof schema[key] === 'object') {
if (!excludeKeys(obj[key], schema[key])) {
return false
}
}
}
return true
}