Skip to content

Commit df61f48

Browse files
committed
null safety update
1 parent 5322eec commit df61f48

File tree

10 files changed

+46
-77
lines changed

10 files changed

+46
-77
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes
22

3+
## 0.2.0-nullsafety.0
4+
5+
- Updated for null safety
6+
37
## 0.1.4
48

59
- Bug fix for `isURL()` with `require_protocol` option.

README.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The is a new fork of [validator.dart](https://github.com/karan/validator.dart),
1111
- **contains(String str, seed)** - check if the string contains the seed.
1212
- **matches(String str, pattern)** - check if string matches the pattern. `matches('foobar', 'foo')`.
1313
- **isEmail(String str)** - check if the string is an email.
14-
- **isURL(String str [, options])** - check if the string is an URL. `options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, allow_underscores: false, host_whitelist: false, host_blacklist: false }`.
14+
- **isURL(String str [, options])** - check if the string is an URL. `options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, allow_underscores: false }`.
1515
- **isFQDN(String str [, options])** - check if the string is a fully qualified domain name (e.g. domain.com). `options` is an object which defaults to `{ require_tld: true, allow_underscores: false }`.
1616
- **isIP(String str [, version])** - check if the string is an IP (version 4 or 6).
1717
- **isAlpha(String str)** - check if the string contains only letters (a-zA-Z).
@@ -58,12 +58,4 @@ The is a new fork of [validator.dart](https://github.com/karan/validator.dart),
5858
- **stripLow(String input [, keep_new_lines])** - remove characters with a numerical value < 32 and 127, mostly control characters. If `keep_new_lines` is `true`, newline characters are preserved (`\n` and `\r`, hex `0xA` and `0xD`). Unicode-safe in JavaScript.
5959
- **whitelist(String input, chars)** - remove characters that do not appear in the whitelist. The characters are used in a RegExp and so you will need to escape some chars, e.g. whitelist(String input, '\\[\\]').
6060
- **blacklist(String input, chars)** - remove characters that appear in the blacklist. The characters are used in a RegExp and so you will need to escape some chars, e.g. blacklist(String input, '\\[\\]').
61-
- **normalizeEmail(String email [, options])** - canonicalize an email address. `options` is an object which defaults to `{ lowercase: true }`. With `lowercase` set to `true`, the local part of the email address is lowercased for all domains; the hostname is always lowercased and the local part of the email address is always lowercased for hosts that are known to be case-insensitive (currently only GMail). Normalization follows special rules for known providers: currently, GMail addresses have dots removed in the local part and are stripped of tags (e.g. `some.one+tag@gmail.com` becomes `someone@gmail.com`) and all `@googlemail.com` addresses are normalized to `@gmail.com`.
62-
63-
## Tests
64-
65-
To test the package, run:
66-
67-
```
68-
$ pub run test test/test_all.dart
69-
```
61+
- **normalizeEmail(String email [, options])** - canonicalize an email address. `options` is an object which defaults to `{ lowercase: true }`. With `lowercase` set to `true`, the local part of the email address is lowercased for all domains; the hostname is always lowercased and the local part of the email address is always lowercased for hosts that are known to be case-insensitive (currently only GMail). Normalization follows special rules for known providers: currently, GMail addresses have dots removed in the local part and are stripped of tags (e.g. `some.one+tag@gmail.com` becomes `someone@gmail.com`) and all `@googlemail.com` addresses are normalized to `@gmail.com`.

example/example.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import 'package:string_validator/string_validator.dart';
22

3-
main() {
3+
void main() {
44
String userInput = 'http://localhost:61500this is an invalid url!!!!';
55
bool isValid = isURL(userInput); // false
6+
print(isValid);
67

78
userInput = 'me@example.com';
89
isValid = isEmail(userInput); // true
10+
print(isValid);
911

1012
userInput = 'password1';
1113
isValid = isLength(userInput, 12); // false
14+
print(isValid);
1215
}

lib/src/helpers.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ shift(List l) {
99
return null;
1010
}
1111

12-
Map merge(Map obj, defaults) {
12+
Map<String, Object> merge(Map<String, Object>? obj, Map<String, Object> defaults) {
1313
if (obj == null) {
14-
obj = Map();
14+
return defaults;
1515
}
1616
defaults.forEach((key, val) => obj.putIfAbsent(key, () => val));
1717
return obj;

lib/src/sanitizer.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'helpers.dart';
22
import 'validator.dart';
33

4-
Map _default_normalize_email_options = {'lowercase': true};
4+
Map<String, Object> _default_normalize_email_options = {'lowercase': true};
55

66
/// convert the input to a string
77
String toString(input) {
@@ -12,7 +12,7 @@ String toString(input) {
1212
}
1313

1414
/// convert the input to a date, or null if the input is not a date
15-
DateTime toDate(String str) {
15+
DateTime? toDate(String str) {
1616
try {
1717
return DateTime.parse(str);
1818
} catch (e) {
@@ -51,29 +51,29 @@ num toInt(String str, {int radix = 10}) {
5151
///
5252
/// Everything except for '0', 'false' and ''
5353
/// returns `true`. In `strict` mode only '1' and 'true' return `true`.
54-
bool toBoolean(String str, [bool strict]) {
54+
bool toBoolean(String str, [bool strict = false]) {
5555
if (strict == true) {
5656
return str == '1' || str == 'true';
5757
}
5858
return str != '0' && str != 'false' && str != '';
5959
}
6060

6161
/// trim characters (whitespace by default) from both sides of the input
62-
String trim(String str, [String chars]) {
62+
String trim(String str, [String? chars]) {
6363
RegExp pattern = (chars != null)
6464
? RegExp('^[$chars]+|[$chars]+\$')
6565
: RegExp(r'^\s+|\s+$');
6666
return str.replaceAll(pattern, '');
6767
}
6868

6969
/// trim characters from the left-side of the input
70-
String ltrim(String str, [String chars]) {
70+
String ltrim(String str, [String? chars]) {
7171
var pattern = chars != null ? RegExp('^[$chars]+') : RegExp(r'^\s+');
7272
return str.replaceAll(pattern, '');
7373
}
7474

7575
/// trim characters from the right-side of the input
76-
String rtrim(String str, [String chars]) {
76+
String rtrim(String str, [String? chars]) {
7777
var pattern = chars != null ? RegExp('[$chars]+\$') : RegExp(r'\s+$');
7878
return str.replaceAll(pattern, '');
7979
}
@@ -98,7 +98,7 @@ String blacklist(String str, String chars) {
9898
///
9999
/// If `keep_new_lines` is `true`, newline characters are preserved
100100
/// `(\n and \r, hex 0xA and 0xD)`.
101-
String stripLow(String str, [bool keep_new_lines]) {
101+
String stripLow(String str, [bool keep_new_lines = false]) {
102102
String chars = keep_new_lines == true
103103
? '\x00-\x09\x0B\x0C\x0E-\x1F\x7F'
104104
: '\x00-\x1F\x7F';
@@ -117,7 +117,7 @@ String escape(String str) {
117117

118118
/// canonicalize an email address.
119119
///
120-
/// `options` is an `Map` which defaults to
120+
/// `options` is a `Map` which defaults to
121121
/// `{ lowercase: true }`. With lowercase set to true, the local part of the
122122
/// email address is lowercased for all domains; the hostname is always
123123
/// lowercased and the local part of the email address is always lowercased
@@ -126,7 +126,7 @@ String escape(String str) {
126126
/// GMail addresses have dots removed in the local part and are stripped of
127127
/// tags (e.g. `some.one+tag@gmail.com` becomes `someone@gmail.com`) and all
128128
/// `@googlemail.com` addresses are normalized to `@gmail.com`.
129-
String normalizeEmail(String email, [Map options]) {
129+
String normalizeEmail(String email, [Map<String, Object>? options]) {
130130
options = merge(options, _default_normalize_email_options);
131131
if (isEmail(email) == false) {
132132
return '';

lib/src/validator.dart

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,17 @@ bool isEmail(String str) {
7272
///
7373
/// `options` is a `Map` which defaults to
7474
/// `{ 'protocols': ['http','https','ftp'], 'require_tld': true,
75-
/// 'require_protocol': false, 'allow_underscores': false,
76-
/// 'host_whitelist': false, 'host_blacklist': false }`.
77-
bool isURL(String str, [Map options]) {
78-
if (str == null ||
79-
str.isEmpty ||
80-
str.length > 2083 ||
81-
str.indexOf('mailto:') == 0) {
75+
/// 'require_protocol': false, 'allow_underscores': false }`.
76+
bool isURL(String str, [Map<String, Object>? options]) {
77+
if (str.isEmpty || str.length > 2083 || str.indexOf('mailto:') == 0) {
8278
return false;
8379
}
8480

85-
Map default_url_options = {
81+
final default_url_options = {
8682
'protocols': ['http', 'https', 'ftp'],
8783
'require_tld': true,
8884
'require_protocol': false,
89-
'allow_underscores': false
85+
'allow_underscores': false,
9086
};
9187

9288
options = merge(options, default_url_options);
@@ -108,7 +104,8 @@ bool isURL(String str, [Map options]) {
108104
split = str.split('://');
109105
if (split.length > 1) {
110106
protocol = shift(split);
111-
if (options['protocols'].indexOf(protocol) == -1) {
107+
final protocols = options['protocols'] as List<String>;
108+
if (protocols.indexOf(protocol) == -1) {
112109
return false;
113110
}
114111
} else if (options['require_protocol'] == true) {
@@ -177,16 +174,6 @@ bool isURL(String str, [Map options]) {
177174
return false;
178175
}
179176

180-
if (options['host_whitelist'] == true &&
181-
options['host_whitelist'].indexOf(host) == -1) {
182-
return false;
183-
}
184-
185-
if (options['host_blacklist'] == true &&
186-
options['host_blacklist'].indexOf(host) != -1) {
187-
return false;
188-
}
189-
190177
return true;
191178
}
192179

@@ -211,12 +198,15 @@ bool isIP(String str, [version]) {
211198
/// check if the string is a fully qualified domain name (e.g. domain.com).
212199
///
213200
/// `options` is a `Map` which defaults to `{ 'require_tld': true, 'allow_underscores': false }`.
214-
bool isFQDN(str, [options]) {
215-
Map default_fqdn_options = {'require_tld': true, 'allow_underscores': false};
201+
bool isFQDN(String str, [Map<String, Object>? options]) {
202+
final default_fqdn_options = {
203+
'require_tld': true,
204+
'allow_underscores': false
205+
};
216206

217207
options = merge(options, default_fqdn_options);
218208
List parts = str.split('.');
219-
if (options['require_tld']) {
209+
if (options['require_tld'] as bool) {
220210
var tld = parts.removeLast();
221211
if (parts.isEmpty || !RegExp(r'^[a-z]{2,}$').hasMatch(tld)) {
222212
return false;
@@ -225,7 +215,7 @@ bool isFQDN(str, [options]) {
225215

226216
for (var part, i = 0; i < parts.length; i++) {
227217
part = parts[i];
228-
if (options['allow_underscores']) {
218+
if (options['allow_underscores'] as bool) {
229219
if (part.indexOf('__') >= 0) {
230220
return false;
231221
}
@@ -303,23 +293,18 @@ bool isDivisibleBy(String str, n) {
303293
}
304294
}
305295

306-
/// check if the string is null
307-
bool isNull(String str) {
308-
return str == null || str.isEmpty;
309-
}
310-
311296
/// check if the string's length falls in a range
312297
/// If no max is given then any length above min is ok.
313298
///
314299
/// Note: this function takes into account surrogate pairs.
315-
bool isLength(String str, int min, [int max]) {
300+
bool isLength(String str, int min, [int? max]) {
316301
List surrogatePairs = _surrogatePairsRegExp.allMatches(str).toList();
317302
int len = str.length - surrogatePairs.length;
318303
return len >= min && (max == null || len <= max);
319304
}
320305

321306
/// check if the string's length (in bytes) falls in a range.
322-
bool isByteLength(String str, int min, [int max]) {
307+
bool isByteLength(String str, int min, [int? max]) {
323308
return str.length >= min && (max == null || str.length <= max);
324309
}
325310

@@ -331,7 +316,7 @@ bool isUUID(String str, [version]) {
331316
version = version.toString();
332317
}
333318

334-
RegExp pat = _uuid[version];
319+
RegExp? pat = _uuid[version];
335320
return (pat != null && pat.hasMatch(str.toUpperCase()));
336321
}
337322

pubspec.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
name: string_validator
2-
version: 0.1.4
2+
version: 0.2.0-nullsafety.0
33
description: Dart library for validating and sanitizing strings, especially those from user input.
44
homepage: https://github.com/suragch/string_validator
5+
56
environment:
6-
sdk: '>=2.0.0 <3.0.0'
7+
sdk: '>=2.12.0-29.10.beta <3.0.0'
8+
79
dev_dependencies:
8-
test: ^1.5.3
10+
pedantic: ^1.10.0-nullsafety.3
11+
test: ^1.16.0-nullsafety.11

test/sanitizer_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import "package:test/test.dart";
1+
import 'package:test/test.dart';
22
import 'package:string_validator/string_validator.dart' as s;
33

44
void main() {

test/test_all.dart

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/validator_test.dart

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void main() {
9898
expect(v.isURL('http://www.foo---bar.com/'), equals(false));
9999
expect(v.isURL('http://www.foo_bar.com/'), equals(false));
100100
expect(v.isURL(''), equals(false));
101-
expect(v.isURL('http://foobar.com/' + new List(2083).join('f')),
101+
expect(v.isURL('http://foobar.com/' + ('f' * 2083)),
102102
equals(false));
103103
expect(v.isURL('http://*.foo.com'), equals(false));
104104
expect(v.isURL('*.foo.com'), equals(false));
@@ -269,15 +269,6 @@ void main() {
269269
expect(v.isDivisibleBy('abc', '2'), equals(false));
270270
});
271271

272-
test('string is null', () {
273-
// valid
274-
expect(v.isNull(null), equals(true));
275-
expect(v.isNull(''), equals(true));
276-
// invalid
277-
expect(v.isNull(' '), equals(false));
278-
expect(v.isNull('foo'), equals(false));
279-
});
280-
281272
test('length at least', () {
282273
// valid
283274
expect(v.isLength('ab', 2), equals(true));

0 commit comments

Comments
 (0)