Skip to content

Instantly share code, notes, and snippets.

@BahiHussein
Created June 30, 2016 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BahiHussein/4d979f22002b1ea4fd001d432d849949 to your computer and use it in GitHub Desktop.
Save BahiHussein/4d979f22002b1ea4fd001d432d849949 to your computer and use it in GitHub Desktop.
var Say = require('../config/say.js');
module.exports = {
init: function(req){
req.errors = [];
},
required: function(req, array){
//checkRequired
for(i = 0; i < array.length; i++){
if(!(array[i] in req.body)){
req.errors.push({error: Say.error.missingParams + " > " + array[i]});
}
}
//returnFalse
if(req.errors.length > 0){
return false;
} else {
return true;
}
},
regex: function(req, array){
for(i = 0; i < array.length; i++){
if(array[i].param in req.body){
var pattern = new RegExp(array[i].regex);
if(pattern.test(req.body[array[i].param]) == false){
req.errors.push({error: Say.error.regex + " > " + array[i].param + " > " +req.body[array[i].param]});
//req.errors.push({error: Say.error.regex + " > " + array[i].param + " > " + array[i].regex + " > " +req.body[array[i].param]});
}
}
}
},
length: function(req, array){
for(i = 0; i < array.length; i++){
//checkLengthIfObjectExists
//{param:'x', min:'',max:''}
if(array[i].param in req.body){
if(!(req.body[array[i].param].length >= array[i].min) || !(req.body[array[i].param].length <= array[i].max)){
req.errors.push({error: Say.error.wrongFormat + "(size) > " + array[i].param});
}
}
}
},
isArray: function(req, array){
for(i = 0; i < array.length; i++){
if(!_.isArray(array[i])){
if(!(array[i] in req.body)){
req.errors.push({error: + Say.error.wrongFormat + " > " + array[i]});
}
}
}
},
isDate: function(req, array){
for(i = 0; i < array.length; i++){
if(array[i] in req.body){
if(isNaN(Date.parse(req.body[array[i]])) == true){
req.errors.push({error: + Say.error.wrongFormat});
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment