Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

usage of ValidationContext in ValidateActionParametersAttribute #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

usage of ValidationContext in ValidateActionParametersAttribute #2

wants to merge 2 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Mar 31, 2018

Hi Mark!
I've found that custom model validation attributes sometimes override IsValid method with two parameters: object value and ValidationContext validationContext. That's why I decided to add following improvements in ValidateActionParametersAttribute.

Hope you'll find it helpful.

Anyway, since ASP.Net Core 2.1 we will no longer need this attribute: https://andrewlock.net/coming-in-asp-net-core-2-1-top-level-mvc-parameter-validation/

@ghost
Copy link
Author

ghost commented Apr 2, 2018

Oh, I've forgot to fix tests. I'll try to fix it this week

validationContext.ObjectInstance,
validationContext);

if (!ReferenceEquals(validationResult, ValidationResult.Success))
Copy link
Owner

@markvincze markvincze Apr 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use just simple !=, that's easier to read, plus the framework seems to be using it too (for example here: https://github.com/aspnet/Mvc/blob/24eaa740f5b1736700d8d91053f60d690f4fc17e/src/Microsoft.AspNetCore.Mvc.DataAnnotations/Internal/ValidatableObjectAdapter.cs#L51).

context.ActionArguments[parameter.Name] : null;

EvaluateValidationAttributes(parameter, argument, context.ModelState);
var argument = executingContext.ActionArguments[parameter.Name];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you actually test that this doesn't throw if the collection doesn't contain this key? (I don't know what the runtime type of ActionArguments is, but if it's a simple Dictionary<K, V>, then it's supposed to throw.)
If it can throw, the I'd prefer to have a proper check here, even if it shouldn't happen in practice.

@markvincze
Copy link
Owner

Hey @LutsenkoKirill,
Thanks for the PR! I left two comments, once those are addressed and the tests are fixed, we can merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant