using Atomx.Admin.Client.Models;
using FluentValidation;

namespace Atomx.Admin.Client.Validators
{
    public class CorporationModelValidator : AbstractValidator<CorporationModel>
    {
        public CorporationModelValidator()
        {
            RuleFor(p => p.Name).NotEmpty().WithMessage("公司名称不能为空");
        }

        public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
        {
            var result = await ValidateAsync(ValidationContext<CorporationModel>.CreateWithOptions((CorporationModel)model, x => { x.IncludeProperties(propertyName); }));
            if (result.IsValid)
                return Array.Empty<string>();
            return result.Errors.Select(e => e.ErrorMessage);
        };
    }
}
