|
| 1 | +using System.Reflection; |
| 2 | +using System.Text.RegularExpressions; |
| 3 | +using AutoSettingUI.Core.Attributes; |
| 4 | + |
| 5 | +namespace AutoSettingUI.Extension.Shared; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Shared validation logic for property values across all UI frameworks. |
| 9 | +/// </summary> |
| 10 | +public static class ValidationHelper |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Gets validation attributes from a property. |
| 14 | + /// </summary> |
| 15 | + public static ValidationAttribute[] GetValidations(PropertyInfo property) |
| 16 | + { |
| 17 | + return property.GetCustomAttributes<ValidationAttribute>().ToArray(); |
| 18 | + } |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Validates a value against the validation attributes of a property. |
| 22 | + /// </summary> |
| 23 | + /// <param name="property">The property being validated.</param> |
| 24 | + /// <param name="value">The value to validate.</param> |
| 25 | + /// <param name="target">The target object containing the property.</param> |
| 26 | + /// <param name="errorMessage">Output error message if validation fails.</param> |
| 27 | + /// <returns>True if validation passes, false otherwise.</returns> |
| 28 | + public static bool ValidateValue(PropertyInfo property, object? value, object target, out string? errorMessage) |
| 29 | + { |
| 30 | + errorMessage = null; |
| 31 | + var validations = GetValidations(property); |
| 32 | + |
| 33 | + foreach (var validation in validations) |
| 34 | + { |
| 35 | + if (!ValidateSingle(validation, property, value, target, out errorMessage)) |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + return true; |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Validates a single validation attribute. |
| 44 | + /// </summary> |
| 45 | + private static bool ValidateSingle(ValidationAttribute validation, PropertyInfo property, object? value, object target, out string? errorMessage) |
| 46 | + { |
| 47 | + errorMessage = null; |
| 48 | + |
| 49 | + if (validation.Required && IsNullOrEmpty(value)) |
| 50 | + { |
| 51 | + errorMessage = validation.ErrorMessage ?? $"{property.Name} is required."; |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + if (value is string stringValue && !string.IsNullOrEmpty(stringValue)) |
| 56 | + { |
| 57 | + if (!ValidateStringLength(validation, stringValue, property, out errorMessage)) |
| 58 | + return false; |
| 59 | + |
| 60 | + if (!ValidatePattern(validation, stringValue, property, out errorMessage)) |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + var actualValue = TryConvertValue(value, property.PropertyType); |
| 65 | + if (!ValidateRange(validation, actualValue, property, out errorMessage)) |
| 66 | + return false; |
| 67 | + |
| 68 | + if (!ValidateCustomMethod(validation, actualValue, target, property, out errorMessage)) |
| 69 | + return false; |
| 70 | + |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + private static bool IsNullOrEmpty(object? value) |
| 75 | + { |
| 76 | + return value == null || (value is string str && string.IsNullOrWhiteSpace(str)); |
| 77 | + } |
| 78 | + |
| 79 | + private static bool ValidateStringLength(ValidationAttribute validation, string value, PropertyInfo property, out string? errorMessage) |
| 80 | + { |
| 81 | + errorMessage = null; |
| 82 | + |
| 83 | + if (validation.MinLength >= 0 && value.Length < validation.MinLength) |
| 84 | + { |
| 85 | + errorMessage = validation.ErrorMessage ?? $"{property.Name} must be at least {validation.MinLength} characters."; |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + if (validation.MaxLength >= 0 && value.Length > validation.MaxLength) |
| 90 | + { |
| 91 | + errorMessage = validation.ErrorMessage ?? $"{property.Name} must be at most {validation.MaxLength} characters."; |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + return true; |
| 96 | + } |
| 97 | + |
| 98 | + private static bool ValidatePattern(ValidationAttribute validation, string value, PropertyInfo property, out string? errorMessage) |
| 99 | + { |
| 100 | + errorMessage = null; |
| 101 | + |
| 102 | + if (!string.IsNullOrEmpty(validation.Pattern) && !Regex.IsMatch(value, validation.Pattern)) |
| 103 | + { |
| 104 | + errorMessage = validation.ErrorMessage ?? $"{property.Name} format is invalid."; |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + return true; |
| 109 | + } |
| 110 | + |
| 111 | + private static bool ValidateRange(ValidationAttribute validation, object? value, PropertyInfo property, out string? errorMessage) |
| 112 | + { |
| 113 | + errorMessage = null; |
| 114 | + |
| 115 | + if (value is not IComparable comparable) |
| 116 | + return true; |
| 117 | + |
| 118 | + if (!double.IsNaN(validation.MinValue)) |
| 119 | + { |
| 120 | + try |
| 121 | + { |
| 122 | + var min = Convert.ChangeType(validation.MinValue, comparable.GetType()); |
| 123 | + if (comparable.CompareTo(min) < 0) |
| 124 | + { |
| 125 | + errorMessage = validation.ErrorMessage ?? $"{property.Name} must be at least {validation.MinValue}."; |
| 126 | + return false; |
| 127 | + } |
| 128 | + } |
| 129 | + catch { } |
| 130 | + } |
| 131 | + |
| 132 | + if (!double.IsNaN(validation.MaxValue)) |
| 133 | + { |
| 134 | + try |
| 135 | + { |
| 136 | + var max = Convert.ChangeType(validation.MaxValue, comparable.GetType()); |
| 137 | + if (comparable.CompareTo(max) > 0) |
| 138 | + { |
| 139 | + errorMessage = validation.ErrorMessage ?? $"{property.Name} must be at most {validation.MaxValue}."; |
| 140 | + return false; |
| 141 | + } |
| 142 | + } |
| 143 | + catch { } |
| 144 | + } |
| 145 | + |
| 146 | + return true; |
| 147 | + } |
| 148 | + |
| 149 | + private static bool ValidateCustomMethod(ValidationAttribute validation, object? value, object target, PropertyInfo property, out string? errorMessage) |
| 150 | + { |
| 151 | + errorMessage = null; |
| 152 | + |
| 153 | + if (string.IsNullOrEmpty(validation.ValidateMethod)) |
| 154 | + return true; |
| 155 | + |
| 156 | + var method = target.GetType().GetMethod(validation.ValidateMethod, |
| 157 | + BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); |
| 158 | + |
| 159 | + if (method == null) |
| 160 | + return true; |
| 161 | + |
| 162 | + var result = method.IsStatic |
| 163 | + ? method.Invoke(null, new[] { value }) |
| 164 | + : method.Invoke(target, new[] { value }); |
| 165 | + |
| 166 | + if (result is bool isValid && !isValid) |
| 167 | + { |
| 168 | + errorMessage = validation.ErrorMessage ?? $"{property.Name} is invalid."; |
| 169 | + return false; |
| 170 | + } |
| 171 | + |
| 172 | + return true; |
| 173 | + } |
| 174 | + |
| 175 | + private static object? TryConvertValue(object? value, Type targetType) |
| 176 | + { |
| 177 | + if (value is not string stringValue || string.IsNullOrWhiteSpace(stringValue)) |
| 178 | + return value; |
| 179 | + |
| 180 | + if (targetType == typeof(string)) |
| 181 | + return value; |
| 182 | + |
| 183 | + try |
| 184 | + { |
| 185 | + return Convert.ChangeType(stringValue, targetType); |
| 186 | + } |
| 187 | + catch |
| 188 | + { |
| 189 | + return value; |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments