-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Closed
Description
Is your feature request related to a problem? Please describe.
No response
Describe the solution you'd like
Setup
Say there are the following classes in a solution.
public class Range<T> where T : struct, IComparable<T>, IComparable
{
public RangeBoundary<T>? Minimum { get; set; }
public RangeBoundary<T>? Maximum { get; set; }
}
public class RangeBoundary<T> where T : struct, IComparable<T>, IComparable
{
public T? Value { get; set; }
public bool? Inclusive { get; set; }
}In this scenario, say the two different types that may be used for T are decimal and DateTime. Validators for RangeBoundary may look something like this:
public class RangeBoundaryValidator<T> : AbstractValidator<RangeBoundary<T>> where T : struct, IComparable<T>, IComparable
{
public RangeBoundaryValidator()
{
RuleFor(boundary => boundary.Inclusive)
.NotNull();
RuleFor(boundary => boundary.Value)
.NotNull();
}
}Proposed Solution
It would be ideal to be able to support have something like any of the following within RangeBoundaryValidator:
Option 1
RuleFor(boundary => boundary.Value)
.WithGeneric<decimal>()
.GreaterThanOrEqualTo(0)
.WithGeneric<DateTime>()
.GreaterThanOrEqualTo(DateTime.UnixEpoch);Option 2
RuleFor(boundary => boundary.Value)
.SetGenericValidator<decimal>(new GreaterthanOrEqualValidator(0))
.SetGenericValidator<DateTime>(new GreaterthanOrEqualValidator(DateTime.UnixEpoch));Describe alternatives you've considered
Current options that have worked but definitely give some code smell:
Check outside of the Fluent calls
if (typeof(T) == typeof(decimal))
{
RuleFor(boundary => boundary.Value as decimal?)
.GreaterThanOrEqualTo(0);
}
else if (typeof(T) == typeof(DateTimeOffset))
{
RuleFor(boundary => boundary.Value as DateTimeOffset?)
.GreaterThanOrEqualTo(DateTimeOffset.UnixEpoch);
}Check using Fluent's When
When(boundary => boundary?.Value is decimal, () => {
RuleFor(boundary => boundary.Value as decimal?)
.GreaterThanOrEqualTo(0);
});
When(boundary => boundary?.Value is DateTime, () => {
RuleFor(boundary => boundary.Value as DateTime?)
.GreaterThanOrEqualTo(DateTime.UnixEpoch);
});Additional Context
No response
Metadata
Metadata
Assignees
Labels
No labels