UNPKG

ez-validation

Version:
157 lines (156 loc) 6.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var EZValidationAPI = /** @class */ (function () { function EZValidationAPI(validating, errorMessage, errorMessages) { if (errorMessages === void 0) { errorMessages = []; } this.validating = validating; this.errorMessage = errorMessage; this.errorMessages = errorMessages; this.hasError = false; } EZValidationAPI.prototype._returnError = function (errorMessage) { this.hasError = true; this.errorMessage = this.errorMessage || errorMessage; this.errorMessages.push(errorMessage); }; EZValidationAPI.prototype.required = function (errorMessage) { if (errorMessage === void 0) { errorMessage = "This is required"; } if (this.validating === "" || this.validating === undefined || (Array.isArray(this.validating) && this.validating.length === 0)) { this._returnError(errorMessage); } return this; }; EZValidationAPI.prototype.isString = function (errorMessage) { if (errorMessage === void 0) { errorMessage = "Needs to be a string"; } if (typeof this.validating !== "string") { this._returnError(errorMessage); } return this; }; EZValidationAPI.prototype.isNumber = function (errorMessage) { if (errorMessage === void 0) { errorMessage = "Needs to be a number"; } if (typeof this.validating !== "number" && this.validating !== "") { this._returnError(errorMessage); } return this; }; EZValidationAPI.prototype.isWholeNumber = function (errorMessage) { if (errorMessage === void 0) { errorMessage = "Needs to be a whole number"; } if (isNaN(Number(this.validating)) || Number(this.validating) % 1 != 0) { this._returnError(errorMessage); } return this; }; EZValidationAPI.prototype.isBoolean = function (errorMessage) { if (errorMessage === void 0) { errorMessage = "Needs to be a true or false"; } if (typeof this.validating !== "boolean") { this._returnError(errorMessage); } return this; }; EZValidationAPI.prototype.isEmpty = function (errorMessage) { if (errorMessage === void 0) { errorMessage = "Value is empty"; } if (this.validating === null || this.validating === "") { this._returnError(errorMessage); } if (Array.isArray(this.validating) && this.validating.length === 0) { this._returnError(errorMessage); } if (typeof this.validating === "object" && Object.keys(this.validating).length === 0) { this._returnError(errorMessage); } return this; }; EZValidationAPI.prototype.isObject = function (errorMessage) { if (errorMessage === void 0) { errorMessage = "Not an object"; } if (typeof this.validating !== "object") { this._returnError(errorMessage); } return this; }; EZValidationAPI.prototype.isAlphanumeric = function (errorMessage) { if (errorMessage === void 0) { errorMessage = "Must be Alphanumeric"; } if (this.validating.match(/^[a-zA-Z0-9_]*$/i) === null) { this._returnError(errorMessage); } return this; }; EZValidationAPI.prototype.isEmail = function (errorMessage) { if (errorMessage === void 0) { errorMessage = "Must be valid Email"; } if (this.validating.match(/\S+@\S+\.\S+/) === null) { this._returnError(errorMessage); } return this; }; EZValidationAPI.prototype.isPhoneNumber = function (errorMessage) { if (errorMessage === void 0) { errorMessage = "Must be valid Phone Number"; } if (String(this.validating).match(/^(1\s?)?((\([0-9]{3}\))|[0-9]{3})[\s\-]?[\0-9]{3}[\s\-]?[0-9]{4}$/gm) === null) { this._returnError(errorMessage); } return this; }; EZValidationAPI.prototype.isUSAZipCode = function (errorMessage) { if (errorMessage === void 0) { errorMessage = "Must be valid Zip Code"; } if (String(this.validating).match(/^[0-9]{5}(?:-[0-9]{4})?$/) === null) { this._returnError(errorMessage); } return this; }; EZValidationAPI.prototype.maxLength = function (max, errorMessage) { if (errorMessage === void 0) { errorMessage = "Exceeds maximum length of " + max.toString(); } var str = this.validating.toString(); if (str.length > max) { this._returnError(errorMessage); } return this; }; EZValidationAPI.prototype.minLength = function (min, errorMessage) { if (errorMessage === void 0) { errorMessage = "Value is below minimum length of " + min; } var str = this.validating.toString(); if (str.length < min) { this._returnError(errorMessage); } return this; }; EZValidationAPI.prototype.maxValue = function (max, errorMessage) { if (errorMessage === void 0) { errorMessage = "Value exceeds maximum value of " + max.toString(); } var str = this.validating.toString(); if (str > max) { this._returnError(errorMessage); } return this; }; EZValidationAPI.prototype.minValue = function (min, errorMessage) { if (errorMessage === void 0) { errorMessage = "Value is below minimum value of " + min.toString(); } var str = this.validating.toString(); if (str < min) { this._returnError(errorMessage); } return this; }; EZValidationAPI.prototype.customRegex = function (regex, errorMessage) { if (errorMessage === void 0) { errorMessage = "Value is invalid"; } if (this.validating.match(regex) === null) { this._returnError(errorMessage); } return this; }; EZValidationAPI.prototype.customValidation = function (cb, errorMessage) { var cbOutput = cb(this.validating); if (!cbOutput && errorMessage) { this._returnError(errorMessage); } else if (typeof (cbOutput) === "string") { this._returnError(cbOutput); } return this; }; return EZValidationAPI; }()); exports.EZValidationAPI = EZValidationAPI; exports.EzValidation = function (val, defaultErrorMessage) { return new EZValidationAPI(val, defaultErrorMessage); };