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

Introduction of th AnyAttribute #25

Merged
merged 1 commit into from Aug 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -20,7 +20,7 @@ A Value Object that can be represented by a single scalar.

Technical requirements
----------------------
Because we use .NET standard to support both .NET 4.5 (and higher) as .NET Core (2.0)
Because we use .NET standard to support both .NET 4.5 (and higher) as .NET Standard (2.0)
the Visual Studio solution file requires VS2017.3 or higher. Visual Studio can be downloaded
here: [visualstudio.com/downloads](https://www.visualstudio.com/downloads/).

Expand Down Expand Up @@ -225,6 +225,7 @@ We're extending the DataAnnotations from Microsoft with some more attributes:
* [Mandatory] Here the difference with Microsoft's [Required] attribute is that it works for value types as well, it will be invalid if the default value is used.
* [AllowedValues] and
* [ForbiddenValues] make it easy to validate string values, or objects/value types that have a string representation.
* [Any] Tells that a collection should have at least one item.

Result model
------------
Expand Down
22 changes: 22 additions & 0 deletions src/Qowaiv.ComponentModel/DataAnnotations/AnyAttribute.cs
@@ -0,0 +1,22 @@
using System;
using System.Collections;
using System.ComponentModel.DataAnnotations;

namespace Qowaiv.ComponentModel.DataAnnotations
{
/// <summary>Specifies that a field should at least have one item in its collection.</summary>
public class AnyAttribute : RequiredAttribute
{
/// <summary>Returns true if the value is not null and the collection
/// has any item, otherwise false.
/// </summary>
public override bool IsValid(object value)
{
if (value is IEnumerable enumerable)
{
return enumerable.GetEnumerator().MoveNext();
}
return base.IsValid(value);
}
}
}
@@ -0,0 +1,38 @@
using NUnit.Framework;
using Qowaiv.ComponentModel.DataAnnotations;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Qowaiv.ComponentModel.UnitTests.DataAnnotations
{
public class AnyAttributeTest
{
[Test]
public void IsValid_Null_IsFalse()
{
var attribute = new AnyAttribute();
IEnumerable collection = null;
Assert.IsFalse(attribute.IsValid(collection));
}

[Test]
public void IsValid_Empty_IsFalse()
{
var attribute = new AnyAttribute();
var collection = new int[0];
Assert.IsFalse(attribute.IsValid(collection));
}

[Test]
public void IsValid_1Item_IsTrue()
{
var attribute = new AnyAttribute();
var collection = new int[1];
Assert.IsTrue(attribute.IsValid(collection));
}
}
}