UNPKG

container.ts

Version:
581 lines 19.8 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var moment = require("moment-timezone"); var validator = require("validator"); var error_1 = require("../error"); var data_1 = require("./data"); /** Validate error codes. */ var EValidateError; (function (EValidateError) { EValidateError[EValidateError["InvalidBoolean"] = 0] = "InvalidBoolean"; EValidateError[EValidateError["InvalidInteger"] = 1] = "InvalidInteger"; EValidateError[EValidateError["InvalidFloat"] = 2] = "InvalidFloat"; EValidateError[EValidateError["InvalidHexadecimal"] = 3] = "InvalidHexadecimal"; EValidateError[EValidateError["InvalidString"] = 4] = "InvalidString"; EValidateError[EValidateError["InvalidAscii"] = 5] = "InvalidAscii"; EValidateError[EValidateError["InvalidBase64"] = 6] = "InvalidBase64"; EValidateError[EValidateError["InvalidAlpha"] = 7] = "InvalidAlpha"; EValidateError[EValidateError["InvalidAlphanumeric"] = 8] = "InvalidAlphanumeric"; EValidateError[EValidateError["InvalidCreditCard"] = 9] = "InvalidCreditCard"; EValidateError[EValidateError["InvalidEmail"] = 10] = "InvalidEmail"; EValidateError[EValidateError["InvalidDomain"] = 11] = "InvalidDomain"; EValidateError[EValidateError["InvalidHexColour"] = 12] = "InvalidHexColour"; EValidateError[EValidateError["InvalidIp"] = 13] = "InvalidIp"; EValidateError[EValidateError["InvalidJson"] = 14] = "InvalidJson"; EValidateError[EValidateError["InvalidMacAddress"] = 15] = "InvalidMacAddress"; EValidateError[EValidateError["InvalidMd5"] = 16] = "InvalidMd5"; EValidateError[EValidateError["InvalidMobilePhone"] = 17] = "InvalidMobilePhone"; EValidateError[EValidateError["InvalidMongoId"] = 18] = "InvalidMongoId"; EValidateError[EValidateError["InvalidPostalCode"] = 19] = "InvalidPostalCode"; EValidateError[EValidateError["InvalidUrl"] = 20] = "InvalidUrl"; EValidateError[EValidateError["InvalidUuid"] = 21] = "InvalidUuid"; EValidateError[EValidateError["InvalidPort"] = 22] = "InvalidPort"; EValidateError[EValidateError["InvalidLanguage"] = 23] = "InvalidLanguage"; EValidateError[EValidateError["InvalidCountry"] = 24] = "InvalidCountry"; EValidateError[EValidateError["InvalidLocale"] = 25] = "InvalidLocale"; EValidateError[EValidateError["InvalidTimeZone"] = 26] = "InvalidTimeZone"; EValidateError[EValidateError["InvalidDate"] = 27] = "InvalidDate"; EValidateError[EValidateError["InvalidDuration"] = 28] = "InvalidDuration"; })(EValidateError = exports.EValidateError || (exports.EValidateError = {})); /** Validate error chain class. */ var ValidateError = /** @class */ (function (_super) { __extends(ValidateError, _super); function ValidateError(code, value, cause) { return _super.call(this, { name: EValidateError[code], value: value }, cause) || this; } return ValidateError; }(error_1.ErrorChain)); exports.ValidateError = ValidateError; /** Wrapper for validator.toBoolean. */ function isBoolean(value, options) { if (value === void 0) { value = ""; } if (options === void 0) { options = {}; } var strict = !!options.strict; try { return validator.toBoolean(value, strict); } catch (error) { throw new ValidateError(EValidateError.InvalidBoolean, value, error); } } exports.isBoolean = isBoolean; /** Wrapper for validator.isInt. */ function isInteger(value, options) { if (value === void 0) { value = ""; } if (options === void 0) { options = {}; } var isValid = false; try { isValid = validator.isInt(value, options); } catch (error) { throw new ValidateError(EValidateError.InvalidInteger, value, error); } if (!isValid) { throw new ValidateError(EValidateError.InvalidInteger, value); } return validator.toInt(value, 10); } exports.isInteger = isInteger; /** Wrapper for validator.isFloat. */ function isFloat(value, options) { if (value === void 0) { value = ""; } if (options === void 0) { options = {}; } var isValid = false; try { isValid = validator.isFloat(value, options); } catch (error) { throw new ValidateError(EValidateError.InvalidFloat, value, error); } if (!isValid) { throw new ValidateError(EValidateError.InvalidFloat, value); } return validator.toFloat(value); } exports.isFloat = isFloat; /** Wrapper for validator.isHexadecimal. */ function isHexadecimal(value) { if (value === void 0) { value = ""; } var isValid = false; try { isValid = validator.isHexadecimal(value); } catch (error) { throw new ValidateError(EValidateError.InvalidHexadecimal, value, error); } if (!isValid) { throw new ValidateError(EValidateError.InvalidHexadecimal, value); } return validator.toInt(value, 16); } exports.isHexadecimal = isHexadecimal; /** Wrapper for validator methods isLength, isUppercase, isLowercase and isIn. */ function isString(value, options) { if (value === void 0) { value = ""; } if (options === void 0) { options = {}; } var uppercase = !!options.uppercase; var lowercase = !!options.lowercase; var values = options.values || []; var isValid = false; // If minimum length is undefined, default to 1. // Passing undefined minimum causes empty strings to pass validation. if (options.min == null) { options.min = 1; } try { // Validate is string of length. isValid = validator.isLength(value, { min: options.min, max: options.max }); // Check in values array if provided. if (values.length > 0) { isValid = isValid && validator.isIn(value, values); } // Check if uppercase/lowercase if required. if (uppercase && !validator.isUppercase(value)) { isValid = false; } if (lowercase && !validator.isLowercase(value)) { isValid = false; } } catch (error) { throw new ValidateError(EValidateError.InvalidString, value, error); } if (!isValid) { throw new ValidateError(EValidateError.InvalidString, value); } return value; } exports.isString = isString; /** Wrapper for validator.isAscii. */ function isAscii(value, options) { if (value === void 0) { value = ""; } if (options === void 0) { options = {}; } var isValid = false; try { isValid = validator.isAscii(value); value = isString(value, options); } catch (error) { throw new ValidateError(EValidateError.InvalidAscii, value, error); } if (!isValid) { throw new ValidateError(EValidateError.InvalidAscii, value); } return value; } exports.isAscii = isAscii; /** Wrapper for validator.isBase64. */ function isBase64(value, options) { if (value === void 0) { value = ""; } if (options === void 0) { options = {}; } var isValid = false; try { isValid = validator.isBase64(value); value = isString(value, options); } catch (error) { throw new ValidateError(EValidateError.InvalidBase64, value, error); } if (!isValid) { throw new ValidateError(EValidateError.InvalidBase64, value); } return value; } exports.isBase64 = isBase64; /** Wrapper for validator.isAlpha. */ function isAlpha(value, options) { if (value === void 0) { value = ""; } if (options === void 0) { options = {}; } var locale = options.locale || "en-GB"; var isValid = false; try { isValid = validator.isAlpha(value, locale); value = isString(value, options); } catch (error) { throw new ValidateError(EValidateError.InvalidAlpha, value, error); } if (!isValid) { throw new ValidateError(EValidateError.InvalidAlpha, value); } return value; } exports.isAlpha = isAlpha; /** Wrapper for validator.isAlphanumeric. */ function isAlphanumeric(value, options) { if (value === void 0) { value = ""; } if (options === void 0) { options = {}; } var locale = options.locale || "en-GB"; var isValid = false; try { isValid = validator.isAlphanumeric(value, locale); value = isString(value, options); } catch (error) { throw new ValidateError(EValidateError.InvalidAlphanumeric, value); } if (!isValid) { throw new ValidateError(EValidateError.InvalidAlphanumeric, value); } return value; } exports.isAlphanumeric = isAlphanumeric; /** Wrapper for validator.isCreditCard. */ function isCreditCard(value) { if (value === void 0) { value = ""; } var isValid = false; try { isValid = validator.isCreditCard(value); } catch (error) { throw new ValidateError(EValidateError.InvalidCreditCard, value, error); } if (!isValid) { throw new ValidateError(EValidateError.InvalidCreditCard, value); } return value; } exports.isCreditCard = isCreditCard; /** Wrapper for validator.isEmail. */ function isEmail(value, options) { if (value === void 0) { value = ""; } if (options === void 0) { options = {}; } var isValid = false; try { isValid = validator.isEmail(value, options); } catch (error) { throw new ValidateError(EValidateError.InvalidEmail, value, error); } if (!isValid) { throw new ValidateError(EValidateError.InvalidEmail, value); } return value; } exports.isEmail = isEmail; /** Wrapper for validator.isFQDN. */ function isDomain(value, options) { if (value === void 0) { value = ""; } if (options === void 0) { options = {}; } var isValid = false; try { isValid = validator.isFQDN(value, options); } catch (error) { throw new ValidateError(EValidateError.InvalidDomain, value, error); } if (!isValid) { throw new ValidateError(EValidateError.InvalidDomain, value); } return value; } exports.isDomain = isDomain; /** Wrapper for validator.isHexColor. */ function isHexColour(value) { if (value === void 0) { value = ""; } var isValid = false; try { isValid = validator.isHexColor(value); } catch (error) { throw new ValidateError(EValidateError.InvalidHexColour, value, error); } if (!isValid) { throw new ValidateError(EValidateError.InvalidHexColour, value); } return value; } exports.isHexColour = isHexColour; /** Wrapper for validator.isIP. */ function isIp(value, options) { if (value === void 0) { value = ""; } if (options === void 0) { options = {}; } var version = options.version || 4; var isValid = false; try { isValid = validator.isIP(value, version); } catch (error) { throw new ValidateError(EValidateError.InvalidIp, value, error); } if (!isValid) { throw new ValidateError(EValidateError.InvalidIp, value); } return value; } exports.isIp = isIp; /** Wrapper for validator.isJSON. */ function isJson(value) { if (value === void 0) { value = ""; } var isValid = false; try { isValid = validator.isJSON(value); } catch (error) { throw new ValidateError(EValidateError.InvalidJson, value, error); } if (!isValid) { throw new ValidateError(EValidateError.InvalidJson, value); } return JSON.parse(value); } exports.isJson = isJson; /** Wrapper for validator.isMACAddress. */ function isMacAddress(value) { if (value === void 0) { value = ""; } var isValid = false; try { isValid = validator.isMACAddress(value); } catch (error) { throw new ValidateError(EValidateError.InvalidMacAddress, value, error); } if (!isValid) { throw new ValidateError(EValidateError.InvalidMacAddress, value); } return value; } exports.isMacAddress = isMacAddress; /** Wrapper for validator.isMD5. */ function isMd5(value) { if (value === void 0) { value = ""; } var isValid = false; try { isValid = validator.isMD5(value); } catch (error) { throw new ValidateError(EValidateError.InvalidMd5, value, error); } if (!isValid) { throw new ValidateError(EValidateError.InvalidMd5, value); } return value; } exports.isMd5 = isMd5; /** Wrapper for validator.isMobilePhone. */ function isMobilePhone(value, options) { if (value === void 0) { value = ""; } if (options === void 0) { options = {}; } var locale = options.locale || "en-GB"; var isValid = false; try { isValid = validator.isMobilePhone(value, locale); } catch (error) { throw new ValidateError(EValidateError.InvalidMobilePhone, value, error); } if (!isValid) { throw new ValidateError(EValidateError.InvalidMobilePhone, value); } return value; } exports.isMobilePhone = isMobilePhone; /** Wrapper for validator.isMongoId. */ function isMongoId(value) { if (value === void 0) { value = ""; } var isValid = false; try { isValid = validator.isMongoId(value); } catch (error) { throw new ValidateError(EValidateError.InvalidMongoId, value, error); } if (!isValid) { throw new ValidateError(EValidateError.InvalidMongoId, value); } return value; } exports.isMongoId = isMongoId; /** Wrapper for validator.isPostalCode. */ function isPostalCode(value, options) { if (value === void 0) { value = ""; } if (options === void 0) { options = {}; } var locale = options.locale || "GB"; var isValid = false; try { isValid = validator.isPostalCode(value, locale); } catch (error) { throw new ValidateError(EValidateError.InvalidPostalCode, value, error); } if (!isValid) { throw new ValidateError(EValidateError.InvalidPostalCode, value); } return value; } exports.isPostalCode = isPostalCode; /** Wrapper for validator.isURL. */ function isUrl(value, options) { if (value === void 0) { value = ""; } if (options === void 0) { options = { require_host: true }; } var isValid = false; try { isValid = validator.isURL(value, options); } catch (error) { throw new ValidateError(EValidateError.InvalidUrl, value, error); } if (!isValid) { throw new ValidateError(EValidateError.InvalidUrl, value); } return value; } exports.isUrl = isUrl; /** Wrapper for validator.isUUID. */ function isUuid(value, options) { if (value === void 0) { value = ""; } if (options === void 0) { options = {}; } var version = options.version || "all"; var isValid = false; try { isValid = validator.isUUID(value, version); } catch (error) { throw new ValidateError(EValidateError.InvalidUuid, value, error); } if (!isValid) { throw new ValidateError(EValidateError.InvalidUuid, value); } return value; } exports.isUuid = isUuid; /** Validate that value is a valid port number (1 - 65535). */ function isPort(value) { if (value === void 0) { value = ""; } try { return isInteger(value, { min: 0x1, max: 0xFFFF }); } catch (error) { throw new ValidateError(EValidateError.InvalidPort, value, error); } } exports.isPort = isPort; /** Validate that value is a valid ISO639 language code. */ function isLanguage(value) { if (value === void 0) { value = ""; } try { return isString(value.toLowerCase(), { min: 2, max: 2, values: data_1.ISO639 }); } catch (error) { throw new ValidateError(EValidateError.InvalidLanguage, value, error); } } exports.isLanguage = isLanguage; /** Validate that value is a valid ISO3166 country code. */ function isCountry(value) { if (value === void 0) { value = ""; } try { return isString(value.toUpperCase(), { min: 2, max: 2, values: data_1.ISO3166 }); } catch (error) { throw new ValidateError(EValidateError.InvalidCountry, value, error); } } exports.isCountry = isCountry; /** Validate that value is a valid locale (language_COUNTRY). */ function isLocale(value, options) { if (value === void 0) { value = ""; } if (options === void 0) { options = {}; } var separator = options.separator || "_"; try { var parts = value.split(separator); var language = isLanguage(parts[0]); var country = isCountry(parts[1]); return "" + language + separator + country; } catch (error) { throw new ValidateError(EValidateError.InvalidLocale, value, error); } } exports.isLocale = isLocale; /** Validate that value is a valid time zone defined in 'moment-timezone' library. */ function isTimeZone(value) { if (value === void 0) { value = ""; } try { return isString(value, { values: moment.tz.names() }); } catch (error) { throw new ValidateError(EValidateError.InvalidTimeZone, value, error); } } exports.isTimeZone = isTimeZone; /** Validate that value is a valid date parsed by 'moment-timezone' library. */ function isDate(value, options) { if (value === void 0) { value = ""; } if (options === void 0) { options = {}; } var format = options.format || moment.ISO_8601; var timezone = options.timezone || "Etc/UTC"; var date; var isValid = false; try { date = moment.tz(value, format, timezone); isValid = date.isValid(); } catch (error) { throw new ValidateError(EValidateError.InvalidDate, value, error); } if (!isValid) { throw new ValidateError(EValidateError.InvalidDate, value); } return date; } exports.isDate = isDate; /** Validate that value is a valid duration parsed by 'moment-timezone' library. */ function isDuration(value, options) { if (value === void 0) { value = ""; } if (options === void 0) { options = {}; } try { return moment.duration(value, options.unit); } catch (error) { throw new ValidateError(EValidateError.InvalidDuration, value, error); } } exports.isDuration = isDuration; /** Static validate methods container. */ var Validate = /** @class */ (function () { function Validate() { } Validate.isBoolean = isBoolean; Validate.isInteger = isInteger; Validate.isFloat = isFloat; Validate.isHexadecimal = isHexadecimal; Validate.isString = isString; Validate.isAscii = isAscii; Validate.isBase64 = isBase64; Validate.isAlpha = isAlpha; Validate.isAlphanumeric = isAlphanumeric; Validate.isCreditCard = isCreditCard; Validate.isEmail = isEmail; Validate.isDomain = isDomain; Validate.isHexColour = isHexColour; Validate.isIp = isIp; Validate.isJson = isJson; Validate.isMacAddress = isMacAddress; Validate.isMd5 = isMd5; Validate.isMobilePhone = isMobilePhone; Validate.isMongoId = isMongoId; Validate.isPostalCode = isPostalCode; Validate.isUrl = isUrl; Validate.isUuid = isUuid; Validate.isPort = isPort; Validate.isLanguage = isLanguage; Validate.isCountry = isCountry; Validate.isLocale = isLocale; Validate.isTimeZone = isTimeZone; Validate.isDate = isDate; Validate.isDuration = isDuration; return Validate; }()); exports.Validate = Validate; //# sourceMappingURL=Validate.js.map