瀏覽代碼

Added FluentValidation package

Lukas Angerer 1 年之前
父節點
當前提交
ae825ec891
共有 5 個文件被更改,包括 54 次插入4 次删除
  1. 4 0
      BlazorEditForms.csproj
  2. 12 1
      Model/Aggregate.cs
  3. 15 3
      Model/Employee.cs
  4. 11 0
      Model/PhoneNumber.cs
  5. 12 0
      Model/User.cs

+ 4 - 0
BlazorEditForms.csproj

@@ -6,4 +6,8 @@
         <ImplicitUsings>enable</ImplicitUsings>
     </PropertyGroup>
 
+    <ItemGroup>
+      <PackageReference Include="FluentValidation" Version="11.10.0" />
+    </ItemGroup>
+
 </Project>

+ 12 - 1
Model/Aggregate.cs

@@ -1,4 +1,6 @@
-namespace BlazorEditForms.Model;
+using FluentValidation;
+
+namespace BlazorEditForms.Model;
 
 public class Aggregate
 {
@@ -11,3 +13,12 @@ public class Aggregate
     public Employee Employee { get; set; }
     public User? User { get; set; }
 }
+
+public class AggregateValidator : AbstractValidator<Aggregate>
+{
+    public AggregateValidator()
+    {
+        RuleFor(x => x.Employee).NotNull().SetValidator(new EmployeeValidator());
+        When(x => x.User != null, () => RuleFor(x => x.User!).SetValidator(new UserValidator()));
+    }
+}

+ 15 - 3
Model/Employee.cs

@@ -1,13 +1,25 @@
-using System.ComponentModel.DataAnnotations;
+using FluentValidation;
 
 namespace BlazorEditForms.Model;
 
 public class Employee
 {
-    [Required]
-    [MinLength(3)]
     public string Code { get; set; } = string.Empty;
     public string Firstname { get; set; } = string.Empty;
     public string Lastname { get; set; } = string.Empty;
     public IList<PhoneNumber> PhoneNumbers { get; set; } = [];
 }
+
+public class EmployeeValidator : AbstractValidator<Employee>
+{
+    public EmployeeValidator()
+    {
+        RuleFor(x => x.Code).NotEmpty().MinimumLength(3);
+        RuleForEach(x => x.PhoneNumbers).SetValidator(new PhoneNumberValidator());
+        RuleFor(x => x.Firstname).MustAsync(async (value, ct) =>
+        {
+            await Task.Delay(500);
+            return value == "Foo";
+        });
+    }
+}

+ 11 - 0
Model/PhoneNumber.cs

@@ -1,5 +1,7 @@
 using System.ComponentModel.DataAnnotations;
 
+using FluentValidation;
+
 namespace BlazorEditForms.Model;
 
 public class PhoneNumber
@@ -8,3 +10,12 @@ public class PhoneNumber
     public string Number { get; set; } = string.Empty;
     public PhoneNumberType Type { get; set; }
 }
+
+public class PhoneNumberValidator : AbstractValidator<PhoneNumber>
+{
+    public PhoneNumberValidator()
+    {
+        RuleFor(x => x.Number).NotEmpty().Matches(@"\+?\d+");
+        RuleFor(x => x.Type).IsInEnum();
+    }
+}

+ 12 - 0
Model/User.cs

@@ -1,5 +1,8 @@
 using System.ComponentModel.DataAnnotations;
 
+using FluentValidation;
+using FluentValidation.Validators;
+
 namespace BlazorEditForms.Model;
 
 public class User
@@ -11,3 +14,12 @@ public class User
     [MinLength(3)]
     public string Code { get; set; } = string.Empty;
 }
+
+public class UserValidator : AbstractValidator<User>
+{
+    public UserValidator()
+    {
+        RuleFor(u => u.UserId).NotEmpty().EmailAddress();
+        RuleFor(x => x.Code).NotEmpty().MinimumLength(3);
+    }
+}