Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 156 additions & 15 deletions src/GuardClauses/GuardAgainstNegativeExtensions.cs

Large diffs are not rendered by default.

31 changes: 26 additions & 5 deletions src/GuardClauses/GuardAgainstNotFoundExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using JetBrainsInvokerParameterNameAttribute = JetBrains.Annotations.InvokerParameterNameAttribute;
using JetBrainsNoEnumerationAttribute = JetBrains.Annotations.NoEnumerationAttribute;
using JetBrainsNotNullAttribute = JetBrains.Annotations.NotNullAttribute;
using JetBrainsInvokerParameterNameAttribute = JetBrains.Annotations.InvokerParameterNameAttribute;

namespace Ardalis.GuardClauses
{
Expand All @@ -17,13 +18,23 @@ public static partial class GuardClauseExtensions
/// <param name="parameterName"></param>
/// <returns><paramref name="input" /> if the value is not null.</returns>
/// <exception cref="NotFoundException"></exception>
public static T NotFound<T>([JetBrainsNotNull] this IGuardClause guardClause, [NotNull, JetBrainsNotNull][ValidatedNotNull] string key, [NotNull, JetBrainsNotNull][ValidatedNotNull][JetBrainsNoEnumeration] T input, [JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName)
#if NETSTANDARD || NETFRAMEWORK
public static T NotFound<T>([JetBrainsNotNull] this IGuardClause guardClause,
[NotNull, JetBrainsNotNull][ValidatedNotNull] string key,
[NotNull, JetBrainsNotNull][ValidatedNotNull][JetBrainsNoEnumeration] T input,
[JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName)
#else
public static T NotFound<T>([JetBrainsNotNull] this IGuardClause guardClause,
[NotNull, JetBrainsNotNull][ValidatedNotNull] string key,
[NotNull, JetBrainsNotNull][ValidatedNotNull][JetBrainsNoEnumeration] T input,
[JetBrainsNotNull][CallerArgumentExpression("input")] string? parameterName = null)
#endif
{
guardClause.NullOrEmpty(key, nameof(key));

if (input is null)
{
throw new NotFoundException(key, parameterName);
throw new NotFoundException(key, parameterName!);
}

return input;
Expand All @@ -40,14 +51,24 @@ public static T NotFound<T>([JetBrainsNotNull] this IGuardClause guardClause, [N
/// <param name="parameterName"></param>
/// <returns><paramref name="input" /> if the value is not null.</returns>
/// <exception cref="NotFoundException"></exception>
public static T NotFound<TKey, T>([JetBrainsNotNull] this IGuardClause guardClause, [NotNull, JetBrainsNotNull][ValidatedNotNull] TKey key, [NotNull, JetBrainsNotNull][ValidatedNotNull][JetBrainsNoEnumeration] T input, [JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName) where TKey : struct
#if NETSTANDARD || NETFRAMEWORK
public static T NotFound<TKey, T>([JetBrainsNotNull] this IGuardClause guardClause,
[NotNull, JetBrainsNotNull][ValidatedNotNull] TKey key,
[NotNull, JetBrainsNotNull][ValidatedNotNull][JetBrainsNoEnumeration] T input,
[JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName) where TKey : struct
#else
public static T NotFound<TKey, T>([JetBrainsNotNull] this IGuardClause guardClause,
[NotNull, JetBrainsNotNull][ValidatedNotNull] TKey key,
[NotNull, JetBrainsNotNull][ValidatedNotNull][JetBrainsNoEnumeration] T input,
[JetBrainsNotNull][JetBrainsInvokerParameterName][CallerArgumentExpression("input")] string? parameterName = null) where TKey : struct
#endif
{
guardClause.Null(key, nameof(key));

if (input is null)
{
// TODO: Can we safely consider that ToString() won't return null for struct?
throw new NotFoundException(key.ToString()!, parameterName);
throw new NotFoundException(key.ToString()!, parameterName!);
}

return input;
Expand Down
42 changes: 40 additions & 2 deletions src/GuardClauses/GuardAgainstNullExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using JetBrainsInvokerParameterNameAttribute = JetBrains.Annotations.InvokerParameterNameAttribute;
using JetBrainsNoEnumerationAttribute = JetBrains.Annotations.NoEnumerationAttribute;
using JetBrainsNotNullAttribute = JetBrains.Annotations.NotNullAttribute;
using JetBrainsInvokerParameterNameAttribute = JetBrains.Annotations.InvokerParameterNameAttribute;

namespace Ardalis.GuardClauses
{
Expand Down Expand Up @@ -61,10 +61,17 @@ public static T Null<T>([JetBrainsNotNull] this IGuardClause guardClause,
/// <returns><paramref name="input" /> if the value is not an empty string or null.</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
#if NETSTANDARD || NETFRAMEWORK
public static string NullOrEmpty([JetBrainsNotNull] this IGuardClause guardClause,
[NotNull, JetBrainsNotNull][ValidatedNotNull] string? input,
[JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName,
string? message = null)
#else
public static string NullOrEmpty([JetBrainsNotNull] this IGuardClause guardClause,
[NotNull, JetBrainsNotNull][ValidatedNotNull] string? input,
[JetBrainsNotNull][JetBrainsInvokerParameterName][CallerArgumentExpression("input")] string? parameterName = null,
string? message = null)
#endif
{
Guard.Against.Null(input, parameterName, message);
if (input == string.Empty)
Expand All @@ -86,10 +93,17 @@ public static string NullOrEmpty([JetBrainsNotNull] this IGuardClause guardClaus
/// <returns><paramref name="input" /> if the value is not an empty guid or null.</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
#if NETSTANDARD || NETFRAMEWORK
public static Guid NullOrEmpty([JetBrainsNotNull] this IGuardClause guardClause,
[NotNull, JetBrainsNotNull][ValidatedNotNull] Guid? input,
[JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName,
string? message = null)
#else
public static Guid NullOrEmpty([JetBrainsNotNull] this IGuardClause guardClause,
[NotNull, JetBrainsNotNull][ValidatedNotNull] Guid? input,
[JetBrainsNotNull][JetBrainsInvokerParameterName][CallerArgumentExpression("input")] string? parameterName = null,
string? message = null)
#endif
{
Guard.Against.Null(input, parameterName, message);
if (input == Guid.Empty)
Expand All @@ -111,10 +125,17 @@ public static Guid NullOrEmpty([JetBrainsNotNull] this IGuardClause guardClause,
/// <returns><paramref name="input" /> if the value is not an empty enumerable or null.</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
#if NETSTANDARD || NETFRAMEWORK
public static IEnumerable<T> NullOrEmpty<T>([JetBrainsNotNull] this IGuardClause guardClause,
[NotNull, JetBrainsNotNull][ValidatedNotNull] IEnumerable<T>? input,
[JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName,
string? message = null)
#else
public static IEnumerable<T> NullOrEmpty<T>([JetBrainsNotNull] this IGuardClause guardClause,
[NotNull, JetBrainsNotNull][ValidatedNotNull] IEnumerable<T>? input,
[JetBrainsNotNull][JetBrainsInvokerParameterName][CallerArgumentExpression("input")] string? parameterName = null,
string? message = null)
#endif
{
Guard.Against.Null(input, parameterName, message);
if (!input.Any())
Expand All @@ -136,10 +157,17 @@ public static IEnumerable<T> NullOrEmpty<T>([JetBrainsNotNull] this IGuardClause
/// <returns><paramref name="input" /> if the value is not an empty or whitespace string.</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
#if NETSTANDARD || NETFRAMEWORK
public static string NullOrWhiteSpace([JetBrainsNotNull] this IGuardClause guardClause,
[NotNull, JetBrainsNotNull][ValidatedNotNull] string? input,
[JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName,
string? message = null)
#else
public static string NullOrWhiteSpace([JetBrainsNotNull] this IGuardClause guardClause,
[NotNull, JetBrainsNotNull][ValidatedNotNull] string? input,
[JetBrainsNotNull][JetBrainsInvokerParameterName][CallerArgumentExpression("input")] string? parameterName = null,
string? message = null)
#endif
{
Guard.Against.NullOrEmpty(input, parameterName, message);
if (String.IsNullOrWhiteSpace(input))
Expand All @@ -159,7 +187,17 @@ public static string NullOrWhiteSpace([JetBrainsNotNull] this IGuardClause guard
/// <param name="message">Optional. Custom error message</param>
/// <returns><paramref name="input" /> if the value is not default for that type.</returns>
/// <exception cref="ArgumentException"></exception>
public static T Default<T>([JetBrainsNotNull] this IGuardClause guardClause, [AllowNull, NotNull, JetBrainsNotNull][JetBrainsNoEnumeration] T input, [JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName, string? message = null)
#if NETSTANDARD || NETFRAMEWORK
public static T Default<T>([JetBrainsNotNull] this IGuardClause guardClause,
[AllowNull, NotNull, JetBrainsNotNull][JetBrainsNoEnumeration] T input,
[JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName,
string? message = null)
#else
public static T Default<T>([JetBrainsNotNull] this IGuardClause guardClause,
[AllowNull, NotNull, JetBrainsNotNull][JetBrainsNoEnumeration] T input,
[JetBrainsNotNull][JetBrainsInvokerParameterName][CallerArgumentExpression("input")] string? parameterName = null,
string? message = null)
#endif
{
if (EqualityComparer<T>.Default.Equals(input, default(T)!) || input is null)
{
Expand Down
36 changes: 32 additions & 4 deletions src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using JetBrainsNotNullAttribute = JetBrains.Annotations.NotNullAttribute;
using System.Runtime.CompilerServices;
using JetBrainsInvokerParameterNameAttribute = JetBrains.Annotations.InvokerParameterNameAttribute;
using JetBrainsNotNullAttribute = JetBrains.Annotations.NotNullAttribute;

namespace Ardalis.GuardClauses
{
Expand All @@ -19,7 +20,17 @@ public static partial class GuardClauseExtensions
/// <param name="message">Optional. Custom error message</param>
/// <returns><paramref name="input" /> if the value is not out of range.</returns>
/// <exception cref="InvalidEnumArgumentException"></exception>
public static int EnumOutOfRange<T>([JetBrainsNotNull] this IGuardClause guardClause, int input, [JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName, string? message = null) where T : struct, Enum
#if NETSTANDARD || NETFRAMEWORK
public static int EnumOutOfRange<T>([JetBrainsNotNull] this IGuardClause guardClause,
int input,
[JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName,
string? message = null) where T : struct, Enum
#else
public static int EnumOutOfRange<T>([JetBrainsNotNull] this IGuardClause guardClause,
int input,
[JetBrainsNotNull][JetBrainsInvokerParameterName][CallerArgumentExpression("input")] string? parameterName = null,
string? message = null) where T : struct, Enum
#endif
{
if (!Enum.IsDefined(typeof(T), input))
{
Expand All @@ -43,7 +54,17 @@ public static int EnumOutOfRange<T>([JetBrainsNotNull] this IGuardClause guardCl
/// /// <param name="message">Optional. Custom error message</param>
/// <returns><paramref name="input" /> if the value is not out of range.</returns>
/// <exception cref="InvalidEnumArgumentException"></exception>
public static T EnumOutOfRange<T>([JetBrainsNotNull] this IGuardClause guardClause, T input, [JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName, string? message = null) where T : struct, Enum
#if NETSTANDARD || NETFRAMEWORK
public static T EnumOutOfRange<T>([JetBrainsNotNull] this IGuardClause guardClause,
T input,
[JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName,
string? message = null) where T : struct, Enum
#else
public static T EnumOutOfRange<T>([JetBrainsNotNull] this IGuardClause guardClause,
T input,
[JetBrainsNotNull][JetBrainsInvokerParameterName][CallerArgumentExpression("input")] string? parameterName = null,
string? message = null) where T : struct, Enum
#endif
{
if (!Enum.IsDefined(typeof(T), input))
{
Expand Down Expand Up @@ -96,16 +117,23 @@ public static IEnumerable<T> OutOfRange<T>(this IGuardClause guardClause, IEnume
/// <param name="message">Optional. Custom error message</param>
/// <returns><paramref name="input" /> if the value is in the range of valid SqlDateTime values.</returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
#if NETSTANDARD || NETFRAMEWORK
public static DateTime OutOfSQLDateRange([JetBrainsNotNull] this IGuardClause guardClause,
DateTime input,
[JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName,
string? message = null)
#else
public static DateTime OutOfSQLDateRange([JetBrainsNotNull] this IGuardClause guardClause,
DateTime input,
[JetBrainsNotNull][JetBrainsInvokerParameterName][CallerArgumentExpression("input")] string? parameterName = null,
string? message = null)
#endif
{
// System.Data is unavailable in .NET Standard so we can't use SqlDateTime.
const long sqlMinDateTicks = 552877920000000000;
const long sqlMaxDateTicks = 3155378975999970000;

return OutOfRange<DateTime>(guardClause, input, parameterName, new DateTime(sqlMinDateTicks), new DateTime(sqlMaxDateTicks), message);
return OutOfRange<DateTime>(guardClause, input, parameterName!, new DateTime(sqlMinDateTicks), new DateTime(sqlMaxDateTicks), message);
}

/// <summary>
Expand Down
Loading