不要怂,就是干,撸起袖子干!

Commit 3f24f443 by James Sapara

Updated validator with latest master changes that got lost in rebase

1 parent 7ec60a7b
Showing with 14 additions and 65 deletions
...@@ -207,60 +207,10 @@ module.exports = (function() { ...@@ -207,60 +207,10 @@ module.exports = (function() {
*/ */
DAO.prototype.validate = function() { DAO.prototype.validate = function() {
var self = this 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 : details
if (!Array.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]
}
}
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
var chainer = new QueryChainer; var chainer = new QueryChainer();
// for each field and value // for each field and value
Utils._.each(self.values, function(value, field) { Utils._.each(self.values, function(value, field) {
...@@ -271,33 +221,32 @@ module.exports = (function() { ...@@ -271,33 +221,32 @@ module.exports = (function() {
Utils._.each(self.validators[field], function(details, validatorType) { Utils._.each(self.validators[field], function(details, validatorType) {
//var is_custom_fn = false // if true then it's a custom validation method //var is_custom_fn = false // if true then it's a custom validation method
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_method = null // the validation function to call
var fn_args = [] // extra arguments to pass to validation function var fn_args = [] // extra arguments to pass to validation function
var fn_msg = "" // the error message to return if validation fails var fn_msg = "" // the error message to return if validation fails
// is it a custom validator function? // is it a custom validator function?
if (Utils._.isFunction(details)) { if (Utils._.isFunction(details)) {
is_custom_fn = true;
fn_method = function (next) { fn_method = function (next) {
//console.log(field, arguments); details.apply(self, [value, next])
details.apply(self, [value, next])
} }
} }
// is it a validator module function? // is it a validator module function?
else { else {
// extra args // extra args
if (Utils._.isArray(details)) { fn_args = details.hasOwnProperty("args") ? details.args : details
fn_args = Array.prototype.slice.call(details, 0);
} else {
fn_args = details.hasOwnProperty("args") ? details.args : []
}
if (!Utils._.isArray(fn_args)) if (!Utils._.isArray(fn_args))
fn_args = [fn_args] fn_args = [fn_args]
// error msg // error msg
fn_msg = details.hasOwnProperty("msg") ? details.msg : false fn_msg = details.hasOwnProperty("msg") ? details.msg : false
// check method exists // check method exists
var v = Validator.check(value, fn_msg) var v = Validator.check(value, fn_msg)
if (!Utils._.isFunction(v[validatorType])) if (!Utils._.isFunction(v[validatorType])) {
throw new Error("Invalid validator function: " + validatorType) throw new Error("Invalid validator function: " + validatorType)
}
fn_method = function (next) { fn_method = function (next) {
var args = Array.prototype.slice.call(arguments, 1); var args = Array.prototype.slice.call(arguments, 1);
...@@ -310,14 +259,14 @@ module.exports = (function() { ...@@ -310,14 +259,14 @@ module.exports = (function() {
} }
} }
chainer.add(new Utils.CustomEventEmitter(function(emitter) { chainer.add(new Utils.CustomEventEmitter(function(_emitter) {
var next = function (err) { var next = function (err) {
if (err) { if (err) {
var error = {}; var error = {};
error[field] = err; error[field] = [err];
emitter.emit('error', error); _emitter.emit('error', error);
} else { } else {
emitter.emit('success') _emitter.emit('success')
} }
} }
fn_args.unshift(next); fn_args.unshift(next);
...@@ -334,10 +283,10 @@ module.exports = (function() { ...@@ -334,10 +283,10 @@ module.exports = (function() {
Utils._.each(err, function (value) { Utils._.each(err, function (value) {
Utils._.extend(errors, value); Utils._.extend(errors, value);
}); });
emitter.emit('error', errors); emitter.emit('success', errors);
}) })
.success(function () { .success(function () {
emitter.emit('success'); emitter.emit('success');
}) })
}).run(); }).run();
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!