Skip to content

Commit

Permalink
feature: implement support nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
wellgenio committed Dec 15, 2024
1 parent 3695c0d commit fd44bcd
Show file tree
Hide file tree
Showing 25 changed files with 2,119 additions and 71 deletions.
96 changes: 96 additions & 0 deletions lib/src/validations/exclusive_between_datetime_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,99 @@ extension ExclusiveBetweenDatetimeValidation
});
}
}

extension ExclusiveBetweenDatetimeNullableValidation
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') // event.start is nullable
/// .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 != null && (value.isAfter(start) && value.isBefore(end))) return null;

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

return ValidationException(message: currentMessage, code: currentCode);
});
}
}

extension ExclusiveBetweenDatetimeOrNullableValidation
on SimpleValidationBuilder<DateTime?> {
/// Adds a validation rule that checks if the [DateTime?] is greater than [comparison] or [null].
///
/// [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') //
/// .exclusiveBetweenOrNull(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?> exclusiveBetweenOrNull({
required DateTime start,
required DateTime end,
String? message,
String? code,
}) {
return use((value, entity) {
if (value == null || (value.isAfter(start) && value.isBefore(end))) return null;

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

return ValidationException(message: currentMessage, code: currentCode);
});
}
}
86 changes: 86 additions & 0 deletions lib/src/validations/greater_than_datetime_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,89 @@ extension GreaterThanDateTimeValidation on SimpleValidationBuilder<DateTime> {
});
}
}

extension GreaterThanDateTimeNullableValidation 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') // event.start is nullable
/// .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 != null && value.isAfter(comparison)) return null;

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

return ValidationException(message: currentMessage, code: currentCode);
});
}
}

extension GreaterThanDateTimeOrNullableValidation on SimpleValidationBuilder<DateTime?> {
/// Adds a validation rule that checks if the [DateTime?] is greater than [comparison] or [null].
///
/// [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') //
/// .greaterThanOrNull(DateTime.now());
/// ```
///
/// String format args:
/// - **{PropertyName}**: The name of the property.
/// - **{ComparisonValue}**: The value to compare against.
///
SimpleValidationBuilder<DateTime?> greaterThanOrNull(
DateTime comparison, {
String? message,
String? code,
}) {
return use((value, entity) {
if (value == null || value.isAfter(comparison)) return null;

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

return ValidationException(message: currentMessage, code: currentCode);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,101 @@ extension GreaterThanOrEqualToDateTimeValidation
String? code,
}) {
return use((value, entity) {
if (value.isAfter(comparison) || value.isAtSameMomentAs(comparison))
if (value.isAfter(comparison) || value.isAtSameMomentAs(comparison)) {
return null;
}

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

return ValidationException(message: currentMessage, code: currentCode);
});
}
}

extension GreaterThanOrEqualToDateTimeNullableValidation
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') // event.start is nullable
/// .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 != null && (value.isAfter(comparison) || value.isAtSameMomentAs(comparison))) {
return null;
}

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

return ValidationException(message: currentMessage, code: currentCode);
});
}
}

extension GreaterThanOrEqualToDateTimeOrNullableValidation
on SimpleValidationBuilder<DateTime?> {
/// Adds a validation rule that checks if the [DateTime?] is greater than [comparison] or [null].
///
/// [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') //
/// .greaterThanOrEqualToOrNull(DateTime.now());
/// ```
///
/// String format args:
/// - **{PropertyName}**: The name of the property.
/// - **{ComparisonValue}**: The value to compare against.
///
SimpleValidationBuilder<DateTime?> greaterThanOrEqualToOrNull(
DateTime comparison, {
String? message,
String? code,
}) {
return use((value, entity) {
if (value == null || (value.isAfter(comparison) || value.isAtSameMomentAs(comparison))) {
return null;
}

final currentCode = code ?? Language.code.greaterThanOrEqualToDateTime;
final currentMessage = LucidValidation.global.languageManager.translate(
Expand Down
86 changes: 86 additions & 0 deletions lib/src/validations/greater_than_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,89 @@ extension GreaterThanValidation on SimpleValidationBuilder<num> {
});
}
}

extension GreaterThanNullablealidation on SimpleValidationBuilder<num?> {
/// Adds a validation rule that checks if the [num?] is greater than [minValue].
///
/// [minValue] is the value that the number 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((user) => user.age, key: 'age') // user.age is nullable
/// .greaterThan(18);
/// ```
///
/// String format args:
/// - **{PropertyName}**: The name of the property.
/// - **{ComparisonValue}**: The value to compare against.
///
SimpleValidationBuilder<num?> greaterThan(
num minValue, {
String? message,
String? code,
}) {
return use((value, entity) {
if (value != null && value > minValue) return null;

final currentCode = code ?? Language.code.greaterThan;
final currentMessage = LucidValidation.global.languageManager.translate(
currentCode,
parameters: {
'PropertyName': label.isNotEmpty ? label : key,
'ComparisonValue': '$minValue',
},
defaultMessage: message,
);

return ValidationException(message: currentMessage, code: currentCode);
});
}
}

extension GreaterThanOrNullableValidation on SimpleValidationBuilder<num?> {
/// Adds a validation rule that checks if the [num?] is greater than [minValue] or [null].
///
/// [minValue] is the value that the number 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((user) => user.age, key: 'age')
/// .greaterThanOrNull(18);
/// ```
///
/// String format args:
/// - **{PropertyName}**: The name of the property.
/// - **{ComparisonValue}**: The value to compare against.
///
SimpleValidationBuilder<num?> greaterThanOrNull(
num minValue, {
String? message,
String? code,
}) {
return use((value, entity) {
if (value == null || value > minValue) return null;

final currentCode = code ?? Language.code.greaterThan;
final currentMessage = LucidValidation.global.languageManager.translate(
currentCode,
parameters: {
'PropertyName': label.isNotEmpty ? label : key,
'ComparisonValue': '$minValue',
},
defaultMessage: message,
);

return ValidationException(message: currentMessage, code: currentCode);
});
}
}
Loading

0 comments on commit fd44bcd

Please sign in to comment.