Skip to content

Instantly share code, notes, and snippets.

@denismaster
Created February 15, 2017 13:04
Show Gist options
  • Save denismaster/6f870a6be10674b536b1aae3dab04167 to your computer and use it in GitHub Desktop.
Save denismaster/6f870a6be10674b536b1aae3dab04167 to your computer and use it in GitHub Desktop.
Server validation errors in .NET Core with FluentValidation via ExMethod
public static class ControllerExtensions
{
public static void GetErrors(this Controller controller, object model, OperationResult result)
{
if (model == null)
{
result.Errors.Add("Ошибка ввода данных");
}
result.Errors.AddRange(controller.ModelState.Values.SelectMany(v => v.Errors
.Where(b => !string.IsNullOrEmpty(b.ErrorMessage))
.Select(b => b.ErrorMessage)));
}
}
@denismaster
Copy link
Author

We provide result as parameter because in my buissness logic we also do some checks before and after validation.
But in other cases I'd love to return a new instance of OperationResult.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment