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

namespace Atomx.Admin.Client.Validators
{
    public class ProductAttributeModelValidator : AbstractValidator<ProductAttributeModel>
    {
        public ProductAttributeModelValidator()
        {
            RuleFor(p => p.Name).NotEmpty().WithMessage("名称不能为空");
            RuleFor(p => p.Status).GreaterThan(0).WithMessage("请设置正确的状态");
            RuleFor(p => p.CategoryId).GreaterThan(0).WithMessage("请设置属性归属分类");
        }

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