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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,7 @@ _Pvt_Extensions

# FAKE - F# Make
.fake/

# JetBrains Rider
.idea/
*.sln.iml
43 changes: 31 additions & 12 deletions src/RestApiHelpers/Validation/ValidateActionParametersAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,43 @@ public ValidateActionParametersAttribute()
Order = 1;
}

public override void OnActionExecuting(ActionExecutingContext context)
public override void OnActionExecuting(ActionExecutingContext executingContext)
{
var descriptor = context.ActionDescriptor as ControllerActionDescriptor;
var descriptor = executingContext.ActionDescriptor as ControllerActionDescriptor;

if (descriptor != null)
{
var parameters = descriptor.MethodInfo.GetParameters();

foreach (var parameter in parameters)
{
var argument = context.ActionArguments.ContainsKey(parameter.Name) ?
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.

var validationContext = PrepareValidationContext(parameter, argument);
EvaluateValidationAttributes(parameter, validationContext, executingContext.ModelState);
}
}

base.OnActionExecuting(context);
base.OnActionExecuting(executingContext);
}

private void EvaluateValidationAttributes(ParameterInfo parameter, object argument, ModelStateDictionary modelState)
private ValidationContext PrepareValidationContext(ParameterInfo parameter, object argument)
{
var validationAttributes = parameter.CustomAttributes;
var validationContext = new ValidationContext(argument, serviceProvider: null, items: null);

validationContext.DisplayName = parameter.Name;
validationContext.MemberName = parameter.Member.Name;

return validationContext;
}

private void EvaluateValidationAttributes(
ParameterInfo parameter,
ValidationContext validationContext,
ModelStateDictionary modelState)
{
var validationAttributes = parameter.CustomAttributes;

foreach (var attributeData in validationAttributes)
{
var attributeInstance = CustomAttributeExtensions.GetCustomAttribute(parameter, attributeData.AttributeType);
Expand All @@ -52,10 +65,16 @@ private void EvaluateValidationAttributes(ParameterInfo parameter, object argume

if (validationAttribute != null)
{
var isValid = validationAttribute.IsValid(argument);
if (!isValid)
var validationResult = validationAttribute
.GetValidationResult(
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).

{
modelState.AddModelError(parameter.Name, validationAttribute.FormatErrorMessage(parameter.Name));
modelState.AddModelError(
validationContext.DisplayName,
validationResult?.ErrorMessage);
}
}
}
Expand Down