Allow passing a dictionary to ValidationProblemDetails
Fixes https://github.com/aspnet/Mvc/issues/8645
This commit is contained in:
parent
ccde910b29
commit
35d2ab37f7
|
|
@ -22,6 +22,10 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
Title = Resources.ValidationProblemDescription_Title;
|
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)
|
public ValidationProblemDetails(ModelStateDictionary modelState)
|
||||||
: this()
|
: this()
|
||||||
{
|
{
|
||||||
|
|
@ -63,7 +67,22 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
[JsonProperty(PropertyName = "errors")]
|
[JsonProperty(PropertyName = "errors")]
|
||||||
public IDictionary<string, string[]> Errors { get; } = new Dictionary<string, string[]>(StringComparer.Ordinal);
|
public IDictionary<string, string[]> Errors { get; } = new Dictionary<string, string[]>(StringComparer.Ordinal);
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
|
@ -73,5 +74,34 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
Assert.Equal(new[] { "The input was not valid." }, item.Value);
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue