UNPKG

iq-validator

Version:

An intelligent string validator with configurable sanitisation options.

128 lines (109 loc) 4.83 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; var _assert = require('assert'); var assert = _interopRequireWildcard(_assert); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * @summary Tests the validity of an object representing a single sanitisation * rule. The object must have the follwing structure: * { * regex: 'regex', * regexFlags: 'ig', * sanitiseFunction: function(str) { * //do sanitisation * return str; * } * } * The regexFlags property is optional. * @function * @private * * @param {object} ruleObj - the sanitisation rule object * @throws {AssertionError} If the argument is invalid. */ var validateRule = function validateRule(ruleObj) { assert.equal(typeof ruleObj === 'undefined' ? 'undefined' : _typeof(ruleObj), 'object', 'config rule must be a valid object'); assert.notEqual(ruleObj, null, 'config rule must be a valid object'); assert.ok(Object.prototype.hasOwnProperty.call(ruleObj, 'regex'), 'invalid config rule: no \'regex\' element found'); assert.ok(Object.prototype.hasOwnProperty.call(ruleObj, 'sanitiseFunction'), 'invalid config rule: no \'sanitiseFunction\' element found'); assert.equal(_typeof(ruleObj.regex), 'string', 'invalid config rule: \'regex\' must be a string'); assert.equal(_typeof(ruleObj.sanitiseFunction), 'function', 'invalid config rule: \'sanitiseFunction\' must be a function'); if (Object.prototype.hasOwnProperty.call(ruleObj, 'regexFlags')) { assert.equal(_typeof(ruleObj.regexFlags), 'string', 'invalid config rule: \'regexFlags\' must be a string'); } }; /** * Class representing a single sanitisation rule. */ var SanitiseRule = function () { /** * @summary Create an instance of SanitiseRule * @name SanitiseRule * @class * @public * * @param {Object} ruleObj - An object representing the sanitisation * rule to apply. This should be the object structure: * { * regex: 'regex', * regexFlags: 'ig', * sanitiseFunction: function(str) { * //do sanitisation * return str; * } * } * @throws {AssertionError} If the argument is invalid. * @throws {SyntaxError} If the regex configuration is invalid */ function SanitiseRule(ruleObj) { _classCallCheck(this, SanitiseRule); validateRule(ruleObj); /** * @type {RegExp} */ this.regexObj = new RegExp(ruleObj.regex, ruleObj.regexFlags); /** * @type {function} */ this.sanitiseFx = ruleObj.sanitiseFunction; } /** * @summary Checks if the rule's regex is matching the provided string * @method * @public * * @param {string} str - The string to be checked * @returns {boolean} true if the rule matches the provided string */ _createClass(SanitiseRule, [{ key: 'matches', value: function matches(str) { return this.regexObj.test(str); } /** * @summary Checks if the rule's regex is matching the provided string * @method * @public * * @param {string} str - The string to be sanitised * @returns {string} the sanitised string * * @throws {AssertionError} If the sanitise function doesn't return a string. */ }, { key: 'runSanitise', value: function runSanitise(str) { var resultStr = this.sanitiseFx(str); assert.equal(typeof resultStr === 'undefined' ? 'undefined' : _typeof(resultStr), 'string', 'sanitise function must return a string'); return resultStr; } }]); return SanitiseRule; }(); exports.default = SanitiseRule; module.exports = exports['default'];