Skip to content

Commit

Permalink
Update the MinimumElementsAttribute to support all Enumerable value
Browse files Browse the repository at this point in the history
  • Loading branch information
FAREAST\jianxia committed Apr 6, 2017
1 parent 9ca3925 commit a643270
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 7 deletions.
5 changes: 5 additions & 0 deletions BigEgg.Core.Tests/BigEgg.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,18 @@
<Compile Include="Utils\PathExtensionsTest.cs" />
<Compile Include="Utils\XmlSerializeExtensionsTest.cs" />
<Compile Include="ValidatableObjectTest.cs" />
<Compile Include="Validations\MinimumElementsAttributeTest.cs" />
<Compile Include="Validations\RequiredIfAttributeTest.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BigEgg.Core\BigEgg.Core.csproj">
<Project>{34CE1CF3-A6C4-4022-BC02-6985FEEB9473}</Project>
<Name>BigEgg.Core</Name>
</ProjectReference>
<ProjectReference Include="..\BigEgg.UnitTesting\BigEgg.UnitTesting.csproj">
<Project>{a70395a7-1790-4e49-b8b5-1eb78c65b13c}</Project>
<Name>BigEgg.UnitTesting</Name>
</ProjectReference>
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
Expand Down
102 changes: 102 additions & 0 deletions BigEgg.Core.Tests/Validations/MinimumElementsAttributeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
namespace BigEgg.Tests.Validations
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using BigEgg.UnitTesting;

using BigEgg.Validations;

public class MinimumElementsAttributeTest
{
[TestClass]
public class GeneralTest
{
[TestMethod]
public void ConstructorTest()
{
AssertHelper.ExpectedException<ArgumentException>(() => new MinimumElementsAttribute(-1));
}
}

[TestClass]
public class AllowNullTest
{
[TestMethod]
public void AllowNull()
{
var data = new AllowNullData() { Data = null };
Assert.IsFalse(data.Validate().Any());

data.Data = new List<string>();
Assert.IsFalse(data.Validate().Any());
}

[TestMethod]
public void NotAllowNull()
{
var data = new NotAllowNullData() { Data = null };
Assert.IsTrue(data.Validate().Any());

data.Data = new List<string>();
Assert.IsFalse(data.Validate().Any());
}


public class NotAllowNullData : ValidatableObject
{
[MinimumElements(0, AllowNull = false)]
public object Data { get; set; }
}

public class AllowNullData : ValidatableObject
{
[MinimumElements(0, AllowNull = true)]
public object Data { get; set; }
}
}

[TestClass]
public class ValidationTest
{
[TestMethod]
public void List()
{
var data = new DataInfo() { Data = new List<string>() };
Assert.IsTrue(data.Validate().Any());

data = new DataInfo() { Data = new List<string>() { "test" } };
Assert.IsFalse(data.Validate().Any());
}

[TestMethod]
public void Array()
{
var data = new DataInfo() { Data = new string[0] };
Assert.IsTrue(data.Validate().Any());

data = new DataInfo() { Data = new string[1] };
Assert.IsFalse(data.Validate().Any());
}

[TestMethod]
public void Dictionary()
{
var data = new DataInfo() { Data = new Dictionary<string, string>() };
Assert.IsTrue(data.Validate().Any());

data = new DataInfo() { Data = new Dictionary<string, string>() { { "key", "value" } } };
Assert.IsFalse(data.Validate().Any());
}
}

public class DataInfo : ValidatableObject
{
[MinimumElements(1)]
public IEnumerable Data { get; set; }
}
}
}
33 changes: 26 additions & 7 deletions BigEgg.Core/Validations/MinimumElementsAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

/// <summary>
/// Specifies that a data field (a list) have at lease some elements.
/// Specifies that a data field (a IEnumerable property) have at lease some elements.
/// </summary>
/// <seealso cref="IEnumerable"/>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class MinimumElementsAttribute : ValidationAttribute
{

/// <summary>
/// Initializes a new instance of the <see cref="MinimumElementsAttribute"/> class.
/// </summary>
/// <param name="minElementsCount">The minimum elements count.</param>
public MinimumElementsAttribute(int minElementsCount)
{
Preconditions.Check(minElementsCount >= 0, "The minimum elements count cannot less than 0.");

MinElementsCount = minElementsCount;
}

Expand Down Expand Up @@ -48,16 +51,32 @@ public MinimumElementsAttribute(int minElementsCount)
/// </summary>
/// <param name="value">The value of the object to validate.</param>
/// <returns>
/// true if if the data field (list) has at least some elements; otherwise, false.
/// true if the data field (list) has at least some elements; otherwise, false.
/// </returns>
public override bool IsValid(object value)
{
if (value == null && AllowNull) { return true; }
if (value == null) { return AllowNull; }
Preconditions.Check<ValidationException>(value is IEnumerable, $"The property should be a IEnumerable data.");

Preconditions.Check<ValidationException>(value is IList, $"The property should be a list.");
var count = 0;

var list = value as IList;
return list.Count >= MinElementsCount;
if (value is ICollection)
{
count = (value as ICollection).Count;
}
else if (value is ICollection<object>)
{
count = (value as ICollection<object>).Count;
}
else
{
var enumerator = (value as IEnumerable).GetEnumerator();
while (enumerator.MoveNext())
{
count++;
}
}
return count >= MinElementsCount;
}
}
}

0 comments on commit a643270

Please sign in to comment.