< Summary

Information
Class: Allyaria.Abstractions.Validation.AryChecks
Assembly: Allyaria.Abstractions
File(s): /home/runner/work/allyaria/allyaria/src/Allyaria.Abstractions/Validation/AryChecks.cs
Line coverage
100%
Covered lines: 89
Uncovered lines: 0
Coverable lines: 89
Total lines: 254
Line coverage: 100%
Branch coverage
100%
Covered branches: 52
Total branches: 52
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Between(...)100%44100%
Check(...)100%22100%
EnumDefined(...)100%22100%
EqualTo(...)100%22100%
False(...)100%22100%
GreaterThan(...)100%22100%
GreaterThanOrEqualTo(...)100%22100%
InRange(...)100%44100%
IsAssignableTo(...)100%44100%
LessThan(...)100%22100%
LessThanOrEqualTo(...)100%22100%
NotDefault(...)100%22100%
NotEqualTo(...)100%22100%
NotNull(...)100%22100%
NotNullOrEmpty(...)100%22100%
NotNullOrEmpty(...)100%44100%
NotNullOrWhiteSpace(...)100%22100%
SameType(...)100%88100%
True(...)100%22100%

File(s)

/home/runner/work/allyaria/allyaria/src/Allyaria.Abstractions/Validation/AryChecks.cs

