< Summary

Information
Class: Allyaria.Abstractions.Validation.AryValidation<T>
Assembly: Allyaria.Abstractions
File(s): /home/runner/work/allyaria/allyaria/src/Allyaria.Abstractions/Validation/AryValidation.cs
Line coverage
100%
Covered lines: 19
Uncovered lines: 0
Coverable lines: 19
Total lines: 68
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_ArgName()100%11100%
get_ArgValue()100%11100%
get_Errors()100%11100%
get_IsValid()100%11100%
Add(...)100%22100%
ThrowIfInvalid()100%44100%

File(s)

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

#LineLine coverage
 1namespace Allyaria.Abstractions.Validation;
 2
 3/// <summary>
 4/// Represents a validation context for an argument, allowing multiple validation checks to be accumulated before throwi
 5/// a combined <see cref="AryArgumentException" />.
 6/// </summary>
 7/// <typeparam name="T">The type of the argument being validated.</typeparam>
 8public sealed class AryValidation<T>
 9{
 10    /// <summary>A collection of accumulated validation errors for the current argument.</summary>
 5211    private readonly List<AryArgumentException> _errors = new();
 12
 13    /// <summary>
 14    /// Initializes a new instance of the <see cref="AryValidation{T}" /> class with a specified argument value and name
 15    /// </summary>
 16    /// <param name="argValue">The value of the argument to validate.</param>
 17    /// <param name="argName">The name of the argument being validated.</param>
 5218    public AryValidation(T argValue, string argName)
 19    {
 5220        ArgValue = argValue;
 5221        ArgName = argName;
 5222    }
 23
 24    /// <summary>Gets the name of the argument being validated.</summary>
 4225    public string ArgName { get; }
 26
 27    /// <summary>Gets the value of the argument being validated.</summary>
 3628    public T ArgValue { get; }
 29
 30    /// <summary>Gets the collection of validation errors accumulated so far.</summary>
 4831    public IReadOnlyList<AryArgumentException> Errors => _errors;
 32
 33    /// <summary>Gets a value indicating whether the validation context is valid (i.e., contains no errors).</summary>
 1034    public bool IsValid => _errors.Count == 0;
 35
 36    /// <summary>Adds a validation error to the internal error collection if one exists.</summary>
 37    /// <param name="ex">The exception to add. If <c>null</c>, no action is taken.</param>
 38    internal void Add(AryArgumentException? ex)
 39    {
 4840        if (ex is not null)
 41        {
 4642            _errors.Add(item: ex);
 43        }
 4844    }
 45
 46    /// <summary>
 47    /// Throws a combined <see cref="AryArgumentException" /> if the current validation context contains errors.
 48    /// </summary>
 49    /// <remarks>
 50    /// When multiple validation errors exist, their messages are concatenated with line breaks into a single exception 
 51    /// for easier consumption.
 52    /// </remarks>
 53    /// <exception cref="AryArgumentException">Thrown when one or more validation errors are present in this context.</e
 54    public void ThrowIfInvalid()
 55    {
 456        if (IsValid)
 57        {
 258            return;
 59        }
 60
 261        var combinedMessage = string.Join(
 262            separator: Environment.NewLine,
 463            values: Errors.Select(selector: e => e.Message)
 264        );
 65
 266        throw new AryArgumentException(message: combinedMessage, argName: ArgName, argValue: ArgValue);
 67    }
 68}