Skip to content

Commit

Permalink
Issue andrebaltieri#6 Added Ability To Have Conditional Rules In Cont…
Browse files Browse the repository at this point in the history
…ract

Desired example use case in issue:

Contract.Required().IfNotNull(x.LastName, contract => contract.MinLength(3));

Implemented feature :

_dummy = new Dummy();
_dummy.stringProp = "abc";

var wrong = new Contract()
    .Requires()
    .IfNotNull(_dummy.stringProp, x => x.IsDigit(_dummy.stringProp, nameof(_dummy.stringProp), "Property should be digit if not null"));
  • Loading branch information
mstrYoda committed Aug 30, 2018
1 parent 150a2d7 commit b9916b1
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
82 changes: 82 additions & 0 deletions Flunt.Tests/ConditionalValidationContractTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using Fluent.Tests.Entities;
using Flunt.Validations;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Flunt.Tests
{
[TestClass]
public class ConditionalValidationContractTests
{
private Dummy _dummy;

[TestMethod]
[TestCategory("ConditionalValidation")]
public void IfNotNullForString()
{
_dummy = new Dummy();
_dummy.stringProp = "abc";

var wrong = new Contract()
.Requires()
.IfNotNull(_dummy.stringProp, x => x.IsDigit(_dummy.stringProp, nameof(_dummy.stringProp), "Property should be digit if not null"));

Assert.AreEqual(false, wrong.Valid);
Assert.AreEqual(1, wrong.Notifications.Count);

_dummy.stringProp = "1234";

var right = new Contract()
.Requires()
.IfNotNull(_dummy.stringProp, x => x.IsDigit(_dummy.stringProp, nameof(_dummy.stringProp), "Property should be digit if not null"))
.IfNotNull(_dummy.stringProp, x => x.HasMinLen(_dummy.stringProp, 1, nameof(_dummy.stringProp), "Property should be digit if not null"));

Assert.AreEqual(true, right.Valid);
}

[TestMethod]
[TestCategory("ConditionalValidation")]
public void IfNotNullForNullableInt()
{
_dummy = new Dummy();
_dummy.nullableIntProp = 1;

var wrong = new Contract()
.Requires()
.IfNotNull(_dummy.nullableIntProp, x => x.IsGreaterOrEqualsThan(_dummy.nullableIntProp.Value, 5, nameof(_dummy.nullableIntProp), "Property should be greater or equal than given value if not null"));

Assert.AreEqual(false, wrong.Valid);
Assert.AreEqual(1, wrong.Notifications.Count);

_dummy.nullableIntProp = null;

var right = new Contract()
.Requires()
.IfNotNull(_dummy.nullableIntProp, x => x.IsGreaterOrEqualsThan(_dummy.nullableIntProp.Value, 5, nameof(_dummy.nullableIntProp), "Property should be greater or equal than given value if not null"));

Assert.AreEqual(true, right.Valid);
}

[TestMethod]
[TestCategory("ConditionalValidation")]
public void IfNotNullForObject()
{
_dummy = new Dummy();
_dummy.objectProp = "some other object";

var wrong = new Contract()
.Requires()
.IfNotNull(_dummy.objectProp, x => x.AreEquals(_dummy.objectProp, "some object", nameof(_dummy.objectProp), "Property should be equal if not null"));

Assert.AreEqual(false, wrong.Valid);
Assert.AreEqual(1, wrong.Notifications.Count);

_dummy.objectProp = null;

var right = new Contract()
.Requires()
.IfNotNull(_dummy.objectProp, x => x.AreEquals(_dummy.objectProp, "some object", nameof(_dummy.objectProp), "Property should be equal if not null"));

Assert.AreEqual(true, right.Valid);
}
}
}
1 change: 1 addition & 0 deletions Flunt.Tests/Entities/Dummy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public class Dummy
public DateTime dateTimeProp {get;set;}
public Guid guidProp {get;set;}
public TimeSpan timeSpanProp { get; set; }
public int? nullableIntProp { get; set; }
}
}
14 changes: 13 additions & 1 deletion Flunt/Validations/Contract.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Flunt.Notifications;
using System;
using System.Linq.Expressions;
using Flunt.Notifications;

namespace Flunt.Validations
{
Expand All @@ -22,5 +24,15 @@ public Contract Join(params Notifiable[] items)

return this;
}

public Contract IfNotNull(object parameterType, Expression<Func<Contract, Contract>> contractExpression)
{
if (parameterType != null)
{
contractExpression.Compile().Invoke(this);
}

return this;
}
}
}

0 comments on commit b9916b1

Please sign in to comment.