Skip to content

Instantly share code, notes, and snippets.

@yuricamara
Last active July 7, 2017 15:55
Show Gist options
  • Save yuricamara/ffc4a43027a76aa3418f2d58a5183171 to your computer and use it in GitHub Desktop.
Save yuricamara/ffc4a43027a76aa3418f2d58a5183171 to your computer and use it in GitHub Desktop.
Validators
'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 = {
email
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment