Skip to content

Commit

Permalink
feature: Support for GMAIL infinite mailboxes (PATTERN_EMAIL) changed.
Browse files Browse the repository at this point in the history
…Closes #3
  • Loading branch information
MikeMitterer committed Aug 21, 2018
1 parent d4e70c8 commit 4af87cb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -26,7 +26,7 @@ You can see further changes on the [CHANGELOG](https://goo.gl/2d63eC)!


### License ### License


Copyright 2017 Michael Mitterer, IT-Consulting and Development Limited, Copyright 2018 Michael Mitterer, IT-Consulting and Development Limited,
Austrian Branch Austrian Branch


Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
2 changes: 1 addition & 1 deletion lib/expect.dart
Expand Up @@ -15,7 +15,7 @@ T notNull<T>(final T object,{ final LazyMessage message = _DEFAULT_IS_NULL_EX_ME
} }


bool isTrue(final bool expression,{ final LazyMessage message = _DEFAULT_IS_TRUE_EX_MESSAGE }) { bool isTrue(final bool expression,{ final LazyMessage message = _DEFAULT_IS_TRUE_EX_MESSAGE }) {
if (expression == null) { if (expression == null || expression == false) {
throw new ArgumentError(message()); throw new ArgumentError(message());
} }
return expression; return expression;
Expand Down
4 changes: 2 additions & 2 deletions lib/validate.dart
Expand Up @@ -54,7 +54,7 @@ part "src/utils.dart";
* http://www.mkyong.com/regular-expressions/how-to-validate-password-with-regular-expression/ * http://www.mkyong.com/regular-expressions/how-to-validate-password-with-regular-expression/
*/ */
abstract class Validate { abstract class Validate {
static const String PATTERN_EMAIL = "^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})\$"; static const String PATTERN_EMAIL = "^([0-9a-zA-Z]([-.+\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})\$";
static const String PATTERN_PW = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#\$%?])[0-9a-zA-Z@#\$%?]{8,15}\$"; static const String PATTERN_PW = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#\$%?])[0-9a-zA-Z@#\$%?]{8,15}\$";
static const String PATTERN_ALPHANUMERIC = "^[a-zA-Z0-9öäüÖÄÜß]+\$"; static const String PATTERN_ALPHANUMERIC = "^[a-zA-Z0-9öäüÖÄÜß]+\$";
static const String PATTERN_HEX = "^(0x[a-fA-F0-9]+)|([a-fA-F0-9])+\$"; static const String PATTERN_HEX = "^(0x[a-fA-F0-9]+)|([a-fA-F0-9])+\$";
Expand Down Expand Up @@ -99,7 +99,7 @@ abstract class Validate {
* [message] the exception message if invalid, not null * [message] the exception message if invalid, not null
* Throws [ArgumentError] if expression is [false] * Throws [ArgumentError] if expression is [false]
*/ */
static void isTrue(final bool expression, [ final String message = DEFAULT_IS_TRUE_EX_MESSAGE]) static bool isTrue(final bool expression, [ final String message = DEFAULT_IS_TRUE_EX_MESSAGE])
=> expect.isTrue(expression, message: () => message); => expect.isTrue(expression, message: () => message);


// notNull // notNull
Expand Down
8 changes: 7 additions & 1 deletion pubspec.yaml
Expand Up @@ -5,11 +5,17 @@ description: This class assists in validating method arguments
homepage: https://github.com/MikeMitterer/dart-validate homepage: https://github.com/MikeMitterer/dart-validate


environment: environment:
sdk: ">=1.8.0 <3.0.0" sdk: ">=2.0.0 <3.0.0"


dev_dependencies: dev_dependencies:
test: any test: any
grinder: any grinder: any


console_log_handler: ^1.0.0 console_log_handler: ^1.0.0
# path: /Volumes/Daten/DevLocal/DevDart/ConsoleLogHandler # path: /Volumes/Daten/DevLocal/DevDart/ConsoleLogHandler

build_runner: any
build_test: any
build_web_compilers: any


74 changes: 37 additions & 37 deletions test/unit/validate_test.dart
Expand Up @@ -11,41 +11,49 @@ class _IAmAJsonObject extends _NotAJsonObject {
} }
} }


/// A matcher for [IllegalStateError].
const isIllegalStateError = const TypeMatcher<IllegalStateError>();

/// A matcher for functions that throw IllegalStateError.
// ignore: deprecated_member_use
const Matcher throwsIllegalStateError = const Throws(isIllegalStateError);


main() { main() {
// final _logger = new Logger('validate.testValidate'); // final _logger = new Logger('validate.testValidate');
// configLogging(); // configLogging();


group('Validator-Test', () { group('Validator-Test', () {
test('isTrue', () { test('isTrue', () {
expect(() => (Validate.isTrue(true)), returnsNormally); expect(() => (Validate.isTrue(true)), returnsNormally);
expect(() => (Validate.isTrue(false)), throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.isTrue(false)), throwsArgumentError);
}); });


test('> notNull', () { test('> notNull', () {
expect(() => (Validate.notNull("Test")), returnsNormally); expect(() => (Validate.notNull("Test")), returnsNormally);
expect(() => (Validate.notNull(null)), throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.notNull(null)), throwsArgumentError);
}); });


test('> notEmpty', () { test('> notEmpty', () {
expect(() => (Validate.notEmpty("Test")), returnsNormally); expect(() => (Validate.notEmpty("Test")), returnsNormally);
expect(() => (Validate.notEmpty([10, 20])), returnsNormally); expect(() => (Validate.notEmpty([10, 20])), returnsNormally);


final Map<String, Object> map = new Map<String, Object>(); final Map<String, Object> map = new Map<String, Object>();
expect(() => (Validate.notEmpty(map)), throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.notEmpty(map)), throwsArgumentError);


map["Hallo"] = "Test"; map["Hallo"] = "Test";
expect(() => (Validate.notEmpty(map)), returnsNormally); expect(() => (Validate.notEmpty(map)), returnsNormally);


// int has not Method "isEmpty" // int has not Method "isEmpty"
expect(() => (Validate.notEmpty(0)), throwsA(new isInstanceOf<NoSuchMethodError>())); expect(() => (Validate.notEmpty(0)), throwsNoSuchMethodError);


expect(() => (Validate.notEmpty("")), throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.notEmpty("")), throwsArgumentError);
}); });


test('> notBlank', () { test('> notBlank', () {
expect(() => (Validate.notBlank("Test")), returnsNormally); expect(() => (Validate.notBlank("Test")), returnsNormally);
expect(() => (Validate.notBlank(null)), throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.notBlank(null)), throwsArgumentError);
expect(() => (Validate.notBlank("")), throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.notBlank("")), throwsArgumentError);


// Dart should point out at least a warning!!!! // Dart should point out at least a warning!!!!
//expect(() => (Validate.notBlank(10)),throwsA(new isInstanceOf<ArgumentError>())); //expect(() => (Validate.notBlank(10)),throwsA(new isInstanceOf<ArgumentError>()));
Expand All @@ -61,14 +69,14 @@ main() {


list.add(null); list.add(null);
expect(4, list.length); expect(4, list.length);
expect(() => (Validate.noNullElements(list)), throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.noNullElements(list)), throwsArgumentError);


expect(() => (Validate.noNullElements(new List<String>())), returnsNormally); expect(() => (Validate.noNullElements(new List<String>())), returnsNormally);
}); });


test('> validIndex', () { test('> validIndex', () {
expect(() => (Validate.validIndex([1, 2], 1)), returnsNormally); expect(() => (Validate.validIndex([1, 2], 1)), returnsNormally);
expect(() => (Validate.validIndex([1, 2], 3)), throwsA(new isInstanceOf<RangeError>())); expect(() => (Validate.validIndex([1, 2], 3)), throwsRangeError);


final List<String> list = new List<String>() final List<String> list = new List<String>()
..addAll(["one", "two", "three"]); ..addAll(["one", "two", "three"]);
Expand All @@ -78,7 +86,7 @@ main() {


test('> validState', () { test('> validState', () {
expect(() => (Validate.validState(true)), returnsNormally); expect(() => (Validate.validState(true)), returnsNormally);
expect(() => (Validate.validState(false)), throwsA(new isInstanceOf<IllegalStateError>())); expect(() => (Validate.validState(false)), throwsIllegalStateError);
}); });


test('> matchesPattern', () { test('> matchesPattern', () {
Expand All @@ -87,33 +95,33 @@ main() {
throwsA(new isInstanceOf<ArgumentError>())); throwsA(new isInstanceOf<ArgumentError>()));


expect(() => (Validate.isEmail("urbi@orbi.it")), returnsNormally); expect(() => (Validate.isEmail("urbi@orbi.it")), returnsNormally);
expect(() => (Validate.isEmail("urbi@orbit")), throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.isEmail("urbi@orbit")), throwsArgumentError);


expect(() => (Validate.isAlphaNumeric("123abcdö")), returnsNormally); expect(() => (Validate.isAlphaNumeric("123abcdö")), returnsNormally);
expect(() => (Validate.isAlphaNumeric("123a#bcd")), throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.isAlphaNumeric("123a#bcd")), throwsArgumentError);


expect(() => (Validate.isHex("1234567890abcdef")), returnsNormally); expect(() => (Validate.isHex("1234567890abcdef")), returnsNormally);
expect(() => (Validate.isHex("0x1234567890abcdef")), returnsNormally); expect(() => (Validate.isHex("0x1234567890abcdef")), returnsNormally);
expect(() => (Validate.isHex("1234567890abcdefg")), throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.isHex("1234567890abcdefg")), throwsArgumentError);
}); });


test('> password', () { test('> password', () {
expect(() => (Validate.isPassword("1abcdefGH#")), returnsNormally); expect(() => (Validate.isPassword("1abcdefGH#")), returnsNormally);
expect(() => (Validate.isPassword("1abcdefGH?")), returnsNormally); expect(() => (Validate.isPassword("1abcdefGH?")), returnsNormally);


expect(() => (Validate.isPassword("urbi@orbi.it")), throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.isPassword("urbi@orbi.it")), throwsArgumentError);
expect(() => (Validate.isPassword("1234567890abcdefGH#")), throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.isPassword("1234567890abcdefGH#")), throwsArgumentError);
expect(() => (Validate.isPassword("12345678aA# ")), throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.isPassword("12345678aA# ")), throwsArgumentError);
expect(() => (Validate.isPassword("12345678aA'")), throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.isPassword("12345678aA'")), throwsArgumentError);
expect(() => (Validate.isPassword("")), throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.isPassword("")), throwsArgumentError);
expect(() => (Validate.isPassword("1abcdefGH;")), throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.isPassword("1abcdefGH;")), throwsArgumentError);
}); });


