Files
blazor-saas-crm/Shared/Utils/FluentValidationHelper.cs
T
2026-07-21 14:14:44 +07:00

21 lines
665 B
C#

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);
};
}