Skip to content

Commit

Permalink
new date and time validations added
Browse files Browse the repository at this point in the history
  • Loading branch information
wellgenio committed Aug 28, 2024
1 parent 647cd96 commit 5346842
Show file tree
Hide file tree
Showing 15 changed files with 534 additions and 8 deletions.
12 changes: 12 additions & 0 deletions lib/src/localization/language.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ abstract class Language {
validCNPJ: 'validCNPJ',
validCreditCard: 'validCreditCard',
validEmail: 'validEmail',
greaterThanOrEqualToDateTime: 'greaterThanOrEqualToDateTime',
greaterThanDatetime: 'greaterThanDatetime',
lessThanOrEqualToDateTime: 'lessThanOrEqualToDateTime',
lessThanDateTime: 'lessThanDateTime',
inclusiveBetweenDatetime: 'inclusiveBetweenDatetime',
exclusiveBetweenDatetime: 'exclusiveBetweenDatetime',
);

Language(this.culture, [Map<String, String> translations = const {}]) {
Expand Down Expand Up @@ -55,6 +61,12 @@ abstract class Language {
code.validCNPJ: "'{PropertyName}' is not a valid CNPJ.",
code.validCreditCard: "'{PropertyName}' is not a valid credit card number.",
code.validEmail: "'{PropertyName}' is not a valid email address.",
code.greaterThanOrEqualToDateTime: "'{PropertyName}' must be greater than or equal to date '{ComparisonValue}'.",
code.greaterThanDatetime: "'{PropertyName}' must be greater than date '{ComparisonValue}'.",
code.lessThanOrEqualToDateTime: "'{PropertyName}' must be less than or equal to date '{ComparisonValue}'.",
code.lessThanDateTime: "'{PropertyName}' must be less than date '{ComparisonValue}'.",
code.inclusiveBetweenDatetime: "'{PropertyName}' must be less than or equal to '{StartValue}' date and less than or equal to '{EndValue}' date.",
code.exclusiveBetweenDatetime: "'{PropertyName}' must be less than the '{StartValue}' date and less than the '{EndValue}' date."
};

String? getTranslation(String key) => _translations[key];
Expand Down
52 changes: 52 additions & 0 deletions lib/src/validations/exclusive_between_datetime_validation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
part of 'validations.dart';

/// Extension on [LucidValidationBuilder] for [DateTime] properties to add a exclusive between validation.
///
/// This extension adds an `inclusiveBetween` method that can be used to
/// ensure that a date is exclusive between two specified dates.
extension ExclusiveBetweenDatetimeValidation on SimpleValidationBuilder<DateTime> {
/// Adds a validation rule that checks if the [DateTime] is greater than [comparison].
///
/// [start] is the date and time value must be greater than.
/// [end] is the date and time value must be less than.
/// [message] is the error message returned if the validation fails.
/// [code] is an optional error code for translation purposes.
///
/// Returns the [LucidValidationBuilder] to allow for method chaining.
///
/// Example:
/// ```dart
/// ...
/// .ruleFor((event) => event.start, key: 'start') //
/// .exclusiveBetween(start: DateTime.now(), end: DateTime.now().add(Duration(days: 1)));
/// ```
///
/// String format args:
/// - **{PropertyName}**: The name of the property.
/// - **{StartValue}**: The value must be greater than or equal to.
/// - **{EndValue}**: The value must be less than or equal to.
///
SimpleValidationBuilder<DateTime> exclusiveBetween({
required DateTime start,
required DateTime end,
String? message,
String? code,
}) {
return use((value, entity) {
if (value.isAfter(start) && value.isBefore(end)) return null;

final currentCode = code ?? Language.code.exclusiveBetweenDatetime;
final currentMessage = LucidValidation.global.languageManager.translate(
currentCode,
parameters: {
'PropertyName': key,
'StartValue': start.toString(),
'EndValue': end.toString(),
},
defaultMessage: message,
);

return ValidationException(message: currentMessage, code: currentCode);
});
}
}
48 changes: 48 additions & 0 deletions lib/src/validations/greater_than_datetime_validation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
part of 'validations.dart';

/// Extension on [LucidValidationBuilder] for [DateTime] properties to add a greater than validation.
///
/// This extension adds a `greaterThan` method that can be used to ensure that
/// a date is greater than a specified date.
extension GreaterThanDateTimeValidation on SimpleValidationBuilder<DateTime> {
/// Adds a validation rule that checks if the [DateTime] is greater than [comparison].
///
/// [comparison] is the date and time value must be greater than.
/// [message] is the error message returned if the validation fails.
/// [code] is an optional error code for translation purposes.
///
/// Returns the [LucidValidationBuilder] to allow for method chaining.
///
/// Example:
/// ```dart
/// ...
/// .ruleFor((event) => event.start, key: 'start') //
/// .greaterThan(DateTime.now());
/// ```
///
/// String format args:
/// - **{PropertyName}**: The name of the property.
/// - **{ComparisonValue}**: The value to compare against.
///
SimpleValidationBuilder<DateTime> greaterThan(
DateTime comparison, {
String? message,
String? code,
}) {
return use((value, entity) {
if (value.isAfter(comparison)) return null;

final currentCode = code ?? Language.code.greaterThanDatetime;
final currentMessage = LucidValidation.global.languageManager.translate(
currentCode,
parameters: {
'PropertyName': key,
'ComparisonValue': comparison.toString(),
},
defaultMessage: message,
);

return ValidationException(message: currentMessage, code: currentCode);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
part of 'validations.dart';

/// Extension on [LucidValidationBuilder] for [DateTime] properties to add a greater than validation.
///
/// This extension adds a `greaterThanOrEqualTo` method that can be used to ensure that
/// a date is greater than or equal to a specified date.
extension GreaterThanOrEqualToDateTimeValidation on SimpleValidationBuilder<DateTime> {
/// Adds a validation rule that checks if the [DateTime] is greater than [comparison].
///
/// [comparison] is the date and time value must be greater than or equal.
/// [message] is the error message returned if the validation fails.
/// [code] is an optional error code for translation purposes.
///
/// Returns the [LucidValidationBuilder] to allow for method chaining.
///
/// Example:
/// ```dart
/// ...
/// .ruleFor((event) => event.start, key: 'start') //
/// .greaterThanOrEqualTo(DateTime.now());
/// ```
///
/// String format args:
/// - **{PropertyName}**: The name of the property.
/// - **{ComparisonValue}**: The value to compare against.
///
SimpleValidationBuilder<DateTime> greaterThanOrEqualTo(
DateTime comparison, {
String? message,
String? code,
}) {
return use((value, entity) {
if (value.isAfter(comparison) || value.isAtSameMomentAs(comparison)) return null;

final currentCode = code ?? Language.code.greaterThanOrEqualToDateTime;
final currentMessage = LucidValidation.global.languageManager.translate(
currentCode,
parameters: {
'PropertyName': key,
'ComparisonValue': comparison.toString(),
},
defaultMessage: message,
);

return ValidationException(message: currentMessage, code: currentCode);
});
}
}
52 changes: 52 additions & 0 deletions lib/src/validations/inclusive_between_datetime_validation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
part of 'validations.dart';

/// Extension on [LucidValidationBuilder] for [DateTime] properties to add a inclusive between validation.
///
/// This extension adds an `inclusiveBetween` method that can be used to
/// ensure that a date is inclusive between two specified dates.
extension InclusiveBetweenDatetimeValidation on SimpleValidationBuilder<DateTime> {
/// Adds a validation rule that checks if the [DateTime] is greater than [comparison].
///
/// [start] is the date and time value must be greater than or equal to.
/// [end] is the date and time value must be less than or equal to.
/// [message] is the error message returned if the validation fails.
/// [code] is an optional error code for translation purposes.
///
/// Returns the [LucidValidationBuilder] to allow for method chaining.
///
/// Example:
/// ```dart
/// ...
/// .ruleFor((event) => event.start, key: 'start') //
/// .inclusiveBetween(start: DateTime.now(), end: DateTime.now().add(Duration(days: 1)));
/// ```
///
/// String format args:
/// - **{PropertyName}**: The name of the property.
/// - **{StartValue}**: The value must be greater than or equal to.
/// - **{EndValue}**: The value must be less than or equal to.
///
SimpleValidationBuilder<DateTime> inclusiveBetween({
required DateTime start,
required DateTime end,
String? message,
String? code,
}) {
return use((value, entity) {
if (value.isAfter(start) || value.isAtSameMomentAs(start) && value.isBefore(end) || value.isAtSameMomentAs(end)) return null;

final currentCode = code ?? Language.code.inclusiveBetweenDatetime;
final currentMessage = LucidValidation.global.languageManager.translate(
currentCode,
parameters: {
'PropertyName': key,
'StartValue': start.toString(),
'EndValue': end.toString(),
},
defaultMessage: message,
);

return ValidationException(message: currentMessage, code: currentCode);
});
}
}
48 changes: 48 additions & 0 deletions lib/src/validations/less_than_datetime_validation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
part of 'validations.dart';

/// Extension on [LucidValidationBuilder] for [DateTime] properties to add a less than validation.
///
/// This extension adds a `lessThan` method that can be used to ensure that
/// a date is less than a specified date.
extension LessThanDatetimeValidation on SimpleValidationBuilder<DateTime> {
/// Adds a validation rule that checks if the [DateTime] is greater than [comparison].
///
/// [comparison] is the date and time value must be less than.
/// [message] is the error message returned if the validation fails.
/// [code] is an optional error code for translation purposes.
///
/// Returns the [LucidValidationBuilder] to allow for method chaining.
///
/// Example:
/// ```dart
/// ...
/// .ruleFor((event) => event.end, key: 'start') //
/// .lessThan(DateTime.now());
/// ```
///
/// String format args:
/// - **{PropertyName}**: The name of the property.
/// - **{ComparisonValue}**: The value to compare against.
///
SimpleValidationBuilder<DateTime> lessThan(
DateTime comparison, {
String? message,
String? code,
}) {
return use((value, entity) {
if (value.isBefore(comparison)) return null;

final currentCode = code ?? Language.code.lessThanDateTime;
final currentMessage = LucidValidation.global.languageManager.translate(
currentCode,
parameters: {
'PropertyName': key,
'ComparisonValue': comparison.toString(),
},
defaultMessage: message,
);

return ValidationException(message: currentMessage, code: currentCode);
});
}
}
48 changes: 48 additions & 0 deletions lib/src/validations/less_than_or_equal_to_datetime_validation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
part of 'validations.dart';

/// Extension on [LucidValidationBuilder] for [DateTime] properties to add a less than validation.
///
/// This extension adds a `lessThanOrEqualTo` method that can be used to ensure that
/// a date is less than or equal to a specified date.
extension LessThanOrEqualToDatetimeValidation on SimpleValidationBuilder<DateTime> {
/// Adds a validation rule that checks if the [DateTime] is greater than [comparison].
///
/// [comparison] is the date and time value must be less than or equal to.
/// [message] is the error message returned if the validation fails.
/// [code] is an optional error code for translation purposes.
///
/// Returns the [LucidValidationBuilder] to allow for method chaining.
///
/// Example:
/// ```dart
/// ...
/// .ruleFor((event) => event.end, key: 'start') //
/// .lessThanOrEqualTo(DateTime.now());
/// ```
///
/// String format args:
/// - **{PropertyName}**: The name of the property.
/// - **{ComparisonValue}**: The value to compare against.
///
SimpleValidationBuilder<DateTime> lessThanOrEqualTo(
DateTime comparison, {
String? message,
String? code,
}) {
return use((value, entity) {
if (value.isBefore(comparison) || value.isAtSameMomentAs(comparison)) return null;

final currentCode = code ?? Language.code.lessThanOrEqualToDateTime;
final currentMessage = LucidValidation.global.languageManager.translate(
currentCode,
parameters: {
'PropertyName': key,
'ComparisonValue': comparison.toString(),
},
defaultMessage: message,
);

return ValidationException(message: currentMessage, code: currentCode);
});
}
}
6 changes: 6 additions & 0 deletions lib/src/validations/validations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import '../../lucid_validation.dart';

part 'equal_validation.dart';
part 'greater_than_validation.dart';
part 'greater_than_datetime_validation.dart';
part 'greater_than_or_equal_to_datetime_validation.dart';
part 'less_than_or_equal_to_datetime_validation.dart';
part 'less_than_datetime_validation.dart';
part 'inclusive_between_datetime_validation.dart';
part 'exclusive_between_datetime_validation.dart';
part 'is_empty_validation.dart';
part 'is_not_null_validation.dart';
part 'is_null_validation.dart';
Expand Down
18 changes: 10 additions & 8 deletions test/mocks/mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ extension CustomValidPhoneValidator on LucidValidationBuilder<String, dynamic> {
}
}

extension CustomValidPasswordValidator
on LucidValidationBuilder<String, dynamic> {
extension CustomValidPasswordValidator on LucidValidationBuilder<String, dynamic> {
LucidValidationBuilder<String, dynamic> customValidPassword() {
return notEmpty() //
.minLength(5, message: 'Must be at least 8 characters long')
Expand Down Expand Up @@ -86,13 +85,10 @@ class CredentialsRegisterValidator extends LucidValidator<CredentialsRegister> {

ruleFor((credentials) => credentials.password, key: 'password') //
.customValidPassword()
.equalTo((entity) => entity.confirmPassword,
message: 'Must be equal to confirmPassword');
.equalTo((entity) => entity.confirmPassword, message: 'Must be equal to confirmPassword');

ruleFor((credentials) => credentials.confirmPassword,
key: 'confirmPassword') //
.equalTo((entity) => entity.password,
message: 'Must be equal to password');
ruleFor((credentials) => credentials.confirmPassword, key: 'confirmPassword') //
.equalTo((entity) => entity.password, message: 'Must be equal to password');
}
}

Expand Down Expand Up @@ -144,6 +140,12 @@ class CustomerValidator extends LucidValidator<Customer> {
}
}

class EventModel {
DateTime start = DateTime.now();
DateTime end = DateTime.now();
DateTime dateEvent = DateTime.now();
}

class CreditCardModel {
String number = '';
}
Expand Down
Loading

0 comments on commit 5346842

Please sign in to comment.