test('> inclusive', () { test('> inclusive', () {
expect(() => (Validate.inclusiveBetween(0, 2, 2)), returnsNormally); expect(() => (Validate.inclusiveBetween(0, 2, 2)), returnsNormally);
expect(() => (Validate.inclusiveBetween(0, 2, 3)), throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.inclusiveBetween(0, 2, 3)), throwsArgumentError);


expect(() => (Validate.exclusiveBetween(0, 2, 2)), throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.exclusiveBetween(0, 2, 2)), throwsArgumentError);
expect(() => (Validate.exclusiveBetween(0, 2, 1)), returnsNormally); expect(() => (Validate.exclusiveBetween(0, 2, 1)), returnsNormally);
}); });


Expand All @@ -122,7 +130,7 @@ main() {
expect(() => (Validate.isJson(1)), returnsNormally); expect(() => (Validate.isJson(1)), returnsNormally);
expect(() => (Validate.isJson(["3", "4"])), returnsNormally); expect(() => (Validate.isJson(["3", "4"])), returnsNormally);


expect(() => (Validate.isJson(new _NotAJsonObject())), throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.isJson(new _NotAJsonObject())), throwsArgumentError);
expect(() => (Validate.isJson(new _IAmAJsonObject())), returnsNormally); expect(() => (Validate.isJson(new _IAmAJsonObject())), returnsNormally);
}); });


