diff --git a/src/GuardClauses/GuardAgainstExpressionExtensions.cs b/src/GuardClauses/GuardAgainstExpressionExtensions.cs
index ae78d00..6351294 100644
--- a/src/GuardClauses/GuardAgainstExpressionExtensions.cs
+++ b/src/GuardClauses/GuardAgainstExpressionExtensions.cs
@@ -1,73 +1,60 @@
using System;
using System.Threading.Tasks;
+using System.Runtime.CompilerServices;
namespace Ardalis.GuardClauses;
public static partial class GuardClauseExtensions
{
///
- /// Throws an if evaluates to false for given
+ /// Validates the using the specified and throws an if it evaluates to true.
+ /// The should return true to indicate an invalid or undesirable state of the input.
+ /// If returns true, an is thrown, signifying that the input is invalid.
///
- ///
- ///
- ///
- ///
- ///
- /// if the evaluates to true
- ///
- public static T AgainstExpression(this IGuardClause guardClause,
+ /// The type of the input parameter.
+ /// The guard clause instance.
+ /// The function that evaluates the input. It should return true if the input is considered invalid or in a negative state.
+ /// The input to evaluate.
+ /// The message to include in the exception if the input is invalid.
+ /// The name of the parameter to include in the thrown exception, captured automatically from the input expression.
+ /// The if the evaluates to false, indicating a valid state.
+ /// Thrown when the validation function returns true, indicating that the input is invalid.
+ public static T Expression(this IGuardClause guardClause,
Func func,
T input,
- string message) where T : struct
+ string message,
+ [CallerArgumentExpression("input")] string? parameterName = null) where T : struct
{
- if (!func(input))
+ if (func(input))
{
- throw new ArgumentException(message);
+ throw new ArgumentException(message, parameterName!);
}
return input;
}
///
- /// Throws an if evaluates to false for given
+ /// Validates the asynchronously and throws an if it evaluates to false for given
+ /// The should return true to indicate an invalid or undesirable state.
+ /// If returns true, indicating that the input is invalid, an is thrown.
///
- ///
- ///
- ///
- ///
- ///
+ /// The type of the input parameter.
+ /// The function that evaluates the input. It should return true if the input is considered invalid or in a negative state.
+ /// The guard clause instance.
+ /// The input to evaluate.
+ /// The message to include in the exception if the input is invalid.
+ /// The name of the parameter to include in the thrown exception, captured automatically from the input expression.
/// if the evaluates to true
- ///
- public static async Task AgainstExpressionAsync(this IGuardClause guardClause,
+ /// Thrown when the validation function returns true, indicating that the input is invalid.
+ public static async Task ExpressionAsync(this IGuardClause guardClause,
Func> func,
T input,
- string message) where T : struct
- {
- if (!await func(input))
- {
- throw new ArgumentException(message);
- }
-
- return input;
- }
-
- ///
- /// Throws an if evaluates to false for given
- ///
- ///
- ///
- ///
- ///
- ///
- /// The name of the parameter that is invalid
- /// if the evaluates to true
- ///
- public static T AgainstExpression(this IGuardClause guardClause, Func func,
- T input, string message, string paramName) where T : struct
+ string message,
+ [CallerArgumentExpression("input")] string? parameterName = null) where T : struct
{
- if (!func(input))
+ if (await func(input))
{
- throw new ArgumentException(message, paramName);
+ throw new ArgumentException(message, parameterName!);
}
return input;
diff --git a/src/GuardClauses/GuardAgainstExpressionExtensionsDeprecated.cs b/src/GuardClauses/GuardAgainstExpressionExtensionsDeprecated.cs
new file mode 100644
index 0000000..3f0ca0c
--- /dev/null
+++ b/src/GuardClauses/GuardAgainstExpressionExtensionsDeprecated.cs
@@ -0,0 +1,78 @@
+using System;
+using System.Threading.Tasks;
+
+namespace Ardalis.GuardClauses;
+
+public static partial class GuardClauseExtensions
+{
+ ///
+ /// Throws an if evaluates to false for given
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ /// if the evaluates to true
+ ///
+ [Obsolete("Deprecated: Switch to Expression for validation.")]
+ public static T AgainstExpression(this IGuardClause guardClause,
+ Func func,
+ T input,
+ string message) where T : struct
+ {
+ if (!func(input))
+ {
+ throw new ArgumentException(message);
+ }
+
+ return input;
+ }
+
+ ///
+ /// Throws an if evaluates to false for given
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ /// if the evaluates to true
+ ///
+ [Obsolete("Deprecated: Switch to ExpressionAsync for asynchronous validation.")]
+ public static async Task AgainstExpressionAsync(this IGuardClause guardClause,
+ Func> func,
+ T input,
+ string message) where T : struct
+ {
+ if (!await func(input))
+ {
+ throw new ArgumentException(message);
+ }
+
+ return input;
+ }
+
+ ///
+ /// Throws an if evaluates to false for given
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ /// The name of the parameter that is invalid
+ /// if the evaluates to true
+ ///
+ [Obsolete("Deprecated: Switch to Expression for validation.")]
+ public static T AgainstExpression(this IGuardClause guardClause, Func func,
+ T input, string message, string paramName) where T : struct
+ {
+ if (!func(input))
+ {
+ throw new ArgumentException(message, paramName);
+ }
+
+ return input;
+ }
+}
diff --git a/test/GuardClauses.UnitTests/GuardAgainstExpression.cs b/test/GuardClauses.UnitTests/GuardAgainstExpression.cs
index 794ef44..ffd43b7 100644
--- a/test/GuardClauses.UnitTests/GuardAgainstExpression.cs
+++ b/test/GuardClauses.UnitTests/GuardAgainstExpression.cs
@@ -22,66 +22,56 @@ public static IEnumerable