From 0faab483a945184fe7c09be7764d310bda867e8b Mon Sep 17 00:00:00 2001
From: falcon <9504402@qq.com>
Date: Thu, 24 Nov 2022 10:58:46 +0800
Subject: [PATCH] =?UTF-8?q?=E6=89=A9=E5=B1=95Object=E5=A2=9E=E5=8A=A0TryMo?=
=?UTF-8?q?delValidation=E6=96=B9=E6=B3=95=EF=BC=8C=E9=AA=8C=E8=AF=81?=
=?UTF-8?q?=E6=A8=A1=E5=9E=8B=E6=98=AF=E5=90=A6=E5=8F=AF=E4=BB=A5=E9=80=9A?=
=?UTF-8?q?=E8=BF=87=E9=AA=8C=E8=AF=81=EF=BC=8C=E5=B9=B6=E5=A2=9E=E5=8A=A0?=
=?UTF-8?q?=E6=B5=8B=E8=AF=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Falcon.SugarApi.Test/ModelValidationTest.cs | 76 +++++++++++++++++++
.../ModelValidation/ObjectValidateExtend.cs | 23 ++++++
2 files changed, 99 insertions(+)
create mode 100644 Falcon.SugarApi.Test/ModelValidationTest.cs
create mode 100644 Falcon.SugarApi/ModelValidation/ObjectValidateExtend.cs
diff --git a/Falcon.SugarApi.Test/ModelValidationTest.cs b/Falcon.SugarApi.Test/ModelValidationTest.cs
new file mode 100644
index 0000000..5b3cff9
--- /dev/null
+++ b/Falcon.SugarApi.Test/ModelValidationTest.cs
@@ -0,0 +1,76 @@
+using Falcon.SugarApi.ModelValidation;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+using System.Linq;
+
+namespace Falcon.SugarApi.Test
+{
+ ///
+ /// Json序列化测试
+ ///
+ [TestClass]
+ public class ModelValidationTest
+ {
+ [TestMethod]
+ public void ValidateTest() {
+ TestModel model;
+ List results;
+ bool r;
+
+ model = new TestModel {
+ Age = "30",
+ Name = "Tom",
+ };
+ r = model.TryModelValidation(out results);
+ Assert.IsTrue(r);
+ Assert.IsTrue(results != null);
+ Assert.IsTrue(results.Count == 0);
+
+ model = new TestModel {
+ Age = "60",
+ };
+ r = model.TryModelValidation(out results);
+ Assert.IsFalse(r);
+ Assert.IsTrue(results.Count == 2);
+
+ model = new TestModel {
+ MaxLength = "111111111111111111",
+ Name = "Tom",
+ Age = "30",
+ };
+ r = model.TryModelValidation(out results);
+ Assert.IsFalse(r);
+ Assert.IsTrue(results.Count == 1);
+
+ model = new TestModel {
+ Name = "abc",
+ Age = "20",
+ IsIntProp = "abc",
+ MaxLength = "1",
+ };
+ r = model.TryModelValidation(out results);
+ Assert.IsFalse(r);
+ Assert.IsTrue(results.Count == 1);
+ Assert.IsTrue(results.First().MemberNames.First() == "IsIntProp");
+ }
+ ///
+ /// 测试用模型
+ ///
+ public class TestModel
+ {
+ [Required]
+ public string Name { get; set; }
+
+ [Range(10, 50)]
+ public string Age { get; set; }
+
+ [MaxLength(3)]
+ public string MaxLength { get; set; }
+
+ [IsInt]
+ public string IsIntProp { get; set; } = "123";
+ }
+ }
+}
diff --git a/Falcon.SugarApi/ModelValidation/ObjectValidateExtend.cs b/Falcon.SugarApi/ModelValidation/ObjectValidateExtend.cs
new file mode 100644
index 0000000..34dd8c2
--- /dev/null
+++ b/Falcon.SugarApi/ModelValidation/ObjectValidateExtend.cs
@@ -0,0 +1,23 @@
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+
+namespace Falcon.SugarApi.ModelValidation
+{
+ ///
+ /// 扩展对象验证方法
+ ///
+ public static class ObjectValidateExtend
+ {
+ ///
+ /// 尝试执行对象验证,返回验证结果
+ ///
+ /// 要验证的模型
+ /// 验证结果
+ /// 通过验证True,否则False
+ public static bool TryModelValidation(this object model, out List results) {
+ var context = new ValidationContext(model);
+ results = new List();
+ return Validator.TryValidateObject(model, context, results, true);
+ }
+ }
+}