Last active
July 7, 2017 15:55
-
-
Save yuricamara/ffc4a43027a76aa3418f2d58a5183171 to your computer and use it in GitHub Desktop.
Validators
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
'use strict'; | |
// validators.js | |
const mongodb = require('mongodb'); | |
const regex = require('./regex.helper'); | |
const validator = require('validator'); | |
module.exports = { | |
isArray: function(value) { | |
return Array.isArray(value); | |
}, | |
isEmail: function(email) { | |
return regex.email.test(email); | |
}, | |
isMongoIdString: function (value) { | |
if(!value){ return false; } | |
const firstCheck = value.length === 24 && validator.isHexadecimal(value); | |
return firstCheck && mongodb.ObjectID.isValid(value) && new mongodb.ObjectID(value).toString() === value; | |
}, | |
isObject: function(value){ | |
const firstCheck = value && typeof value === 'object'; | |
return firstCheck && !Array.isArray(value); | |
}, | |
isPassword: function (value) { | |
return this.isString(value) && value.length > 5; | |
}, | |
isString: function (value) { | |
return Object.prototype.toString.call(value) === '[object String]'; | |
} | |
}; | |
// regex.helper | |
const email = /^[a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i; | |
module.exports = { | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment