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 diff --git a/lib/src/logic/validators.dart b/lib/src/logic/validators.dart index 621a49c..1822037 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 isInclusive; ShouldInBetweenDatesValidator( {required this.min, + this.isInclusive = 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.compareTo(min) > 0 && date.compareTo(max) < 0); + if(isInclusive){ + return (date.compareTo(min) >= 0 && date.compareTo(max) <= 0); + }else { + return (date.compareTo(min) > 0 && date.compareTo(max) < 0); + } } @override diff --git a/pubspec.yaml b/pubspec.yaml index ec9e2d4..304d8ae 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,19 +1,19 @@ 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.5.0 <4.0.0" 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 + intl: ^0.19.0 + flutter_bloc: ^8.1.3 dev_dependencies: - bloc_test: ^8.1.0 + bloc_test: ^9.1.5 flutter_test: sdk: flutter diff --git a/test/invalid_test.dart b/test/invalid_test.dart index 8515dbe..4b3f857 100644 --- a/test/invalid_test.dart +++ b/test/invalid_test.dart @@ -336,6 +336,79 @@ 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); + }); + + 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); + }); + }); }); }