Vogen
Vogen copied to clipboard
Validation with TimeProvider
Describe the feature
Is there a way to validate a DateTimeOffset, DateTime, DateOnly or TimeOnly against a TimeProvider?
I order to make the code unit testable, we need to be time lords :-).
[ValueObject<DateOnly>]
internal readonly partial record struct ExpiryDate
{
private const string ExpiryDatePassed = "The expiration date has passed already.";
private static Validation Validate(DateOnly input, TimeProvider timeProvider)
{
DateOnly lowerLimit = DateOnly.FromDateTime(timeProvider.GetLocalNow().DateTime);
return input < lowerLimit
? Validation.Invalid(ExpiryDatePassed)
: Validation.Ok;
}
}