UNPKG

@lakutata/core

Version:

Lakutata Framework Core

514 lines 20.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ValidatorClass = void 0; const yup_1 = require("yup"); const mapValues_1 = __importDefault(require("lodash/mapValues")); const ValidationException_1 = require("../exceptions/ValidationException"); const is_ssh_1 = __importDefault(require("is-ssh")); const BaseObject_1 = require("./BaseObject"); const ValidDataUrl_1 = require("../utils/ValidDataUrl"); const IPV4_METHOD = 'ipv4'; const IPV6_METHOD = 'ipv6'; const IP_ALL_METHOD = 'ip'; const SSH_METHOD = 'ssh'; const URL_METHOD = 'url'; const DATA_URL_METHOD = 'dataUrl'; const STRUCTURE_METHOD = 'structure'; class ValidatorClass extends BaseObject_1.BaseObject { constructor() { super(); this.COUNTRY_CODE = { 'Afghanistan': 'AF', 'Albania': 'AL', 'Algeria': 'DZ', 'American Samoa': 'AS', 'Andorra': 'AD', 'Angola': 'AO', 'Anguilla': 'AI', 'Antarctica': 'AQ', 'Antigua and Barbuda': 'AG', 'Argentina': 'AR', 'Armenia': 'AM', 'Aruba': 'AW', 'Australia': 'AU', 'Austria': 'AT', 'Azerbaijan': 'AZ', 'Bahamas': 'BS', 'Bahrain': 'BH', 'Bangladesh': 'BD', 'Barbados': 'BB', 'Belarus': 'BY', 'Belgium': 'BE', 'Belize': 'BZ', 'Benin': 'BJ', 'Bermuda': 'BM', 'Bhutan': 'BT', 'Bolivia': 'BO', 'Bosnia and Herzegovina': 'BA', 'Botswana': 'BW', 'Brazil': 'BR', 'British Indian Ocean Territory': 'IO', 'British Virgin Islands': 'VG', 'Brunei': 'BN', 'Bulgaria': 'BG', 'Burkina Faso': 'BF', 'Burundi': 'BI', 'Cambodia': 'KH', 'Cameroon': 'CM', 'Canada': 'CA', 'Cape Verde': 'CV', 'Cayman Islands': 'KY', 'Central African Republic': 'CF', 'Chad': 'TD', 'Chile': 'CL', 'China': 'CN', 'Christmas Island': 'CX', 'Cocos Islands': 'CC', 'Colombia': 'CO', 'Comoros': 'KM', 'Cook Islands': 'CK', 'Costa Rica': 'CR', 'Croatia': 'HR', 'Cuba': 'CU', 'Curacao': 'CW', 'Cyprus': 'CY', 'Czech Republic': 'CZ', 'Democratic Republic of the Congo': 'CD', 'Denmark': 'DK', 'Djibouti': 'DJ', 'Dominica': 'DM', 'Dominican Republic': 'DO', 'East Timor': 'TL', 'Ecuador': 'EC', 'Egypt': 'EG', 'El Salvador': 'SV', 'Equatorial Guinea': 'GQ', 'Eritrea': 'ER', 'Estonia': 'EE', 'Ethiopia': 'ET', 'Falkland Islands': 'FK', 'Faroe Islands': 'FO', 'Fiji': 'FJ', 'Finland': 'FI', 'France': 'FR', 'French Polynesia': 'PF', 'Gabon': 'GA', 'Gambia': 'GM', 'Georgia': 'GE', 'Germany': 'DE', 'Ghana': 'GH', 'Gibraltar': 'GI', 'Greece': 'GR', 'Greenland': 'GL', 'Grenada': 'GD', 'Guam': 'GU', 'Guatemala': 'GT', 'Guernsey': 'GG', 'Guinea': 'GN', 'Guinea-Bissau': 'GW', 'Guyana': 'GY', 'Haiti': 'HT', 'Honduras': 'HN', 'Hong Kong': 'HK', 'Hungary': 'HU', 'Iceland': 'IS', 'India': 'IN', 'Indonesia': 'ID', 'Iran': 'IR', 'Iraq': 'IQ', 'Ireland': 'IE', 'Isle of Man': 'IM', 'Israel': 'IL', 'Italy': 'IT', 'Ivory Coast': 'CI', 'Jamaica': 'JM', 'Japan': 'JP', 'Jersey': 'JE', 'Jordan': 'JO', 'Kazakhstan': 'KZ', 'Kenya': 'KE', 'Kiribati': 'KI', 'Kosovo': 'XK', 'Kuwait': 'KW', 'Kyrgyzstan': 'KG', 'Laos': 'LA', 'Latvia': 'LV', 'Lebanon': 'LB', 'Lesotho': 'LS', 'Liberia': 'LR', 'Libya': 'LY', 'Liechtenstein': 'LI', 'Lithuania': 'LT', 'Luxembourg': 'LU', 'Macau': 'MO', 'Macedonia': 'MK', 'Madagascar': 'MG', 'Malawi': 'MW', 'Malaysia': 'MY', 'Maldives': 'MV', 'Mali': 'ML', 'Malta': 'MT', 'Marshall Islands': 'MH', 'Mauritania': 'MR', 'Mauritius': 'MU', 'Mayotte': 'YT', 'Mexico': 'MX', 'Micronesia': 'FM', 'Moldova': 'MD', 'Monaco': 'MC', 'Mongolia': 'MN', 'Montenegro': 'ME', 'Montserrat': 'MS', 'Morocco': 'MA', 'Mozambique': 'MZ', 'Myanmar': 'MM', 'Namibia': 'NA', 'Nauru': 'NR', 'Nepal': 'NP', 'Netherlands': 'NL', 'Netherlands Antilles': 'AN', 'New Caledonia': 'NC', 'New Zealand': 'NZ', 'Nicaragua': 'NI', 'Niger': 'NE', 'Nigeria': 'NG', 'Niue': 'NU', 'North Korea': 'KP', 'Northern Mariana Islands': 'MP', 'Norway': 'NO', 'Oman': 'OM', 'Pakistan': 'PK', 'Palau': 'PW', 'Palestine': 'PS', 'Panama': 'PA', 'Papua New Guinea': 'PG', 'Paraguay': 'PY', 'Peru': 'PE', 'Philippines': 'PH', 'Pitcairn': 'PN', 'Poland': 'PL', 'Portugal': 'PT', 'Puerto Rico': 'PR', 'Qatar': 'QA', 'Republic of the Congo': 'CG', 'Reunion': 'RE', 'Romania': 'RO', 'Russia': 'RU', 'Rwanda': 'RW', 'Saint Barthelemy': 'BL', 'Saint Helena': 'SH', 'Saint Kitts and Nevis': 'KN', 'Saint Lucia': 'LC', 'Saint Martin': 'MF', 'Saint Pierre and Miquelon': 'PM', 'Saint Vincent and the Grenadines': 'VC', 'Samoa': 'WS', 'San Marino': 'SM', 'Sao Tome and Principe': 'ST', 'Saudi Arabia': 'SA', 'Senegal': 'SN', 'Serbia': 'RS', 'Seychelles': 'SC', 'Sierra Leone': 'SL', 'Singapore': 'SG', 'Sint Maarten': 'SX', 'Slovakia': 'SK', 'Slovenia': 'SI', 'Solomon Islands': 'SB', 'Somalia': 'SO', 'South Africa': 'ZA', 'South Korea': 'KR', 'South Sudan': 'SS', 'Spain': 'ES', 'Sri Lanka': 'LK', 'Sudan': 'SD', 'Suriname': 'SR', 'Svalbard and Jan Mayen': 'SJ', 'Swaziland': 'SZ', 'Sweden': 'SE', 'Switzerland': 'CH', 'Syria': 'SY', 'Taiwan': 'TW', 'Tajikistan': 'TJ', 'Tanzania': 'TZ', 'Thailand': 'TH', 'Togo': 'TG', 'Tokelau': 'TK', 'Tonga': 'TO', 'Trinidad and Tobago': 'TT', 'Tunisia': 'TN', 'Turkey': 'TR', 'Turkmenistan': 'TM', 'Turks and Caicos Islands': 'TC', 'Tuvalu': 'TV', 'U.S. Virgin Islands': 'VI', 'Uganda': 'UG', 'Ukraine': 'UA', 'United Arab Emirates': 'AE', 'United Kingdom': 'GB', 'United States': 'US', 'Uruguay': 'UY', 'Uzbekistan': 'UZ', 'Vanuatu': 'VU', 'Vatican': 'VA', 'Venezuela': 'VE', 'Vietnam': 'VN', 'Wallis and Futuna': 'WF', 'Western Sahara': 'EH', 'Yemen': 'YE', 'Zambia': 'ZM', 'Zimbabwe': 'ZW' }; (0, yup_1.addMethod)(yup_1.string, IPV4_METHOD, function (message = 'Invalid IP address') { return this.matches(/(^(\d{1,3}\.){3}(\d{1,3})$)/, { message, excludeEmptyString: true }).test('ip', message, value => { return value === undefined || value.trim() === '' ? true : value.split('.').find(i => parseInt(i, 10) > 255) === undefined; }); }); (0, yup_1.addMethod)(yup_1.string, IPV6_METHOD, function (message = 'Invalid IP address') { return this.matches(/(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/, { message, excludeEmptyString: true }).test('ip', message, value => { return !(value === undefined || value.trim() === ''); }); }); (0, yup_1.addMethod)(yup_1.string, IP_ALL_METHOD, function (message = 'Invalid IP address') { const combinedIpRegExp = /((^(\d{1,3}\.){3}(\d{1,3})$)|([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/; const ipv4RegExp = /(^(\d{1,3}\.){3}(\d{1,3})$)/; const ipv6RegExp = /(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/; return this.matches(combinedIpRegExp, { message, excludeEmptyString: true }).test('ip', message, value => { if (ipv4RegExp.test(value ? value.toString() : '')) { return value === undefined || value.trim() === '' ? true : value.split('.').find(i => parseInt(i, 10) > 255) === undefined; } else if (ipv6RegExp.test(value ? value.toString() : '')) { return !(value === undefined || value.trim() === ''); } else { return false; } }); }); (0, yup_1.addMethod)(yup_1.string, SSH_METHOD, function (message = 'Invalid SSH URL') { return this.matches(/[\s\S]*/, { message, excludeEmptyString: true }).test('url', message, value => { if (!value) { return false; } return (0, is_ssh_1.default)(value); }); }); (0, yup_1.addMethod)(yup_1.string, DATA_URL_METHOD, function (message = 'Invalid Data URL') { return this.matches(/[\s\S]*/, { message, excludeEmptyString: true }).test('url', message, value => { if (!value) { return false; } return (0, ValidDataUrl_1.validDataUrl)(value); }); }); (0, yup_1.addMethod)(yup_1.string, URL_METHOD, function (message = 'Invalid URL') { return this.matches(/^((https|http|ftp|rtsp|mms|wss|ws):\/\/)(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?(([0-9]{1,3}.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z].[a-z]{2,6})(:[0-9]{1,4})?((\/?)|(\/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+\/?)$/, { message, excludeEmptyString: true }).test('url', message, value => { if (!value) { return false; } else { const urlOption = Object.assign({ acceptParams: true, protocol: ['http', 'https', 'ftp', 'rtsp', 'mms', 'wss', 'ws'] }, arguments[0] ? (typeof arguments[0] === 'object' ? arguments[0] : {}) : {}); const urlObject = new URL(value); const blackHostnameList = urlOption.blackHostnameList ? (() => { const _blackHostnameList = []; for (const blackHostname of urlOption.blackHostnameList) { _blackHostnameList.push(new RegExp(blackHostname.trim().replace(/\*/g, '[\\s\\S]*'))); } return _blackHostnameList; })() : []; const whiteHostnameList = urlOption.whiteHostnameList ? (() => { const _whiteHostnameList = []; for (const whiteHostname of urlOption.whiteHostnameList) { _whiteHostnameList.push(new RegExp(whiteHostname.trim().replace(/\*/g, '[\\s\\S]*'))); } return _whiteHostnameList; })() : []; const protocols = Array.isArray(urlOption.protocol) ? (() => { const _protocols = []; for (const protocol of urlOption.protocol) { _protocols.push(protocol.toLowerCase()); } return _protocols; })() : [urlOption.protocol.toLowerCase()]; const acceptParams = urlOption.acceptParams; if (!acceptParams && !!urlObject.search) return false; if (protocols.indexOf(urlObject.protocol.replace(':', '').trim().toLowerCase()) === -1) return false; if (blackHostnameList.length > 0 || whiteHostnameList.length > 0) { const listMode = (() => { if (whiteHostnameList.length > 0) { return 'white'; } else { return 'black'; } })(); switch (listMode) { case 'black': { for (const blackRegExp of blackHostnameList) { if (blackRegExp.test(urlObject.hostname)) return false; } return true; } case 'white': { for (const whiteRegExp of whiteHostnameList) { if (whiteRegExp.test(urlObject.hostname)) return true; } return false; } } } return true; } }); }); (0, yup_1.addMethod)(yup_1.object, STRUCTURE_METHOD, function () { return this.test('structure', value => { const schema = arguments[0]; (0, yup_1.lazy)(obj => (0, yup_1.object)((0, mapValues_1.default)(obj, () => { return schema.required(); })).default({})).validateSync(value, { strict: true }); return true; }); }); } get String() { return (0, yup_1.string)(); } get Email() { return (0, yup_1.string)().email(); } get SSH() { return (0, yup_1.string)().ssh(); } get DataURL() { return (0, yup_1.string)().dataUrl(); } get Bool() { return this.Boolean; } get Boolean() { return (0, yup_1.boolean)(); } get Number() { return (0, yup_1.number)(); } get Integer() { return this.Number.integer(); } get Date() { return (0, yup_1.date)(); } get Any() { return (0, yup_1.mixed)(); } URL(option) { return this.String.url(option); } Phone(countryCode, strict) { return (0, yup_1.string)().phone(countryCode?.toUpperCase(), strict); } IP(mode = 'both') { switch (mode) { case 'ipv4': { return (0, yup_1.string)().ipv4(); } case 'ipv6': { return (0, yup_1.string)().ipv6(); } case 'both': { return (0, yup_1.string)().ip(); } default: { return (0, yup_1.string)().ip(); } } } Object(a, b) { if (a !== undefined) { if (arguments.length === 2) { return (0, yup_1.object)(a).structure(b); } else { if (a.__isYupSchema__) { return (0, yup_1.object)().structure(a); } else { return (0, yup_1.object)(a); } } } else { return (0, yup_1.object)(a); } } KeyValueStructure(valueType) { return this.Lazy(obj => this.Object((0, mapValues_1.default)(obj, () => { return valueType.required(); })).default({})); } Array(type) { return (0, yup_1.array)(type); } Ref(key, options) { return (0, yup_1.ref)(key, options); } Lazy(builder) { return (0, yup_1.lazy)(builder); } async validate(schema, data) { try { return await schema.validate(data, { strict: true }); } catch (e) { throw this.generateException(ValidationException_1.ValidationException, e.message); } } validateSync(schema, data) { try { return schema.validateSync(data, { strict: true }); } catch (e) { throw this.generateException(ValidationException_1.ValidationException, e.message); } } async isValid(schema, data) { return await schema.isValid(data, { strict: true }); } isValidSync(schema, data) { return schema.isValidSync(data, { strict: true }); } } exports.ValidatorClass = ValidatorClass; //# sourceMappingURL=ValidatorClass.js.map