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

Refactored validation part #152

Merged
merged 2 commits into from
Dec 17, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 5 additions & 25 deletions src/Valit/Rules/CollectionValitRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,14 @@ IValitResult IValitRule<TObject>.Validate(TObject @object)

var collection = _collectionSelector(@object);

var result = ValitResult.Success;

foreach(var property in collection)
var rules = collection.SelectMany(p =>
{
Func<TObject, TProperty> selector = _ => property;
Func<TObject, TProperty> selector = _ => p;
var lastEnsureRule = _ruleFunc(new ValitRule<TObject, TProperty>(selector, _messageProvider));
var propertyRules = lastEnsureRule.GetAllEnsureRules();

result &= ValidatePropertyRules(propertyRules, @object);
return lastEnsureRule.GetAllEnsureRules();
});

if(!result.Succeeded)
{
_strategy.Fail(default(IValitRule<TObject>), result, out bool cancel);
if(cancel)
{
break;
}
}
}

return result;
return rules.ValidateRules(_strategy, @object);
}

private IValitResult ValidatePropertyRules(IEnumerable<IValitRule<TObject>> propertyRules, TObject @object)
=> ValitRules<TObject>
.Create(propertyRules)
.WithStrategy(_strategy)
.For(@object)
.Validate();
}
}
23 changes: 5 additions & 18 deletions src/Valit/Rules/NestedObjectCollectionValitRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,13 @@ IValitResult IValitRule<TObject>.Validate(TObject @object)

var collection = _collectionSelector(@object);

var result = ValitResult.Success;

foreach(var property in collection)
var rules = collection.Select(p =>
{
Func<TObject, TProperty> selector = _ => property;
var nestedObjectValitRule = new NestedObjectValitRule<TObject, TProperty>(selector, _valitRulesProvider, _strategy);

result &= nestedObjectValitRule.Validate(@object);

if(!result.Succeeded)
{
_strategy.Fail(default(IValitRule<TObject>), result, out bool cancel);
if(cancel)
{
break;
}
}
}
Func<TObject, TProperty> selector = _ => p;
return new NestedObjectValitRule<TObject, TProperty>(selector, _valitRulesProvider, _strategy);
});

return result;
return rules.ValidateRules(_strategy, @object);
}

}
Expand Down
35 changes: 35 additions & 0 deletions src/Valit/Rules/ValitRuleExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Collections.Generic;
using System.Linq;
using Valit.Exceptions;
using Valit.Result;

namespace Valit.Rules
{
internal static class ValitRuleExtensions
{
internal static IValitResult ValidateRules<TObject>(this IEnumerable<IValitRule<TObject>> rules, IValitStrategy strategy, TObject @object) where TObject : class
{
rules.ThrowIfNull();

var result = ValitResult.Success;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like an enum to me, maybe it would be clearer to do new ValitResult()?

Copy link
Member Author

@GooRiOn GooRiOn Dec 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can always hover on var so it's clear that's IValitStrategy object :D

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True 😄


foreach(var rule in rules.ToList())
{
result &= rule.Validate(@object);

if(!result.Succeeded)
{
strategy.Fail(rule, result, out bool cancel);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I said this a couple of days ago, but maybe lets make strategy.Fail return a bool?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arekbal did this as an out param. I think he's the right person to ask why we actualy use it :D

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaner API when implementing that method. Instead of guessing or going through some docs to find why method returns bool and what it means(true for cancel or continue?), one just implements a method with clear declaration.

if(cancel)
{
break;
}
}
}

strategy.Done(result);

return result;
}
}
}
22 changes: 1 addition & 21 deletions src/Valit/ValitRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,27 +117,7 @@ IValitResult IValitRules<TObject>.Validate(Func<IValitRule<TObject>, bool> predi
}

private IValitResult Validate(IEnumerable<IValitRule<TObject>> rules)
{
var result = ValitResult.Success;

foreach(var rule in rules.ToList())
{
result &= rule.Validate(_object);

if(!result.Succeeded)
{
_strategy.Fail(rule, result, out bool cancel);
if(cancel)
{
break;
}
}
}

_strategy.Done(result);

return result;
}
=> rules.ValidateRules(_strategy, _object);

private void AddEnsureRules<TProperty>(Func<TObject,TProperty> propertySelector, Func<IValitRule<TObject, TProperty>,IValitRule<TObject, TProperty>> ruleFunc)
{
Expand Down