UNPKG

@incdevco/framework

Version:
43 lines (23 loc) 712 B
var Expect = require('chai').expect; var Promise = require('bluebird'); function RegExpValidator(config) { 'use strict'; this.message = config.message; this.regexp = config.regexp; Expect(this.regexp).to.be.an.instanceof(RegExp, 'config.regexp'); if (!this.message) { this.message = 'Does not pass RegExp: ' + this.regexp.toString('utf8'); } } RegExpValidator.prototype.validate = function (input) { 'use strict'; var self = this; return Promise.try(function () { if (self.regexp.test(input)) { return true; } else { throw new Error(self.message); } }); }; module.exports = RegExpValidator;