Skip to content
1 change: 0 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion lib/src/logic/validators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,11 @@ class ShouldInBetweenDatesValidator<KeyType>
extends FieldValidator<DateTime, KeyType, ShouldInBetweenDatesValidator<KeyType>> {
final DateTime max;
final DateTime min;
final bool isInclusive;

ShouldInBetweenDatesValidator(
{required this.min,
this.isInclusive = false,
required this.max,
String Function(ShouldInBetweenDatesValidator<KeyType> validator, Field field)? buildErrorMessage,
KeyType? key})
Expand All @@ -264,7 +266,11 @@ class ShouldInBetweenDatesValidator<KeyType>

@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
Expand Down
12 changes: 6 additions & 6 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
73 changes: 73 additions & 0 deletions test/invalid_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,79 @@ void main() {
false);
});
});
group('ShouldInBetweenDatesValidator', () {
test('Valid date within range (inclusive)', () {
final validator = ShouldInBetweenDatesValidator<DateTime>(
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<DateTime>(
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<DateTime>(
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<DateTime>(
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<DateTime>(
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<DateTime>(
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<DateTime>(
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<DateTime>(
min: DateTime(2022, 1, 1),
max: DateTime(2022, 12, 31),
isInclusive: false,
);
expect(validator.isValid(DateTime(2022, 12, 31)), false);
});
});
});
}

Expand Down