public static ValidationResult Merge(IValidationContext?previousContext, ValidationResult previousResult, IValidationContext?currentContext, ValidationResult currentResult)
        {
            if (previousResult == null)
            {
                throw new ArgumentNullException(nameof(previousResult));
            }
            if (currentResult == null)
            {
                throw new ArgumentNullException(nameof(currentResult));
            }

            if (currentContext != null && ReferenceEquals(previousContext?.InstanceToValidate, currentContext.InstanceToValidate))
            {
                var fakeRule = new FakeRule {
                    RuleSets = previousResult.RuleSetsExecuted
                };
                var selector = currentContext.Selector;

                bool NotInvalidated(ValidationFailure error) => !selector.CanExecute(fakeRule, error.PropertyName, previousContext);

                foreach (var error in previousResult.Errors.Where(NotInvalidated))
                {
                    currentResult.Errors.Add(error);
                }
            }

            return(currentResult);
        }
Ejemplo n.º 2
0
        public void RulesThatCheckContextValue()
        {
            var identity = new Identity("device", "1");
            var context  = CreateContext(identity, new Tuple <string, JsonValue>("PartnerBrand", JsonValue.NewString("ABC")));

            var rulesRepo = RulesRepositoryHelpers
                            .With("path/to/key", FakeRule.Create(ctx => ctx("device.PartnerBrand") == JsonValue.NewString("ABC") ? new ConfigurationValue(JsonValue.NewString("SomeValue")) : Option <ConfigurationValue> .None));

            var value = EngineCore.GetRulesEvaluator(new IdentityHashSet {
                identity
            }, context, rulesRepo)("path/to/key").Map(x => x.Value);

            Assert.Equal(JsonValue.NewString("SomeValue"), value);

            rulesRepo = rulesRepo
                        .With("path/to/other/key", FakeRule.Create(ctx => ctx("device.OtherProp") == JsonValue.NewString("DEF") ? new ConfigurationValue(JsonValue.NewString("SomeValue")) : Option <ConfigurationValue> .None));

            value = EngineCore.GetRulesEvaluator(new IdentityHashSet {
                identity
            }, context, rulesRepo)("path/to/other/key").Map(x => x.Value);
            Assert.True(value.IsNone);

            rulesRepo = rulesRepo
                        .With("path/to/other/key", FakeRule.Create(ctx => ctx("device.PartnerBrand") == JsonValue.NewString("ABC") ? new ConfigurationValue(JsonValue.NewString("SomeValue")) : Option <ConfigurationValue> .None));

            value = EngineCore.GetRulesEvaluator(new IdentityHashSet {
                identity
            }, context, rulesRepo)("path/to/other/key").Map(x => x.Value);

            Assert.Equal(JsonValue.NewString("SomeValue"), value);
        }
Ejemplo n.º 3
0
        public static void Level_PropertyGet_MatchesCtorArg()
        {
            const string    id    = "TEST_ID";
            const string    title = "test";
            const RuleLevel level = RuleLevel.Error;
            var             rule  = new FakeRule(id, title, level);

            Assert.That(rule.Level, Is.EqualTo(level));
        }
Ejemplo n.º 4
0
        public void PathWithNoMatchingRule()
        {
            var identity  = new Identity("device", "1");
            var context   = CreateContext(identity);
            var rulesRepo = RulesRepositoryHelpers.With("path/to/key", FakeRule.Create(ctx => new ConfigurationValue(JsonValue.NewString("SomeValue"))));

            var missingValue = EngineCore.GetRulesEvaluator(new IdentityHashSet {
                identity
            }, context, rulesRepo)("path/to/key2");

            Assert.True(missingValue.IsNone);
        }
        public void adds_rule_values_to_the_list_dependency()
        {
            var rule = new FakeRule();
            theConfigurationHelper.AddRule(rule);

            theObjectDef
                .Dependencies
                .OfType<ListDependency>()
                .Single()
                .Items
                .First()
                .Value.ShouldBeTheSameAs(rule);
        }
Ejemplo n.º 6
0
        public void ExternalPathRefernceInContext()
        {
            var identity  = new Identity("device", "1");
            var context   = CreateContext(identity);
            var rulesRepo = RulesRepositoryHelpers.With("path/to/key2", FakeRule.Create(ctx => new ConfigurationValue(JsonValue.NewString("SomeValue"))))
                            .With("path/to/key", FakeRule.Create(ctx => ctx("@@key:path/to/key2").Map(x => new ConfigurationValue(x))));

            var value = EngineCore.GetRulesEvaluator(new IdentityHashSet {
                identity
            }, context, rulesRepo)("path/to/key").Map(x => x.Value);

            Assert.Equal(JsonValue.NewString("SomeValue"), value);
        }
Ejemplo n.º 7
0
        public void PathWithSimpleRule()
        {
            var identity   = new Identity("device", "1");
            var identities = new IdentityHashSet {
                identity
            };
            var context   = CreateContext(identity);
            var rulesRepo = RulesRepositoryHelpers.With("path/to/key", FakeRule.Create(ctx => new ConfigurationValue(JsonValue.NewString("SomeValue"))));

            var value = EngineCore.GetRulesEvaluator(identities, context, rulesRepo)("path/to/key").Map(x => x.Value);

            Assert.Equal(JsonValue.NewString("SomeValue"), value);
        }
Ejemplo n.º 8
0
        public void adds_rule_values_to_the_list_dependency()
        {
            var rule = new FakeRule();

            theConfigurationHelper.AddRule(rule);

            theObjectDef
            .Dependencies
            .OfType <ListDependency>()
            .Single()
            .Items
            .First()
            .Value.ShouldBeTheSameAs(rule);
        }
Ejemplo n.º 9
0
        public async Task ProcessAsyncCanChangeStatusAndContinueProcessing(string scenario, bool expectedContinueProcessing,
                                                                           RuleStatusType expectedRuleStatusType, FakeRule rule, List <IBaseRule <FakeRequest, FakeResponse> > rules)
        {
            var request = new FakeRequest {
                ContinueProcessing = expectedContinueProcessing
            };
            var response = new FakeResponse();

            await rule.ProcessAsync(request, response, rules);

            Assert.AreEqual(expectedContinueProcessing, rule.ContinueProcessing, scenario);
            Assert.AreEqual(expectedRuleStatusType, rule.Status, scenario);
        }