Last active
September 23, 2016 22:46
-
-
Save jalev/ec9c491fb44a525cec5dd04844ec1283 to your computer and use it in GitHub Desktop.
validating some datas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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