| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | |
| | | 3 | | namespace Allyaria.Abstractions.Validation; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides guard clauses for argument validation by invoking <see cref="AryChecks" /> and throwing |
| | | 7 | | /// <see cref="AryArgumentException" /> when validation fails. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class AryGuard |
| | | 10 | | { |
| | | 11 | | /// <summary>Ensures that the specified value lies within the given exclusive range.</summary> |
| | | 12 | | /// <typeparam name="T">A comparable type.</typeparam> |
| | | 13 | | /// <param name="value">The value to validate.</param> |
| | | 14 | | /// <param name="min">The exclusive lower bound.</param> |
| | | 15 | | /// <param name="max">The exclusive upper bound.</param> |
| | | 16 | | /// <param name="argName">The argument name, automatically captured.</param> |
| | | 17 | | /// <exception cref="AryArgumentException">Thrown if the value is outside the range.</exception> |
| | | 18 | | public static void Between<T>(T value, |
| | | 19 | | T min, |
| | | 20 | | T max, |
| | | 21 | | [CallerArgumentExpression(parameterName: nameof(value))] |
| | | 22 | | string? argName = null) |
| | | 23 | | where T : IComparable<T> |
| | 4 | 24 | | => CheckError( |
| | 4 | 25 | | ex: AryChecks.Between( |
| | 4 | 26 | | value: value, min: min, max: max, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value)) |
| | 4 | 27 | | ) |
| | 4 | 28 | | ); |
| | | 29 | | |
| | | 30 | | /// <summary>Ensures that a condition is true; throws otherwise.</summary> |
| | | 31 | | /// <param name="condition">The condition to evaluate.</param> |
| | | 32 | | /// <param name="argName">The argument name.</param> |
| | | 33 | | /// <param name="message">The message describing the violation.</param> |
| | | 34 | | /// <exception cref="AryArgumentException">Thrown if the condition is false.</exception> |
| | | 35 | | public static void Check(bool condition, string argName, string message) |
| | 21477 | 36 | | => CheckError(ex: AryChecks.Check(condition: condition, argName: argName, message: message)); |
| | | 37 | | |
| | | 38 | | /// <summary>Throws the specified <see cref="AryArgumentException" /> if it is not null.</summary> |
| | | 39 | | /// <param name="ex">The exception to check and potentially throw.</param> |
| | | 40 | | private static void CheckError(AryArgumentException? ex) |
| | | 41 | | { |
| | 1382036 | 42 | | if (ex is not null) |
| | | 43 | | { |
| | 116 | 44 | | throw ex; |
| | | 45 | | } |
| | 1381920 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary>Ensures that the specified enumeration value is defined within the enumeration type.</summary> |
| | | 49 | | /// <typeparam name="TEnum">The enumeration type.</typeparam> |
| | | 50 | | /// <param name="value">The enumeration value to validate.</param> |
| | | 51 | | /// <param name="argName">The argument name, automatically captured.</param> |
| | | 52 | | /// <exception cref="AryArgumentException">Thrown if the value is not defined in the enumeration.</exception> |
| | | 53 | | public static void EnumDefined<TEnum>(TEnum value, |
| | | 54 | | [CallerArgumentExpression(parameterName: nameof(value))] |
| | | 55 | | string? argName = null) |
| | | 56 | | where TEnum : struct, Enum |
| | 4 | 57 | | => CheckError( |
| | 4 | 58 | | ex: AryChecks.EnumDefined(value: value, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value))) |
| | 4 | 59 | | ); |
| | | 60 | | |
| | | 61 | | /// <summary>Ensures that the specified value is equal to another value.</summary> |
| | | 62 | | /// <typeparam name="T">The value type.</typeparam> |
| | | 63 | | /// <param name="value">The first value to compare.</param> |
| | | 64 | | /// <param name="compare">The second value to compare.</param> |
| | | 65 | | /// <param name="argName">The argument name, automatically captured.</param> |
| | | 66 | | /// <exception cref="AryArgumentException">Thrown if the values are not equal.</exception> |
| | | 67 | | public static void EqualTo<T>(T value, |
| | | 68 | | T compare, |
| | | 69 | | [CallerArgumentExpression(parameterName: nameof(value))] |
| | | 70 | | string? argName = null) |
| | | 71 | | where T : IEquatable<T> |
| | 4 | 72 | | => CheckError( |
| | 4 | 73 | | ex: AryChecks.EqualTo( |
| | 4 | 74 | | value: value, compare: compare, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value)) |
| | 4 | 75 | | ) |
| | 4 | 76 | | ); |
| | | 77 | | |
| | | 78 | | /// <summary>Ensures that the specified condition is false.</summary> |
| | | 79 | | /// <param name="condition">The condition to check.</param> |
| | | 80 | | /// <param name="argName">The argument name, automatically captured.</param> |
| | | 81 | | /// <exception cref="AryArgumentException">Thrown if the condition is true.</exception> |
| | | 82 | | public static void False(bool condition, |
| | | 83 | | [CallerArgumentExpression(parameterName: nameof(condition))] |
| | | 84 | | string? argName = null) |
| | 4 | 85 | | => CheckError( |
| | 4 | 86 | | ex: AryChecks.False( |
| | 4 | 87 | | condition: condition, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(condition)) |
| | 4 | 88 | | ) |
| | 4 | 89 | | ); |
| | | 90 | | |
| | | 91 | | /// <summary>Creates a new <see cref="AryValidation{T}" /> context for the specified value.</summary> |
| | | 92 | | /// <typeparam name="T">The type of the value to validate.</typeparam> |
| | | 93 | | /// <param name="value">The value to validate.</param> |
| | | 94 | | /// <param name="argName">The argument name, automatically captured.</param> |
| | | 95 | | /// <returns>A new validation context.</returns> |
| | | 96 | | public static AryValidation<T> For<T>(T value, |
| | | 97 | | [CallerArgumentExpression(parameterName: nameof(value))] |
| | | 98 | | string? argName = null) |
| | 2 | 99 | | => new(argValue: value, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value))); |
| | | 100 | | |
| | | 101 | | /// <summary>Ensures that the specified value is greater than the given minimum (exclusive).</summary> |
| | | 102 | | /// <typeparam name="T">A comparable type.</typeparam> |
| | | 103 | | /// <param name="value">The value to validate.</param> |
| | | 104 | | /// <param name="minExclusive">The exclusive minimum bound.</param> |
| | | 105 | | /// <param name="argName">The argument name, automatically captured.</param> |
| | | 106 | | /// <exception cref="AryArgumentException">Thrown if the value is less than or equal to the minimum.</exception> |
| | | 107 | | public static void GreaterThan<T>(T value, |
| | | 108 | | T minExclusive, |
| | | 109 | | [CallerArgumentExpression(parameterName: nameof(value))] |
| | | 110 | | string? argName = null) |
| | | 111 | | where T : IComparable<T> |
| | 4 | 112 | | => CheckError( |
| | 4 | 113 | | ex: AryChecks.GreaterThan( |
| | 4 | 114 | | value: value, minExclusive: minExclusive, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value)) |
| | 4 | 115 | | ) |
| | 4 | 116 | | ); |
| | | 117 | | |
| | | 118 | | /// <summary>Ensures that the specified value is greater than or equal to the given minimum (inclusive).</summary> |
| | | 119 | | /// <typeparam name="T">A comparable type.</typeparam> |
| | | 120 | | /// <param name="value">The value to validate.</param> |
| | | 121 | | /// <param name="minInclusive">The inclusive minimum bound.</param> |
| | | 122 | | /// <param name="argName">The argument name, automatically captured.</param> |
| | | 123 | | /// <exception cref="AryArgumentException">Thrown if the value is less than the minimum.</exception> |
| | | 124 | | public static void GreaterThanOrEqualTo<T>(T value, |
| | | 125 | | T minInclusive, |
| | | 126 | | [CallerArgumentExpression(parameterName: nameof(value))] |
| | | 127 | | string? argName = null) |
| | | 128 | | where T : IComparable<T> |
| | 4 | 129 | | => CheckError( |
| | 4 | 130 | | ex: AryChecks.GreaterThanOrEqualTo( |
| | 4 | 131 | | value: value, minInclusive: minInclusive, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value)) |
| | 4 | 132 | | ) |
| | 4 | 133 | | ); |
| | | 134 | | |
| | | 135 | | /// <summary>Ensures that the specified value lies within the given inclusive range.</summary> |
| | | 136 | | /// <typeparam name="T">A comparable type.</typeparam> |
| | | 137 | | /// <param name="value">The value to validate.</param> |
| | | 138 | | /// <param name="min">The inclusive lower bound.</param> |
| | | 139 | | /// <param name="max">The inclusive upper bound.</param> |
| | | 140 | | /// <param name="argName">The argument name, automatically captured.</param> |
| | | 141 | | /// <exception cref="AryArgumentException">Thrown if the value is outside the range.</exception> |
| | | 142 | | public static void InRange<T>(T value, |
| | | 143 | | T min, |
| | | 144 | | T max, |
| | | 145 | | [CallerArgumentExpression(parameterName: nameof(value))] |
| | | 146 | | string? argName = null) |
| | | 147 | | where T : IComparable<T> |
| | 1345949 | 148 | | => CheckError( |
| | 1345949 | 149 | | ex: AryChecks.InRange( |
| | 1345949 | 150 | | value: value, min: min, max: max, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value)) |
| | 1345949 | 151 | | ) |
| | 1345949 | 152 | | ); |
| | | 153 | | |
| | | 154 | | /// <summary>Ensures that the given value is assignable to the specified target type.</summary> |
| | | 155 | | /// <typeparam name="TTarget">The target type.</typeparam> |
| | | 156 | | /// <param name="value">The value to check.</param> |
| | | 157 | | /// <param name="argName">The argument name, automatically captured.</param> |
| | | 158 | | /// <exception cref="AryArgumentException">Thrown if the value cannot be assigned to the target type.</exception> |
| | | 159 | | public static void IsAssignableTo<TTarget>(object? value, |
| | | 160 | | [CallerArgumentExpression(parameterName: nameof(value))] |
| | | 161 | | string? argName = null) |
| | 4 | 162 | | => CheckError( |
| | 4 | 163 | | ex: AryChecks.IsAssignableTo<TTarget>( |
| | 4 | 164 | | value: value, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value)) |
| | 4 | 165 | | ) |
| | 4 | 166 | | ); |
| | | 167 | | |
| | | 168 | | /// <summary>Ensures that the specified value is less than the given maximum (exclusive).</summary> |
| | | 169 | | /// <typeparam name="T">A comparable type.</typeparam> |
| | | 170 | | /// <param name="value">The value to validate.</param> |
| | | 171 | | /// <param name="maxExclusive">The exclusive upper bound.</param> |
| | | 172 | | /// <param name="argName">The argument name, automatically captured.</param> |
| | | 173 | | /// <exception cref="AryArgumentException">Thrown if the value is greater than or equal to the maximum.</exception> |
| | | 174 | | public static void LessThan<T>(T value, |
| | | 175 | | T maxExclusive, |
| | | 176 | | [CallerArgumentExpression(parameterName: nameof(value))] |
| | | 177 | | string? argName = null) |
| | | 178 | | where T : IComparable<T> |
| | 4 | 179 | | => CheckError( |
| | 4 | 180 | | ex: AryChecks.LessThan( |
| | 4 | 181 | | value: value, maxExclusive: maxExclusive, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value)) |
| | 4 | 182 | | ) |
| | 4 | 183 | | ); |
| | | 184 | | |
| | | 185 | | /// <summary>Ensures that the specified value is less than or equal to the given maximum (inclusive).</summary> |
| | | 186 | | /// <typeparam name="T">A comparable type.</typeparam> |
| | | 187 | | /// <param name="value">The value to validate.</param> |
| | | 188 | | /// <param name="maxInclusive">The inclusive upper bound.</param> |
| | | 189 | | /// <param name="argName">The argument name, automatically captured.</param> |
| | | 190 | | /// <exception cref="AryArgumentException">Thrown if the value is greater than the maximum.</exception> |
| | | 191 | | public static void LessThanOrEqualTo<T>(T value, |
| | | 192 | | T maxInclusive, |
| | | 193 | | [CallerArgumentExpression(parameterName: nameof(value))] |
| | | 194 | | string? argName = null) |
| | | 195 | | where T : IComparable<T> |
| | 4 | 196 | | => CheckError( |
| | 4 | 197 | | ex: AryChecks.LessThanOrEqualTo( |
| | 4 | 198 | | value: value, maxInclusive: maxInclusive, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value)) |
| | 4 | 199 | | ) |
| | 4 | 200 | | ); |
| | | 201 | | |
| | | 202 | | /// <summary>Ensures that the specified value is not equal to its default value.</summary> |
| | | 203 | | /// <typeparam name="T">The value type.</typeparam> |
| | | 204 | | /// <param name="value">The value to validate.</param> |
| | | 205 | | /// <param name="argName">The argument name, automatically captured.</param> |
| | | 206 | | /// <exception cref="AryArgumentException">Thrown if the value equals its default.</exception> |
| | | 207 | | public static void NotDefault<T>(T value, |
| | | 208 | | [CallerArgumentExpression(parameterName: nameof(value))] |
| | | 209 | | string? argName = null) |
| | | 210 | | where T : struct, IEquatable<T> |
| | 4 | 211 | | => CheckError( |
| | 4 | 212 | | ex: AryChecks.NotDefault(value: value, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value))) |
| | 4 | 213 | | ); |
| | | 214 | | |
| | | 215 | | /// <summary>Ensures that the specified value is not equal to another value.</summary> |
| | | 216 | | /// <typeparam name="T">The value type.</typeparam> |
| | | 217 | | /// <param name="value">The first value to compare.</param> |
| | | 218 | | /// <param name="compare">The value to compare against.</param> |
| | | 219 | | /// <param name="argName">The argument name, automatically captured.</param> |
| | | 220 | | /// <exception cref="AryArgumentException">Thrown if the values are equal.</exception> |
| | | 221 | | public static void NotEqualTo<T>(T value, |
| | | 222 | | T compare, |
| | | 223 | | [CallerArgumentExpression(parameterName: nameof(value))] |
| | | 224 | | string? argName = null) |
| | | 225 | | where T : IEquatable<T> |
| | 4 | 226 | | => CheckError( |
| | 4 | 227 | | ex: AryChecks.NotEqualTo( |
| | 4 | 228 | | value: value, compare: compare, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value)) |
| | 4 | 229 | | ) |
| | 4 | 230 | | ); |
| | | 231 | | |
| | | 232 | | /// <summary>Ensures that the specified value is not null.</summary> |
| | | 233 | | /// <typeparam name="T">The reference type.</typeparam> |
| | | 234 | | /// <param name="value">The value to validate.</param> |
| | | 235 | | /// <param name="argName">The argument name, automatically captured.</param> |
| | | 236 | | /// <exception cref="AryArgumentException">Thrown if the value is null.</exception> |
| | | 237 | | public static void NotNull<T>(T? value, |
| | | 238 | | [CallerArgumentExpression(parameterName: nameof(value))] |
| | | 239 | | string? argName = null) |
| | 1480 | 240 | | => CheckError( |
| | 1480 | 241 | | ex: AryChecks.NotNull(value: value, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value))) |
| | 1480 | 242 | | ); |
| | | 243 | | |
| | | 244 | | /// <summary>Ensures that the specified string is not null or empty.</summary> |
| | | 245 | | /// <param name="value">The string to validate.</param> |
| | | 246 | | /// <param name="argName">The argument name, automatically captured.</param> |
| | | 247 | | /// <exception cref="AryArgumentException">Thrown if the string is null or empty.</exception> |
| | | 248 | | public static void NotNullOrEmpty(string? value, |
| | | 249 | | [CallerArgumentExpression(parameterName: nameof(value))] |
| | | 250 | | string? argName = null) |
| | 4 | 251 | | => CheckError( |
| | 4 | 252 | | ex: AryChecks.NotNullOrEmpty(value: value, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value))) |
| | 4 | 253 | | ); |
| | | 254 | | |
| | | 255 | | /// <summary>Ensures that the specified collection is not null or empty.</summary> |
| | | 256 | | /// <typeparam name="T">The element type of the collection.</typeparam> |
| | | 257 | | /// <param name="collection">The collection to validate.</param> |
| | | 258 | | /// <param name="argName">The argument name, automatically captured.</param> |
| | | 259 | | /// <exception cref="AryArgumentException">Thrown if the collection is null or empty.</exception> |
| | | 260 | | public static void NotNullOrEmpty<T>(IReadOnlyCollection<T>? collection, |
| | | 261 | | [CallerArgumentExpression(parameterName: nameof(collection))] |
| | | 262 | | string? argName = null) |
| | 4 | 263 | | => CheckError( |
| | 4 | 264 | | ex: AryChecks.NotNullOrEmpty( |
| | 4 | 265 | | collection: collection, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(collection)) |
| | 4 | 266 | | ) |
| | 4 | 267 | | ); |
| | | 268 | | |
| | | 269 | | /// <summary>Ensures that the specified string is not null or whitespace.</summary> |
| | | 270 | | /// <param name="value">The string to validate.</param> |
| | | 271 | | /// <param name="argName">The argument name, automatically captured.</param> |
| | | 272 | | /// <exception cref="AryArgumentException">Thrown if the string is null, empty, or whitespace only.</exception> |
| | | 273 | | public static void NotNullOrWhiteSpace(string? value, |
| | | 274 | | [CallerArgumentExpression(parameterName: nameof(value))] |
| | | 275 | | string? argName = null) |
| | 13066 | 276 | | => CheckError( |
| | 13066 | 277 | | ex: AryChecks.NotNullOrWhiteSpace( |
| | 13066 | 278 | | value: value, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value)) |
| | 13066 | 279 | | ) |
| | 13066 | 280 | | ); |
| | | 281 | | |
| | | 282 | | /// <summary>Ensures that the two provided values are of the same type.</summary> |
| | | 283 | | /// <typeparam name="T1">The type of the first value.</typeparam> |
| | | 284 | | /// <typeparam name="T2">The type of the second value.</typeparam> |
| | | 285 | | /// <param name="value1">The first value to compare.</param> |
| | | 286 | | /// <param name="value2">The second value to compare.</param> |
| | | 287 | | /// <param name="argName">The argument name, automatically captured.</param> |
| | | 288 | | /// <exception cref="AryArgumentException">Thrown if the types differ.</exception> |
| | | 289 | | public static void SameType<T1, T2>(T1 value1, |
| | | 290 | | T2 value2, |
| | | 291 | | [CallerArgumentExpression(parameterName: nameof(value1))] |
| | | 292 | | string? argName = null) |
| | 8 | 293 | | => CheckError( |
| | 8 | 294 | | ex: AryChecks.SameType( |
| | 8 | 295 | | value1: value1, value2: value2, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(value1)) |
| | 8 | 296 | | ) |
| | 8 | 297 | | ); |
| | | 298 | | |
| | | 299 | | /// <summary>Ensures that the specified condition is true.</summary> |
| | | 300 | | /// <param name="condition">The condition to check.</param> |
| | | 301 | | /// <param name="argName">The argument name, automatically captured.</param> |
| | | 302 | | /// <exception cref="AryArgumentException">Thrown if the condition is false.</exception> |
| | | 303 | | public static void True(bool condition, |
| | | 304 | | [CallerArgumentExpression(parameterName: nameof(condition))] |
| | | 305 | | string? argName = null) |
| | 4 | 306 | | => CheckError( |
| | 4 | 307 | | ex: AryChecks.True(condition: condition, argName: argName.OrDefaultIfEmpty(defaultValue: nameof(condition))) |
| | 4 | 308 | | ); |
| | | 309 | | } |