From 71bfd2839c8643750fb00e0ecbfd42c1caa3d743 Mon Sep 17 00:00:00 2001 From: Dariusz Pawlukiewicz Date: Wed, 13 Dec 2017 20:43:36 +0100 Subject: [PATCH] Refactored validation part --- src/Valit/Rules/CollectionValitRule.cs | 30 +++------------- .../Rules/NestedObjectCollectionValitRule.cs | 23 +++--------- src/Valit/Rules/ValitRuleExtensions.cs | 35 +++++++++++++++++++ src/Valit/ValitRules.cs | 22 +----------- 4 files changed, 46 insertions(+), 64 deletions(-) create mode 100644 src/Valit/Rules/ValitRuleExtensions.cs diff --git a/src/Valit/Rules/CollectionValitRule.cs b/src/Valit/Rules/CollectionValitRule.cs index 9a11715..b76f67f 100644 --- a/src/Valit/Rules/CollectionValitRule.cs +++ b/src/Valit/Rules/CollectionValitRule.cs @@ -34,34 +34,14 @@ IValitResult IValitRule.Validate(TObject @object) var collection = _collectionSelector(@object); - var result = ValitResult.Success; - - foreach(var property in collection) + var rules = collection.SelectMany(p => { - Func selector = _ => property; + Func selector = _ => p; var lastEnsureRule = _ruleFunc(new ValitRule(selector, _messageProvider)); - var propertyRules = lastEnsureRule.GetAllEnsureRules(); - - result &= ValidatePropertyRules(propertyRules, @object); + return lastEnsureRule.GetAllEnsureRules(); + }); - if(!result.Succeeded) - { - _strategy.Fail(default(IValitRule), result, out bool cancel); - if(cancel) - { - break; - } - } - } - - return result; + return rules.ValidateRules(_strategy, @object); } - - private IValitResult ValidatePropertyRules(IEnumerable> propertyRules, TObject @object) - => ValitRules - .Create(propertyRules) - .WithStrategy(_strategy) - .For(@object) - .Validate(); } } diff --git a/src/Valit/Rules/NestedObjectCollectionValitRule.cs b/src/Valit/Rules/NestedObjectCollectionValitRule.cs index 03bceb9..7fc3365 100644 --- a/src/Valit/Rules/NestedObjectCollectionValitRule.cs +++ b/src/Valit/Rules/NestedObjectCollectionValitRule.cs @@ -30,26 +30,13 @@ IValitResult IValitRule.Validate(TObject @object) var collection = _collectionSelector(@object); - var result = ValitResult.Success; - - foreach(var property in collection) + var rules = collection.Select(p => { - Func selector = _ => property; - var nestedObjectValitRule = new NestedObjectValitRule(selector, _valitRulesProvider, _strategy); - - result &= nestedObjectValitRule.Validate(@object); - - if(!result.Succeeded) - { - _strategy.Fail(default(IValitRule), result, out bool cancel); - if(cancel) - { - break; - } - } - } + Func selector = _ => p; + return new NestedObjectValitRule(selector, _valitRulesProvider, _strategy); + }); - return result; + return rules.ValidateRules(_strategy, @object); } } diff --git a/src/Valit/Rules/ValitRuleExtensions.cs b/src/Valit/Rules/ValitRuleExtensions.cs new file mode 100644 index 0000000..a1eb1be --- /dev/null +++ b/src/Valit/Rules/ValitRuleExtensions.cs @@ -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(this IEnumerable> rules, IValitStrategy strategy, TObject @object) where TObject : class + { + rules.ThrowIfNull(); + + 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; + } + } +} \ No newline at end of file diff --git a/src/Valit/ValitRules.cs b/src/Valit/ValitRules.cs index f80605e..a063fbd 100644 --- a/src/Valit/ValitRules.cs +++ b/src/Valit/ValitRules.cs @@ -117,27 +117,7 @@ IValitResult IValitRules.Validate(Func, bool> predi } private IValitResult Validate(IEnumerable> 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(Func propertySelector, Func,IValitRule> ruleFunc) {