Comments (8)
I just finished the initial work for a custom result factory. In your case it would work something like this:
using SharpGrip.FluentValidation.AutoValidation.Mvc.Extensions;
builder.Services.AddFluentValidationAutoValidation(configuration =>
{
// Replace the default result factory with a custom implementation.
configuration.OverrideDefaultResultFactoryWith<CustomResultFactory>();
});
public class CustomResultFactory : IFluentValidationAutoValidationResultFactory
{
public IActionResult CreateActionResult(ActionExecutingContext context, ValidationProblemDetails? validationProblemDetails)
{
return new BadRequestObjectResult(new {Title = "Validation errors", ValidationErrors = validationProblemDetails?.Errors});
}
}
You can return a fully custom response when the model state is invalid. You will be able to access the ValidationProblemDetails
containing the validation errors and the full ActionExecutingContext
. Would something like this help you?
from fluentvalidation.autovalidation.
Hi @xplatsolutions, I just released the v1.1.0 versions of the packages containing the option for providing a custom result factory.
from fluentvalidation.autovalidation.
I will check it out ASAP! Thanks for this one
from fluentvalidation.autovalidation.
Confirming the approach worked like a charm! Thank you
from fluentvalidation.autovalidation.
Hi @xplatsolutions, interesting feature. Perhaps it's already possible by customizing the default problem details as explained here: https://learn.microsoft.com/en-us/aspnet/core/web-api/handle-errors?view=aspnetcore-7.0#implement-problemdetailsfactory.
Let me look into it and if it's not possible I'll integrate it into the MVC package and perhaps the Minimal APIs package.
from fluentvalidation.autovalidation.
That would be amazing if we figure it out, can be tricky but I imagine something like adding our own JSON for each error.
Example:
{
"errorCode":200,
"domainCode":100,
"errors":{
"ShownGenderOnProfile":[
{
"domainCode":4,
"errorCode":20,
"errorMessage":"ShownGenderOnProfile must not be empty",
"data":null
}
]
}
}
from fluentvalidation.autovalidation.
This looks like exactly what I need to create my own custom response for all the errors! I can try it out ASAP if you want :)
from fluentvalidation.autovalidation.
Awesome! Thanks for getting back.
from fluentvalidation.autovalidation.
Related Issues (20)
- MVC filter breaks if parameter is not present in the action arguments
- Auto validation with custom model binder HOT 3
- Improve/extend context in the IFluentValidationAutoValidationResultFactory HOT 3
- Disabling validation of parameters with [NeverValidate] HOT 3
- add FluentValidationAutoValidationActionFilter BindingSource.Path HOT 4
- Net8 FluentAutovalidation and the default ModelStateInvalidFilter HOT 4
- Razor Pages support HOT 1
- Multiple binding sources are not supported HOT 6
- Enable the user to customize the validation context based on MVC context HOT 4
- Automatic Validation not work HOT 1
- Add option to include the `ErrorCode` instead of the `ErrorMessage` in the `ModelState` HOT 1
- Remove obsolete code in v2
- Simplifying TypeExtensions.cs IsCustomType method HOT 2
- ICollection<T> support HOT 2
- Improve FluentValidation DI setup documentation in README
- Improve MVC endpoint detection HOT 1
- Rework the logic of checking if a FluentValidationAutoValidationAttribute is present on the controller or method
- Validators not applied to parameters with BindingSource = Form HOT 7
- Enable support for .NET 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fluentvalidation.autovalidation.