Skip to content

Commit

Permalink
#17 Reducing code complexity.
Browse files Browse the repository at this point in the history
  • Loading branch information
lsolano committed Nov 11, 2021
1 parent d243040 commit 0fb8862
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 72 deletions.
114 changes: 58 additions & 56 deletions src/Validations/Arguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,64 @@ public static string NotEmpty([ValidatedNotNull] string? value, [ValidatedNotNul

#endregion

#region Emptyness
/// <summary>
/// Checks that the provided value is not empty.
/// </summary>
/// <param name="value">Value to check</param>
/// <param name="paramName">Parameter name, from caller's context.</param>
/// <returns><paramref name="value"/></returns>
/// <exception cref="ArgumentException">If <paramref name="value"/> is an empty <see cref="Guid"/>.</exception>
[DebuggerStepThrough]
public static Guid NotEmpty(Guid value, [ValidatedNotNull] string paramName)
{
string validParamName = paramName.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly(nameof(paramName));

if (IsEmpty(value))
{
throw new ArgumentException(validParamName);
}

return value;
}

/// <summary>
/// Checks that the provided value is not empty.
/// </summary>
/// <remarks>
/// <para>
/// * Certain derivatives from <see cref="ArgumentException" /> such as <see cref="ArgumentNullException" />
/// does not allow direct message setting.
/// </para>
/// <para>
/// This one will use the provided message and used to build a final one.
/// </para>
/// </remarks>
/// <param name="value">Value to check</param>
/// <param name="paramName">Parameter name, from caller's context.</param>
/// <param name="customMessage">Custom exception error message</param>
/// <returns><paramref name="value"/></returns>
/// <exception cref="ArgumentException">If <paramref name="value"/> is an empty <see cref="Guid"/>.</exception>
[DebuggerStepThrough]
public static Guid NotEmpty(Guid value, [ValidatedNotNull] string paramName,
[ValidatedNotNull] string customMessage)
{
(string validParamName, string validCustomMessage) =
(paramName.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly(nameof(paramName)),
customMessage.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly(nameof(customMessage)));

if (IsEmpty(value))
{
throw new ArgumentException(paramName: validParamName, message: validCustomMessage);
}

return value;
}

private static bool IsEmpty(Guid value) => value == default;

#endregion //Emptyness

#region Enumerations Checks

/// <summary>
Expand Down Expand Up @@ -472,63 +530,7 @@ private static bool IsBase64String(string base64)

#endregion // Known Encodings

#region Emptyness
/// <summary>
/// Checks that the provided value is not empty.
/// </summary>
/// <param name="value">Value to check</param>
/// <param name="paramName">Parameter name, from caller's context.</param>
/// <returns><paramref name="value"/></returns>
/// <exception cref="ArgumentException">If <paramref name="value"/> is an empty <see cref="Guid"/>.</exception>
[DebuggerStepThrough]
public static Guid NotEmpty(Guid value, [ValidatedNotNull] string paramName)
{
string validParamName = paramName.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly(nameof(paramName));

if (IsEmpty(value))
{
throw new ArgumentException(validParamName);
}

return value;
}

/// <summary>
/// Checks that the provided value is not empty.
/// </summary>
/// <remarks>
/// <para>
/// * Certain derivatives from <see cref="ArgumentException" /> such as <see cref="ArgumentNullException" />
/// does not allow direct message setting.
/// </para>
/// <para>
/// This one will use the provided message and used to build a final one.
/// </para>
/// </remarks>
/// <param name="value">Value to check</param>
/// <param name="paramName">Parameter name, from caller's context.</param>
/// <param name="customMessage">Custom exception error message</param>
/// <returns><paramref name="value"/></returns>
/// <exception cref="ArgumentException">If <paramref name="value"/> is an empty <see cref="Guid"/>.</exception>
[DebuggerStepThrough]
public static Guid NotEmpty(Guid value, [ValidatedNotNull] string paramName,
[ValidatedNotNull] string customMessage)
{
(string validParamName, string validCustomMessage) =
(paramName.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly(nameof(paramName)),
customMessage.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly(nameof(customMessage)));

if (IsEmpty(value))
{
throw new ArgumentException(paramName: validParamName, message: validCustomMessage);
}

return value;
}

private static bool IsEmpty(Guid value) => value == default;

#endregion //Emptyness

#region General Purpose Checks

Expand Down
43 changes: 27 additions & 16 deletions src/Validations/Utilities/ComparableRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,42 +109,53 @@ internal TComparable IsWithin(TComparable value, string paramName, string? custo

private void CheckLowerBoundary(TComparable value, string paramName, string? customMessage)
{
if (!Min.HasValue)
if (!Min.HasValue || ValueIsNotBelowMinimum(value))
{
return;
}

int valueVersusMinimum = value.CompareTo(Min.ValueOrFailure);
bool valueBelowMinimum = MinInclusive ? valueVersusMinimum < 0 : valueVersusMinimum <= 0;
ThrowBoundaryViolatedException(value, paramName, customMessage, BoundaryViolations.Lower);
}

if (!valueBelowMinimum)
{
return;
}
private bool ValueIsNotBelowMinimum(TComparable value)
{
int valueVersusMinimum = value.CompareTo(Min.ValueOrFailure);

string orEqualToOption = MinInclusive ? "or equal to " : string.Empty;
throw new ArgumentOutOfRangeException(paramName, value,
customMessage ?? $"Must be greater than {orEqualToOption}{Min.ValueOrFailure}.");
return !(MinInclusive ? valueVersusMinimum < 0 : valueVersusMinimum <= 0);
}

private void CheckUpperBoundary(TComparable value, string paramName, string? customMessage)
{
if (!Max.HasValue)
if (!Max.HasValue || ValueIsNotAboveMaximum(value))
{
return;
}

ThrowBoundaryViolatedException(value, paramName, customMessage, BoundaryViolations.Upper);
}

private bool ValueIsNotAboveMaximum(TComparable value)
{
int valueVersusMaximum = value.CompareTo(Max.ValueOrFailure);
bool valueIsAboveMaximum = MaxInclusive ? valueVersusMaximum > 0 : valueVersusMaximum >= 0;

if (!valueIsAboveMaximum)
return !(MaxInclusive ? valueVersusMaximum > 0 : valueVersusMaximum >= 0);
}

private void ThrowBoundaryViolatedException(TComparable value, string paramName, string? customMessage,
BoundaryViolations violation)
{
static string OrEqualToOptionMessage(bool inclusive) => inclusive ? "or equal to " : string.Empty;

if (violation == BoundaryViolations.Lower)
{
return;
throw new ArgumentOutOfRangeException(paramName, value,
customMessage ?? $"Must be greater than {OrEqualToOptionMessage(MinInclusive)}{Min.ValueOrFailure}.");
}

string orEqualToOption = MaxInclusive ? "or equal to " : string.Empty;
throw new ArgumentOutOfRangeException(paramName, value,
customMessage ?? $"Must be less than {orEqualToOption}{Max.ValueOrFailure}.");
customMessage ?? $"Must be less than {OrEqualToOptionMessage(MaxInclusive)}{Max.ValueOrFailure}.");
}

private enum BoundaryViolations { Lower = 0, Upper = 1 }
}
#pragma warning restore CA1303 // Do not pass literals as localized parameters

0 comments on commit 0fb8862

Please sign in to comment.