| | | 1 | | namespace Allyaria.Abstractions.Extensions; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Provides a set of extension methods for <see cref="AryValidation{T}" /> that enable fluent-style argument validation |
| | | 5 | | /// using <see cref="AryChecks" />. |
| | | 6 | | /// </summary> |
| | | 7 | | public static class AryValidationExtensions |
| | | 8 | | { |
| | | 9 | | /// <summary>Validates that the current value is within the specified exclusive range.</summary> |
| | | 10 | | /// <typeparam name="T">A comparable type.</typeparam> |
| | | 11 | | /// <param name="validation">The current validation context.</param> |
| | | 12 | | /// <param name="min">The minimum exclusive bound.</param> |
| | | 13 | | /// <param name="max">The maximum exclusive bound.</param> |
| | | 14 | | /// <returns>The same validation context.</returns> |
| | | 15 | | public static AryValidation<T> Between<T>(this AryValidation<T> validation, T min, T max) |
| | | 16 | | where T : IComparable<T> |
| | | 17 | | { |
| | 2 | 18 | | validation.Add( |
| | 2 | 19 | | ex: AryChecks.Between(value: validation.ArgValue, min: min, max: max, argName: validation.ArgName) |
| | 2 | 20 | | ); |
| | | 21 | | |
| | 2 | 22 | | return validation; |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary>Validates a custom condition and associates a custom message when it fails.</summary> |
| | | 26 | | /// <typeparam name="T">The argument type.</typeparam> |
| | | 27 | | /// <param name="validation">The current validation context.</param> |
| | | 28 | | /// <param name="condition">The boolean condition to evaluate.</param> |
| | | 29 | | /// <param name="message">The error message to associate with the failure.</param> |
| | | 30 | | /// <returns>The same validation context.</returns> |
| | | 31 | | public static AryValidation<T> Check<T>(this AryValidation<T> validation, bool condition, string message) |
| | | 32 | | { |
| | 2 | 33 | | validation.Add(ex: AryChecks.Check(condition: condition, argName: validation.ArgName, message: message)); |
| | | 34 | | |
| | 2 | 35 | | return validation; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary>Validates that the current argument value is a defined enumeration constant.</summary> |
| | | 39 | | /// <typeparam name="T">The enumeration type.</typeparam> |
| | | 40 | | /// <param name="validation">The current validation context.</param> |
| | | 41 | | /// <returns>The same <see cref="AryValidation{T}" /> for chaining.</returns> |
| | | 42 | | public static AryValidation<T> EnumDefined<T>(this AryValidation<T> validation) |
| | | 43 | | where T : struct, Enum |
| | | 44 | | { |
| | 2 | 45 | | validation.Add(ex: AryChecks.EnumDefined(value: validation.ArgValue, argName: validation.ArgName)); |
| | | 46 | | |
| | 2 | 47 | | return validation; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary>Validates that the current value equals a specified comparison value.</summary> |
| | | 51 | | /// <typeparam name="T">The value type.</typeparam> |
| | | 52 | | /// <param name="validation">The current validation context.</param> |
| | | 53 | | /// <param name="compare">The value to compare against.</param> |
| | | 54 | | /// <returns>The same <see cref="AryValidation{T}" /> for chaining.</returns> |
| | | 55 | | public static AryValidation<T> EqualTo<T>(this AryValidation<T> validation, T compare) |
| | | 56 | | where T : IEquatable<T> |
| | | 57 | | { |
| | 2 | 58 | | validation.Add( |
| | 2 | 59 | | ex: AryChecks.EqualTo(value: validation.ArgValue, compare: compare, argName: validation.ArgName) |
| | 2 | 60 | | ); |
| | | 61 | | |
| | 2 | 62 | | return validation; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary>Validates that the specified condition is <c>false</c>.</summary> |
| | | 66 | | /// <typeparam name="T">The type of the argument being validated.</typeparam> |
| | | 67 | | /// <param name="validation">The current validation context.</param> |
| | | 68 | | /// <param name="condition">The condition to test.</param> |
| | | 69 | | /// <returns>The same validation context.</returns> |
| | | 70 | | public static AryValidation<T> False<T>(this AryValidation<T> validation, bool condition) |
| | | 71 | | { |
| | 2 | 72 | | validation.Add(ex: AryChecks.False(condition: condition, argName: validation.ArgName)); |
| | | 73 | | |
| | 2 | 74 | | return validation; |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary>Validates that the current value is greater than a specified minimum (exclusive).</summary> |
| | | 78 | | /// <typeparam name="T">A comparable type.</typeparam> |
| | | 79 | | /// <param name="validation">The current validation context.</param> |
| | | 80 | | /// <param name="minExclusive">The exclusive lower bound.</param> |
| | | 81 | | /// <returns>The same validation context.</returns> |
| | | 82 | | public static AryValidation<T> GreaterThan<T>(this AryValidation<T> validation, T minExclusive) |
| | | 83 | | where T : IComparable<T> |
| | | 84 | | { |
| | 2 | 85 | | validation.Add( |
| | 2 | 86 | | ex: AryChecks.GreaterThan( |
| | 2 | 87 | | value: validation.ArgValue, minExclusive: minExclusive, argName: validation.ArgName |
| | 2 | 88 | | ) |
| | 2 | 89 | | ); |
| | | 90 | | |
| | 2 | 91 | | return validation; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary>Validates that the current value is greater than or equal to a specified minimum (inclusive).</summary> |
| | | 95 | | /// <typeparam name="T">A comparable type.</typeparam> |
| | | 96 | | /// <param name="validation">The current validation context.</param> |
| | | 97 | | /// <param name="minInclusive">The inclusive lower bound.</param> |
| | | 98 | | /// <returns>The same validation context.</returns> |
| | | 99 | | public static AryValidation<T> GreaterThanOrEqualTo<T>(this AryValidation<T> validation, T minInclusive) |
| | | 100 | | where T : IComparable<T> |
| | | 101 | | { |
| | 2 | 102 | | validation.Add( |
| | 2 | 103 | | ex: AryChecks.GreaterThanOrEqualTo( |
| | 2 | 104 | | value: validation.ArgValue, minInclusive: minInclusive, argName: validation.ArgName |
| | 2 | 105 | | ) |
| | 2 | 106 | | ); |
| | | 107 | | |
| | 2 | 108 | | return validation; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary>Validates that the current value is within the specified inclusive range.</summary> |
| | | 112 | | /// <typeparam name="T">A comparable type.</typeparam> |
| | | 113 | | /// <param name="validation">The current validation context.</param> |
| | | 114 | | /// <param name="min">The minimum inclusive bound.</param> |
| | | 115 | | /// <param name="max">The maximum inclusive bound.</param> |
| | | 116 | | /// <returns>The same validation context.</returns> |
| | | 117 | | public static AryValidation<T> InRange<T>(this AryValidation<T> validation, T min, T max) |
| | | 118 | | where T : IComparable<T> |
| | | 119 | | { |
| | 2 | 120 | | validation.Add( |
| | 2 | 121 | | ex: AryChecks.InRange(value: validation.ArgValue, min: min, max: max, argName: validation.ArgName) |
| | 2 | 122 | | ); |
| | | 123 | | |
| | 2 | 124 | | return validation; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <summary>Validates that the current value is assignable to a target type.</summary> |
| | | 128 | | /// <typeparam name="T">The source type.</typeparam> |
| | | 129 | | /// <typeparam name="TTarget">The target type.</typeparam> |
| | | 130 | | /// <param name="validation">The current validation context.</param> |
| | | 131 | | /// <returns>The same validation context.</returns> |
| | | 132 | | public static AryValidation<T> IsAssignableTo<T, TTarget>(this AryValidation<T> validation) |
| | | 133 | | { |
| | 2 | 134 | | validation.Add(ex: AryChecks.IsAssignableTo<TTarget>(value: validation.ArgValue, argName: validation.ArgName)); |
| | | 135 | | |
| | 2 | 136 | | return validation; |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <summary>Validates that the current value is less than the specified maximum (exclusive).</summary> |
| | | 140 | | /// <typeparam name="T">A comparable type.</typeparam> |
| | | 141 | | /// <param name="validation">The current validation context.</param> |
| | | 142 | | /// <param name="maxExclusive">The exclusive upper bound.</param> |
| | | 143 | | /// <returns>The same validation context.</returns> |
| | | 144 | | public static AryValidation<T> LessThan<T>(this AryValidation<T> validation, T maxExclusive) |
| | | 145 | | where T : IComparable<T> |
| | | 146 | | { |
| | 2 | 147 | | validation.Add( |
| | 2 | 148 | | ex: AryChecks.LessThan(value: validation.ArgValue, maxExclusive: maxExclusive, argName: validation.ArgName) |
| | 2 | 149 | | ); |
| | | 150 | | |
| | 2 | 151 | | return validation; |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | /// <summary>Validates that the current value is less than or equal to the specified maximum (inclusive).</summary> |
| | | 155 | | /// <typeparam name="T">A comparable type.</typeparam> |
| | | 156 | | /// <param name="validation">The current validation context.</param> |
| | | 157 | | /// <param name="maxInclusive">The inclusive upper bound.</param> |
| | | 158 | | /// <returns>The same validation context.</returns> |
| | | 159 | | public static AryValidation<T> LessThanOrEqualTo<T>(this AryValidation<T> validation, T maxInclusive) |
| | | 160 | | where T : IComparable<T> |
| | | 161 | | { |
| | 2 | 162 | | validation.Add( |
| | 2 | 163 | | ex: AryChecks.LessThanOrEqualTo( |
| | 2 | 164 | | value: validation.ArgValue, maxInclusive: maxInclusive, argName: validation.ArgName |
| | 2 | 165 | | ) |
| | 2 | 166 | | ); |
| | | 167 | | |
| | 2 | 168 | | return validation; |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | /// <summary>Validates that the current value is not its type’s default value.</summary> |
| | | 172 | | /// <typeparam name="T">A struct type.</typeparam> |
| | | 173 | | /// <param name="validation">The current validation context.</param> |
| | | 174 | | /// <returns>The same validation context.</returns> |
| | | 175 | | public static AryValidation<T> NotDefault<T>(this AryValidation<T> validation) |
| | | 176 | | where T : struct, IEquatable<T> |
| | | 177 | | { |
| | 2 | 178 | | validation.Add(ex: AryChecks.NotDefault(value: validation.ArgValue, argName: validation.ArgName)); |
| | | 179 | | |
| | 2 | 180 | | return validation; |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | /// <summary>Validates that the current value is not equal to the specified comparison value.</summary> |
| | | 184 | | /// <typeparam name="T">A struct type.</typeparam> |
| | | 185 | | /// <param name="validation">The current validation context.</param> |
| | | 186 | | /// <param name="compare">The value to compare against.</param> |
| | | 187 | | /// <returns>The same validation context.</returns> |
| | | 188 | | public static AryValidation<T> NotEqualTo<T>(this AryValidation<T> validation, T compare) |
| | | 189 | | where T : IEquatable<T> |
| | | 190 | | { |
| | 2 | 191 | | validation.Add( |
| | 2 | 192 | | ex: AryChecks.NotEqualTo(value: validation.ArgValue, compare: compare, argName: validation.ArgName) |
| | 2 | 193 | | ); |
| | | 194 | | |
| | 2 | 195 | | return validation; |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | /// <summary>Validates that the current value is not <c>null</c>.</summary> |
| | | 199 | | /// <typeparam name="T">The type of the argument being validated.</typeparam> |
| | | 200 | | /// <param name="validation">The current validation context.</param> |
| | | 201 | | /// <returns>The same validation context.</returns> |
| | | 202 | | public static AryValidation<T> NotNull<T>(this AryValidation<T> validation) |
| | | 203 | | { |
| | 2 | 204 | | validation.Add(ex: AryChecks.NotNull(value: validation.ArgValue, argName: validation.ArgName)); |
| | | 205 | | |
| | 2 | 206 | | return validation; |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <summary>Validates that the current string value is not null or empty.</summary> |
| | | 210 | | /// <param name="validation">The current validation context.</param> |
| | | 211 | | /// <returns>The same validation context.</returns> |
| | | 212 | | public static AryValidation<string?> NotNullOrEmpty(this AryValidation<string?> validation) |
| | | 213 | | { |
| | 2 | 214 | | validation.Add(ex: AryChecks.NotNullOrEmpty(value: validation.ArgValue, argName: validation.ArgName)); |
| | | 215 | | |
| | 2 | 216 | | return validation; |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | /// <summary>Validates that the current collection is not null or empty.</summary> |
| | | 220 | | /// <typeparam name="TItem">The type of elements in the collection.</typeparam> |
| | | 221 | | /// <param name="validation">The current validation context.</param> |
| | | 222 | | /// <returns>The same validation context.</returns> |
| | | 223 | | public static AryValidation<IReadOnlyCollection<TItem>?> NotNullOrEmpty<TItem>( |
| | | 224 | | this AryValidation<IReadOnlyCollection<TItem>?> validation) |
| | | 225 | | { |
| | 2 | 226 | | validation.Add(ex: AryChecks.NotNullOrEmpty(collection: validation.ArgValue, argName: validation.ArgName)); |
| | | 227 | | |
| | 2 | 228 | | return validation; |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | /// <summary>Validates that the current string value is not null, empty, or whitespace.</summary> |
| | | 232 | | /// <param name="validation">The current validation context.</param> |
| | | 233 | | /// <returns>The same validation context.</returns> |
| | | 234 | | public static AryValidation<string?> NotNullOrWhiteSpace(this AryValidation<string?> validation) |
| | | 235 | | { |
| | 2 | 236 | | validation.Add(ex: AryChecks.NotNullOrWhiteSpace(value: validation.ArgValue, argName: validation.ArgName)); |
| | | 237 | | |
| | 2 | 238 | | return validation; |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | /// <summary>Validates that the current value is of the same runtime type as another specified value.</summary> |
| | | 242 | | /// <typeparam name="T">The current argument type.</typeparam> |
| | | 243 | | /// <typeparam name="TOther">The comparison type.</typeparam> |
| | | 244 | | /// <param name="validation">The current validation context.</param> |
| | | 245 | | /// <param name="other">The value to compare against.</param> |
| | | 246 | | /// <returns>The same validation context.</returns> |
| | | 247 | | public static AryValidation<T> SameTypeAs<T, TOther>(this AryValidation<T> validation, TOther other) |
| | | 248 | | { |
| | 2 | 249 | | validation.Add(ex: AryChecks.SameType(value1: validation.ArgValue, value2: other, argName: validation.ArgName)); |
| | | 250 | | |
| | 2 | 251 | | return validation; |
| | | 252 | | } |
| | | 253 | | |
| | | 254 | | /// <summary>Validates that a specified condition is <c>true</c>.</summary> |
| | | 255 | | /// <typeparam name="T">The argument type.</typeparam> |
| | | 256 | | /// <param name="validation">The current validation context.</param> |
| | | 257 | | /// <param name="condition">The boolean condition to evaluate.</param> |
| | | 258 | | /// <returns>The same validation context.</returns> |
| | | 259 | | public static AryValidation<T> True<T>(this AryValidation<T> validation, bool condition) |
| | | 260 | | { |
| | 2 | 261 | | validation.Add(ex: AryChecks.True(condition: condition, argName: validation.ArgName)); |
| | | 262 | | |
| | 2 | 263 | | return validation; |
| | | 264 | | } |
| | | 265 | | } |