Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Allow passing a dictionary to ValidationProblemDetails
Browse files Browse the repository at this point in the history
Fixes #8645
  • Loading branch information
pranavkm committed Oct 29, 2018
1 parent ccde910 commit 35d2ab3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/Microsoft.AspNetCore.Mvc.Core/ValidationProblemDetails.cs
Expand Up @@ -22,6 +22,10 @@ public ValidationProblemDetails()
Title = Resources.ValidationProblemDescription_Title;
}

/// <summary>
/// Initializes a new instance of <see cref="ValidationProblemDetails"/> using the specified <paramref name="modelState"/>.
/// </summary>
/// <param name="modelState"><see cref="ModelStateDictionary"/> containing the validation errors.</param>
public ValidationProblemDetails(ModelStateDictionary modelState)
: this()
{
Expand Down Expand Up @@ -63,7 +67,22 @@ string GetErrorMessage(ModelError error)
}

/// <summary>
/// Gets or sets the validation errors associated with this instance of <see cref="ValidationProblemDetails"/>.
/// Initializes a new instance of <see cref="ValidationProblemDetails"/> using the specified <paramref name="errors"/>.
/// </summary>
/// <param name="errors">The validation errors.</param>
public ValidationProblemDetails(IDictionary<string, string[]> errors)
: this()
{
if (errors == null)
{
throw new ArgumentNullException(nameof(errors));
}

Errors = new Dictionary<string, string[]>(errors, StringComparer.Ordinal);
}

/// <summary>
/// Gets the validation errors associated with this instance of <see cref="ValidationProblemDetails"/>.
/// </summary>
[JsonProperty(PropertyName = "errors")]
public IDictionary<string, string[]> Errors { get; } = new Dictionary<string, string[]>(StringComparer.Ordinal);
Expand Down
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Xunit;

Expand Down Expand Up @@ -73,5 +74,34 @@ public void Constructor_SerializesErrorsFromModelStateDictionary_AddsDefaultMess
Assert.Equal(new[] { "The input was not valid." }, item.Value);
});
}

[Fact]
public void Constructor_CopiesPassedInDictionary()
{
// Arrange
var errors = new Dictionary<string, string[]>
{
["key1"] = new[] { "error1", "error2" },
["key2"] = new[] { "error3", },
};

// Act
var problemDescription = new ValidationProblemDetails(errors);

// Assert
Assert.Equal("One or more validation errors occurred.", problemDescription.Title);
Assert.Collection(
problemDescription.Errors,
item =>
{
Assert.Equal("key1", item.Key);
Assert.Equal(new[] { "error1", "error2" }, item.Value);
},
item =>
{
Assert.Equal("key2", item.Key);
Assert.Equal(new[] { "error3" }, item.Value);
});
}
}
}

0 comments on commit 35d2ab3

Please sign in to comment.