Skip to content

Commit

Permalink
don't $unset when the prop is within an object that is already being …
Browse files Browse the repository at this point in the history
…$set; closes #301
  • Loading branch information
aldeed committed Mar 28, 2015
1 parent 0ff97be commit a6e0dc8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
18 changes: 18 additions & 0 deletions simple-schema-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,19 @@ var reqCust = new SimpleSchema({
}
});

var optionalInObject = new SimpleSchema({
requiredObj: {
type: Object
},
'requiredObj.optionalProp': {
type: String,
optional: true
},
'requiredObj.requiredProp': {
type: String
}
});

/*
* END SETUP FOR TESTS
*/
Expand Down Expand Up @@ -3046,6 +3059,11 @@ Tinytest.add("SimpleSchema - Clean", function(test) {
true
);

// Don't $unset when the prop is within an object that is already being $set
myObj = {$set: {requiredObj: {requiredProp: 'blah', optionalProp: '' } }};
optionalInObject.clean(myObj, {isModifier: true});
test.equal(myObj, {$set: {requiredObj: {requiredProp: 'blah'} }});

});

Tinytest.add("SimpleSchema - Clean - trimStrings", function(test) {
Expand Down
7 changes: 4 additions & 3 deletions simple-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,8 @@ SimpleSchema.prototype.clean = function(doc, options) {
// For a document, we remove any fields that are being set to an empty string
newVal = void 0;
// For a modifier, we $unset any fields that are being set to an empty string
if (this.operator === "$set") {
if (this.operator === "$set" && this.position.match(/\[.+?\]/g).length < 2) {

p = this.position.replace("$set", "$unset");
mDoc.setValueForPosition(p, "");
}
Expand All @@ -761,8 +762,8 @@ SimpleSchema.prototype.clean = function(doc, options) {
if (options.removeEmptyStrings && (!this.operator || this.operator === "$set") && typeof val === "string" && !val.length) {
// For a document, we remove any fields that are being set to an empty string
this.remove();
// For a modifier, we $unset any fields that are being set to an empty string
if (this.operator === "$set") {
// For a modifier, we $unset any fields that are being set to an empty string. But only if we're not already within an entire object that is being set.
if (this.operator === "$set" && this.position.match(/\[.+?\]/g).length < 2) {
p = this.position.replace("$set", "$unset");
mDoc.setValueForPosition(p, "");
}
Expand Down

0 comments on commit a6e0dc8

Please sign in to comment.