UNPKG

nope-validator

Version:
152 lines (147 loc) 5.8 kB
define(['exports', './_virtual/_tslib', './consts', './NopePrimitive', './utils'], (function (exports, _tslib, consts, NopePrimitive, utils) { 'use strict'; var NopeString = /** @class */ (function (_super) { _tslib.__extends(NopeString, _super); function NopeString() { var _this = _super !== null && _super.apply(this, arguments) || this; _this._type = 'string'; return _this; } NopeString.prototype.validate = function (entry, context) { var value = !!entry ? String(entry) : entry; return _super.prototype.validate.call(this, value, context); }; NopeString.prototype.validateAsync = function (entry, context) { var value = !!entry ? String(entry) : entry; return _super.prototype.validateAsync.call(this, value, context); }; NopeString.prototype.isEmpty = function (value) { return utils.isNil(value) || value.trim().length === 0; }; NopeString.prototype.regex = function (regex, message) { var _this = this; if (message === void 0) { message = "Doesn't satisfy the rule"; } var rule = function (entry) { if (_this.isEmpty(entry)) { return; } if (!regex.test(entry)) { return message; } }; return this.test(rule); }; NopeString.prototype.url = function (message) { if (message === void 0) { message = 'Input is not a valid url'; } this.regex(consts.urlRegex, message); return this; }; NopeString.prototype.email = function (message) { if (message === void 0) { message = 'Input is not a valid email'; } this.regex(consts.emailRegex, message); return this; }; NopeString.prototype.min = function (length, message) { this.greaterThan(length, message); return this; }; NopeString.prototype.max = function (length, message) { this.lessThan(length, message); return this; }; NopeString.prototype.greaterThan = function (length, message) { var _this = this; if (message === void 0) { message = 'Input is too short'; } var rule = function (entry) { if (_this.isEmpty(entry)) { return; } var value = entry; if (value.length <= length) { return message; } }; return this.test(rule); }; NopeString.prototype.lessThan = function (length, message) { var _this = this; if (message === void 0) { message = 'Input is too long'; } var rule = function (entry) { if (_this.isEmpty(entry)) { return; } var value = entry; if (value.length >= length) { return message; } }; return this.test(rule); }; NopeString.prototype.atLeast = function (length, message) { var _this = this; if (message === void 0) { message = 'Input is too short'; } var rule = function (entry) { if (_this.isEmpty(entry)) { return; } var value = entry; if (value.length < length) { return message; } }; return this.test(rule); }; NopeString.prototype.atMost = function (length, message) { var _this = this; if (message === void 0) { message = 'Input is too long'; } var rule = function (entry) { if (_this.isEmpty(entry)) { return; } var value = entry; if (value.length > length) { return message; } }; return this.test(rule); }; NopeString.prototype.between = function (startLength, endLength, atLeastMessage, atMostMessage) { if (atLeastMessage === void 0) { atLeastMessage = 'Input is too short'; } if (atMostMessage === void 0) { atMostMessage = 'Input is too long'; } if (startLength && endLength && startLength > endLength) { var rule = function () { throw Error('between must receive an initial length (startLength) smaller than the final length (endLength) parameter'); }; return this.test(rule); } this.atLeast(startLength, atLeastMessage); this.atMost(endLength, atMostMessage); return this; }; NopeString.prototype.exactLength = function (length, message) { var _this = this; if (message === void 0) { message = "Must be at exactly of length ".concat(length); } var rule = function (entry) { if (_this.isEmpty(entry)) { return; } var value = entry; if (value.length !== length) { return message; } }; return this.test(rule); }; NopeString.prototype.trim = function () { var _this = this; var rule = function (entry) { _this._entry = entry.trim(); return; }; return this.test(rule); }; return NopeString; }(NopePrimitive.NopePrimitive)); exports.NopeString = NopeString; Object.defineProperty(exports, '__esModule', { value: true }); })); //# sourceMappingURL=NopeString.js.map