Alex's Coding Blog
  • Home
  • About
  • Projects
  • Contact me

Blog

How to disable object model validation .NET 7

  • January 12, 2023 January 12, 2023
  • by Alexander

After migration to .NET 7 my project's integration tests begin fail because of build-in model validation. The API Request class including not required properties such as string(?) and List<T>, were processed as mandatory and gave 400 error.

For years, I use my own validators for the requests model, its allows me more flexibility for response I return, http codes and etc.

More info for the topic can be found here

I didn't spend mush time on it but here are two solutions I found and tested:

  1. Create and register own dummy IObjectModelValidator
public class NullObjectModelValidator : IObjectModelValidator
{
    public void Validate(ActionContext actionContext,
        ValidationStateDictionary validationState, string prefix, object model)
    {

    }
}

and then add the following to Startup.cs

services.AddSingleton<IObjectModelValidator, NullObjectModelValidator>();
  1. Disable validation
 services.AddControllers(o => o.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true )

I'm not a big fan of dummy object in production code, rather in tests, second option was chosen.

.NET Core How-to

Debugging microservices is always tricky and time consuming specially without proper code level logging in place.

Meet Konso. It's developed as a solution for this challenge and can help with saving development effort for your team up to 30%. 🎯🎉

The key functions are:

🔥 Centralized logging for your microservices
🔥 Tracing with metrics and value tracking events
🔥 Ad-Hoc events exploration
🔥 Saving your queries
🔥 Create alerts and get notified


You can start collecting you project's logs in 5 minutes. 🕐💪

To Learn more about logging tool, book a free demo.
To get started for free, Create your free account now

Alexander Lvovich

Solution Architect and Software Developer.

Share on:

No comments are allowed for this post

Recent Posts

  • Easily track and troubleshoot errors in your .NET 7 web app using Konso's logging feature
  • Keep Your Passwords Safe: 5 Tips for Protecting Yourself After the Latest LastPass Data Breach
  • How to ping from .net application container
  • Freelancing myths you should consider before dive in
  • How to set up a honey token for dotnet project with Konso

Categories

  • Azure
  • Architecture
  • .NET Core
  • Certification
  • DevOps
  • How-to
  • Azure Functions
  • Serverless
  • Cosmos DB
  • Security
  • Thoughts
© Copyright 2023, Alexander Lvovich. Theme by Colorlib