Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added extra validators #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ packages/

# Include when developing application packages.
pubspec.lock
.pub
.packages
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/src/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ Map _merge(Map obj, defaults) {
}
defaults.forEach((key, val) => obj.putIfAbsent(key, () => val));
return obj;
}
}
48 changes: 20 additions & 28 deletions lib/src/sanitizer.dart
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -50,7 +46,6 @@ num toInt(String str, {int radix:10}) {
}
}


/// convert the input to a boolean.
///
/// Everything except for '0', 'false' and ''
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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'&'), '&amp;')
.replaceAll(new RegExp(r'"'), '&quot;')
.replaceAll(new RegExp(r"'"), '&#x27;')
.replaceAll(new RegExp(r'<'), '&lt;')
.replaceAll(new RegExp(r'>'), '&gt;'));
return (str
.replaceAll(new RegExp(r'&'), '&amp;')
.replaceAll(new RegExp(r'"'), '&quot;')
.replaceAll(new RegExp(r"'"), '&#x27;')
.replaceAll(new RegExp(r'<'), '&lt;')
.replaceAll(new RegExp(r'>'), '&gt;'));
}


/// canonicalize an email address.
///
/// `options` is an `Map` which defaults to
Expand All @@ -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';
Expand Down
Loading