/** * FIXME: add documentation here ! */ function Validator() { this.runAtom = function (atomName, value) { console.log('runAtom input: ', atomName, value); var response = this['responseMessage']; if (!this['registeredAtoms'][atomName]) { response.error = true; response.code = this.invalidAtomCode; response.message = 'Invalid validation rule: ' + atomName; return response; } if(!this['atom_' + atomName](value)) { response.error = true; return response; } return response; }; this.runOption = function (optName, value, args) { console.log('runOption input: ', optName, value, args); var response = this['responseMessage']; if (!this['registeredOptions'][optName]) { response.error = true; response.code = this.invalidOptionCode; response.message = 'Invalid validation rule: ' + optName; return response; } args = (!args) ? [] : args; if(!this['option_' + optName](value, args)) { response.error = true; return response; } return response; }; this.run = function (atomList, optionList, value) { var response = this['responseMessage']; if(atomList && atomList.length > 0) { for (var i=0; i < atomList.length ;i++) { response = this.runAtom(atomList[i], value); if (response.error) { return response; } } } if (optionList && Object.keys(optionList).length !== 0) { for (var key in optionList) { if (optionList.hasOwnProperty(key)) { response = this.runOption(key, value, optionList[key]); if (response.error) { return response; } } } } return response; }; this.getErrorMapLabel = function() { return this['errorMapLabel']; }; this.getResponseObject = function() { return this['responseMessage']; }; /** * Validator - atoms functions */ this.atom_int = function (val) { if (!val) return true; return Number(val) === val && val % 1 === 0; }; this.atom_float = function (val) { if (!val) return true; return Number(val) === val && val % 1 !== 0; }; this.atom_numeric = function (val) { if (!val) return true; var regex = new RegExp('^[0-9]+$'); return regex.test(val); }; this.atom_string = function (val) { if (!val) return true; return (typeof val == 'string' || val instanceof String); }; this.atom_alfanum = function (val) { if (!val) return true; var regex = new RegExp('^[a-z0-9]+$', 'i'); return regex.test(val); }; this.atom_object_id = function (val) { if (!val) return true; return this.atom_numeric(val); }; this.atom_text = function (val) { if (!val) return true; var regex_notaccept = new RegExp('([\"@><\{\}\[\]\+\/#!~\|\$%\^])'); return this.atom_string(val) && !(regex_notaccept.test(val)); }; this.atom_name = function (val) { if (!val) return true; return this.atom_text(val) && val.length>=3 && val.length<=64; }; this.atom_email = function (val) { if (!val) return true; return val.length <= 255 && /^("(?:[\\x00-\\x21\\x23-\\x5B\\x5D-\\x7F]|\\[\\x00-\\x7F])*"|(?:[!#-'*+\-\/-9=\/A-Z^-~]|\\[\\x00-\\x7F])+(?:\.(?:[!#-'*+\-\/-9=\/A-Z^-~]|\\[\\x00-\\x7F])+)*)@((?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+(?:[0-9][0-9-]*)?[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?)$/.test(val); }; this.atom_password = function (val) { if (!val) return true; var PASSWORD_1_RE = /^[!-~]{8,64}$/; var PASSWORD_2_RE = /(.)\\1{3,}/i; return PASSWORD_1_RE.test(val) && !PASSWORD_2_RE.test(val); }; this.atom_username = function (val) { if (!val) return true; var regex = new RegExp('^[a-z0-9_.-@-]{1,62}$', 'i'); return regex.test(val); }; // FIXME !!! this.atom_date = function (val) { return true; //if (!val) return true; //var regex = new RegExp('^[12][0-9]{3}\-[01]?[0-9]\-[0-3]?[0-9]$', 'i'); //return regex.test(val); }; this.atom_activation_id = function (val) { if (!val) return true; return this.atom_base64(val) }; this.atom_base64 = function (val) { if (!val) return true; var regex = new RegExp('^[a-z0-9+\/=]+$', 'i'); return regex.test(val); }; this.atom_jwt = function (val) { if (!val) return true; var regex = new RegExp('^(?:[a-z0-9+\/=_-]+\.){2}[a-z0-9+\/=_-]+$', 'i'); return regex.test(val); }; this.registeredAtoms = { 'int': true, 'numeric': true, 'float': true, 'string': true, 'alfanum': true, 'object_id': true, 'text': true, 'name': true, 'date': true, 'email': true, 'password': true, 'username': true, 'base64': true, 'jwt': true, 'activation_id': true }; /** * Validator - options functions */ this.option_notempty = function (val, args) { return !!(typeof val != 'undefined' && val); }; this.option_minval = function (val, args) { if (this.atom_int(val)) { return val >= args[0]; } if (this.atom_string(val)) { return val.length >= args[0]; } return false; }; this.option_maxval = function (val, args) { if (this.atom_int(val)) { return val <= args[0]; } if (this.atom_string(val)) { return val.length <= args[0]; } return false; }; this.options_eq = function (val, args) { if (this.atom_int(val)) { return val == args[0]; } if (this.atom_string(val)) { return val.length == args[0]; } return false; }; this.options_neq = function (val, args) { if (this.atom_int(val)) { return val != args[0]; } if (this.atom_string(val)) { return val.length != args[0]; } return false; }; this.options_gt = function (val, args) { if (this.atom_int(val)) { return val > args[0]; } return false; }; this.options_st = function (val, args) { if (this.atom_int(val)) { return val < args[0]; } return false; }; this.options_enum = function (val, args) { return args.indexOf(val); }; this.registeredOptions = { 'notempty': true, 'minlen': true, 'maxlen': true, 'eq': true, // equal 'neq': true, // not equal 'gt': true, // grater than 'st': true, // smaller than 'enum': true // specific matching values }; this.responseMessage = { error: false, code: 0, message: '', context: '', field: '' }; this.invalidAtomCode = -1; this.invalidOptionCode = -2; this.errorMapLabel = 'validator'; }