31 lines
1.1 KiB
C#
31 lines
1.1 KiB
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) =>
|
|
// {
|
|
// var result = await validator.ValidateAsync(ValidationContext<T>.CreateWithOptions((T)model, x => x.IncludeProperties(propertyName)));
|
|
// if (result.IsValid) return Array.Empty<string>();
|
|
// return result.Errors.Select(e => e.ErrorMessage);
|
|
// };
|
|
//}
|
|
|
|
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);
|
|
};
|
|
}
|