From 2b4d863bb356c81cf69dd6a328f87c3857a151bf Mon Sep 17 00:00:00 2001 From: Malith Kuruppu Date: Wed, 31 Jan 2024 12:07:21 +0100 Subject: [PATCH 1/9] Update bloc --- pubspec.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pubspec.yaml b/pubspec.yaml index ec9e2d4..b5c2096 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -8,12 +8,12 @@ environment: dependencies: flutter: sdk: flutter - bloc: ^7.1.0 + bloc: ^8.1.2 equatable: ^2.0.5 intl: ^0.17.0 - flutter_bloc: ^7.2.0 + flutter_bloc: ^8.1.3 dev_dependencies: - bloc_test: ^8.1.0 + bloc_test: ^9.1.5 flutter_test: sdk: flutter From 3e821c5a922d616b76770677972e203d5d9077a7 Mon Sep 17 00:00:00 2001 From: Salma Date: Thu, 28 Mar 2024 10:22:57 +0100 Subject: [PATCH 2/9] check if string is empty --- lib/src/logic/type_converter.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/src/logic/type_converter.dart b/lib/src/logic/type_converter.dart index d06fd18..22d4b00 100644 --- a/lib/src/logic/type_converter.dart +++ b/lib/src/logic/type_converter.dart @@ -16,7 +16,9 @@ class StringDoubleTypeConverter extends TypeConverter { double? canConvert(String inputType) { double convertedDouble; try { + if(inputType.isNotEmpty){ convertedDouble = NumberFormat().parse(inputType) as double; + } } on FormatException catch (_) { return null; } on TypeError catch (_) { From 9f199ac78a1bf155b38f3fd9d184925ba4d398d9 Mon Sep 17 00:00:00 2001 From: Salma Date: Thu, 28 Mar 2024 10:47:54 +0100 Subject: [PATCH 3/9] add date check --- lib/src/logic/type_converter.dart | 2 +- lib/src/logic/validators.dart | 38 ++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/src/logic/type_converter.dart b/lib/src/logic/type_converter.dart index 22d4b00..a2ab71c 100644 --- a/lib/src/logic/type_converter.dart +++ b/lib/src/logic/type_converter.dart @@ -14,7 +14,7 @@ abstract class TypeConverter { class StringDoubleTypeConverter extends TypeConverter { @override double? canConvert(String inputType) { - double convertedDouble; + double? convertedDouble; try { if(inputType.isNotEmpty){ convertedDouble = NumberFormat().parse(inputType) as double; diff --git a/lib/src/logic/validators.dart b/lib/src/logic/validators.dart index 621a49c..2b3c4d5 100644 --- a/lib/src/logic/validators.dart +++ b/lib/src/logic/validators.dart @@ -264,7 +264,7 @@ class ShouldInBetweenDatesValidator @override bool isValid(DateTime date) { - return (date.compareTo(min) > 0 && date.compareTo(max) < 0); + return (date.isBetween(min, max)); } @override @@ -416,4 +416,40 @@ class ShouldBeBetweenOrEqualValidator @override List get props => [...super.props, min, max]; +} + + + extension DateTimeExtension on DateTime? { + + bool? isAfterOrEqualTo(DateTime dateTime) { + final date = this; + if (date != null) { + final isAtSameMomentAs = dateTime.isAtSameMomentAs(date); + return isAtSameMomentAs | date.isAfter(dateTime); + } + return null; + } + + bool? isBeforeOrEqualTo(DateTime dateTime) { + final date = this; + if (date != null) { + final isAtSameMomentAs = dateTime.isAtSameMomentAs(date); + return isAtSameMomentAs | date.isBefore(dateTime); + } + return null; + } + + bool isBetween( + DateTime fromDateTime, + DateTime toDateTime, + ) { + final date = this; + if (date != null) { + final isAfter = date.isAfterOrEqualTo(fromDateTime) ?? false; + final isBefore = date.isBeforeOrEqualTo(toDateTime) ?? false; + return isAfter && isBefore; + } + return false; + } + } From e5bf10e2741576db0f3abed320263582ffd28676 Mon Sep 17 00:00:00 2001 From: Salma Date: Tue, 2 Apr 2024 13:34:08 +0200 Subject: [PATCH 4/9] add use extension --- lib/src/logic/type_converter.dart | 4 +--- lib/src/logic/validators.dart | 40 +++++++++++++++---------------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/lib/src/logic/type_converter.dart b/lib/src/logic/type_converter.dart index a2ab71c..d06fd18 100644 --- a/lib/src/logic/type_converter.dart +++ b/lib/src/logic/type_converter.dart @@ -14,11 +14,9 @@ abstract class TypeConverter { class StringDoubleTypeConverter extends TypeConverter { @override double? canConvert(String inputType) { - double? convertedDouble; + double convertedDouble; try { - if(inputType.isNotEmpty){ convertedDouble = NumberFormat().parse(inputType) as double; - } } on FormatException catch (_) { return null; } on TypeError catch (_) { diff --git a/lib/src/logic/validators.dart b/lib/src/logic/validators.dart index 2b3c4d5..1f5dc28 100644 --- a/lib/src/logic/validators.dart +++ b/lib/src/logic/validators.dart @@ -249,9 +249,11 @@ class ShouldInBetweenDatesValidator extends FieldValidator> { final DateTime max; final DateTime min; + final bool useExtension; ShouldInBetweenDatesValidator( {required this.min, + this.useExtension = false, required this.max, String Function(ShouldInBetweenDatesValidator validator, Field field)? buildErrorMessage, KeyType? key}) @@ -264,7 +266,11 @@ class ShouldInBetweenDatesValidator @override bool isValid(DateTime date) { - return (date.isBetween(min, max)); + if(useExtension){ + return (date.isBetween(min, max)); + }else { + return (date.compareTo(min) >= 0 && date.compareTo(max) <= 0); + } } @override @@ -419,37 +425,29 @@ class ShouldBeBetweenOrEqualValidator } - extension DateTimeExtension on DateTime? { + extension DateTimeExtension on DateTime { - bool? isAfterOrEqualTo(DateTime dateTime) { - final date = this; - if (date != null) { - final isAtSameMomentAs = dateTime.isAtSameMomentAs(date); - return isAtSameMomentAs | date.isAfter(dateTime); - } - return null; + bool isAfterOrEqualTo(DateTime dateTime) { + final isAtSameMomentAs = dateTime.isAtSameMomentAs(this); + return isAtSameMomentAs || isAfter(dateTime); } - bool? isBeforeOrEqualTo(DateTime dateTime) { + bool isBeforeOrEqualTo(DateTime dateTime) { final date = this; - if (date != null) { + final isAtSameMomentAs = dateTime.isAtSameMomentAs(date); - return isAtSameMomentAs | date.isBefore(dateTime); - } - return null; + return isAtSameMomentAs || date.isBefore(dateTime); + } bool isBetween( DateTime fromDateTime, DateTime toDateTime, ) { - final date = this; - if (date != null) { - final isAfter = date.isAfterOrEqualTo(fromDateTime) ?? false; - final isBefore = date.isBeforeOrEqualTo(toDateTime) ?? false; + final isAfter = isAfterOrEqualTo(fromDateTime) ; + final isBefore = isBeforeOrEqualTo(toDateTime) ; return isAfter && isBefore; } - return false; - } - + + } From 1e3028a66da6547d0166f0e0f0626379b8ddac55 Mon Sep 17 00:00:00 2001 From: Salma Date: Thu, 4 Apr 2024 17:17:02 +0200 Subject: [PATCH 5/9] rename variable and add tests --- lib/src/logic/validators.dart | 6 ++-- test/invalid_test.dart | 55 +++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/lib/src/logic/validators.dart b/lib/src/logic/validators.dart index 1f5dc28..6e3b405 100644 --- a/lib/src/logic/validators.dart +++ b/lib/src/logic/validators.dart @@ -249,11 +249,11 @@ class ShouldInBetweenDatesValidator extends FieldValidator> { final DateTime max; final DateTime min; - final bool useExtension; + final bool isInclusive; ShouldInBetweenDatesValidator( {required this.min, - this.useExtension = false, + this.isInclusive = false, required this.max, String Function(ShouldInBetweenDatesValidator validator, Field field)? buildErrorMessage, KeyType? key}) @@ -266,7 +266,7 @@ class ShouldInBetweenDatesValidator @override bool isValid(DateTime date) { - if(useExtension){ + if(isInclusive){ return (date.isBetween(min, max)); }else { return (date.compareTo(min) >= 0 && date.compareTo(max) <= 0); diff --git a/test/invalid_test.dart b/test/invalid_test.dart index 8515dbe..3af2e7b 100644 --- a/test/invalid_test.dart +++ b/test/invalid_test.dart @@ -336,6 +336,61 @@ void main() { false); }); }); +group('ShouldInBetweenDatesValidator', () { + test('Valid date within range (inclusive)', () { + final validator = ShouldInBetweenDatesValidator( + min: DateTime(2022, 1, 1), + max: DateTime(2022, 12, 31), + isInclusive: true, + ); + expect(validator.isValid(DateTime(2022, 6, 15)), true); + }); + + test('Valid date within range (exclusive)', () { + final validator = ShouldInBetweenDatesValidator( + min: DateTime(2022, 1, 1), + max: DateTime(2022, 12, 31), + isInclusive: false, + ); + expect(validator.isValid(DateTime(2022, 6, 15)), true); + }); + + test('Invalid date before range (inclusive)', () { + final validator = ShouldInBetweenDatesValidator( + min: DateTime(2022, 1, 1), + max: DateTime(2022, 12, 31), + isInclusive: true, + ); + expect(validator.isValid(DateTime(2021, 12, 31)), false); + }); + + test('Invalid date after range (inclusive)', () { + final validator = ShouldInBetweenDatesValidator( + min: DateTime(2022, 1, 1), + max: DateTime(2022, 12, 31), + isInclusive: true, + ); + expect(validator.isValid(DateTime(2023, 1, 1)), false); + }); + + test('valid date equal to min in range (Inclusive)', () { + final validator = ShouldInBetweenDatesValidator( + min: DateTime(2022, 1, 1), + max: DateTime(2022, 12, 31), + isInclusive: true, + ); + expect(validator.isValid(DateTime(2022, 1, 1)), true); + }); + + test('valid date equal to max in range (Inclusive)', () { + final validator = ShouldInBetweenDatesValidator( + min: DateTime(2022, 1, 1), + max: DateTime(2022, 12, 31), + isInclusive: true, + ); + expect(validator.isValid(DateTime(2022, 12, 31)), true); + }); + }); }); } From cab1d09809198b2437397bb70267a4bf958d9551 Mon Sep 17 00:00:00 2001 From: Salma Date: Fri, 5 Apr 2024 12:10:58 +0200 Subject: [PATCH 6/9] simplify ShouldInBetweenDatesValidator --- lib/src/logic/validators.dart | 32 ++------------------------------ test/invalid_test.dart | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 30 deletions(-) diff --git a/lib/src/logic/validators.dart b/lib/src/logic/validators.dart index 6e3b405..1822037 100644 --- a/lib/src/logic/validators.dart +++ b/lib/src/logic/validators.dart @@ -267,9 +267,9 @@ class ShouldInBetweenDatesValidator @override bool isValid(DateTime date) { if(isInclusive){ - return (date.isBetween(min, max)); - }else { return (date.compareTo(min) >= 0 && date.compareTo(max) <= 0); + }else { + return (date.compareTo(min) > 0 && date.compareTo(max) < 0); } } @@ -422,32 +422,4 @@ class ShouldBeBetweenOrEqualValidator @override List get props => [...super.props, min, max]; -} - - - extension DateTimeExtension on DateTime { - - bool isAfterOrEqualTo(DateTime dateTime) { - final isAtSameMomentAs = dateTime.isAtSameMomentAs(this); - return isAtSameMomentAs || isAfter(dateTime); - } - - bool isBeforeOrEqualTo(DateTime dateTime) { - final date = this; - - final isAtSameMomentAs = dateTime.isAtSameMomentAs(date); - return isAtSameMomentAs || date.isBefore(dateTime); - - } - - bool isBetween( - DateTime fromDateTime, - DateTime toDateTime, - ) { - final isAfter = isAfterOrEqualTo(fromDateTime) ; - final isBefore = isBeforeOrEqualTo(toDateTime) ; - return isAfter && isBefore; - } - - } diff --git a/test/invalid_test.dart b/test/invalid_test.dart index 3af2e7b..4b3f857 100644 --- a/test/invalid_test.dart +++ b/test/invalid_test.dart @@ -390,6 +390,24 @@ group('ShouldInBetweenDatesValidator', () { ); expect(validator.isValid(DateTime(2022, 12, 31)), true); }); + + test('valid date equal to min in range (Exclusive)', () { + final validator = ShouldInBetweenDatesValidator( + min: DateTime(2022, 1, 1), + max: DateTime(2022, 12, 31), + isInclusive: false, + ); + expect(validator.isValid(DateTime(2022, 1, 1)), false); + }); + + test('valid date equal to max in range (Exclusive)', () { + final validator = ShouldInBetweenDatesValidator( + min: DateTime(2022, 1, 1), + max: DateTime(2022, 12, 31), + isInclusive: false, + ); + expect(validator.isValid(DateTime(2022, 12, 31)), false); + }); }); }); } From 99b3209b2755038f8eb51dfe890ead3add7f6a37 Mon Sep 17 00:00:00 2001 From: Mostafa ElAwady Date: Mon, 11 Nov 2024 18:05:33 +0100 Subject: [PATCH 7/9] Change SDK number --- pubspec.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pubspec.yaml b/pubspec.yaml index b5c2096..d06d0ef 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,16 +1,16 @@ name: invalid -description: A new Flutter package project. +description: Package used for input validation. version: 0.0.1 environment: - sdk: ">=2.19.2 <3.0.0" + sdk: ">=3.0.0 <4.0.0" dependencies: flutter: sdk: flutter bloc: ^8.1.2 equatable: ^2.0.5 - intl: ^0.17.0 + intl: ^0.18.0 flutter_bloc: ^8.1.3 dev_dependencies: From 8842ca65053ac3f4e78ee7c4569d57b67d8da623 Mon Sep 17 00:00:00 2001 From: Mostafa ElAwady Date: Thu, 9 Jan 2025 14:39:43 +0100 Subject: [PATCH 8/9] Remove obsolete lint rule --- analysis_options.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/analysis_options.yaml b/analysis_options.yaml index 133121f..d39c96f 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -15,7 +15,6 @@ analyzer: linter: rules: - always_declare_return_types - - always_require_non_null_named_parameters - annotate_overrides - avoid_empty_else - avoid_init_to_null From 5f597316def7240b816d0d16ae3b289af6628802 Mon Sep 17 00:00:00 2001 From: Mostafa ElAwady Date: Thu, 9 Jan 2025 14:39:47 +0100 Subject: [PATCH 9/9] Update dart --- pubspec.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pubspec.yaml b/pubspec.yaml index d06d0ef..304d8ae 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -3,14 +3,14 @@ description: Package used for input validation. version: 0.0.1 environment: - sdk: ">=3.0.0 <4.0.0" + sdk: ">=3.5.0 <4.0.0" dependencies: flutter: sdk: flutter bloc: ^8.1.2 equatable: ^2.0.5 - intl: ^0.18.0 + intl: ^0.19.0 flutter_bloc: ^8.1.3 dev_dependencies: