UNPKG

iq-validator

Version:

An intelligent string validator with configurable sanitisation options.

99 lines (81 loc) 3.37 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 _assert = require('assert'); var assert = _interopRequireWildcard(_assert); var _SanitiseRule = require('./SanitiseRule'); var _SanitiseRule2 = _interopRequireDefault(_SanitiseRule); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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 Validates the sanitise configuration and builds * returns an array of SanitiseRule objects. * @function * @private * * @param {object} cfg - the sanitiseConfig configuration * @returns {Array.<SanitiseRule>} * @throws {AssertionError} If the argument is invalid. */ var validateConfig = function validateConfig(cfg) { assert.equal(Array.isArray(cfg), true, 'argument \'cfg\' must be an Array'); var sanitiseRuleArray = []; cfg.forEach(function (rule) { sanitiseRuleArray.push(new _SanitiseRule2.default(rule)); }); return sanitiseRuleArray; }; var SanitiseRules = function () { /** * @summary Create an instance of SanitiseRules * @name SanitiseRules * @class * @public * * @param {Array} cfg - An array containing a list of objects representing the sanitisation * rules to apply. Each object's structure must be the following: * { * regex: 'regex', * regexFlags: 'ig', * sanitiseFunction: function(str) { * //do sanitisation * return str; * } * } * @throws {AssertionError} If the argument is invalid. */ function SanitiseRules(cfg) { _classCallCheck(this, SanitiseRules); /** * @type {Array.<SanitiseRule>} */ this.cfg = validateConfig(cfg); } /** * @summary Retrieves and returns all the sanitise rules that match the argument * @method * @public * * @param {string} str - The string to be matched against the rules * @returns {Array.<SanitiseRule>} an array containing a list of SanitiseRule * objects matching the given argument. */ _createClass(SanitiseRules, [{ key: 'getMatchingRules', value: function getMatchingRules(str) { var matchingRules = []; this.cfg.forEach(function (rule) { if (rule.matches(str)) { matchingRules.push(rule); } }); return matchingRules; } }]); return SanitiseRules; }(); exports.default = SanitiseRules; module.exports = exports['default'];