Permalink
Browse files

validation redone

  • Loading branch information...
aguidrevitch committed Sep 28, 2012
1 parent c5c1ea6 commit 8a60ccdfa11966eeb6531a26d4e43b4f194bcd5a
Showing with 87 additions and 60 deletions.
  1. +87 −60 lib/dao.js
View
@@ -1,7 +1,8 @@
-var Utils = require("./utils")
- , Mixin = require("./associations/mixin")
- , DataTypes = require("./data-types")
- , Validator = require("validator")
+var Utils = require("./utils")
+ , Mixin = require("./associations/mixin")
+ , DataTypes = require("./data-types")
+ , QueryChainer = require("./query-chainer")
+ , Validator = require("validator")
module.exports = (function() {
var DAO = function(values, options) {
@@ -131,65 +132,91 @@ module.exports = (function() {
* @return null if and only if validation successful; otherwise an object containing { field name : [error msgs] } entries.
*/
DAO.prototype.validate = function() {
- var self = this
- var failures = {}
-
- // for each field and value
- Utils._.each(self.values, function(value, field) {
-
- // if field has validators
- if (self.validators.hasOwnProperty(field)) {
- // for each validator
- Utils._.each(self.validators[field], function(details, validatorType) {
-
- var is_custom_fn = false // if true then it's a custom validation method
- var fn_method = null // the validation function to call
- var fn_args = [] // extra arguments to pass to validation function
- var fn_msg = "" // the error message to return if validation fails
-
- // is it a custom validator function?
- if (Utils._.isFunction(details)) {
- is_custom_fn = true
- fn_method = Utils._.bind(details, self, value)
- }
- // is it a validator module function?
- else {
- // extra args
- fn_args = details.hasOwnProperty("args") ? details.args : []
- if (!Utils._.isArray(fn_args))
- fn_args = [fn_args]
- // error msg
- fn_msg = details.hasOwnProperty("msg") ? details.msg : false
- // check method exists
- var v = Validator.check(value, fn_msg)
- if (!Utils._.isFunction(v[validatorType]))
- throw new Error("Invalid validator function: " + validatorType)
- // bind to validator obj
- fn_method = Utils._.bind(v[validatorType], v)
- }
-
- try {
- fn_method.apply(null, fn_args)
- } catch (err) {
- err = err.message
- // if we didn't provide a custom error message then augment the default one returned by the validator
- if (!fn_msg && !is_custom_fn)
- err += ": " + field
- // each field can have multiple validation failures stored against it
- if (failures.hasOwnProperty(field)) {
- failures[field].push(err)
- } else {
- failures[field] = [err]
- }
- }
+ var self = this;
- }) // for each validator for this field
- } // if field has validator set
- }) // for each field
+ return new Utils.CustomEventEmitter(function(emitter) {
- return (Utils._.isEmpty(failures) ? null : failures)
- }
+ var chainer = new QueryChainer;
+
+ // for each field and value
+ Utils._.each(self.values, function(value, field) {
+
+ // if field has validators
+ if (self.validators.hasOwnProperty(field)) {
+ // for each validator
+ Utils._.each(self.validators[field], function(details, validatorType) {
+ //var is_custom_fn = false // if true then it's a custom validation method
+ var fn_method = null // the validation function to call
+ var fn_args = [] // extra arguments to pass to validation function
+ var fn_msg = "" // the error message to return if validation fails
+
+ // is it a custom validator function?
+ if (Utils._.isFunction(details)) {
+ fn_method = function (next) {
+ //console.log(field, arguments);
+ details.apply(self, [value, next])
+ }
+ }
+ // is it a validator module function?
+ else {
+ // extra args
+ if (Utils._.isArray(details)) {
+ fn_args = Array.prototype.slice.call(details, 0);
+ } else {
+ fn_args = details.hasOwnProperty("args") ? details.args : []
+ }
+ if (!Utils._.isArray(fn_args))
+ fn_args = [fn_args]
+ // error msg
+ fn_msg = details.hasOwnProperty("msg") ? details.msg : false
+ // check method exists
+ var v = Validator.check(value, fn_msg)
+ if (!Utils._.isFunction(v[validatorType]))
+ throw new Error("Invalid validator function: " + validatorType)
+
+ fn_method = function (next) {
+ var args = Array.prototype.slice.call(arguments, 1);
+ try {
+ v[validatorType].apply(v, args);
+ next();
+ } catch (err) {
+ next(err.message);
+ }
+ }
+ }
+
+ chainer.add(new Utils.CustomEventEmitter(function(emitter) {
+ var next = function (err) {
+ if (err) {
+ var error = {};
+ error[field] = err;
+ emitter.emit('error', error);
+ } else {
+ emitter.emit('success')
+ }
+ }
+ fn_args.unshift(next);
+ fn_method.apply(self, fn_args)
+ }).run());
+
+ }) // for each validator for this field
+ } // if field has validator set
+ }) // for each field
+
+ chainer.run()
+ .error(function (err) {
+ var errors = {};
+ Utils._.each(err, function (value) {
+ Utils._.extend(errors, value);
+ });
+ emitter.emit('error', errors);
+ })
+ .success(function () {
+ emitter.emit('success');
+ })
+ }).run();
+ }
DAO.prototype.updateAttributes = function(updates) {
this.setAttributes(updates)

0 comments on commit 8a60ccd

Please sign in to comment.