Skip to content

Instantly share code, notes, and snippets.

@jalev
Last active September 23, 2016 22:46
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 jalev/ec9c491fb44a525cec5dd04844ec1283 to your computer and use it in GitHub Desktop.
Save jalev/ec9c491fb44a525cec5dd04844ec1283 to your computer and use it in GitHub Desktop.
validating some datas
var validator = require('validator');
var validation = function(data, next){
if (! validator.isLength(`${data['startDate']}`, {min: 1})){
var msg = '"startDate" must not be empty';
console.log(msg);
return next(new Error(msg));
}
if (! validator.isLength(`${data['endDate']}`, {min: 1})){
var msg = '"endDate" must not be empty';
console.log(msg);
return next(new Error(msg));
}
if (! validator.isLength(`${data['reason']}`, {min: 1})){
var msg = '"reason" must not be empty';
console.log(msg);
return next(new Error(msg));
}
if (! validator.isNumeric(`${data['startDate']}`)){
var msg = '"startDate" is not a valid integer';
console.log(msg);
return next(new Error(msg));
}
if (! validator.isNumeric(`${data['endDate']}`)){
var msg = '"endDate" is not a valid integer';
console.log(msg);
return next(new Error(msg));
}
if (data['endDate'] <= data['startDate']){
var msg = '"endDate" cannot be smaller than "startDate"';
console.log(msg);
return next(new Error(msg));
}
else{
return next(null, data)
}
}
var input = {
startDate: 000001,
endDate: 000002,
reason: "hihghh"
}
validation(input, function(err, datas){
if (err){
console.log(err);
return "FAILURE";
} else{
return "SUCCESS";
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment