Skip to content

Instantly share code, notes, and snippets.

@mattwcole
Created August 20, 2019 15:24
Show Gist options
  • Save mattwcole/bd66fe39c1c6b34c83e2438acf4395db to your computer and use it in GitHub Desktop.
Save mattwcole/bd66fe39c1c6b34c83e2438acf4395db to your computer and use it in GitHub Desktop.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddFluentValidation(config =>
{
config.RegisterValidatorsFromAssemblyContaining<FooValidator>();
config.RunDefaultMvcValidationAfterFluentValidationExecutes = false;
})
.ConfigureApiBehaviorOptions(options =>
{
options.InvalidModelStateResponseFactory = context => new BadRequestObjectResult(context.ModelState.SelectMany(
modelState => modelState.Value.Errors.Select(error =>
new ClientError
{
Item = modelState.Key,
Message = error.ErrorMessage
})));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment