Skip to content
Draft
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
25 changes: 25 additions & 0 deletions src/Common/IValidatable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Throw;

/// <summary>
/// asdf.
/// </summary>
/// <typeparam name="T">asdf.</typeparam>
public interface IValidatable<out T>
{
/// <summary>
/// asdf.
/// </summary>
/// <value></value>
T Value { get; }

/// <summary>
/// asdf.
/// </summary>
/// <value></value>
string ParamName { get; }

/// <summary>
/// asdf.
/// </summary>
ExceptionCustomizations? ExceptionCustomizations { get; }
}
4 changes: 3 additions & 1 deletion src/Common/Validatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ namespace Throw;
/// <param name="Value">The value to be validated.</param>
/// <param name="ParamName">The name of the parameter holding the <paramref name="Value"/>.</param>
/// <param name="ExceptionCustomizations">Customizations to the exception, which will be applied if an exception is thrown.</param>
public readonly record struct Validatable<TValue>(TValue Value, string ParamName, ExceptionCustomizations? ExceptionCustomizations = null) where TValue : notnull
public readonly record struct Validatable<TValue>(TValue Value, string ParamName, ExceptionCustomizations? ExceptionCustomizations = null)
: IValidatable<TValue>
where TValue : notnull
{
/// <summary>
/// Implicit conversion operator back to the original value's type.
Expand Down
9 changes: 9 additions & 0 deletions src/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Throw;

int[] collection = new[] { 1, 2, 3 };
var collection2 = Enumerable.Range(0, 10);
List<int> collection3 = collection2.ToList();

collection.Throw().IfAny(x => x > 2);
collection2.Throw().IfAny(x => x > 2);
collection3.Throw().IfAny(x => x > 2);
1 change: 1 addition & 0 deletions src/Throw.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<OutputType>Exe</OutputType>
</PropertyGroup>

<PropertyGroup>
Expand Down
15 changes: 15 additions & 0 deletions src/ValidatableExtensions/ValidatableExtensions.Collections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,19 @@ public static ref readonly Validatable<TValue> IfHasNullElements<TValue>(this in

return ref validatable;
}

/// <summary>
/// Throws an exception if any of the items in the collection matches the <paramref name="predicate"/>.
/// Important note: if the collection is a non-evaluated expression, the expression will be evaluated.
/// </summary>
/// <remarks>
/// The default exception thrown is an <see cref="ArgumentException"/>.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IValidatable<IEnumerable<TItem>> IfAny<TItem>(this IValidatable<IEnumerable<TItem>> validatable, Func<TItem, bool> predicate, [CallerArgumentExpression("predicate")] string? predicateName = null)
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definition:

    this IValidatable<IEnumerable<TItem>> validatable

will box the struct.. Any way around this 🤔?

{
Validator.ThrowIfAny(validatable.Value, predicate, predicateName!, validatable.ParamName, validatable.ExceptionCustomizations);

return validatable;
}
}
13 changes: 13 additions & 0 deletions src/Validators/Validator.Collections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ internal static void ThrowIfHasNullElements<TValue>(TValue value, string paramNa
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void ThrowIfAny<TValue, TItem>(TValue value, Func<TItem, bool> predicate, string predicateName, string paramName, ExceptionCustomizations? exceptionCustomizations)
where TValue : notnull, IEnumerable<TItem>
{
foreach (var item in value)
{
if (predicate(item))
{
ExceptionThrower.Throw(paramName, exceptionCustomizations, $"Collection should not have any elements matching {predicateName}.");
}
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int GetCollectionCount<TValue>(TValue value)
where TValue : notnull, IEnumerable
Expand Down