question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ValidateNever applied to action parameters is not honored

See original GitHub issue

As part of MVC’s support for record types, ValidateNeverAttribute is now allowed to be applied to parameters. However, when applied to action parameters, the attribute has no effect and the parameter will continue to be validated.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
Neutrino-Sunsetcommented, Oct 3, 2022

I’d like to take this opportunity to advocate for having this addressed.

I have a scenario in which an endpoint may receive a payload consisting of a large number (50k) of complex messages. The specification requires that messages whose validatation succeeds are processed, while messages whose validation fails are not processed, and a response with a custom format is generated which indicates which messages succeeded or failed along with any validation errors.

The ValidateNeverAttribute is one of the few ways to actually disable model validation. There are other approaches that ignore the results of validation, but these do not disable it. Ignoring the automatic validation is not an option in my case due to the performance implications of performing complex validation twice.

The fact that ValidateNeverAttribute doesn’t work for parameters means that in some cases the only way to use it is to apply it to a class. But if the class you wish to disable automatic validation for, is the same one you then need to perform manual validation on later, then you have a problem, since the ValidateNeverAttribute applied to the class also disables manual validation.

This is compounded by the fact that ControllerBase.TryValidateModel will recurse and validate a complete payload object graph, while Validator.TryValidateObject does not. The upshot of all this is that if you have a payload object containing nested objects that you wish to disable automatic validation of, but then perform manual validation of in the controller or service, you have no obvious solution at all. You face the prospect of disabling the builtin validation completely and coding something bespoke.

If ValidateNeverAttribute worked in such a way that you could apply it to an action method parameter and it would disable the automatic validation, but still allow you to run validation of the model manually via ControllerBase.TryValidateModel, then anything you wanted to do regarding validation would be possible and life would be rosy.

0reactions
ygoecommented, Feb 10, 2023

I’m also surprised that the documentation contains content that has never been implemented.

My REST API application accepts a new model object in the POST method, but also a partial object in the PUT method so that only the set properties will be changed. That means I need a model class with Required attributes and an exact copy without them. Or I’ll just ditch ASP.NET Core model validation for its uselessness in real life and do it all myself. Looks like that’s what I have to do.

The default validation kicks in before the action is even called, so I cannot use any validation attributes that might be dynamic. Which is very often the case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Correct way to disable model validation in ASP.Net Core 2 ...
I tried typeof(ControllerBase) as the type, but it didn't work. Then I realized that this needs to be the type of an incoming...
Read more >
ValidateNeverAttribute Class (Microsoft.AspNetCore.Mvc. ...
Indicates that a property or parameter should be excluded from validation. When applied to a property, the validation system excludes that property.
Read more >
ASP.NET MVC controller not receiving the parameter from ...
I am getting null value for the actor(which is an action parameter). I tried all possible ways to call the action method.
Read more >
Model Validation Attributes in .NET Core Web APIs
e.g. let's say, an action has two parameters – id and name. ... The API controllers do not need to check for ModelState.IsValid...
Read more >
How to validate action parameters with DataAnnotation ...
A simple approach to evaluate DataAnnotation validation attributes not only on model properties, but on the action method parameters as ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found