From 695571c6009ecbc12e7d4a4fb147df7967a1260e Mon Sep 17 00:00:00 2001
From: wangxinhui <wangxinhui@hnkhzn.com>
Date: 星期一, 07 七月 2025 20:21:19 +0800
Subject: [PATCH] Merge branch 'master' of http://115.159.85.185:8098/r/MeiRuiAn/HuaiAn
---
代码管理/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/ModelValidate.cs | 108 +++++++++++++++++++++++++++++++++++------------------
1 files changed, 71 insertions(+), 37 deletions(-)
diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/ModelValidate.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/ModelValidate.cs"
index 3d50266..b8bb9a9 100644
--- "a/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/ModelValidate.cs"
+++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/Utilities/ModelValidate.cs"
@@ -2,8 +2,10 @@
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;
@@ -32,6 +34,15 @@
return SimpleValidate(propertyInfos, data);
}
+ public static (bool, string, object?) ValidateModelData(object data, Type type)
+ {
+ if (data == null) return (false, "浼犲叆鍙傛暟涓嶅彲涓簄ull", data);
+ ModelValidateAttribute? modelAttribute = type.GetCustomAttribute<ModelValidateAttribute>();
+ if (modelAttribute == null) return (false, $"{type.Name}鏈畾涔夈�怣odelValidateAttribute銆戠壒鎬�", data);
+ PropertyInfo[] propertyInfos = type.GetProperties();
+ return SimpleValidate(propertyInfos, data);
+ }
+
/// <summary>
/// 楠岃瘉瀹炰綋鍙傛暟
/// </summary>
@@ -56,32 +67,6 @@
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()
{
try
@@ -92,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)}涓嶅彲涓簄ull", 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})涓嶅彲涓簄ull", 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)}涓嶅彲涓簄ull", data);
+ if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})涓嶅彲涓簄ull", 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)}涓嶅彲涓簄ull", data);
+ if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})涓嶅彲涓簄ull", 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);
}
}
@@ -171,12 +170,16 @@
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)
{
@@ -185,7 +188,6 @@
return (false, $"銆恵property.Name}銆戝睘鎬х殑鍊间负銆恵propertyAttribute.NotNullAndEmptyWithPropertyAndValue[1]}銆戞椂銆恵propertyInfo.Name}銆戝睘鎬х殑鍊间笉鍙负绌�", data);
}
}
-
}
}
else
@@ -193,6 +195,38 @@
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)}({propertyInfo.Name})涓嶅彲涓簄ull", data);
+ if (propertyAttribute.Check.FirstOrDefault(x => x.ToString() == value.ToString()) == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})浣跨敤浜咰heck绾︽潫锛屼紶鍏ョ殑鍊间笉鍦▄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)}({propertyInfo.Name})涓嶅彲涓簄ull", data);
+ else
+ {
+ 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)}({propertyInfo.Name})涓嶅彲涓簄ull", data);
+ else
+ {
+ if (value.ToString().EndsWith(propertyAttribute.EndWith.Trim())) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}({propertyInfo.Name})蹇呴』浠propertyAttribute.EndWith}缁撳熬", data);
+ }
+ }
+
+ if (propertyAttribute.MaxLength > propertyAttribute.MinLength && propertyAttribute.MinLength > 0)
+ {
+ if (value == null) return (false, $"{(string.IsNullOrEmpty(propertyAttribute.Description) ? propertyInfo.Name : propertyAttribute.Description)}涓嶅彲涓簄ull", 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)
--
Gitblit v1.9.3