Skip to content
This repository was archived by the owner on Jun 10, 2024. It is now read-only.

Commit 1344f7c

Browse files
committed
Fix: Don't call merge() when key doesn't exist
1 parent 8ff3ad4 commit 1344f7c

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

‎src/object-schema.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,16 @@ class ObjectSchema {
112112
}
113113

114114
return objects.reduce((result, object) => {
115+
115116
this.validate(object);
117+
116118
for (const [key, strategy] of this[strategies]) {
117119
try {
118-
const value = strategy.merge.call(this, result[key], object[key]);
119-
if (value !== undefined) {
120-
result[key] = value;
120+
if (key in result || key in object) {
121+
const value = strategy.merge.call(this, result[key], object[key]);
122+
if (value !== undefined) {
123+
result[key] = value;
124+
}
121125
}
122126
} catch (ex) {
123127
ex.message = `Key "${key}": ` + ex.message;

‎tests/object-schema.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,23 @@ describe("ObjectSchema", () => {
102102
assert.propertyVal(result, "foo", "bar");
103103
});
104104

105+
it("should not call the merge() strategy when both objects don't contain the key", () => {
106+
107+
let called = false;
108+
109+
schema = new ObjectSchema({
110+
foo: {
111+
merge() {
112+
called = true;
113+
},
114+
validate() {}
115+
}
116+
});
117+
118+
const result = schema.merge({}, {});
119+
assert.isFalse(called, "The merge() strategy should not have been called.");
120+
});
121+
105122
it("should omit returning the key when the merge() strategy returns undefined", () => {
106123
schema = new ObjectSchema({
107124
foo: {

0 commit comments

Comments
 (0)