#LineLine coverage
 1namespace Allyaria.Abstractions.Validation;
 2
 3/// <summary>
 4/// Provides a set of helper methods for argument validation, returning <see cref="AryArgumentException" /> instances wh
 5/// conditions fail.
 6/// </summary>
 7internal static class AryChecks
 8{
 9    /// <summary>Validates that a value lies within a specified exclusive range.</summary>
 10    /// <typeparam name="T">The comparable type.</typeparam>
 11    /// <param name="value">The value to validate.</param>
 12    /// <param name="min">The minimum value (exclusive).</param>
 13    /// <param name="max">The maximum value (exclusive).</param>
 14    /// <param name="argName">The argument name.</param>
 15    /// <returns>An exception if out of range; otherwise, <c>null</c>.</returns>
 16    public static AryArgumentException? Between<T>(T value, T min, T max, string argName)
 17        where T : IComparable<T>
 618        => value.CompareTo(other: min) <= 0 || value.CompareTo(other: max) >= 0
 619            ? new AryArgumentException(
 620                message: $"{argName} must be between {min} and {max} (exclusive).", argName: argName, argValue: value
 621            )
 622            : null;
 23
 24    /// <summary>Validates a condition and throws a custom message if it fails.</summary>
 25    /// <param name="condition">The condition to test.</param>
 26    /// <param name="argName">The argument name.</param>
 27    /// <param name="message">The failure message.</param>
 28    /// <returns>An exception if condition is false; otherwise, <c>null</c>.</returns>
 29    public static AryArgumentException? Check(bool condition, string argName, string message)
 2147930        => !condition
 2147931            ? new AryArgumentException(message: message, argName: argName)
 2147932            : null;
 33
 34    /// <summary>Validates that the specified enumeration value is defined within its type.</summary>
 35    /// <typeparam name="TEnum">The enumeration type.</typeparam>
 36    /// <param name="value">The value to check.</param>
 37    /// <param name="argName">The name of the argument being validated.</param>
 38    /// <returns>An <see cref="AryArgumentException" /> if the value is not defined; otherwise, <c>null</c>.</returns>
 39    public static AryArgumentException? EnumDefined<TEnum>(TEnum value, string argName)
 40        where TEnum : struct, Enum
 641        => !Enum.IsDefined(value: value)
 642            ? new AryArgumentException(
 643                message: $"{argName} is not a valid value for this enum.", argName: argName, argValue: value
 644            )
 645            : null;
 46
 47    /// <summary>Validates that two values are equal.</summary>
 48    /// <typeparam name="T">The value type.</typeparam>
 49    /// <param name="value">The value to validate.</param>
 50    /// <param name="compare">The expected value to compare to.</param>
 51    /// <param name="argName">The name of the argument being validated.</param>
 52    /// <returns>An <see cref="AryArgumentException" /> if the values are not equal; otherwise, <c>null</c>.</returns>
 53    public static AryArgumentException? EqualTo<T>(T value, T compare, string argName)
 54        where T : IEquatable<T>
 655        => !value.Equals(other: compare)
 656            ? new AryArgumentException(
 657                message: $"{argName} must be equal to {compare}.", argName: argName, argValue: value
 658            )
 659            : null;
 60
 61    /// <summary>Validates that a condition is <c>false</c>.</summary>
 62    /// <param name="condition">The boolean condition to evaluate.</param>
 63    /// <param name="argName">The name of the argument being validated.</param>
 64    /// <returns>An <see cref="AryArgumentException" /> if the condition is <c>true</c>; otherwise, <c>null</c>.</return
 65    public static AryArgumentException? False(bool condition, string argName)
 666        => condition
 667            ? new AryArgumentException(message: $"{argName} must be false.", argName: argName)
 668            : null;
 69
 70    /// <summary>Validates that a value is greater than a minimum exclusive value.</summary>
 71    /// <typeparam name="T">The comparable type.</typeparam>
 72    /// <param name="value">The value to validate.</param>
 73    /// <param name="minExclusive">The minimum exclusive value.</param>
 74    /// <param name="argName">The name of the argument.</param>
 75    /// <returns>An <see cref="AryArgumentException" /> if validation fails; otherwise, <c>null</c>.</returns>
 76    public static AryArgumentException? GreaterThan<T>(T value, T minExclusive, string argName)
 77        where T : IComparable<T>
 678        => value.CompareTo(other: minExclusive) <= 0
 679            ? new AryArgumentException(
 680                message: $"{argName} must be greater than {minExclusive}.", argName: argName, argValue: value
 681            )
 682            : null;
 83
 84    /// <summary>Validates that a value is greater than or equal to a specified minimum inclusive value.</summary>
 85    /// <typeparam name="T">The comparable type.</typeparam>
 86    /// <param name="value">The value to validate.</param>
 87    /// <param name="minInclusive">The minimum inclusive value.</param>
 88    /// <param name="argName">The argument name.</param>
 89    /// <returns>An exception if validation fails; otherwise, <c>null</c>.</returns>
 90    public static AryArgumentException? GreaterThanOrEqualTo<T>(T value, T minInclusive, string argName)
 91        where T : IComparable<T>
 692        => value.CompareTo(other: minInclusive) < 0
 693            ? new AryArgumentException(
 694                message: $"{argName} must be greater than or equal to {minInclusive}.", argName: argName,
 695                argValue: value
 696            )
 697            : null;
 98
 99    /// <summary>Validates that a value lies within a specified inclusive range.</summary>
 100    /// <typeparam name="T">The comparable type.</typeparam>
 101    /// <param name="value">The value to validate.</param>
 102    /// <param name="min">The minimum value (inclusive).</param>
 103    /// <param name="max">The maximum value (inclusive).</param>
 104    /// <param name="argName">The argument name.</param>
 105    /// <returns>An exception if out of range; otherwise, <c>null</c>.</returns>
 106    public static AryArgumentException? InRange<T>(T value, T min, T max, string argName)
 107        where T : IComparable<T>
 1345951108        => value.CompareTo(other: min) < 0 || value.CompareTo(other: max) > 0
 1345951109            ? new AryArgumentException(
 1345951110                message: $"{argName} must be between {min} and {max} (inclusive).", argName: argName, argValue: value
 1345951111            )
 1345951112            : null;
 113
 114    /// <summary>Validates that an object is assignable to the specified target type.</summary>
 115    /// <typeparam name="TTarget">The target type.</typeparam>
 116    /// <param name="value">The value to check.</param>
 117    /// <param name="argName">The argument name.</param>
 118    /// <returns>An exception if assignability fails; otherwise, <c>null</c>.</returns>
 119    public static AryArgumentException? IsAssignableTo<TTarget>(object? value, string argName)
 6120        => value is null || value is not TTarget
 6121            ? new AryArgumentException(
 6122                message: $"{argName} must be assignable to {typeof(TTarget).FullName}.", argName: argName,
 6123                argValue: value
 6124            )
 6125            : null;
 126
 127    /// <summary>Validates that a value is less than a maximum exclusive value.</summary>
 128    /// <typeparam name="T">The comparable type.</typeparam>
 129    /// <param name="value">The value to validate.</param>
 130    /// <param name="maxExclusive">The maximum exclusive value.</param>
 131    /// <param name="argName">The argument name.</param>
 132    /// <returns>An exception if validation fails; otherwise, <c>null</c>.</returns>
 133    public static AryArgumentException? LessThan<T>(T value, T maxExclusive, string argName)
 134        where T : IComparable<T>
 6135        => value.CompareTo(other: maxExclusive) >= 0
 6136            ? new AryArgumentException(
 6137                message: $"{argName} must be less than {maxExclusive}.", argName: argName, argValue: value
 6138            )
 6139            : null;
 140
 141    /// <summary>Validates that a value is less than or equal to a maximum inclusive value.</summary>
 142    /// <typeparam name="T">The comparable type.</typeparam>
 143    /// <param name="value">The value to validate.</param>
 144    /// <param name="maxInclusive">The maximum inclusive value.</param>
 145    /// <param name="argName">The argument name.</param>
 146    /// <returns>An exception if validation fails; otherwise, <c>null</c>.</returns>
 147    public static AryArgumentException? LessThanOrEqualTo<T>(T value, T maxInclusive, string argName)
 148        where T : IComparable<T>
 6149        => value.CompareTo(other: maxInclusive) > 0
 6150            ? new AryArgumentException(
 6151                message: $"{argName} must be less than or equal to {maxInclusive}.", argName: argName, argValue: value
 6152            )
 6153            : null;
 154
 155    /// <summary>Validates that a value is not its type’s default.</summary>
 156    /// <typeparam name="T">The struct type.</typeparam>
 157    /// <param name="value">The value to validate.</param>
 158    /// <param name="argName">The argument name.</param>
 159    /// <returns>An exception if value equals default; otherwise, <c>null</c>.</returns>
 160    public static AryArgumentException? NotDefault<T>(T value, string argName)
 161        where T : struct, IEquatable<T>
 6162        => value.Equals(other: default(T))
 6163            ? new AryArgumentException(message: $"{argName} cannot be the default value.", argName: argName)
 6164            : null;
 165
 166    /// <summary>Validates that two values are not equal.</summary>
 167    /// <typeparam name="T">The struct type.</typeparam>
 168    /// <param name="value">The first value.</param>
 169    /// <param name="compare">The second value.</param>
 170    /// <param name="argName">The argument name.</param>
 171    /// <returns>An exception if values are equal; otherwise, <c>null</c>.</returns>
 172    public static AryArgumentException? NotEqualTo<T>(T value, T compare, string argName)
 173        where T : IEquatable<T>
 6174        => value.Equals(other: compare)
 6175            ? new AryArgumentException(
 6176                message: $"{argName} must not be equal to {compare}.", argName: argName, argValue: value
 6177            )
 6178            : null;
 179
 180    /// <summary>Validates that a reference or nullable value is not <c>null</c>.</summary>
 181    /// <typeparam name="T">The type of value.</typeparam>
 182    /// <param name="value">The value to check.</param>
 183    /// <param name="argName">The argument name.</param>
 184    /// <returns>An exception if null; otherwise, <c>null</c>.</returns>
 185    public static AryArgumentException? NotNull<T>(T? value, string argName)
 1482186        => value is null
 1482187            ? new AryArgumentException(message: $"{argName} cannot be null.", argName: argName)
 1482188            : null;
 189
 190    /// <summary>Validates that a string is not null or empty.</summary>
 191    /// <param name="value">The string to check.</param>
 192    /// <param name="argName">The argument name.</param>
 193    /// <returns>An exception if null or empty; otherwise, <c>null</c>.</returns>
 194    public static AryArgumentException? NotNullOrEmpty(string? value, string argName)
 6195        => string.IsNullOrEmpty(value: value)
 6196            ? new AryArgumentException(message: $"{argName} cannot be null or empty.", argName: argName)
 6197            : null;
 198
 199    /// <summary>Validates that a collection is not null or empty.</summary>
 200    /// <typeparam name="T">The element type.</typeparam>
 201    /// <param name="collection">The collection to validate.</param>
 202    /// <param name="argName">The argument name.</param>
 203    /// <returns>An exception if null or empty; otherwise, <c>null</c>.</returns>
 204    public static AryArgumentException? NotNullOrEmpty<T>(IReadOnlyCollection<T>? collection, string argName)
 6205        => collection is null || collection.Count == 0
 6206            ? new AryArgumentException(message: $"{argName} cannot be null or an empty collection.", argName: argName)
 6207            : null;
 208
 209    /// <summary>Validates that a string is not null, empty, or whitespace.</summary>
 210    /// <param name="value">The string to validate.</param>
 211    /// <param name="argName">The argument name.</param>
 212    /// <returns>An exception if invalid; otherwise, <c>null</c>.</returns>
 213    public static AryArgumentException? NotNullOrWhiteSpace(string? value, string argName)
 13068214        => string.IsNullOrWhiteSpace(value: value)
 13068215            ? new AryArgumentException(message: $"{argName} cannot be null, empty, or whitespace.", argName: argName)
 13068216            : null;
 217
 218    /// <summary>Validates that two values are of the same type.</summary>
 219    /// <typeparam name="T1">The first value type.</typeparam>
 220    /// <typeparam name="T2">The second value type.</typeparam>
 221    /// <param name="value1">The first value.</param>
 222    /// <param name="value2">The second value.</param>
 223    /// <param name="argName">The argument name.</param>
 224    /// <returns>An exception if type mismatch occurs; otherwise, <c>null</c>.</returns>
 225    public static AryArgumentException? SameType<T1, T2>(T1 value1, T2 value2, string argName)
 226    {
 10227        if (value1 is null || value2 is null)
 228        {
 4229            return new AryArgumentException(
 4230                message: $"{argName} cannot be compared because one or both values are null.", argName: argName,
 4231                argValue: value1 ?? (object?)value2
 4232            );
 233        }
 234
 6235        var type1 = value1.GetType();
 6236        var type2 = value2.GetType();
 237
 6238        return type1 != type2
 6239            ? new AryArgumentException(
 6240                message: $"{argName} type mismatch: {type1.FullName} cannot be compared with {type2.FullName}.",
 6241                argName: argName, argValue: value1
 6242            )
 6243            : null;
 244    }
 245
 246    /// <summary>Validates that a condition is <c>true</c>.</summary>
 247    /// <param name="condition">The boolean condition to evaluate.</param>
 248    /// <param name="argName">The name of the argument being validated.</param>
 249    /// <returns>An <see cref="AryArgumentException" /> if the condition is <c>false</c>; otherwise, <c>null</c>.</retur
 250    public static AryArgumentException? True(bool condition, string argName)
 6251        => !condition
 6252            ? new AryArgumentException(message: $"{argName} must be true.", argName: argName)
 6253            : null;
 254}