Files
Atomx/Atomx.Admin/Atomx.Admin.Client/Validators/AppVersionModelValidator.cs
2025-12-17 00:37:57 +08:00

40 lines
1.8 KiB
C#

using Atomx.Admin.Client.Models;
using Atomx.Common.Enums;
using FluentValidation;
using Microsoft.Extensions.Localization;
namespace Atomx.Admin.Client.Validators
{
public class AppVersionModelValidator : AbstractValidator<AppVersionModel>
{
public AppVersionModelValidator(IStringLocalizer<LoginModelValidator> localizer)
{
RuleFor(p => p.AppName).NotEmpty().WithMessage("应用名不能为空");
RuleFor(p => p.Title).NotEmpty().WithMessage("版本标题不能为空");
RuleFor(p => p.VersionX).GreaterThanOrEqualTo(0).WithMessage("请填写正确的主版本号");//大于或等于0
RuleFor(p => p.VersionY).GreaterThanOrEqualTo(0).WithMessage("请填写正确的次版本号");//大于或等于0
RuleFor(p => p.VersionZ).GreaterThanOrEqualTo(0).WithMessage("请填写正确的修订版本号");//大于或等于0
RuleFor(p => p.Platform).GreaterThan(0).WithMessage("请选择运行平台");
RuleFor(p => p.VersionState).GreaterThan(0).WithMessage("请选择版本状态");
RuleFor(p => p.Status).Must(ValidateStatus).WithMessage("请选择发布状态");
}
private bool ValidateStatus(int status)
{
if (status != (int)DataStatus.Enable && status != (int)DataStatus.Disable)
{
return false;
}
return true;
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<AppVersionModel>.CreateWithOptions((AppVersionModel)model, x => { x.IncludeProperties(propertyName); }));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}
}