Alex's Coding Blog
  • home
  • about
  • projects
  • contact

Blog

How to disable object model validation .NET 7

  • January 12, 2023 March 11, 2025
  • 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

🚀 Turbocharge Your Kubernetes Cluster with my Terraform Kits! 🚀

🌟 Slash deployment time and costs! Discover the ultimate solution for efficient, cost-effective Kubernetes Terraform Kits. Perfect for DevOps enthusiasts looking for a reliable, scalable setup.

Learn More about Terraform Kits for AKS,EKS and GKE

Alexander Lvovich

Solution Architect & Software Developer | Automating & Scaling Infrastructure

💡 Working with Kubernetes, Istio, and DevOps. Got questions? Feel free to reach out!

Share on:

No comments are allowed for this post

Recent Posts

  • Securing Web Services Against Unwanted Traffic with NGINX
  • Optimizing API by Offloading Responsibilities to an API Gateway
  • How to Clean Up Local Branches of Remote Merged Branches
  • Resolving Namespace Overriding in Argo CD with Kustomize
  • Connecting to Gitlab's private Nuget registry
  • Why Istio?

Categories

  • Azure
  • Architecture
  • .NET Core
  • Certification
  • DevOps
  • How-to
  • Azure Functions
  • Serverless
  • Cosmos DB
  • Security
  • Thoughts
  • Kubernetes
  • Istio
© Copyright 2025, Alexander Lvovich. Theme by Colorlib
I use cookies and similar technologies on our website to enhance your browsing experience and analyze website traffic. By clicking "Accept," you consent with my Privacy Policy to the use of these technologies.
Accept