Skip to content

Instantly share code, notes, and snippets.

@bradwilson
Last active January 4, 2016 12:59

Revisions

  1. bradwilson revised this gist Jan 25, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions ModelStateDictionaryExtensions.cs
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Http;

    public static class ModelStateDictionaryExtensions
    {
  2. bradwilson revised this gist Jan 25, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion ModelStateDictionaryExtensions.cs
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using ECP.WebAPI;

    public static class ModelStateDictionaryExtensions
    {
  3. bradwilson created this gist Jan 25, 2014.
    14 changes: 14 additions & 0 deletions ModelStateDictionaryExtensions.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using ECP.WebAPI;

    public static class ModelStateDictionaryExtensions
    {
    public static IEnumerable<string> ToErrors(this ModelStateDictionary dict)
    {
    return dict.OrderBy(kvp => kvp.Key)
    .SelectMany(kvp => kvp.Value.Errors.Select(e => Tuple.Create(kvp.Key, e.ErrorMessage)))
    .Select(tuple => String.Format("{0} = {1}", tuple.Item1, tuple.Item2));
    }
    }
    11 changes: 11 additions & 0 deletions SampleTest.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    [Fact]
    public void SampleTest()
    {
    var modelStateDictionary = SomethingUnderTest();

    Assert.Collection(modelStateDictionary.ToErrors(),
    error => Assert.Equal("key1 = The first value for key1"),
    error => Assert.Equal("key1 = The second value for key1"),
    error => Assert.Equal("key2 = The first value for key2")
    );
    }