cremor
cremor

Reputation: 6876

How to disable the model property validation of not-nullable properties?

I'm using FluentValidation in an ASP.NET Core 6 Web API project. This works fine for most cases, so:

But there is one specific case that is problematic:

My current workaround is to annotate all that not-nullable properties with [ValidateNever] so that ASP.NET Core ignores them, but this is not nice.

Is there a way to disable ASP.NET Core model property validation of not-nullable properties?

Note: I can't disable ASP.NET Core validation completely because then it won't even return validation error results for JSON syntax errors.

Upvotes: 6

Views: 3078

Answers (1)

Ruikai Feng
Ruikai Feng

Reputation: 11666

try to set as below :

builder.Services.AddControllersWithViews(options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true);

The problem has been explained in this document: https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.mvcoptions.suppressimplicitrequiredattributefornonnullablereferencetypes?view=aspnetcore-6.0

Upvotes: 10

Related Questions