Expand All @@ -135,7 +143,7 @@ main() {
expect(() => (Validate.isKeyInMap("number", map1ToTest)), returnsNormally); expect(() => (Validate.isKeyInMap("number", map1ToTest)), returnsNormally);


//expect(() => (Validate.isKeyInMap("email",map1ToTest)),returnsNormally); //expect(() => (Validate.isKeyInMap("email",map1ToTest)),returnsNormally);
expect(() => (Validate.isKeyInMap("email", map1ToTest)), throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.isKeyInMap("email", map1ToTest)), throwsArgumentError);


try { try {
Validate.isKeyInMap("email", map1ToTest); Validate.isKeyInMap("email", map1ToTest);
Expand All @@ -150,31 +158,23 @@ main() {


test('> isInstanceOf', () { test('> isInstanceOf', () {
expect(() => (Validate.isInstance(new instanceCheck<List<String>>(), new List())), expect(() => (Validate.isInstance(new instanceCheck<List<String>>(), new List())),
throwsA(new isInstanceOf<ArgumentError>())); throwsArgumentError);


expect(() => (Validate.isInstance(new instanceCheck<List<String>>(strict: false), new List<String>())), expect(() => (Validate.isInstance(new instanceCheck<List<String>>(strict: false), new List<String>())),
returnsNormally); returnsNormally);


expect(() => (Validate.isInstance(new instanceCheck<String>(), "Test")), returnsNormally); expect(() => (Validate.isInstance(new instanceCheck<String>(), "Test")), returnsNormally);
expect(() => (Validate.isInstance(new instanceCheck<String>(), 1)), expect(() => (Validate.isInstance(new instanceCheck<String>(), 1)), throwsArgumentError);
throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.isInstance(new instanceCheck<String>(strict: false), 1)),throwsArgumentError);
expect(() => (Validate.isInstance(new instanceCheck<String>(strict: false), 1)),
throwsA(new isInstanceOf<ArgumentError>()));


expect(() => (Validate.isInstance(new instanceCheck<int>(), 29)), returnsNormally); expect(() => (Validate.isInstance(new instanceCheck<int>(), 29)), returnsNormally);
expect(() => (Validate.isInstance(new instanceCheck<String>(), 1)), expect(() => (Validate.isInstance(new instanceCheck<String>(), 1)),throwsArgumentError);
throwsA(new isInstanceOf<ArgumentError>()));

expect(() => (Validate.isInstance(new instanceCheck<double>(), 29.0)), returnsNormally);
expect(() => (Validate.isInstance(new instanceCheck<double>(), 29)),
throwsA(new isInstanceOf<ArgumentError>()));


expect(() => (Validate.isInstance(new instanceCheck<num>(strict: false), 29.0)), returnsNormally); expect(() => (Validate.isInstance(new instanceCheck<num>(strict: false), 29.0)), returnsNormally);
expect(() => (Validate.isInstance(new instanceCheck<num>(), 29.0)), expect(() => (Validate.isInstance(new instanceCheck<num>(), 29.0)),throwsArgumentError);
throwsA(new isInstanceOf<ArgumentError>()));
expect(() => (Validate.isInstance(new instanceCheck<num>(strict: false), 29)), returnsNormally); expect(() => (Validate.isInstance(new instanceCheck<num>(strict: false), 29)), returnsNormally);


expect(() => (Validate.isInstance(null, 29.0)), throwsA(new isInstanceOf<ArgumentError>())); expect(() => (Validate.isInstance(null, 29.0)), throwsArgumentError);
}); });
}); });
} }
Expand Down

0 comments on commit 4af87cb

Please sign in to comment.