Vogen icon indicating copy to clipboard operation
Vogen copied to clipboard

Validation with TimeProvider

Open ckress opened this issue 3 weeks ago • 0 comments

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;
    }
}

ckress avatar Dec 05 '25 18:12 ckress