Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: Never return original object from individual config
  • Loading branch information
nzakas committed Nov 2, 2021
1 parent fd74806 commit 5463c5c
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/object-schema.js
Expand Up @@ -104,12 +104,8 @@ class ObjectSchema {
const schema = new ObjectSchema(definitions[key].schema);
definitions[key] = {
...definitions[key],
merge(first, second) {
if (first && second) {
return schema.merge(first, second);
}

return MergeStrategy.assign(first, second);
merge(first = {}, second = {}) {
return schema.merge(first, second);
},
validate(value) {
ValidationStrategy.object(value);
Expand Down
83 changes: 83 additions & 0 deletions tests/object-schema.js
Expand Up @@ -266,6 +266,53 @@ describe("ObjectSchema", () => {
assert.strictEqual(result.name.last, "z");
});

it("should return separate objects when using subschema", () => {

schema = new ObjectSchema({
age: {
merge: "replace",
validate: "number"
},
address: {
schema: {
street: {
schema: {
number: {
merge: "replace",
validate: "number"
},
streetName: {
merge: "replace",
validate: "string"
}
}
},
state: {
merge: "replace",
validate: "string"
}
}
}
});

const baseObject = {
address: {
street: {
number: 100,
streetName: "Foo St"
},
state: "HA"
}
};

const result = schema.merge(baseObject, {
age: 29
});

assert.notStrictEqual(result.address.street, baseObject.address.street);
assert.deepStrictEqual(result.address, baseObject.address);
});

it("should not error when calling the merge strategy when there's a subschema and no matching key in second object", () => {

schema = new ObjectSchema({
Expand Down Expand Up @@ -295,6 +342,42 @@ describe("ObjectSchema", () => {
assert.strictEqual(result.name.last, "z");
});

it("should not error when calling the merge strategy when there's multiple subschemas and no matching key in second object", () => {

schema = new ObjectSchema({
user: {
schema: {
name: {
schema: {
first: {
merge: "replace",
validate: "string"
},
last: {
merge: "replace",
validate: "string"
}
}
}

}
}
});

const result = schema.merge({
user: {
name: {
first: "n",
last: "z"
}
}
}, {
});

assert.strictEqual(result.user.name.first, "n");
assert.strictEqual(result.user.name.last, "z");
});


});

Expand Down

0 comments on commit 5463c5c

Please sign in to comment.