fix(validation): guard null comparison bounds in comparison builders
Passing a null bound to GreaterThan/LessThan/Between etc. called value.CompareTo(null), throwing NullReferenceException for non-null reference-type values. Short-circuit when the bound is null so the rule is satisfied instead of crashing.
This commit is contained in:
parent
5b368602fe
commit
fd6ed64755
4 changed files with 92 additions and 5 deletions
|
|
@ -170,7 +170,7 @@ public static class RuleBuilderExtensions
|
|||
{
|
||||
ArgumentNullException.ThrowIfNull(rule);
|
||||
|
||||
return rule.Must(value => IsNull(value) || value.CompareTo(comparisonValue) > 0,
|
||||
return rule.Must(value => IsNull(value) || IsNull(comparisonValue) || value.CompareTo(comparisonValue) > 0,
|
||||
$"Value must be greater than {comparisonValue}.");
|
||||
}
|
||||
|
||||
|
|
@ -189,7 +189,7 @@ public static class RuleBuilderExtensions
|
|||
{
|
||||
ArgumentNullException.ThrowIfNull(rule);
|
||||
|
||||
return rule.Must(value => IsNull(value) || value.CompareTo(comparisonValue) >= 0,
|
||||
return rule.Must(value => IsNull(value) || IsNull(comparisonValue) || value.CompareTo(comparisonValue) >= 0,
|
||||
$"Value must be greater than or equal to {comparisonValue}.");
|
||||
}
|
||||
|
||||
|
|
@ -208,7 +208,7 @@ public static class RuleBuilderExtensions
|
|||
{
|
||||
ArgumentNullException.ThrowIfNull(rule);
|
||||
|
||||
return rule.Must(value => IsNull(value) || value.CompareTo(comparisonValue) < 0,
|
||||
return rule.Must(value => IsNull(value) || IsNull(comparisonValue) || value.CompareTo(comparisonValue) < 0,
|
||||
$"Value must be less than {comparisonValue}.");
|
||||
}
|
||||
|
||||
|
|
@ -227,7 +227,7 @@ public static class RuleBuilderExtensions
|
|||
{
|
||||
ArgumentNullException.ThrowIfNull(rule);
|
||||
|
||||
return rule.Must(value => IsNull(value) || value.CompareTo(comparisonValue) <= 0,
|
||||
return rule.Must(value => IsNull(value) || IsNull(comparisonValue) || value.CompareTo(comparisonValue) <= 0,
|
||||
$"Value must be less than or equal to {comparisonValue}.");
|
||||
}
|
||||
|
||||
|
|
@ -254,7 +254,9 @@ public static class RuleBuilderExtensions
|
|||
"Maximum value must be greater than or equal to minimum value.");
|
||||
}
|
||||
|
||||
return rule.Must(value => IsNull(value) || (value.CompareTo(minValue) >= 0 && value.CompareTo(maxValue) <= 0),
|
||||
return rule.Must(value => IsNull(value)
|
||||
|| ((minValue is null || value.CompareTo(minValue) >= 0)
|
||||
&& (maxValue is null || value.CompareTo(maxValue) <= 0)),
|
||||
$"Value must be between {minValue} and {maxValue}.");
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue