Permalink
Browse files

validation method

  • Loading branch information...
1 parent 524edc0 commit 7a06b0b3fbe20dfdbbd4d06441011be25d017cc3 @joeferner committed Aug 29, 2012
Showing with 65 additions and 33 deletions.
  1. +45 −32 lib/connection.js
  2. +20 −1 test/insert.js
View
@@ -57,46 +57,59 @@ var Connection = Class.extend({
_save: function (isNew, obj, callback) {
var self = this;
- if (obj._getModel().onSave) {
- obj._getModel().onSave(obj);
- }
- obj._getModel().emit("beforeSave", obj);
-
- if (isNew) {
- obj._getModel().emit("beforeCreate", obj);
+ if (obj._getModel().validate) {
+ obj._getModel().validate(obj, function (success, message) {
+ if (success) {
+ return doSave();
+ }
+ return callback(new Error("Validation failed: " + message));
+ });
} else {
- obj._getModel().emit("beforeUpdate", obj);
+ return doSave();
}
- var sqlAndValues;
- if (isNew) {
- sqlAndValues = this.driver.getInsertSql(obj);
- } else {
- sqlAndValues = this.driver.getUpdateSql(obj);
- }
- this._runSql(sqlAndValues.sql, sqlAndValues.values, function (err, data) {
- if (err) {
- callback(err);
- return;
+ function doSave() {
+ if (obj._getModel().onSave) {
+ obj._getModel().onSave(obj);
}
- if (isNew && data.lastId) {
- var idPropName = obj._getModel().getIdPropertyName();
- obj[idPropName] = data.lastId;
+ obj._getModel().emit("beforeSave", obj);
+
+ if (isNew) {
+ obj._getModel().emit("beforeCreate", obj);
+ } else {
+ obj._getModel().emit("beforeUpdate", obj);
}
- obj._getConnection = function () { return self; }; // hide from JSON.stringify
- obj._isPersisted = function () { return true; };
- self.saveAssociations(obj, function (err, obj) {
- if (isNew) {
- obj._getModel().emit("afterCreate", obj);
- obj._getModel().emit("afterSave", obj);
- } else {
- obj._getModel().emit("afterUpdate", obj);
- obj._getModel().emit("afterSave", obj);
+
+ var sqlAndValues;
+ if (isNew) {
+ sqlAndValues = self.driver.getInsertSql(obj);
+ } else {
+ sqlAndValues = self.driver.getUpdateSql(obj);
+ }
+ self._runSql(sqlAndValues.sql, sqlAndValues.values, function (err, data) {
+ if (err) {
+ callback(err);
+ return;
}
+ if (isNew && data.lastId) {
+ var idPropName = obj._getModel().getIdPropertyName();
+ obj[idPropName] = data.lastId;
+ }
+ obj._getConnection = function () { return self; }; // hide from JSON.stringify
+ obj._isPersisted = function () { return true; };
+ self.saveAssociations(obj, function (err, obj) {
+ if (isNew) {
+ obj._getModel().emit("afterCreate", obj);
+ obj._getModel().emit("afterSave", obj);
+ } else {
+ obj._getModel().emit("afterUpdate", obj);
+ obj._getModel().emit("afterSave", obj);
+ }
- callback.apply(self, arguments);
+ callback.apply(self, arguments);
+ });
});
- });
+ }
},
updatePartial: function (model, id, data, callback) {
View
@@ -31,10 +31,19 @@ exports['Insert'] = nodeunit.testCase({
obj.lastUpdated = self.testDate2;
})
.on('afterSave', function (obj) {
- if (!obj.updateCount) obj.updateCount = 0;
+ if (!obj.updateCount) {
+ obj.updateCount = 0;
+ }
obj.updateCount++;
});
+ this.Person.validate = function (obj, callback) {
+ if (obj.name === 'bad name') {
+ return callback(false, 'You had a bad name');
+ }
+ return callback(true);
+ };
+
testUtils.connect(persist, function (err, connection) {
self.connection = connection;
callback();
@@ -127,6 +136,16 @@ exports['Insert'] = nodeunit.testCase({
test.equals(p.age, 0);
test.done();
});
+ },
+
+ "validation": function (test) {
+ var self = this;
+ var person1 = new this.Person({ name: "bad name", age: 0 });
+
+ person1.save(self.connection, function (err, p) {
+ test.equals('Validation failed: You had a bad name', err.message);
+ test.done();
+ });
}
});

0 comments on commit 7a06b0b

Please sign in to comment.