25 lines
864 B
C#
25 lines
864 B
C#
using Atomx.Admin.Client.Models;
|
|
using Atomx.Common.Models;
|
|
using FluentValidation;
|
|
using Microsoft.Extensions.Localization;
|
|
|
|
namespace Atomx.Admin.Client.Validators
|
|
{
|
|
public class MenuModelValidator : AbstractValidator<MenuModel>
|
|
{
|
|
public MenuModelValidator(IStringLocalizer<MenuModelValidator> localizer)
|
|
{
|
|
RuleFor(p => p.Name).NotEmpty().WithMessage("名称不能为空");
|
|
}
|
|
|
|
|
|
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
|
{
|
|
var result = await ValidateAsync(ValidationContext<MenuModel>.CreateWithOptions((MenuModel)model, x => { x.IncludeProperties(propertyName); }));
|
|
if (result.IsValid)
|
|
return Array.Empty<string>();
|
|
return result.Errors.Select(e => e.ErrorMessage);
|
|
};
|
|
}
|
|
}
|