Skip to content

Commit

Permalink
Fix $pullAll and clean $pushAll by converting it to $push+$each rathe…
Browse files Browse the repository at this point in the history
…r than deleting it; fixes #9
  • Loading branch information
aldeed committed Oct 29, 2013
1 parent fd88f65 commit fe7950a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ A simple, reactive schema validation smart package for Meteor.

## Change Log

### 0.2.13

Fix $pullAll and clean $pushAll by converting it to $push+$each rather than
deleting it.

### 0.2.12

Add workaround for Safari bug.
Expand Down
28 changes: 22 additions & 6 deletions simple-schema-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2438,13 +2438,29 @@ Tinytest.add("SimpleSchema - Cleanup With Modifier Operators", function(test) {
//type conversion works
doTest({$pop: {allowedNumbersArray: "1"}}, {$pop: {allowedNumbersArray: 1}});

//DEPRECATED OPERATORS SHOULD BE REMOVED

//$PUSHALL
doTest({$pushAll: {allowedNumbersArray: [1, 2, 3]}}, {});

//$PULLALL
doTest({$pullAll: {allowedNumbersArray: [1, 2, 3]}}, {});

doTest({$pullAll: {allowedNumbersArray: [1, 2, 3]}}, {$pullAll: {allowedNumbersArray: [1, 2, 3]}});
doTest({$pullAll: {allowedNumbersArray: ["1", 2, 3]}}, {$pullAll: {allowedNumbersArray: [1, 2, 3]}});

//$PUSHALL (DEPRECATED - SHOULD BE TRANSLATED TO $PUSH+$EACH

doTest({$pushAll: {allowedNumbersArray: [1, 2, 3]}}, {$push: {allowedNumbersArray: {$each: [1, 2, 3]}}});
doTest({$pushAll: {allowedNumbersArray: ["1", 2, 3]}}, {$push: {allowedNumbersArray: {$each: [1, 2, 3]}}});
//if there's also $push for some reason, the two should be combined
doTest({
$push: {
allowedNumbersArray: {$each: ["1", 2, 3]},
allowedStringsArray: {$each: ["tuna", "fish"]}
},
$pushAll: {allowedNumbersArray: ["4", 5, 6]}
}, {
$push: {
allowedNumbersArray: {$each: [1, 2, 3, 4, 5, 6]},
allowedStringsArray: {$each: ["tuna", "fish"]}
}
});

});

Tinytest.add("SimpleSchema - Custom Types", function(test) {
Expand Down
19 changes: 12 additions & 7 deletions simple-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,27 @@ SimpleSchema.prototype.validator = function(func) {
this._validators.push(func);
};

//filter and automatically type convert
// Filter and automatically type convert
SimpleSchema.prototype.clean = function(doc, options) {
var newDoc, self = this;

//by default, doc will be filtered and autoconverted
// By default, doc will be filtered and autoconverted
options = _.extend({
filter: true,
autoConvert: true
}, options || {});

//delete deprecated operators
// Convert $pushAll (deprecated) to $push with $each
if ("$pushAll" in doc) {
delete doc.$pushAll;
}
if ("$pullAll" in doc) {
delete doc.$pullAll;
doc.$push = doc.$push || {};
for (var field in doc.$pushAll) {
doc.$push[field] = doc.$push[field] || {};
doc.$push[field].$each = doc.$push[field].$each || [];
for (var i = 0, ln = doc.$pushAll[field].length; i < ln; i++) {
doc.$push[field].$each.push(doc.$pushAll[field][i]);
}
delete doc.$pushAll;
}
}

var mDoc = new MongoObject(doc);
Expand Down
2 changes: 1 addition & 1 deletion smart.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A simple schema validation object with reactivity. Used by collection2 and autoform.",
"homepage": "https://github.com/aldeed/meteor-simple-schema",
"author": "Eric Dobbertin (http://dairystatedesigns.com)",
"version": "0.2.12",
"version": "0.2.13",
"git": "https://github.com/aldeed/meteor-simple-schema.git",
"packages": {}
}

0 comments on commit fe7950a

Please sign in to comment.