using FluentValidation;
using MasaBlazorApp3.Pojo;

namespace MasaBlazorApp3.Validator
{
    public class LoginModelValidator : AbstractValidator<User>
    {
        public LoginModelValidator()
        {
            RuleFor(u => u.Username).NotEmpty().WithMessage("用户名不能为空");
            RuleFor(u => u.Password).NotEmpty();
        }

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