Skip to content

Commit 099a4f3

Browse files
committed
logic fix
1 parent 75cc632 commit 099a4f3

File tree

1 file changed

+23
-33
lines changed

1 file changed

+23
-33
lines changed

lib/validator.js

+23-33
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,17 @@ class Validator {
140140
* @param {any} value
141141
*/
142142
clone(value) {
143-
if (value == null || value.constructor !== Object) {
144-
// dont need to clone
145-
return value;
146-
}
147-
148-
if (Array.isArray(value)) {
149-
return value.map(val => this.clone(val));
150-
} else {
151-
return Object.keys(value).reduce((obj, key) => {
152-
obj[key] = this.clone(value[key]);
153-
return obj;
154-
}, {});
143+
if (value != null) {
144+
if (Array.isArray(value)) {
145+
return value.map(val => this.clone(val));
146+
} else if (value.constructor === Object) {
147+
return Object.keys(value).reduce((obj, key) => {
148+
obj[key] = this.clone(value[key]);
149+
return obj;
150+
}, {});
151+
}
155152
}
153+
return value;
156154
}
157155

158156
/**
@@ -162,25 +160,17 @@ class Validator {
162160
* @param {any} vals - keep values that will merge to ref
163161
*/
164162
removeChildrenAndMerge(target, vals) {
165-
if (target == null || target.constructor !== Object) {
166-
// dont need to change
167-
return;
168-
}
169-
170-
if (vals == null || vals.constructor !== Object) {
171-
// unexpected, dont change
172-
return;
173-
}
174-
175-
if (Array.isArray(target) && Array.isArray(vals)) {
176-
target.splice(0, target.length, ...vals);
177-
} else {
178-
// clear children
179-
Object.keys(target).forEach(key => {
180-
delete target[key];
181-
});
182-
// merge
183-
Object.assign(target, vals);
163+
if (target != null && vals != null) {
164+
if (Array.isArray(target) && Array.isArray(vals)) {
165+
target.splice(0, target.length, ...vals);
166+
} else if (target.constructor === Object && vals.constructor === Object) {
167+
// clear children
168+
Object.keys(target).forEach(key => {
169+
delete target[key];
170+
});
171+
// merge
172+
Object.assign(target, vals);
173+
}
184174
}
185175
}
186176

@@ -375,8 +365,8 @@ class Validator {
375365

376366
/**
377367
* Parse rule from shorthand string
378-
* @param {String} str shorthand string
379-
* @param {Object} schema schema reference
368+
* @param {String} str - shorthand string
369+
* @return {Object} - schema reference
380370
*/
381371

382372
parseShortHand(str) {

0 commit comments

Comments
 (0)