| | |
| | | using Microsoft.Extensions.DependencyModel; |
| | | using Microsoft.IdentityModel.Tokens; |
| | | using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime; |
| | | using OfficeOpenXml.FormulaParsing.Excel.Functions.Text; |
| | | using SqlSugar; |
| | | using System; |
| | | using System.Collections; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Reflection; |
| | |
| | | return SimpleValidate(propertyInfos, data); |
| | | } |
| | | |
| | | public static (bool, string, object?) ValidateModelData(object data, Type type) |
| | | { |
| | | if (data == null) return (false, "ä¼ å
¥åæ°ä¸å¯ä¸ºnull", data); |
| | | ModelValidateAttribute? modelAttribute = type.GetCustomAttribute<ModelValidateAttribute>(); |
| | | if (modelAttribute == null) return (false, $"{type.Name}æªå®ä¹ãModelValidateAttributeãç¹æ§", data); |
| | | PropertyInfo[] propertyInfos = type.GetProperties(); |
| | | return SimpleValidate(propertyInfos, data); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// éªè¯å®ä½åæ° |
| | | /// </summary> |
| | |
| | | |
| | | return (true, $"æå", datas); |
| | | } |
| | | |
| | | //private static (bool, string, object?) CustomMethodValidate<T>(ModelValidateAttribute modelAttribute, T data) where T : class, new() |
| | | //{ |
| | | // try |
| | | // { |
| | | // if (modelAttribute.CustomValidateMethodTypeName == null) return (false, $"èªå®ä¹éªè¯æ¹æ³éè¦æä¾æ¹æ³æå¨ç±»çç±»å对象", data); |
| | | // if (modelAttribute.CustomValidateMethodName == null) return (false, $"èªå®ä¹éªè¯æ¹æ³éè¦æä¾æ¹æ³å", data); |
| | | |
| | | // string path = Path.Combine(AppContext.BaseDirectory, $"{modelAttribute.CustomValidateAssemblyName}.dll"); |
| | | // Assembly assembly = Assembly.LoadFrom(path); |
| | | |
| | | // Type t = assembly.GetType(modelAttribute.CustomValidateAssemblyName + "." + modelAttribute.CustomValidateMethodTypeName); |
| | | // object bbb = App.GetService(t); |
| | | // MethodInfo? methodInfo = t.GetMethod(modelAttribute.CustomValidateMethodName); |
| | | // var result = methodInfo.Invoke(bbb, new object[] { data }); |
| | | // //MethodInfo? methodInfo = modelAttribute.CustomValidateMethodType.GetMethod(modelAttribute.CustomValidateMethodName); |
| | | // //if (methodInfo == null) return (false, $"æªå¨è¯¥ç±»å对象ã{modelAttribute.CustomValidateMethodType.Name}ã䏿¾å°è¯¥æ¹æ³ã{modelAttribute.CustomValidateMethodName}ã", data); |
| | | // //methodInfo.GetGenericArguments() |
| | | // return (true, "æå", data); |
| | | // } |
| | | // catch(Exception ex) |
| | | // { |
| | | // throw new Exception(); |
| | | // } |
| | | |
| | | //} |
| | | |
| | | private static (bool, string, object?) SimpleValidate<T>(PropertyInfo[] propertyInfos, T data) where T : class, new() |
| | | { |
| | |
| | | if (tempValue.ChangeType(property.PropertyType).ToString() == propertyAttribute.NotNullAndEmptyWithPropertyAndValue[1].ChangeType(property.PropertyType).ToString()) |
| | | { |
| | | string str = value.Serialize(); |
| | | if (str == "[]") |
| | | |
| | | if (typeof(IList).IsAssignableFrom(property.PropertyType) && str == "[]") |
| | | { |
| | | return (false, $"ã{property.Name}ã屿§çå¼ä¸ºã{propertyAttribute.NotNullAndEmptyWithPropertyAndValue[1]}ãæ¶ã{propertyInfo.Name}ã屿§çå¼ä¸å¯ä¸ºç©º", data); |
| | | } |
| | | else if (property.PropertyType.IsArray && str == "[]") |
| | | { |
| | | return (false, $"ã{property.Name}ã屿§çå¼ä¸ºã{propertyAttribute.NotNullAndEmptyWithPropertyAndValue[1]}ãæ¶ã{propertyInfo.Name}ã屿§çå¼ä¸å¯ä¸ºç©º", data); |
| | | } |
| | | } |
| | | |
| | | } |
| | | else if (types.Length == 0) |
| | | { |
| | |
| | | return (false, $"ã{property.Name}ã屿§çå¼ä¸ºã{propertyAttribute.NotNullAndEmptyWithPropertyAndValue[1]}ãæ¶ã{propertyInfo.Name}ã屿§çå¼ä¸å¯ä¸ºç©º", data); |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | else |
| | |
| | | return (false, $"æªæ¾å°å±æ§ã{propertyAttribute.NotNullAndEmptyWithProperty}ã", data); |
| | | } |
| | | } |
| | | |
| | | if (propertyAttribute.Check != null && propertyAttribute.Check.Length > 0) |
| | | { |
| | | if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}ä¸å¯ä¸ºnull", data); |
| | | if (propertyAttribute.Check.FirstOrDefault(x => x.ToString() == value.ToString()) == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}使ç¨äºCheck约æï¼ä¼ å
¥çå¼ä¸å¨{propertyAttribute.Check.Serialize()}ä¸", data); |
| | | } |
| | | |
| | | if (!string.IsNullOrEmpty(propertyAttribute.StartWith) && !string.IsNullOrWhiteSpace(propertyAttribute.EndWith)) |
| | | { |
| | | if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}ä¸å¯ä¸ºnull", data); |
| | | else |
| | | { |
| | | if (value.ToString().StartsWith(propertyAttribute.StartWith.Trim())) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}å¿
须以{propertyAttribute.StartWith}å¼å¤´", data); |
| | | } |
| | | } |
| | | |
| | | if (!string.IsNullOrEmpty(propertyAttribute.EndWith) && !string.IsNullOrWhiteSpace(propertyAttribute.EndWith)) |
| | | { |
| | | if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}ä¸å¯ä¸ºnull", data); |
| | | else |
| | | { |
| | | if (value.ToString().EndsWith(propertyAttribute.EndWith.Trim())) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}å¿
须以{propertyAttribute.EndWith}ç»å°¾", data); |
| | | } |
| | | } |
| | | |
| | | if (propertyAttribute.MaxLength > propertyAttribute.MinLength && propertyAttribute.MinLength > 0) |
| | | { |
| | | if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}ä¸å¯ä¸ºnull", data); |
| | | if (value.ToString().Length > propertyAttribute.MaxLength) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}çå¼è¶
åºæå¤§é¿åº¦ã{propertyAttribute.MaxLength}ãï¼æ°æ®:{value}", data); |
| | | |
| | | if (value.ToString().Length < propertyAttribute.MinLength) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}çå¼å°äºæå°é¿åº¦ã{propertyAttribute.MinLength}ãï¼æ°æ®:{value}", data); |
| | | } |
| | | |
| | | |
| | | } |
| | | } |
| | | catch (Exception ex) |