hutongqing
2024-12-10 8d341db9d2d5699d527c88c935f0c4ce255a57a4
´úÂë¹ÜÀí/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/ModelValidate.cs
@@ -77,35 +77,49 @@
                    if (propertyAttribute == null) continue;
                    object? value = propertyInfo.GetValue(data, null);
                    if (value is IList)
                    {
                        IList list = (IList)value;
                        Type? t = list.GetType().GetGenericArguments().FirstOrDefault();
                        if (t != null && t.IsClass)
                        {
                            foreach (var item in list)
                            {
                                (bool, string, object?) result = ValidateModelData(item, t);
                                if (!result.Item1) return result;
                            }
                        }
                    }
                    if (propertyAttribute.NotNullAndEmpty)
                    {
                        if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}不可为null", data);
                        if (string.IsNullOrEmpty(value.ToString())) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}不可为空字符串", data);
                        if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})不可为null", data);
                        if (string.IsNullOrEmpty(value.ToString())) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})不可为空字符串", data);
                    }
                    if (propertyAttribute.MinValue > int.MinValue)
                    {
                        if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}不可为null", data);
                        if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})不可为null", data);
                        if (propertyAttribute.IsContainMinValue)
                        {
                            if (Convert.ToInt32(value.ToString()) < propertyAttribute.MinValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}的值【{value}】不可小于【{propertyAttribute.MinValue}】", data);
                            if (Convert.ToInt32(value.ToString()) < propertyAttribute.MinValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})的值【{value}】不可小于【{propertyAttribute.MinValue}】", data);
                        }
                        else
                        {
                            if (Convert.ToInt32(value.ToString()) <= propertyAttribute.MinValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}的值【{value}】要大于【{propertyAttribute.MinValue}】", data);
                            if (Convert.ToInt32(value.ToString()) <= propertyAttribute.MinValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})的值【{value}】要大于【{propertyAttribute.MinValue}】", data);
                        }
                    }
                    if (propertyAttribute.MaxValue < int.MaxValue)
                    {
                        if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}不可为null", data);
                        if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})不可为null", data);
                        if (propertyAttribute.IsContainMaxValue)
                        {
                            if (Convert.ToInt32(value.ToString()) >= propertyAttribute.MaxValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}的值【{value}】不可大于【{propertyAttribute.MaxValue}】", data);
                            if (Convert.ToInt32(value.ToString()) >= propertyAttribute.MaxValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})的值【{value}】不可大于【{propertyAttribute.MaxValue}】", data);
                        }
                        else
                        {
                            if (Convert.ToInt32(value.ToString()) > propertyAttribute.MaxValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}的值【{value}】要小于【{propertyAttribute.MaxValue}】", data);
                            if (Convert.ToInt32(value.ToString()) > propertyAttribute.MaxValue) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})的值【{value}】要小于【{propertyAttribute.MaxValue}】", data);
                        }
                    }
@@ -184,25 +198,25 @@
                    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 (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})不可为null", data);
                        if (propertyAttribute.Check.FirstOrDefault(x => x.ToString() == value.ToString()) == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})使用了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);
                        if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})不可为null", data);
                        else
                        {
                            if (value.ToString().StartsWith(propertyAttribute.StartWith.Trim())) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}必须以{propertyAttribute.StartWith}开头", data);
                            if (value.ToString().StartsWith(propertyAttribute.StartWith.Trim())) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})必须以{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);
                        if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})不可为null", data);
                        else
                        {
                            if (value.ToString().EndsWith(propertyAttribute.EndWith.Trim())) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}必须以{propertyAttribute.EndWith}结尾", data);
                            if (value.ToString().EndsWith(propertyAttribute.EndWith.Trim())) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})必须以{propertyAttribute.EndWith}结尾", data);
                        }
                    }
@@ -213,8 +227,6 @@
                        if (value.ToString().Length < propertyAttribute.MinLength) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}的值小于最小长度【{propertyAttribute.MinLength}】,数据:{value}", data);
                    }
                }
            }
            catch (Exception ex)