| | | 1 | | namespace 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> |
| | | 8 | | public sealed class AryValidation<T> |
| | | 9 | | { |
| | | 10 | | /// <summary>A collection of accumulated validation errors for the current argument.</summary> |
| | 52 | 11 | | 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> |
| | 52 | 18 | | public AryValidation(T argValue, string argName) |
| | | 19 | | { |
| | 52 | 20 | | ArgValue = argValue; |
| | 52 | 21 | | ArgName = argName; |
| | 52 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary>Gets the name of the argument being validated.</summary> |
| | 42 | 25 | | public string ArgName { get; } |
| | | 26 | | |
| | | 27 | | /// <summary>Gets the value of the argument being validated.</summary> |
| | 36 | 28 | | public T ArgValue { get; } |
| | | 29 | | |
| | | 30 | | /// <summary>Gets the collection of validation errors accumulated so far.</summary> |
| | 48 | 31 | | public IReadOnlyList<AryArgumentException> Errors => _errors; |
| | | 32 | | |
| | | 33 | | /// <summary>Gets a value indicating whether the validation context is valid (i.e., contains no errors).</summary> |
| | 10 | 34 | | 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 | | { |
| | 48 | 40 | | if (ex is not null) |
| | | 41 | | { |
| | 46 | 42 | | _errors.Add(item: ex); |
| | | 43 | | } |
| | 48 | 44 | | } |
| | | 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 | | { |
| | 4 | 56 | | if (IsValid) |
| | | 57 | | { |
| | 2 | 58 | | return; |
| | | 59 | | } |
| | | 60 | | |
| | 2 | 61 | | var combinedMessage = string.Join( |
| | 2 | 62 | | separator: Environment.NewLine, |
| | 4 | 63 | | values: Errors.Select(selector: e => e.Message) |
| | 2 | 64 | | ); |
| | | 65 | | |
| | 2 | 66 | | throw new AryArgumentException(message: combinedMessage, argName: ArgName, argValue: ArgValue); |
| | | 67 | | } |
| | | 68 | | } |