diff --git a/.gitignore b/.gitignore index 2ceb8ee..efe8bc8 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,5 @@ packages/ # Include when developing application packages. pubspec.lock +.pub +.packages \ No newline at end of file diff --git a/README.md b/README.md index 06e0697..bc88e7a 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,23 @@ Now in your Dart code, you can use: - **isVariableWidth(String str)** - check if the string contains a mixture of full and half-width chars. - **isSurrogatePair(String str)** - check if the string contains any surrogate pairs chars. - **isMongoId(String str)** - check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid]. +- ***isMd5(String str)*** check if the string is a MD5 hash. +- ***isMd4(String str)*** check if the string is a MD4 hash. +- ***isSha1(String str)*** check if the string is a Sha1 hash. +- ***isSha256(String str)*** check if the string is a Sha256 hash. +- ***isSha384(String str)*** check if the string is a Sha384 hash. +- ***isSha512(String str)*** check if the string is a Sha512 hash. +- ***isRipemd128(String str)*** check if the string is a Ripemd128 hash. +- ***isRipemd160(String str)*** check if the string is a Ripemd160 hash. +- ***isTiger128(String str)*** check if the string is a Tiger128 hash. +- ***isTiger160(String str)*** check if the string is a Tiger160 hash. +- ***isTiger192(String str)*** check if the string is a Tiger192 hash. +- ***isCrc32(String str)*** check if the string is a Crc32 hash. +- ***isCrc32b(String str)*** check if the string is a Crc32b hash. +- ***isPort(String str)*** check if the string is a valid port number. +- ***isMACAddress(String str)*** check if the string is a MAC address. +- ***isMobilePhone(String str)*** |check if the string is a is a mobile phone number. +`['ar-AE', 'ar-DZ','ar-EG', 'ar-JO', 'ar-SA', 'ar-SY', 'cs-CZ', 'de-DE', 'da-DK', 'el-GR', 'en-AU', 'en-CA', 'en-GB', 'en-HK', 'en-IN', 'en-KE', 'en-NG', 'en-NZ', 'en-RW', 'en-SG', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-PK', 'es-ES', 'et-EE', 'fa-IR', 'fi-FI', 'fr-FR', 'he-IL', 'hu-HU', 'it-IT', 'ja-JP', 'ko-KR', 'lt-LT', 'ms-MY', 'nb-NO', 'nn-NO', 'pl-PL', 'pt-PT', 'ro-RO', 'ru-RU', 'sk-SK', 'sr-RS', 'tr-TR', 'uk-UA', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-TW']` OR 'any'. If 'any' is used, function will check if any of the locales match). ## Sanitizers diff --git a/lib/src/helpers.dart b/lib/src/helpers.dart index 23cab20..7c68428 100644 --- a/lib/src/helpers.dart +++ b/lib/src/helpers.dart @@ -17,4 +17,4 @@ Map _merge(Map obj, defaults) { } defaults.forEach((key, val) => obj.putIfAbsent(key, () => val)); return obj; -} \ No newline at end of file +} diff --git a/lib/src/sanitizer.dart b/lib/src/sanitizer.dart index 5494a74..cb6de87 100644 --- a/lib/src/sanitizer.dart +++ b/lib/src/sanitizer.dart @@ -1,7 +1,6 @@ part of validator; -Map _default_normalize_email_options = { 'lowercase': true }; - +Map _default_normalize_email_options = {'lowercase': true}; /// convert the input to a string String toString(input) { @@ -11,20 +10,18 @@ String toString(input) { return input.toString(); } - /// convert the input to a date, or null if the input is not a date DateTime toDate(String str) { try { return DateTime.parse(str); - } catch(e) { + } catch (e) { return null; } } - /// convert the input to a float, or NAN if the input is not a float double toFloat(String str) { - try{ + try { return double.parse(str); } catch (e) { return double.NAN; @@ -36,11 +33,10 @@ double toDouble(String str) { return toFloat(str); } - /// convert the input to an integer, or NAN if the input is not an integer -num toInt(String str, {int radix:10}) { +num toInt(String str, {int radix: 10}) { try { - return int.parse(str, radix:radix); + return int.parse(str, radix: radix); } catch (e) { try { return double.parse(str).toInt(); @@ -50,7 +46,6 @@ num toInt(String str, {int radix:10}) { } } - /// convert the input to a boolean. /// /// Everything except for '0', 'false' and '' @@ -62,28 +57,26 @@ bool toBoolean(String str, [bool strict]) { return str != '0' && str != 'false' && str != ''; } - /// trim characters (whitespace by default) from both sides of the input String trim(String str, [String chars]) { - RegExp pattern = (chars != null) ? new RegExp('^[$chars]+|[$chars]+\$') : new RegExp(r'^\s+|\s+$'); + RegExp pattern = (chars != null) + ? new RegExp('^[$chars]+|[$chars]+\$') + : new RegExp(r'^\s+|\s+$'); return str.replaceAll(pattern, ''); } - /// trim characters from the left-side of the input String ltrim(String str, [String chars]) { var pattern = chars != null ? new RegExp('^[$chars]+') : new RegExp(r'^\s+'); return str.replaceAll(pattern, ''); } - /// trim characters from the right-side of the input String rtrim(String str, [String chars]) { var pattern = chars != null ? new RegExp('[$chars]+\$') : new RegExp(r'\s+$'); return str.replaceAll(pattern, ''); } - /// remove characters that do not appear in the whitelist. /// /// The characters are used in a RegExp and so you will need to escape @@ -92,7 +85,6 @@ String whitelist(String str, String chars) { return str.replaceAll(new RegExp('[^' + chars + ']+'), ''); } - /// remove characters that appear in the blacklist. /// /// The characters are used in a RegExp and so you will need to escape @@ -101,27 +93,27 @@ String blacklist(String str, String chars) { return str.replaceAll(new RegExp('[' + chars + ']+'), ''); } - /// remove characters with a numerical value < 32 and 127. /// /// If `keep_new_lines` is `true`, newline characters are preserved /// `(\n and \r, hex 0xA and 0xD)`. String stripLow(String str, [bool keep_new_lines]) { - String chars = keep_new_lines == true ? '\x00-\x09\x0B\x0C\x0E-\x1F\x7F' : '\x00-\x1F\x7F'; + String chars = keep_new_lines == true + ? '\x00-\x09\x0B\x0C\x0E-\x1F\x7F' + : '\x00-\x1F\x7F'; return blacklist(str, chars); } - /// replace `<`, `>`, `&`, `'` and `"` with HTML entities String escape(String str) { - return (str.replaceAll(new RegExp(r'&'), '&') - .replaceAll(new RegExp(r'"'), '"') - .replaceAll(new RegExp(r"'"), ''') - .replaceAll(new RegExp(r'<'), '<') - .replaceAll(new RegExp(r'>'), '>')); + return (str + .replaceAll(new RegExp(r'&'), '&') + .replaceAll(new RegExp(r'"'), '"') + .replaceAll(new RegExp(r"'"), ''') + .replaceAll(new RegExp(r'<'), '<') + .replaceAll(new RegExp(r'>'), '>')); } - /// canonicalize an email address. /// /// `options` is an `Map` which defaults to @@ -136,19 +128,19 @@ String escape(String str) { String normalizeEmail(String email, [Map options]) { options = _merge(options, _default_normalize_email_options); if (isEmail(email) == false) { - return ''; + return ''; } List parts = email.split('@'); parts[1] = parts[1].toLowerCase(); if (options['lowercase'] == true) { - parts[0] = parts[0].toLowerCase(); + parts[0] = parts[0].toLowerCase(); } if (parts[1] == 'gmail.com' || parts[1] == 'googlemail.com') { if (options['lowercase'] == false) { - parts[0] = parts[0].toLowerCase(); + parts[0] = parts[0].toLowerCase(); } parts[0] = parts[0].replaceAll('\.', '').split('+')[0]; parts[1] = 'gmail.com'; diff --git a/lib/src/validator.dart b/lib/src/validator.dart index 20215b4..dbcd19c 100644 --- a/lib/src/validator.dart +++ b/lib/src/validator.dart @@ -1,9 +1,12 @@ part of validator; -RegExp _email = new RegExp(r"^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$"); + RegExp _email = new RegExp( + r"^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$"); -RegExp _ipv4Maybe = new RegExp(r'^(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)$'); -RegExp _ipv6 = new RegExp(r'^::|^::1|^([a-fA-F0-9]{1,4}::?){1,7}([a-fA-F0-9]{1,4})$'); +RegExp _ipv4Maybe = + new RegExp(r'^(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)$'); +RegExp _ipv6 = + new RegExp(r'^::|^::1|^([a-fA-F0-9]{1,4}::?){1,7}([a-fA-F0-9]{1,4})$'); RegExp _surrogatePairsRegExp = new RegExp(r'[\uD800-\uDBFF][\uDC00-\uDFFF]'); @@ -11,55 +14,142 @@ RegExp _alpha = new RegExp(r'^[a-zA-Z]+$'); RegExp _alphanumeric = new RegExp(r'^[a-zA-Z0-9]+$'); RegExp _numeric = new RegExp(r'^-?[0-9]+$'); RegExp _int = new RegExp(r'^(?:-?(?:0|[1-9][0-9]*))$'); -RegExp _float = new RegExp(r'^(?:-?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$'); +RegExp _float = + new RegExp(r'^(?:-?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$'); RegExp _hexadecimal = new RegExp(r'^[0-9a-fA-F]+$'); RegExp _hexcolor = new RegExp(r'^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$'); -RegExp _base64 = new RegExp(r'^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{4})$'); +RegExp _base64 = new RegExp( + r'^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{4})$'); -RegExp _creditCard = new RegExp(r'^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$'); +RegExp _creditCard = new RegExp( + r'^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$'); RegExp _isbn10Maybe = new RegExp(r'^(?:[0-9]{9}X|[0-9]{10})$'); RegExp _isbn13Maybe = new RegExp(r'^(?:[0-9]{13})$'); -Map _uuid = { - '3': new RegExp(r'^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$'), - '4': new RegExp(r'^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$'), - '5': new RegExp(r'^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$'), - 'all': new RegExp(r'^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$') +final Map _uuid = { + '3': new RegExp( + r'^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$'), + '4': new RegExp( + r'^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$'), + '5': new RegExp( + r'^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$'), + 'all': new RegExp( + r'^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$') }; RegExp _multibyte = new RegExp(r'[^\x00-\x7F]'); RegExp _ascii = new RegExp(r'^[\x00-\x7F]+$'); -RegExp _fullWidth = new RegExp(r'[^\u0020-\u007E\uFF61-\uFF9F\uFFA0-\uFFDC\uFFE8-\uFFEE0-9a-zA-Z]'); -RegExp _halfWidth = new RegExp(r'[\u0020-\u007E\uFF61-\uFF9F\uFFA0-\uFFDC\uFFE8-\uFFEE0-9a-zA-Z]'); - +RegExp _fullWidth = new RegExp( + r'[^\u0020-\u007E\uFF61-\uFF9F\uFFA0-\uFFDC\uFFE8-\uFFEE0-9a-zA-Z]'); +RegExp _halfWidth = new RegExp( + r'[\u0020-\u007E\uFF61-\uFF9F\uFFA0-\uFFDC\uFFE8-\uFFEE0-9a-zA-Z]'); + +RegExp _md5 = new RegExp(r'^[a-fA-F0-9]{32}$'); +RegExp _md4 = new RegExp(r'^[a-fA-F0-9]{32}$'); +RegExp _sha1 = new RegExp(r'^[a-fA-F0-9]{40}$'); +RegExp _sha256 = new RegExp(r'^[a-fA-F0-9]{64}$'); +RegExp _sha384 = new RegExp(r'^[a-fA-F0-9]{96}$'); +RegExp _sha512 = new RegExp(r'^[a-fA-F0-9]{128}$'); +RegExp _ripemd128 = new RegExp(r'^[a-fA-F0-9]{32}$'); +RegExp _ripemd160 = new RegExp(r'^[a-fA-F0-9]{40}$'); +RegExp _tiger128 = new RegExp(r'^[a-fA-F0-9]{32}$'); +RegExp _tiger160 = new RegExp(r'^[a-fA-F0-9]{40}$'); +RegExp _tiger192 = new RegExp(r'^[a-fA-F0-9]{48}$'); +RegExp _crc32 = new RegExp(r'^[a-fA-F0-9]{8}$'); +RegExp _crc32b = new RegExp(r'^[a-fA-F0-9]{8}$'); + +RegExp _macAddress = new RegExp(r'^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$'); + +final Map _phones = { + 'ar-AE': new RegExp(r'^((\+?971)|0)?5[024568]\d{7}$'), + 'ar-DZ': new RegExp(r'^(\+?213|0)(5|6|7)\d{8}$'), + 'ar-EG': new RegExp(r'^((\+?20)|0)?1[012]\d{8}$'), + 'ar-JO': new RegExp(r'^(\+?962|0)?7[789]\d{7}$'), + 'ar-SA': new RegExp(r'^(!?(\+?966)|0)?5\d{8}$'), + 'ar-SY': new RegExp(r'^(!?(\+?963)|0)?9\d{8}$'), + 'cs-CZ': new RegExp(r'^(\+?420)? ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$'), + 'da-DK': new RegExp(r'^(\+?45)?\s?\d{2}\s?\d{2}\s?\d{2}\s?\d{2}$'), + 'de-DE': new RegExp( + r'^(\+?49[ \.\-])?([\(]{1}[0-9]{1,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$'), + 'el-GR': new RegExp(r'^(\+?30|0)?(69\d{8})$'), + 'en-AU': new RegExp(r'^(\+?61|0)4\d{8}$'), + 'en-GB': new RegExp(r'^(\+?44|0)7\d{9}$'), + 'en-CA': new RegExp(r'^(\+?1)?[2-9]\d{2}[2-9](?!11)\d{6}$'), + 'en-HK': new RegExp(r'^(\+?852\-?)?[456789]\d{3}\-?\d{4}$'), + 'en-IN': new RegExp(r'^(\+?91|0)?[789]\d{9}$'), + 'en-KE': new RegExp(r'^(\+?254|0)?[7]\d{8}$'), + 'en-NG': new RegExp(r'^(\+?234|0)?[789]\d{9}$'), + 'en-NZ': new RegExp(r'^(\+?64|0)2\d{7,9}$'), + 'en-PK': new RegExp( + r'^((\+92)|(0092))-{0,1}\d{3}-{0,1}\d{7}$|^\d{11}$|^\d{4}-\d{7}$'), + 'en-RW': new RegExp(r'^(\+?250|0)?[7]\d{8}$'), + 'en-SG': new RegExp(r'^(\+65)?[89]\d{7}$'), + 'en-TZ': new RegExp(r'^(\+?255|0)?[67]\d{8}$'), + 'en-UG': new RegExp(r'^(\+?256|0)?[7]\d{8}$'), + 'en-US': new RegExp(r'^(\+?1)?[2-9]\d{2}[2-9](?!11)\d{6}$'), + 'en-ZA': new RegExp(r'^(\+?27|0)\d{9}$'), + 'en-ZM': new RegExp(r'^(\+?26)?09[567]\d{7}$'), + 'es-ES': new RegExp(r'^(\+?34)?(6\d{1}|7[1234])\d{7}$'), + 'et-EE': new RegExp(r'^(\+?372)?\s?(5|8[1-4])\s?([0-9]\s?){6,7}$'), + 'fa-IR': new RegExp(r'^(\+?98[\-\s]?|0)9[0-39]\d[\-\s]?\d{3}[\-\s]?\d{4}$'), + 'fi-FI': new RegExp(r'^(\+?358|0)\s?(4(0|1|2|4|5|6)?|50)\s?(\d\s?){4,8}\d$'), + 'fo-FO': new RegExp(r'^(\+?298)?\s?\d{2}\s?\d{2}\s?\d{2}$'), + 'fr-BE': new RegExp(r'^(\+?32|0)4?\d{8}$'), + 'fr-FR': new RegExp(r'^(\+?33|0)[67]\d{8}$'), + 'he-IL': new RegExp(r'^(\+972|0)([23489]|5[0248]|77)[1-9]\d{6}'), + 'hu-HU': new RegExp(r'^(\+?36)(20|30|70)\d{7}$'), + 'id-ID': new RegExp(r'^(\+?62|0[1-9])[\s|\d]+$'), + 'it-IT': new RegExp(r'^(\+?39)?\s?3\d{2} ?\d{6,7}$'), + 'ja-JP': new RegExp(r'^(\+?81|0)[789]0[ \-]?[1-9]\d{2}[ \-]?\d{5}$'), + 'kl-GL': new RegExp(r'^(\+?299)?\s?\d{2}\s?\d{2}\s?\d{2}$'), + 'ko-KR': new RegExp( + r'^((\+?82)[ \-]?)?0?1([0|1|6|7|8|9]{1})[ \-]?\d{3,4}[ \-]?\d{4}$'), + 'lt-LT': new RegExp(r'^(\+370|8)\d{8}$'), + 'ms-MY': new RegExp( + r'^(\+?6?01){1}(([145]{1}(\-|\s)?\d{7,8})|([236789]{1}(\s|\-)?\d{7}))$'), + 'nb-NO': new RegExp(r'^(\+?47)?[49]\d{7}$'), + 'nl-BE': new RegExp(r'^(\+?32|0)4?\d{8}$'), + 'nn-NO': new RegExp(r'^(\+?47)?[49]\d{7}$'), + 'pl-PL': new RegExp(r'^(\+?48)? ?[5-8]\d ?\d{3} ?\d{2} ?\d{2}$'), + 'pt-BR': new RegExp(r'^(\+?55|0)\-?[1-9]{2}\-?[2-9]{1}\d{3,4}\-?\d{4}$'), + 'pt-PT': new RegExp(r'^(\+?351)?9[1236]\d{7}$'), + 'ro-RO': + new RegExp(r'^(\+?4?0)\s?7\d{2}(\/|\s|\.|\-)?\d{3}(\s|\.|\-)?\d{3}$'), + 'ru-RU': new RegExp(r'^(\+?7|8)?9\d{9}$'), + 'sk-SK': new RegExp(r'^(\+?421)? ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$'), + 'sr-RS': new RegExp(r'^(\+3816|06)[- \d]{5,9}$'), + 'tr-TR': new RegExp(r'^(\+?90|0)?5\d{9}$'), + 'uk-UA': new RegExp(r'^(\+?38|8)?0\d{9}$'), + 'vi-VN': new RegExp( + r'^(\+?84|0)?((1(2([0-9])|6([2-9])|88|99))|(9((?!5)[0-9])))([0-9]{7})$'), + 'zh-CN': new RegExp(r'^(\+?0?86\-?)?1[345789]\d{9}$'), + 'zh-HK': new RegExp(r'^(\+?852\-?)?[456789]\d{3}\-?\d{4}$'), + 'zh-TW': new RegExp(r'^(\+?886\-?|0)?9\d{8}$') +}; /// check if the string matches the comparison -bool equals(String str, comparison) { +bool equals(final String str, comparison) { return str == comparison.toString(); } - /// check if the string contains the seed -bool contains(String str, seed) { +bool contains(final String str, seed) { return str.indexOf(seed.toString()) >= 0; } - /// check if string matches the pattern. -bool matches(String str, pattern) { +bool matches(final String str, final String pattern) { RegExp re = new RegExp(pattern); return re.hasMatch(str); } - /// check if the string is an email -bool isEmail(String str) { +bool isEmail(final String str) { return _email.hasMatch(str.toLowerCase()); } - /// check if the string is a URL /// /// `options` is a `Map` which defaults to @@ -67,13 +157,15 @@ bool isEmail(String str) { /// 'require_protocol': false, 'allow_underscores': false, /// 'host_whitelist': false, 'host_blacklist': false }`. bool isURL(String str, [Map options]) { - if (str == null || str.length == 0 || str.length > 2083 || + if (str == null || + str.length == 0 || + str.length > 2083 || str.indexOf('mailto:') == 0) { return false; } - Map default_url_options = { - 'protocols': [ 'http', 'https', 'ftp' ], + final Map default_url_options = { + 'protocols': ['http', 'https', 'ftp'], 'require_tld': true, 'require_protocol': false, 'allow_underscores': false @@ -81,13 +173,10 @@ bool isURL(String str, [Map options]) { options = _merge(options, default_url_options); - var protocol, user, pass, auth, host, hostname, port, port_str, path, query, - hash, split; - // check protocol - split = str.split('://'); + List split = str.split('://'); if (split.length > 1) { - protocol = _shift(split); + String protocol = _shift(split); if (options['protocols'].indexOf(protocol) == -1) { return false; } @@ -99,7 +188,7 @@ bool isURL(String str, [Map options]) { // check hash split = str.split('#'); str = _shift(split); - hash = split.join('#'); + String hash = split.join('#'); if (hash != null && hash != "" && new RegExp(r'\s').hasMatch(hash)) { return false; } @@ -107,7 +196,7 @@ bool isURL(String str, [Map options]) { // check query params split = str.split('?'); str = _shift(split); - query = split.join('?'); + String query = split.join('?'); if (query != null && query != "" && new RegExp(r'\s').hasMatch(query)) { return false; } @@ -115,7 +204,7 @@ bool isURL(String str, [Map options]) { // check path split = str.split('/'); str = _shift(split); - path = split.join('/'); + String path = split.join('/'); if (path != null && path != "" && new RegExp(r'\s').hasMatch(path)) { return false; } @@ -123,14 +212,13 @@ bool isURL(String str, [Map options]) { // check auth type urls split = str.split('@'); if (split.length > 1) { - auth = _shift(split); + String auth = _shift(split); if (auth.indexOf(':') >= 0) { - auth = auth.split(':'); - user = _shift(auth); + List authSplit = auth.split(':'); + String user = _shift(authSplit); if (!new RegExp(r'^\S+$').hasMatch(user)) { return false; } - pass = auth.join(':'); if (!new RegExp(r'^\S*$').hasMatch(user)) { return false; } @@ -138,17 +226,19 @@ bool isURL(String str, [Map options]) { } // check hostname - hostname = split.join('@'); + String hostname = split.join('@'); split = hostname.split(':'); - host = _shift(split); + String host = _shift(split); if (split.length > 0) { - port_str = split.join(':'); + int port; + String port_str = split.join(':'); try { port = int.parse(port_str, radix: 10); } catch (e) { return false; } - if (!new RegExp(r'^[0-9]+$').hasMatch(port_str) || port <= 0 || + if (!new RegExp(r'^[0-9]+$').hasMatch(port_str) || + port <= 0 || port > 65535) { return false; } @@ -169,10 +259,8 @@ bool isURL(String str, [Map options]) { } return true; - } - /// check if the string is an IP (version 4 or 6) /// /// `version` is a String or an `int`. @@ -191,12 +279,11 @@ bool isIP(String str, [version]) { return version == '6' && _ipv6.hasMatch(str); } - /// check if the string is a fully qualified domain name (e.g. domain.com). /// /// `options` is a `Map` which defaults to `{ 'require_tld': true, 'allow_underscores': false }`. -bool isFQDN(str, [options]) { - Map default_fqdn_options = { +bool isFQDN(String str, [options]) { + final Map default_fqdn_options = { 'require_tld': true, 'allow_underscores': false }; @@ -204,14 +291,14 @@ bool isFQDN(str, [options]) { options = _merge(options, default_fqdn_options); List parts = str.split('.'); if (options['require_tld']) { - var tld = parts.removeLast(); + String tld = parts.removeLast(); if (parts.length == 0 || !new RegExp(r'^[a-z]{2,}$').hasMatch(tld)) { return false; } } - for (var part, i = 0; i < parts.length; i++) { - part = parts[i]; + for (int i = 0; i < parts.length; i++) { + String part = parts[i]; if (options['allow_underscores']) { if (part.indexOf('__') >= 0) { return false; @@ -220,7 +307,8 @@ bool isFQDN(str, [options]) { if (!new RegExp(r'^[a-z\\u00a1-\\uffff0-9-]+$').hasMatch(part)) { return false; } - if (part[0] == '-' || part[part.length - 1] == '-' || + if (part[0] == '-' || + part[part.length - 1] == '-' || part.indexOf('---') >= 0) { return false; } @@ -228,103 +316,88 @@ bool isFQDN(str, [options]) { return true; } - /// check if the string contains only letters (a-zA-Z). -bool isAlpha(String str) { +bool isAlpha(final String str) { return _alpha.hasMatch(str); } - /// check if the string contains only numbers -bool isNumeric(String str) { +bool isNumeric(final String str) { return _numeric.hasMatch(str); } - /// check if the string contains only letters and numbers -bool isAlphanumeric(String str) { +bool isAlphanumeric(final String str) { return _alphanumeric.hasMatch(str); } - /// check if a string is base64 encoded -bool isBase64(String str) { +bool isBase64(final String str) { return _base64.hasMatch(str); } - /// check if the string is an integer -bool isInt(String str) { +bool isInt(final String str) { return _int.hasMatch(str); } - /// check if the string is a float -bool isFloat(String str) { +bool isFloat(final String str) { return _float.hasMatch(str); } - /// check if the string is a hexadecimal number -bool isHexadecimal(String str) { +bool isHexadecimal(final String str) { return _hexadecimal.hasMatch(str); } - /// check if the string is a hexadecimal color -bool isHexColor(String str) { +bool isHexColor(final String str) { return _hexcolor.hasMatch(str); } - /// check if the string is lowercase -bool isLowercase(String str) { +bool isLowercase(final String str) { return str == str.toLowerCase(); } - /// check if the string is uppercase -bool isUppercase(String str) { +bool isUppercase(final String str) { return str == str.toUpperCase(); } - /// check if the string is a number that's divisible by another /// /// [n] is a String or an int. -bool isDivisibleBy(String str, n) { +bool isDivisibleBy(final String str, n) { try { return double.parse(str) % int.parse(n) == 0; - } catch(e) { + } catch (e) { return false; } } - /// check if the string is null -bool isNull(String str) { +bool isNull(final String str) { return str == null || str.length == 0; } - /// check if the string's length falls in a range /// /// Note: this function takes into account surrogate pairs. -bool isLength(String str, int min, [int max]) { +bool isLength(final String str, int min, [int max]) { List surrogatePairs = _surrogatePairsRegExp.allMatches(str).toList(); int len = str.length - surrogatePairs.length; return len >= min && (max == null || len <= max); } - /// check if the string's length (in bytes) falls in a range. -bool isByteLength(String str, int min, [int max]) { +bool isByteLength(final String str, int min, [int max]) { return str.length >= min && (max == null || str.length <= max); } - /// check if the string is a UUID (version 3, 4 or 5). -bool isUUID(String str, [version]) { +bool isUUID(final String str, [version]) { if (version == null) { version = 'all'; } else { @@ -335,9 +408,8 @@ bool isUUID(String str, [version]) { return (pat != null && pat.hasMatch(str.toUpperCase())); } - /// check if the string is a date -bool isDate(String str) { +bool isDate(final String str) { try { DateTime.parse(str); return true; @@ -346,11 +418,10 @@ bool isDate(String str) { } } - /// check if the string is a date that's after the specified date /// /// If `date` is not passed, it defaults to now. -bool isAfter(String str, [date]) { +bool isAfter(final String str, [dynamic date]) { if (date == null) { date = new DateTime.now(); } else if (isDate(date)) { @@ -362,7 +433,7 @@ bool isAfter(String str, [date]) { DateTime str_date; try { str_date = DateTime.parse(str); - } catch(e) { + } catch (e) { return false; } @@ -372,7 +443,7 @@ bool isAfter(String str, [date]) { /// check if the string is a date that's before the specified date /// /// If `date` is not passed, it defaults to now. -bool isBefore(String str, [date]) { +bool isBefore(final String str, [dynamic date]) { if (date == null) { date = new DateTime.now(); } else if (isDate(date)) { @@ -384,16 +455,15 @@ bool isBefore(String str, [date]) { DateTime str_date; try { str_date = DateTime.parse(str); - } catch(e) { + } catch (e) { return false; } return str_date.isBefore(date); } - /// check if the string is in a array of allowed values -bool isIn(String str, values) { +bool isIn(final String str, dynamic values) { if (values == null || values.length == 0) { return false; } @@ -405,9 +475,8 @@ bool isIn(String str, values) { return values.indexOf(str) >= 0; } - /// check if the string is a credit card -bool isCreditCard(String str) { +bool isCreditCard(final String str) { String sanitized = str.replaceAll(new RegExp(r'[^0-9]+'), ''); if (!_creditCard.hasMatch(sanitized)) { return false; @@ -423,14 +492,14 @@ bool isCreditCard(String str) { int tmpNum = int.parse(digit); if (shouldDouble == true) { - tmpNum *= 2; - if (tmpNum >= 10) { - sum += ((tmpNum % 10) + 1); - } else { - sum += tmpNum; - } - } else { + tmpNum *= 2; + if (tmpNum >= 10) { + sum += ((tmpNum % 10) + 1); + } else { sum += tmpNum; + } + } else { + sum += tmpNum; } shouldDouble = !shouldDouble; } @@ -438,9 +507,8 @@ bool isCreditCard(String str) { return (sum % 10 == 0); } - /// check if the string is an ISBN (version 10 or 13) -bool isISBN(String str, [version]) { +bool isISBN(final String str, [dynamic version]) { if (version == null) { return isISBN(str, '10') || isISBN(str, '13'); } @@ -477,9 +545,8 @@ bool isISBN(String str, [version]) { return false; } - /// check if the string is valid JSON -bool isJSON(str) { +bool isJSON(final String str) { try { JSON.decode(str); } catch (e) { @@ -488,44 +555,131 @@ bool isJSON(str) { return true; } - /// check if the string contains one or more multibyte chars -bool isMultibyte(String str) { +bool isMultibyte(final String str) { return _multibyte.hasMatch(str); } - /// check if the string contains ASCII chars only -bool isAscii(String str) { +bool isAscii(final String str) { return _ascii.hasMatch(str); } - /// check if the string contains any full-width chars -bool isFullWidth(String str) { +bool isFullWidth(final String str) { return _fullWidth.hasMatch(str); } - /// check if the string contains any half-width chars -bool isHalfWidth(String str) { +bool isHalfWidth(final String str) { return _halfWidth.hasMatch(str); } - /// check if the string contains a mixture of full and half-width chars -bool isVariableWidth(String str) { +bool isVariableWidth(final String str) { return isFullWidth(str) && isHalfWidth(str); } - /// check if the string contains any surrogate pairs chars -bool isSurrogatePair(String str) { +bool isSurrogatePair(final String str) { return _surrogatePairsRegExp.hasMatch(str); } - /// check if the string is a valid hex-encoded representation of a MongoDB ObjectId -bool isMongoId(String str) { +bool isMongoId(final String str) { return (isHexadecimal(str) && str.length == 24); -} \ No newline at end of file +} + +/// check if the string is a MD5 hash +bool isMd5(final String str) { + return _md5.hasMatch(str); +} + +/// check if the string is a MD5 hash +bool isMd4(final String str) { + return _md4.hasMatch(str); +} + +/// check if the string is a sha1 hash +bool isSha1(final String str) { + return _sha1.hasMatch(str); +} + +/// check if the string is a sha256 hash +bool isSha256(final String str) { + return _sha256.hasMatch(str); +} + +/// check if the string is a sha384 hash +bool isSha384(final String str) { + return _sha384.hasMatch(str); +} + +/// check if the string is a sha512 hash +bool isSha512(final String str) { + return _sha512.hasMatch(str); +} + +/// check if the string is a ripemd128 hash +bool isRipemd128(final String str) { + return _ripemd128.hasMatch(str); +} + +/// check if the string is a ripemd160 hash +bool isRipemd160(final String str) { + return _ripemd160.hasMatch(str); +} + +/// check if the string is a tiger128 hash +bool isTiger128(final String str) { + return _tiger128.hasMatch(str); +} + +/// check if the string is a tiger160 hash +bool isTiger160(final String str) { + return _tiger160.hasMatch(str); +} + +/// check if the string is a tiger192 hash +bool isTiger192(final String str) { + return _tiger192.hasMatch(str); +} + +/// check if the string is a crc32 hash +bool isCrc32(final String str) { + return _crc32.hasMatch(str); +} + +/// check if the string is a crc32b hash +bool isCrc32b(final String str) { + return _crc32b.hasMatch(str); +} + +/// check if the string is a valid port number +bool isPort(final String str) { + int value = int.parse(str, onError: (source) => -1); + return value >= 0 && value <= 65535; +} + +/// check if the string is a MAC address +bool isMACAddress(final String str) { + return _macAddress.hasMatch(str); +} + +/// check if the string is a mobile phone number +bool isMobilePhone(final String str, final String locale) { + if (_phones.containsKey(locale)) { + return _phones[locale].hasMatch(str); + } else if (locale == 'any') { + _phones.forEach((key, value) { + if (_phones.containsKey(key)) { + RegExp phone = _phones[key]; + if (phone.hasMatch(str) == true) { + return true; + } + } + }); + return false; + } + throw new Exception('Invalid locale ${locale}'); +} diff --git a/test/validator_test.dart b/test/validator_test.dart index cab32fb..21b363c 100644 --- a/test/validator_test.dart +++ b/test/validator_test.dart @@ -885,6 +885,1397 @@ void testIsMongoId() { }); } +void testIsMd5() { + test({ + 'validator': v.isMd5, + 'args': [], + 'valid': [ + 'd94f3f016ae679c3008de268209132f2', + '751adbc511ccbe8edf23d486fa4581cd', + '88dae00e614d8f24cfd5a8b3f8002e93', + '0bf1c35032a71a14c2f719e5a14c1e96', + ], + 'invalid': [ + 'KYT0bf1c35032a71a14c2f719e5a14c1', + 'q94375dj93458w34', + '39485729348', + '%&FHKJFvk', + ] + }); +} + +void testIsMd4() { + test({ + 'validator': v.isMd4, + 'args': [], + 'valid': [ + 'd94f3f016ae679c3008de268209132f2', + '751adbc511ccbe8edf23d486fa4581cd', + '88dae00e614d8f24cfd5a8b3f8002e93', + '0bf1c35032a71a14c2f719e5a14c1e96', + ], + 'invalid': [ + 'KYT0bf1c35032a71a14c2f719e5a14c1', + 'q94375dj93458w34', + '39485729348', + '%&FHKJFvk', + ], + }); +} + +void testIsRipemd128() { + test({ + 'validator': v.isMd4, + 'args': [], + 'valid': [ + 'd94f3f016ae679c3008de268209132f2', + '751adbc511ccbe8edf23d486fa4581cd', + '88dae00e614d8f24cfd5a8b3f8002e93', + '0bf1c35032a71a14c2f719e5a14c1e96', + ], + 'invalid': [ + 'KYT0bf1c35032a71a14c2f719e5a14c1', + 'q94375dj93458w34', + '39485729348', + '%&FHKJFvk', + ], + }); +} + +void testIsTiger128() { + test({ + 'validator': v.isTiger128, + 'args': [], + 'valid': [ + 'd94f3f016ae679c3008de268209132f2', + '751adbc511ccbe8edf23d486fa4581cd', + '88dae00e614d8f24cfd5a8b3f8002e93', + '0bf1c35032a71a14c2f719e5a14c1e96', + ], + 'invalid': [ + 'KYT0bf1c35032a71a14c2f719e5a14c1', + 'q94375dj93458w34', + '39485729348', + '%&FHKJFvk', + ], + }); +} + +void testIsCrc32() { + test({ + 'validator': v.isCrc32, + 'args':[], + 'valid': [ + 'd94f3f01', + '751adbc5', + '88dae00e', + '0bf1c350', + ], + 'invalid': [ + 'KYT0bf1c35032a71a14c2f719e5a14c1', + 'q94375dj93458w34', + 'q943', + '39485729348', + '%&FHKJFvk', + ], + }); +} + +void testIsCrc32b() { + test({ + 'validator': v.isCrc32b, + 'args': [], + 'valid': [ + 'd94f3f01', + '751adbc5', + '88dae00e', + '0bf1c350', + ], + 'invalid': [ + 'KYT0bf1c35032a71a14c2f719e5a14c1', + 'q94375dj93458w34', + 'q943', + '39485729348', + '%&FHKJFvk', + ], + }); +} + +void testIsSha1() { + test({ + 'validator': v.isSha1, + 'args': [], + 'valid': [ + '3ca25ae354e192b26879f651a51d92aa8a34d8d3', + 'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d', + 'beb8c3f30da46be179b8df5f5ecb5e4b10508230', + 'efd5d3b190e893ed317f38da2420d63b7ae0d5ed', + ], + 'invalid': [ + 'KYT0bf1c35032a71a14c2f719e5a14c1', + 'KYT0bf1c35032a71a14c2f719e5a14c1dsjkjkjkjkkjk', + 'q94375dj93458w34', + '39485729348', + '%&FHKJFvk', + ], + }); +} + +void testIsTiger160() { + test({ + 'validator': v.isTiger160, + 'args': [], + 'valid': [ + '3ca25ae354e192b26879f651a51d92aa8a34d8d3', + 'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d', + 'beb8c3f30da46be179b8df5f5ecb5e4b10508230', + 'efd5d3b190e893ed317f38da2420d63b7ae0d5ed', + ], + 'invalid': [ + 'KYT0bf1c35032a71a14c2f719e5a14c1', + 'KYT0bf1c35032a71a14c2f719e5a14c1dsjkjkjkjkkjk', + 'q94375dj93458w34', + '39485729348', + '%&FHKJFvk', + ], + }); +} + +void testIsRipemd160() { + test({ + 'validator': v.isRipemd160, + 'args': [], + 'valid': [ + '3ca25ae354e192b26879f651a51d92aa8a34d8d3', + 'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d', + 'beb8c3f30da46be179b8df5f5ecb5e4b10508230', + 'efd5d3b190e893ed317f38da2420d63b7ae0d5ed', + ], + 'invalid': [ + 'KYT0bf1c35032a71a14c2f719e5a14c1', + 'KYT0bf1c35032a71a14c2f719e5a14c1dsjkjkjkjkkjk', + 'q94375dj93458w34', + '39485729348', + '%&FHKJFvk', + ], + }); +} + +void testIsSha256() { + test({ + 'validator': v.isSha256, + 'args': [], + 'valid': [ + '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824', + '1d996e033d612d9af2b44b70061ee0e868bfd14c2dd90b129e1edeb7953e7985', + '80f70bfeaed5886e33536bcfa8c05c60afef5a0e48f699a7912d5e399cdcc441', + '579282cfb65ca1f109b78536effaf621b853c9f7079664a3fbe2b519f435898c', + ], + 'invalid': [ + 'KYT0bf1c35032a71a14c2f719e5a14c1', + 'KYT0bf1c35032a71a14c2f719e5a14c1dsjkjkjkjkkjk', + 'q94375dj93458w34', + '39485729348', + '%&FHKJFvk', + ], + }); +} + +void testIsSha384() { + test({ + 'validator': v.isSha384, + 'args': [], + 'valid': [ + '3fed1f814d28dc5d63e313f8a601ecc4836d1662a19365cbdcf6870f6b56388850b58043f7ebf2418abb8f39c3a42e31', + 'b330f4e575db6e73500bd3b805db1a84b5a034e5d21f0041d91eec85af1dfcb13e40bb1c4d36a72487e048ac6af74b58', + 'bf547c3fc5841a377eb1519c2890344dbab15c40ae4150b4b34443d2212e5b04aa9d58865bf03d8ae27840fef430b891', + 'fc09a3d11368386530f985dacddd026ae1e44e0e297c805c3429d50744e6237eb4417c20ffca8807b071823af13a3f65', + ], + 'invalid': [ + 'KYT0bf1c35032a71a14c2f719e5a14c1', + 'KYT0bf1c35032a71a14c2f719e5a14c1dsjkjkjkjkkjk', + 'q94375dj93458w34', + '39485729348', + '%&FHKJFvk', + ], + }); +} + +void testIsSha512() { + test({ + 'validator': v.isSha512, + 'args': [], + 'valid': [ + '9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043', + '83c586381bf5ba94c8d9ba8b6b92beb0997d76c257708742a6c26d1b7cbb9269af92d527419d5b8475f2bb6686d2f92a6649b7f174c1d8306eb335e585ab5049', + '45bc5fa8cb45ee408c04b6269e9f1e1c17090c5ce26ffeeda2af097735b29953ce547e40ff3ad0d120e5361cc5f9cee35ea91ecd4077f3f589b4d439168f91b9', + '432ac3d29e4f18c7f604f7c3c96369a6c5c61fc09bf77880548239baffd61636d42ed374f41c261e424d20d98e320e812a6d52865be059745fdb2cb20acff0ab', + ], + 'invalid': [ + 'KYT0bf1c35032a71a14c2f719e5a14c1', + 'KYT0bf1c35032a71a14c2f719e5a14c1dsjkjkjkjkkjk', + 'q94375dj93458w34', + '39485729348', + '%&FHKJFvk', + ], + }); +} + +void testIsTiger192() { + test({ + 'validator': v.isTiger192, + 'args': [], + 'valid': [ + '6281a1f098c5e7290927ed09150d43ff3990a0fe1a48267c', + '56268f7bc269cf1bc83d3ce42e07a85632394737918f4760', + '46fc0125a148788a3ac1d649566fc04eb84a746f1a6e4fa7', + '7731ea1621ae99ea3197b94583d034fdbaa4dce31a67404a', + ], + 'invalid': [ + 'KYT0bf1c35032a71a14c2f719e5a14c1', + 'KYT0bf1c35032a71a14c2f719e5a14c1dsjkjkjkjkkjk', + 'q94375dj93458w34', + '39485729348', + '%&FHKJFvk', + ], + }); +} + +void testIsPort() { + test({ + 'validator': v.isPort, + 'args': [], + 'valid': [ + '0', + '22', + '80', + '443', + '3000', + '8080', + '65535', + ], + 'invalid': [ + '', + '-1', + '65536', + ], + }); +} + +void testIsMACAddress() { + test({ + 'validator': v.isMACAddress, + 'args': [], + 'valid': [ + 'ab:ab:ab:ab:ab:ab', + 'FF:FF:FF:FF:FF:FF', + '01:02:03:04:05:ab', + '01:AB:03:04:05:06', + ], + 'invalid': [ + 'abc', + '01:02:03:04:05', + '01:02:03:04::ab', + '1:2:3:4:5:6', + 'AB:CD:EF:GH:01:02', + ], + }); +} + +void testIsMobilePhone() { + test({ + 'validator': v.isMobilePhone, + 'args': ['ar-AE'], + 'valid': [ + '+971502674453', + '+971521247658', + '+971541255684', + '+971555454458', + '+971561498855', + '+971585215778', + '971585215778', + '0585215778', + '585215778', + ], + 'invalid': [ + '12345', + '+971511498855', + '+9715614988556', + '+9745614988556', + '', + '+9639626626262', + '+963332210972', + '0114152198', + '962796477263', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['ar-EG'], + 'valid': [ + '+201004513789', + '+201111453489', + '+201221204610', + '+201144621154', + '+201200124304', + '+201011201564', + '+201124679001', + '+201064790156', + '+201274652177', + '+201280134679', + '+201090124576', + '201090124576', + '01090124576', + '1090124576', + ], + 'invalid': [ + '+221004513789', + '+201404513789', + '12345', + '', + '+9639626626262', + '+963332210972', + '0114152198', + '962796477263', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['ar-JO'], + 'valid': [ + '0796477263', + '0777866254', + '0786725261', + '+962796477263', + '+962777866254', + '+962786725261', + '962796477263', + '962777866254', + '962786725261', + ], + 'invalid': [ + '00962786725261', + '00962796477263', + '12345', + '', + '+9639626626262', + '+963332210972', + '0114152198', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['ar-SY'], + 'valid': [ + '0944549710', + '+963944549710', + '956654379', + '0944549710', + '0962655597', + ], + 'invalid': [ + '12345', + '', + '+9639626626262', + '+963332210972', + '0114152198', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['ar-SA'], + 'valid': [ + '0556578654', + '+966556578654', + '966556578654', + '596578654', + '572655597', + ], + 'invalid': [ + '12345', + '', + '+9665626626262', + '+96633221097', + '0114152198', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['cs-CZ'], + 'valid': [ + '+420 123 456 789', + '+420 123456789', + '+420123456789', + '123 456 789', + '123456789', + ], + 'invalid': [ + '', + '+42012345678', + '+421 123 456 789', + '+420 023456789', + '+4201234567892', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['sk-SK'], + 'valid': [ + '+421 123 456 789', + '+421 123456789', + '+421123456789', + '123 456 789', + '123456789', + ], + 'invalid': [ + '', + '+42112345678', + '+422 123 456 789', + '+421 023456789', + '+4211234567892', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['de-DE'], + 'valid': [ + '+49 (0) 123 456 789', + '+49 (0) 123 456789', + '0123/4567890', + '+49 01234567890', + '01234567890', + ], + 'invalid': [ + '', + 'Vml2YW11cyBmZXJtZtesting123', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['pt-BR'], + 'valid': [ + '55-17-3332-2155', + '55-15-25661234', + '551223456789', + '01523456987', + '022995678947', + '+55-12-996551215', + ], + 'invalid': [ + '+017-123456789', + '5501599623874', + '+55012962308', + '+55-015-1234-3214', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['zh-CN'], + 'valid': [ + '15323456787', + '13523333233', + '13898728332', + '+086-13238234822', + '08613487234567', + '8617823492338', + '86-17823492338', + ], + 'invalid': [ + '12345', + '', + 'Vml2YW11cyBmZXJtZtesting123', + '010-38238383', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['zh-TW'], + 'valid': [ + '0987123456', + '+886987123456', + '886987123456', + '+886-987123456', + '886-987123456', + ], + 'invalid': [ + '12345', + '', + 'Vml2YW11cyBmZXJtZtesting123', + '0-987123456', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['en-ZA'], + 'valid': [ + '0821231234', + '+27821231234', + '27821231234', + ], + 'invalid': [ + '082123', + '08212312345', + '21821231234', + '+21821231234', + '+0821231234', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['en-AU'], + 'valid': [ + '61404111222', + '+61411222333', + '0417123456', + ], + 'invalid': [ + '082123', + '08212312345', + '21821231234', + '+21821231234', + '+0821231234', + '04123456789', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['en-HK'], + 'valid': [ + '91234567', + '9123-4567', + '61234567', + '51234567', + '+85291234567', + '+852-91234567', + '+852-9123-4567', + '852-91234567', + ], + 'invalid': [ + '999', + '+852-912345678', + '123456789', + '+852-1234-56789', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['en-KE'], + 'valid': [ + '+254728590432', + '+254733875610', + '254728590234', + '0733346543', + '0700459022', + ], + 'invalid': [ + '999', + '+25489032', + '123456789', + '+254800723845', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['en-UG'], + 'valid': [ + '+256728590432', + '+256733875610', + '256728590234', + '0773346543', + '0700459022', + ], + 'invalid': [ + '999', + '+25489032', + '123456789', + '+254800723845', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['en-RW'], + 'valid': [ + '+250728590432', + '+250733875610', + '250738590234', + '0753346543', + '0780459022', + ], + 'invalid': [ + '999', + '+254728590432', + '+25089032', + '123456789', + '+250800723845', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['en-TZ'], + 'valid': [ + '+255728590432', + '+255733875610', + '255628590234', + '0673346543', + '0600459022', + ], + 'invalid': [ + '999', + '+254728590432', + '+25589032', + '123456789', + '+255800723845' + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['fr-FR'], + 'valid': [ + '0612457898', + '+33612457898', + '33612457898', + '0712457898', + '+33712457898', + '33712457898', + ], + 'invalid': [ + '061245789', + '06124578980', + '0112457898', + '0212457898', + '0312457898', + '0412457898', + '0512457898', + '0812457898', + '0912457898', + '+34612457898', + '+336124578980', + '+3361245789', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['el-GR'], + 'valid': [ + '+306944848966', + '6944848966', + '306944848966', + ], + 'invalid': [ + '2102323234', + '+302646041461', + '120000000', + '20000000000', + '68129485729', + '6589394827', + '298RI89572', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['en-GB'], + 'valid': [ + '447789345856', + '+447861235675', + '07888814488', + ], + 'invalid': [ + '67699567', + '0773894868', + '077389f8688', + '+07888814488', + '0152456999', + '442073456754', + '+443003434751', + '05073456754', + '08001123123', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['en-SG'], + 'valid': [ + '87654321', + '98765432', + '+6587654321', + '+6598765432', + ], + 'invalid': [ + '987654321', + '876543219', + '8765432', + '9876543', + '12345678', + '+98765432', + '+9876543212', + '+15673628910', + '19876543210', + '8005552222', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['en-US'], + 'valid': [ + '19876543210', + '8005552222', + '+15673628910', + ], + 'invalid': [ + '564785', + '0123456789', + '1437439210', + '8009112340', + '+10345672645', + '11435213543', + '2436119753', + '16532116190', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['en-CA'], + 'valid': [ + '19876543210', + '8005552222', + '+15673628910', + ], + 'invalid': [ + '564785', + '0123456789', + '1437439210', + '8009112340', + '+10345672645', + '11435213543', + '2436119753', + '16532116190', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['en-ZM'], + 'valid': [ + '0956684590', + '0966684590', + '0976684590', + '+260956684590', + '+260966684590', + '+260976684590', + '260976684590', + ], + 'invalid': [ + '12345', + '', + 'Vml2YW11cyBmZXJtZtesting123', + '010-38238383', + '966684590', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['ru-RU'], + 'valid': [ + '+79676338855', + '79676338855', + '89676338855', + '9676338855', + ], + 'invalid': [ + '12345', + '', + 'Vml2YW11cyBmZXJtZtesting123', + '010-38238383', + '+9676338855', + '19676338855', + '6676338855', + '+99676338855', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['sr-RS'], + 'valid': [ + '0640133338', + '063333133', + '0668888878', + '+381645678912', + '+381611314000', + '0655885010', + ], + 'invalid': [ + '12345', + '', + 'Vml2YW11cyBmZXJtZtesting123', + '010-38238383', + '+9676338855', + '19676338855', + '6676338855', + '+99676338855', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['en-NZ'], + 'valid': [ + '+6427987035', + '642240512347', + '0293981646', + '029968425' + ], + 'invalid': [ + '12345', + '', + 'Vml2YW11cyBmZXJtZtesting123', + '010-38238383', + '+9676338855', + '19676338855', + '6676338855', + '+99676338855', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['nb-NO'], + 'valid': [ + '+4796338855', + '+4746338855', + '4796338855', + '4746338855', + '46338855', + '96338855', + ], + 'invalid': [ + '12345', + '', + 'Vml2YW11cyBmZXJtZtesting123', + '+4676338855', + '19676338855', + '+4726338855', + '4736338855', + '66338855', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['nn-NO'], + 'valid': [ + '+4796338855', + '+4746338855', + '4796338855', + '4746338855', + '46338855', + '96338855', + ], + 'invalid': [ + '12345', + '', + 'Vml2YW11cyBmZXJtZtesting123', + '+4676338855', + '19676338855', + '+4726338855', + '4736338855', + '66338855', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['vi-VN'], + 'valid': [ + '01636012403', + '+841636012403', + '1636012403', + '841636012403', + '+84999999999', + '84999999999', + '0999999999', + '999999999', + ], + 'invalid': [ + '12345', + '', + 'Vml2YW11cyBmZXJtZtesting123', + '010-38238383', + '260976684590', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['es-ES'], + 'valid': [ + '+34654789321', + '654789321', + '+34714789321', + '714789321', + '+34744789321', + '744789321', + ], + 'invalid': [ + '12345', + '', + 'Vml2YW11cyBmZXJtZtesting123', + '+3465478932', + '65478932', + '+346547893210', + '6547893210', + '+34704789321', + '704789321', + '+34754789321', + '754789321', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['et-EE'], + 'valid': [ + '+372 512 34 567', + '372 512 34 567', + '+37251234567', + '51234567', + '81234567', + '+372842345678', + ], + 'invalid': [ + '12345', + '', + 'NotANumber', + '+333 51234567', + '61234567', + '+51234567', + '+372 539 57 4', + '+372 900 1234', + '12345678', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['pl-PL'], + 'valid': [ + '+48512689767', + '+48 56 376 87 47', + '56 566 78 46', + '657562855', + '+48657562855', + '+48 887472765', + '+48 56 6572724', + '+48 67 621 5461', + '48 67 621 5461', + ], + 'invalid': [ + '+48 67 621 5461', + '+55657562855', + '3454535', + 'teststring', + '', + '1800-88-8687', + '+6019-5830837', + '357562855', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['fa-IR'], + 'valid': [ + '+989123456789', + '989223456789', + '09323456789', + '09021456789', + '+98-990-345-6789', + '+98 938 345 6789', + '0938 345 6789', + ], + 'invalid': [ + '', + '+989623456789', + '+981123456789', + '01234567890', + '09423456789', + '09823456789', + '9123456789', + '091234567890', + '0912345678', + '+98 912 3456 6789', + '0912 345 678', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['fi-FI'], + 'valid': [ + '+358505557171', + '0455571', + '0505557171', + '358505557171', + '04412345', + '0457 123 45 67', + '+358457 123 45 67', + '+358 50 555 7171', + ], + 'invalid': [ + '12345', + '', + '045557', + '045555717112312332423423421', + 'Vml2YW11cyBmZXJtZtesting123', + '010-38238383', + '+3-585-0555-7171', + '+9676338855', + '19676338855', + '6676338855', + '+99676338855', + '044123', + '019123456789012345678901', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['ms-MY'], + 'valid': [ + '+60128228789', + '+60195830837', + '+6019-5830837', + '+6019-5830837', + '0128737867', + '01468987837', + '016-2838768', + '016 2838768', + ], + 'invalid': [ + '12345', + '601238788657', + '088387675', + '16-2838768', + '032551433', + '6088-387888', + '088-261987', + '1800-88-8687', + '088-320000', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['ko-KR'], + 'valid': [ + '+82-010-1234-5678', + '+82-10-1234-5678', + '82-010-1234-5678', + '82-10-1234-5678', + '+82 10 1234 5678', + '010-123-5678', + '10-1234-5678', + '+82 10 1234 5678', + '011 1234 5678', + '+820112345678', + '01012345678', + '+82 016 1234 5678', + '82 19 1234 5678', + '+82 010 12345678', + ], + 'invalid': [ + 'abcdefghi', + '+82 10 1234 567', + '+82 10o 1234 1234', + '+82 101 1234 5678', + '+82 10 12 5678', + '+011 7766 1234', + '011_7766_1234', + '+820 11 7766 1234', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['ja-JP'], + 'valid': [ + '09012345688', + '090 123 45678', + '+8190-123-45678', + ], + 'invalid': [ + '12345', + '', + '045555717112312332423423421', + 'Vml2YW11cyBmZXJtZtesting123', + '+3-585-0555-7171', + '0 1234 5689', + '16 1234 5689', + '03_1234_5689', + '0312345678', + '0721234567', + '08002345678', + '06 1234 5678', + '072 123 4567', + '0729 12 3456', + '07296 1 2345', + '072961 2345', + '03-1234-5678', + '+81312345678', + '+816-1234-5678', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['it-IT'], + 'valid': [ + '370 3175423', + '333202925', + '+39 310 7688449', + '+39 3339847632', + ], + 'invalid': [ + '011 7387545', + '12345', + '+45 345 6782395', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['fr-BE'], + 'valid': [ + '0470123456', + '+32470123456', + '32470123456', + '021234567', + '+3221234567', + '3221234567', + ], + 'invalid': [ + '12345', + '+3212345', + '3212345', + '04701234567', + '+3204701234567', + '3204701234567', + '0212345678', + '+320212345678', + '320212345678', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['nl-BE'], + 'valid': [ + '0470123456', + '+32470123456', + '32470123456', + '021234567', + '+3221234567', + '3221234567', + ], + 'invalid': [ + '12345', + '+3212345', + '3212345', + '04701234567', + '+3204701234567', + '3204701234567', + '0212345678', + '+320212345678', + '320212345678', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['ro-RO'], + 'valid': [ + '+40740123456', + '+40 740123456', + '+40740 123 456', + '+40740.123.456', + '+40740-123-456', + '40740123456', + '40 740123456', + '40740 123 456', + '40740.123.456', + '40740-123-456', + '0740123456', + '0740/123456', + '0740 123 456', + '0740.123.456', + '0740-123-456', + ], + 'invalid': [ + '', + 'Vml2YW11cyBmZXJtZtesting123', + '123456', + '740123456', + '+40640123456', + '+40210123456', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['id-ID'], + 'valid': [ + '0217123456', + '0811 778 998', + '089931236181900', + '622178878890', + '62811 778 998', + '62811778998', + '6289931236181900', + '6221 740123456', + '62899 740123456', + '62899 7401 2346', + '0341 8123456', + '0778 89800910', + '0741 123456', + '+6221740123456', + '+62811 778 998', + '+62811778998', + ], + 'invalid': [ + '+65740 123 456', + '', + 'ASDFGJKLmZXJtZtesting123', + '123456', + '740123456', + '+65640123456', + '+64210123456', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['lt-LT'], + 'valid': [ + '+37051234567', + '851234567', + ], + 'invalid': [ + '+65740 123 456', + '', + 'ASDFGJKLmZXJtZtesting123', + '123456', + '740123456', + '+65640123456', + '+64210123456', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['uk-UA'], + 'valid': [ + '+380982345679', + '380982345679', + '80982345679', + '0982345679', + ], + 'invalid': [ + '+30982345679', + '982345679', + '+380 98 234 5679', + '+380-98-234-5679', + '', + 'ASDFGJKLmZXJtZtesting123', + '123456', + '740123456', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['da-DK'], + 'valid': [ + '12345678', + '12 34 56 78', + '45 12345678', + '4512345678', + '45 12 34 56 78', + '+45 12 34 56 78', + ], + 'invalid': [ + '', + '+45010203', + 'ASDFGJKLmZXJtZtesting123', + '123456', + '12 34 56', + '123 123 12', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['fo-FO'], + 'valid': [ + '123456', + '12 34 56', + '298 123456', + '298123456', + '298 12 34 56', + '+298 12 34 56', + ], + 'invalid': [ + '', + '+4501020304', + 'ASDFGJKLmZXJtZtesting123', + '12345678', + '12 34 56 78', + ], + }); + + test({ + 'validator': v.isMobilePhone, + 'args': ['kl-GL'], + 'valid': [ + '123456', + '12 34 56', + '299 123456', + '299123456', + '299 12 34 56', + '+299 12 34 56', + ], + 'invalid': [ + '', + '+4501020304', + 'ASDFGJKLmZXJtZtesting123', + '12345678', + '12 34 56 78', + ], + }); +} void main() { testEquals(); @@ -922,6 +2313,22 @@ void main() { testIsVariableWidth(); testIsSurrogatePairs(); testIsMongoId(); + testIsMd5(); + testIsMd4(); + testIsSha1(); + testIsSha256(); + testIsSha384(); + testIsSha512(); + testIsRipemd128(); + testIsRipemd160(); + testIsTiger128(); + testIsTiger160(); + testIsTiger192(); + testIsCrc32(); + testIsCrc32b(); + testIsPort(); + testIsMACAddress(); + testIsMobilePhone(); print('-------------------------------------'); print('All tests in validator.dart complete.');