Skip to content

Commit

Permalink
Refactor LengthOutOfRange.
Browse files Browse the repository at this point in the history
  • Loading branch information
HamidRezaAshkiyan committed Dec 25, 2023
1 parent 409fb6a commit 967e717
Showing 1 changed file with 5 additions and 25 deletions.
30 changes: 5 additions & 25 deletions src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using GuardClauses;

namespace Ardalis.GuardClauses;

Expand Down Expand Up @@ -36,31 +37,10 @@ public static string LengthOutOfRange(this IGuardClause guardClause,
string? message = null)
#endif
{
if (maxLength < minLength)
{
throw new ArgumentException(
message ??
$"Min length must be equal or less than max length.",
parameterName);
}

Guard.Against.NegativeOrZero(minLength, nameof(minLength));
if (input.Length < minLength)
{
throw new ArgumentException(
message ??
$"Input {parameterName} with length {input.Length} is too short. Minimum length is {minLength}.",
parameterName);
}

Guard.Against.NegativeOrZero(maxLength, nameof(maxLength));
if (input.Length > maxLength)
{
throw new ArgumentException(
message ??
$"Input {parameterName} with length {input.Length} is too long. Maxmimum length is {maxLength}.",
parameterName);
}
Guard.Against.Negative<int>(maxLength - minLength, parameterName: "min or max length",
message: "Min length must be equal or less than max length.");
Guard.Against.StringTooShort(input, minLength, nameof(minLength));
Guard.Against.StringTooLong(input, maxLength, nameof(maxLength));

return input;
}
Expand Down

0 comments on commit 967e717

Please sign in to comment.