Git Product home page Git Product logo

Comments (8)

mvdgun avatar mvdgun commented on June 9, 2024 1

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.

mvdgun avatar mvdgun commented on June 9, 2024 1

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.

xplatsolutions avatar xplatsolutions commented on June 9, 2024 1

I will check it out ASAP! Thanks for this one

from fluentvalidation.autovalidation.

xplatsolutions avatar xplatsolutions commented on June 9, 2024 1

Confirming the approach worked like a charm! Thank you

from fluentvalidation.autovalidation.

mvdgun avatar mvdgun commented on June 9, 2024

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.

xplatsolutions avatar xplatsolutions commented on June 9, 2024

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.

xplatsolutions avatar xplatsolutions commented on June 9, 2024

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.

mvdgun avatar mvdgun commented on June 9, 2024

Awesome! Thanks for getting back.

from fluentvalidation.autovalidation.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.