模型自动验证机制

This commit is contained in:
administrator committed 2024-04-06 10:06:38 +08:00
1 parent 055cf5997f
commit 62a8b7f569
8 files changed
+137 -3

No files matched your search

@@ -0,0 +1,24 @@
using FluentValidation;
using HelloShop.IdentityService.Entities;
using HelloShop.IdentityService.EntityFrameworks;
using HelloShop.IdentityService.Models.Users;
using Microsoft.EntityFrameworkCore;
namespace HelloShop.IdentityService.Validations.Users;
public class UserCreateRequestValidator : AbstractValidator<UserCreateRequest>
{
public UserCreateRequestValidator(IdentityServiceDbContext dbContext)
{
RuleFor(m => m.UserName).NotNull().NotEmpty().Length(8, 16).Matches("^[a-zA-Z]+$");
RuleFor(m => m.PhoneNumber).NotNull().NotEmpty().Length(11).Matches(@"^1\d{10}$").Must((phoneNumber) =>
{
return !dbContext.Set<User>().Any(e => e.PhoneNumber == phoneNumber);
});
RuleFor(m => m.Password).NotNull().NotEmpty().Length(8, 16);
RuleFor(m => m.Email).EmailAddress().Length(8, 32);
}
}