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

namespace Atomx.Admin.Client.Validators
{
    public class SpecificationAttributeOptionModelValidator : AbstractValidator<SpecificationAttributeOptionModel>
    {
        public SpecificationAttributeOptionModelValidator()
        {
            RuleFor(p => p.Value).NotEmpty().WithMessage("数值不能为空");
            RuleFor(p => p.Status).GreaterThan(0).WithMessage("请设置正确的状态");
        }

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