using Atomx.Admin.Client.Models; using FluentValidation; using Microsoft.Extensions.Localization; namespace Atomx.Admin.Client.Validators { public class ProductAttributeModelValidator : AbstractValidator { public ProductAttributeModelValidator(IStringLocalizer localizer) { RuleFor(p => p.Name).NotEmpty().WithMessage("名称不能为空"); RuleFor(p => p.Status).GreaterThan(0).WithMessage("请设置正确的状态"); RuleFor(p => p.CategoryId).GreaterThan(0).WithMessage("请设置属性归属分类"); } public Func>> ValidateValue => async (model, propertyName) => { var result = await ValidateAsync(ValidationContext.CreateWithOptions((ProductAttributeModel)model, x => { x.IncludeProperties(propertyName); })); if (result.IsValid) return Array.Empty(); return result.Errors.Select(e => e.ErrorMessage); }; } }