using FluentValidation;

namespace Indotalent.Shared.Utils;

public static class FluentValidationHelper
{
    public static Func<object, string, Task<IEnumerable<string>>> ValidateValue<T>(this AbstractValidator<T> validator) => async (model, propertyName) =>
    {
        if (model is not T typedModel)
            return Array.Empty<string>();

        var context = ValidationContext<T>.CreateWithOptions(typedModel, x => x.IncludeProperties(propertyName));
        var result = await validator.ValidateAsync(context);

        if (result.IsValid)
            return Array.Empty<string>();

        return result.Errors.Select(e => e.ErrorMessage);
    };
}
