promise-to-validate
Advanced tools
Comparing version 0.1.0 to 0.2.0
module.exports = { | ||
Validate:require("./validate"), | ||
Chain:require("./chain") | ||
Validate: require("./validate"), | ||
Check: require("./check") | ||
}; |
{ | ||
"name": "promise-to-validate", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "A simple promise based validation utility that wraps the functionality of node-validator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
130
validate.js
@@ -1,80 +0,74 @@ | ||
Check= require('validator').check; | ||
Q = require('q'); | ||
_ = require('lodash'); | ||
var Check = require('validator').check, | ||
Q = require('q'), | ||
_ = require('lodash'); | ||
Validate = function(input,field){ | ||
this.promise = Q([]); | ||
if(input&&field) | ||
this.set(input,field); | ||
var Validate = function(field, input) { | ||
this.promise = Q([]); | ||
this.set(input, field); | ||
}; | ||
for(var v in new Check()){ | ||
(function(){ | ||
var functionName = v; | ||
Validate.prototype[functionName] = function(){ | ||
var args = arguments; | ||
var that = this; | ||
this.promise = this.promise.then(function(errors) { | ||
try{ | ||
var c = Check(that.value); | ||
c[functionName].apply(c,args); | ||
} | ||
catch(ex){ | ||
errors.push(ex.message); | ||
} | ||
return Q(errors); | ||
}); | ||
return this; | ||
}; | ||
})();// jshint ignore:line | ||
} | ||
Validate.prototype.then = function () { | ||
if (!(this.input && this.field)) { | ||
var reject = Q.reject("Both the field and input are required inorder to validate"); | ||
return reject.then.apply(reject, arguments); | ||
} | ||
return this.promise.then.apply(this.promise, arguments); | ||
}; | ||
function then(){ | ||
if(!(this.input&&this.field)) | ||
{ | ||
var reject = Q.reject("Promise not working as expected"); | ||
return reject.then.apply(reject,arguments); | ||
} | ||
return this.promise.then.apply(this.promise,arguments); | ||
} | ||
Validate.prototype.then = then; | ||
Validate.prototype.set = function set(input,field) { | ||
this.input = input || this.input || {}; | ||
this.field = field || this.field; | ||
return this; | ||
}; | ||
Validate.prototype.getValue = function(){ | ||
return this.input[this.field]; | ||
}; | ||
function set(input,field){ | ||
this.input = input; | ||
this.field = field; | ||
this.value = input[field]; | ||
return this; | ||
} | ||
Validate.prototype.set = set; | ||
Validate.prototype.custom = function(customerValidate, scope) { | ||
var that = this; | ||
function wrapper(errors) { | ||
var promise; | ||
if (_.isFunction(customerValidate)) { | ||
customerValidate = scope ? _.bind(customerValidate, scope) : customerValidate; | ||
promise = Q(customerValidate(that.getValue())); | ||
} else { | ||
promise = customerValidate; | ||
} | ||
return promise.then(function(e) { | ||
if (_.isArray(e)) { | ||
errors = errors.concat(e); | ||
} else if (e) { | ||
errors.push(e); | ||
} | ||
return Q(errors); | ||
}); | ||
} | ||
this.promise = this.promise.then(wrapper); | ||
return this; | ||
}; | ||
function custom(customerValidator,scope){ | ||
var value = this.value; | ||
function wrapper(errors){ | ||
var promise; | ||
if(_.isFunction(customerValidator)){ | ||
customerValidator = scope?_.bind(customerValidator,scope):customerValidator; | ||
promise = Q(customerValidator(value)); | ||
} | ||
else{ | ||
promise = customerValidator; | ||
} | ||
return promise.then(function(e){ | ||
if(_.isArray(e)) { | ||
errors = errors.concat(e); | ||
for (var v in new Check()) { | ||
(function() { | ||
var functionName = v; | ||
Validate.prototype[functionName] = function() { | ||
var args = arguments; | ||
this.custom(function(value){ | ||
var error; | ||
try { | ||
var c = Check(value); | ||
c[functionName].apply(c, args); | ||
} catch (ex) { | ||
error = ex.message; | ||
} | ||
else if(e){ | ||
errors.push(e); | ||
} | ||
return Q(errors); | ||
return error; | ||
}); | ||
} | ||
this.promise = this.promise.then(wrapper); | ||
return this; | ||
return this; | ||
}; | ||
})(); // jshint ignore:line | ||
} | ||
Validate.prototype.custom = custom; | ||
module.exports = Validate; | ||
module.exports = function(field, input){ | ||
return new Validate(field, input); | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
10594
267
32
2