コード例 #1
0
        public void ApplyRuleToSubFieldOrPropertyToAndFromJson()
        {
            var rule = new ValidationRule <Game>
            {
                OperatorToUse          = "GreaterThan",
                ValueToValidateAgainst = new ConstantRule <int> {
                    Value = "3"
                },
                ObjectToValidate = "Name.Length",
                RuleError        = new RuleError {
                    Code = "c1", Message = "Name length must be greater than 3"
                }
            };

            var compileResult = rule.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(rule)}:{Environment.NewLine}" +
                                        $"{rule.ExpressionDebugView()}");

            var validationResult = rule.IsValid(_game);

            validationResult.Should().BeTrue();

            var someGameWithShortName = new Game {
                Name = "foo"
            };

            validationResult = rule.IsValid(someGameWithShortName);
            validationResult.Should().BeFalse();
            _testOutputHelper.WriteLine($"with {nameof(someGameWithShortName.Name)}={someGameWithShortName.Name} " +
                                        $"{nameof(rule)} failed. " +
                                        $"Error code={rule.RuleError.Code}, " +
                                        $"message={rule.RuleError.Message}");

            // convert to json
            var ruleJson = JsonConvert.SerializeObject(rule, new JsonConverterForRule());

            _testOutputHelper.WriteLine($"{nameof(ruleJson)}:{Environment.NewLine}{ruleJson}");
            // re-hydrate from json
            var ruleFromJson = JsonConvert.DeserializeObject <ValidationRule <Game> >(ruleJson, new JsonConverterForRule());

            compileResult = ruleFromJson.Compile();
            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(ruleFromJson)}:{Environment.NewLine}" +
                                        $"{ruleFromJson.ExpressionDebugView()}");

            var validationResult2 = ruleFromJson.IsValid(_game);

            validationResult2.Should().BeTrue();
            validationResult2 = ruleFromJson.IsValid(new Game {
                Name = "foo"
            });
            validationResult2.Should().BeFalse();
        }
コード例 #2
0
        public void ApplyRuleToFieldOrPropertyToAndFromJson()
        {
            var rule = new ValidationRule <Game>
            {
                OperatorToUse          = "LessThan",
                ValueToValidateAgainst = new ConstantRule <int> {
                    Value = "100"
                },
                ObjectToValidate = "Ranking",
                RuleError        = new RuleError {
                    Code = "c1", Message = "Ranking must be less than 100"
                }
            };

            var compileResult = rule.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(rule)}:{Environment.NewLine}" +
                                        $"{rule.ExpressionDebugView()}");

            var validationResult = rule.IsValid(_game);

            validationResult.Should().BeTrue();

            var someOtherGameWithHighRanking = new Game {
                Ranking = 101
            };

            validationResult = rule.IsValid(someOtherGameWithHighRanking);
            validationResult.Should().BeFalse();
            _testOutputHelper.WriteLine($"with {nameof(someOtherGameWithHighRanking.Ranking)}={someOtherGameWithHighRanking.Ranking} " +
                                        $"{nameof(rule)} failed. " +
                                        $"Error code={rule.RuleError.Code}, " +
                                        $"message={rule.RuleError.Message}");

            // convert to json
            var ruleJson = JsonConvert.SerializeObject(rule, new JsonConverterForRule());

            _testOutputHelper.WriteLine($"{nameof(ruleJson)}:{Environment.NewLine}{ruleJson}");
            // re-hydrate from json
            var ruleFromJson = JsonConvert.DeserializeObject <ValidationRule <Game> >(ruleJson, new JsonConverterForRule());

            compileResult = ruleFromJson.Compile();
            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(ruleFromJson)}:{Environment.NewLine}" +
                                        $"{ruleFromJson.ExpressionDebugView()}");

            var validationResult2 = ruleFromJson.IsValid(_game);

            validationResult2.Should().BeTrue();
            validationResult2 = ruleFromJson.IsValid(someOtherGameWithHighRanking);
            validationResult2.Should().BeFalse();
        }
コード例 #3
0
        public void ValidationRuleWithOneNotChildToAndFromJson()
        {
            var rule = new ValidationRule <Game>
            {
                OperatorToUse = "Not",
                RuleError     = new RuleError {
                    Code = "c", Message = "m"
                }
            };

            rule.ChildrenRules.Add(
                new ValidationRule <Game>
            {
                OperatorToUse          = "NotEqual",
                ValueToValidateAgainst = new ConstantRule <Game> {
                    Value = "null"
                }
            }
                );

            var compileResult = rule.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(rule)}:{Environment.NewLine}" +
                                        $"{rule.ExpressionDebugView()}");

            var validationResult = rule.IsValid(_game);

            validationResult.Should().BeFalse();

            validationResult = rule.IsValid(null);
            validationResult.Should().BeTrue();

            // convert to json
            var ruleJson = JsonConvert.SerializeObject(rule, new JsonConverterForRule());

            _testOutputHelper.WriteLine($"{nameof(ruleJson)}:{Environment.NewLine}{ruleJson}");
            // re-hydrate from json
            var ruleFromJson = JsonConvert.DeserializeObject <ValidationRule <Game> >(ruleJson, new JsonConverterForRule());

            compileResult = ruleFromJson.Compile();
            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(ruleFromJson)}:{Environment.NewLine}" +
                                        $"{ruleFromJson.ExpressionDebugView()}");

            var validationResult2 = ruleFromJson.IsValid(_game);

            validationResult2.Should().BeFalse();

            validationResult2 = ruleFromJson.IsValid(null);
            validationResult2.Should().BeTrue();
        }
コード例 #4
0
        public void ValidationRuleWithTwoTypes()
        {
            var twoPlayersScoreGreaterThanRule = new ValidationRule <Player, Player>
            {
                OperatorToUse     = "GreaterThan",
                ObjectToValidate1 = "CurrentScore",
                ObjectToValidate2 = "CurrentScore"
            };

            var compileResult = twoPlayersScoreGreaterThanRule.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(twoPlayersScoreGreaterThanRule)}:{Environment.NewLine}" +
                                        $"{twoPlayersScoreGreaterThanRule.ExpressionDebugView()}");

            var validationResult = twoPlayersScoreGreaterThanRule.IsValid(_game.Players[0], _game.Players[1]);

            validationResult.Should().BeTrue();
            validationResult = twoPlayersScoreGreaterThanRule.IsValid(_game.Players[1], _game.Players[0]);
            validationResult.Should().BeFalse();
        }
コード例 #5
0
        public void ValidationRuleWithTwoTypesToAndFromJson()
        {
            var rule = new ValidationRule <Player, Player>
            {
                OperatorToUse     = "GreaterThan",
                ObjectToValidate1 = "CurrentScore",
                ObjectToValidate2 = "CurrentScore"
            };

            var compileResult = rule.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(rule)}:{Environment.NewLine}{rule.ExpressionDebugView()}");

            var validationResult = rule.IsValid(_game.Players[0], _game.Players[1]);

            validationResult.Should().BeTrue();
            validationResult = rule.IsValid(_game.Players[1], _game.Players[0]);
            validationResult.Should().BeFalse();

            // convert to json
            var ruleJson = JsonConvert.SerializeObject(rule, new JsonConverterForRule());

            _testOutputHelper.WriteLine($"{nameof(ruleJson)}:{Environment.NewLine}{ruleJson}");
            // re-hydrate from json
            var ruleFromJson = JsonConvert.DeserializeObject <ValidationRule <Player, Player> >(ruleJson, new JsonConverterForRule());

            compileResult = ruleFromJson.Compile();
            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(ruleFromJson)}:{Environment.NewLine}" +
                                        $"{ruleFromJson.ExpressionDebugView()}");

            var validationResult2 = ruleFromJson.IsValid(_game.Players[0], _game.Players[1]);

            validationResult2.Should().BeTrue();
            validationResult2 = ruleFromJson.IsValid(_game.Players[1], _game.Players[0]);
            validationResult2.Should().BeFalse();
        }
コード例 #6
0
        public void ApplyRuleToFieldOrProperty()
        {
            var rankingLessThan100Rule = new ValidationRule <Game>
            {
                OperatorToUse          = "LessThan",
                ValueToValidateAgainst = new ConstantRule <int> {
                    Value = "100"
                },
                ObjectToValidate = "Ranking",
                RuleError        = new RuleError {
                    Code = "c1", Message = "Ranking must be less than 100"
                }
            };

            var compileResult = rankingLessThan100Rule.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(rankingLessThan100Rule)}:{Environment.NewLine}" +
                                        $"{rankingLessThan100Rule.ExpressionDebugView()}");


            var validationResult = rankingLessThan100Rule.IsValid(_game);

            validationResult.Should().BeTrue();

            var someOtherGameWithHighRanking = new Game {
                Ranking = 101
            };

            validationResult = rankingLessThan100Rule.IsValid(someOtherGameWithHighRanking);
            validationResult.Should().BeFalse();
            _testOutputHelper.WriteLine($"with {nameof(someOtherGameWithHighRanking.Ranking)}={someOtherGameWithHighRanking.Ranking} " +
                                        $"{nameof(rankingLessThan100Rule)} failed. " +
                                        $"Error code={rankingLessThan100Rule.RuleError.Code}, " +
                                        $"message={rankingLessThan100Rule.RuleError.Message}");
        }
コード例 #7
0
        public void RuleToCheckIfRootObjectIsNullOrNot()
        {
            var checkForNotNullRule = new ValidationRule <Game>
            {
                ValueToValidateAgainst = new ConstantRule <Game> {
                    Value = "null"
                },
                OperatorToUse = "NotEqual"
            };
            var compileResult = checkForNotNullRule.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(checkForNotNullRule)}:{Environment.NewLine}{checkForNotNullRule.ExpressionDebugView()}");


            var checkForNullRule = new ValidationRule <Game>
            {
                ValueToValidateAgainst = new ConstantRule <Game> {
                    Value = "null"
                },
                OperatorToUse = "Equal"
            };

            compileResult = checkForNullRule.Compile();
            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(checkForNullRule)}:{Environment.NewLine}{checkForNullRule.ExpressionDebugView()}");


            var ruleExecuteResult = checkForNotNullRule.IsValid(_game);

            ruleExecuteResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"with non-null parameter validationResult = {ruleExecuteResult}; expecting true");

            ruleExecuteResult = checkForNotNullRule.IsValid(null);
            ruleExecuteResult.Should().BeFalse();
            _testOutputHelper.WriteLine($"with null parameter validationResult = {ruleExecuteResult}; expecting false");

            ruleExecuteResult = checkForNullRule.IsValid(_game);
            ruleExecuteResult.Should().BeFalse();
            _testOutputHelper.WriteLine($"with non-null parameter validationResult = {ruleExecuteResult}; expecting false");

            ruleExecuteResult = checkForNullRule.IsValid(null);
            ruleExecuteResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"with null parameter validationResult = {ruleExecuteResult}; expecting true");
        }
コード例 #8
0
        public void ValidationRuleWithOrElseChildrenValidationRules()
        {
            var gameIsNullOrNameIsGreaterThan3CharsRule = new ValidationRule <Game> {
                OperatorToUse = "OrElse"
            };

            gameIsNullOrNameIsGreaterThan3CharsRule.ChildrenRules.Add(new ValidationRule <Game>
            {
                OperatorToUse          = "Equal",
                ValueToValidateAgainst = new ConstantRule <Game> {
                    Value = "null"
                }
            });
            gameIsNullOrNameIsGreaterThan3CharsRule.ChildrenRules.Add
            (
                new ValidationRule <Game>
            {
                OperatorToUse = "AndAlso",
                ChildrenRules =
                {
                    new ValidationRule <Game>
                    {
                        ValueToValidateAgainst = new ConstantRule <string>{
                            Value = "null"
                        },
                        ObjectToValidate = "Name",
                        OperatorToUse    = "NotEqual"
                    },
                    new ValidationRule <Game>
                    {
                        ValueToValidateAgainst = new ConstantRule <int>{
                            Value = "3"
                        },
                        ObjectToValidate = "Name.Length",
                        OperatorToUse    = "GreaterThan"
                    }
                }
            }
            );

            var compileResult = gameIsNullOrNameIsGreaterThan3CharsRule.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(gameIsNullOrNameIsGreaterThan3CharsRule)}:{Environment.NewLine}{gameIsNullOrNameIsGreaterThan3CharsRule.ExpressionDebugView()}");

            var validationResult = gameIsNullOrNameIsGreaterThan3CharsRule.IsValid(_game);

            validationResult.Should().BeTrue();

            validationResult = gameIsNullOrNameIsGreaterThan3CharsRule.IsValid(null);
            validationResult.Should().BeTrue();

            validationResult = gameIsNullOrNameIsGreaterThan3CharsRule.IsValid(new Game {
                Name = null
            });
            validationResult.Should().BeFalse();

            validationResult = gameIsNullOrNameIsGreaterThan3CharsRule.IsValid(new Game {
                Name = "a"
            });
            validationResult.Should().BeFalse();
        }
コード例 #9
0
        public void ValidationRuleWithOneNotChild()
        {
            var gameNullRuleByUsingNotWithNotEqualToNullChild = new ValidationRule <Game>
            {
                OperatorToUse = "Not",
                RuleError     = new RuleError {
                    Code = "c", Message = "m"
                }
            };

            gameNullRuleByUsingNotWithNotEqualToNullChild.ChildrenRules.Add(
                new ValidationRule <Game>
            {
                OperatorToUse          = "NotEqual",
                ValueToValidateAgainst = new ConstantRule <Game> {
                    Value = "null"
                }
            }
                );

            var compileResult = gameNullRuleByUsingNotWithNotEqualToNullChild.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(gameNullRuleByUsingNotWithNotEqualToNullChild)}:{Environment.NewLine}{gameNullRuleByUsingNotWithNotEqualToNullChild.ExpressionDebugView()}");


            var validationResult = gameNullRuleByUsingNotWithNotEqualToNullChild.IsValid(_game);

            validationResult.Should().BeFalse();

            validationResult = gameNullRuleByUsingNotWithNotEqualToNullChild.IsValid(null);
            validationResult.Should().BeTrue();
        }
コード例 #10
0
        public void RuleToCheckIfAnIntegerMatchesRuleValueOrNot()
        {
            var numberShouldBe5Rule = new ValidationRule <int>
            {
                ValueToValidateAgainst = new ConstantRule <int> {
                    Value = "5"
                },
                OperatorToUse = "Equal",
                RuleError     = new RuleError {
                    Code = "c1", Message = "number is not 5"
                }
            };
            var compileResult = numberShouldBe5Rule.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(numberShouldBe5Rule)}:{Environment.NewLine}{numberShouldBe5Rule.ExpressionDebugView()}");

            var numberShouldNotBe5Rule = new ValidationRule <int>
            {
                ValueToValidateAgainst = new ConstantRule <int> {
                    Value = "5"
                },
                OperatorToUse = "NotEqual",
                RuleError     = new RuleError {
                    Code = "c2", Message = "number is 5"
                }
            };

            compileResult = numberShouldNotBe5Rule.Compile();
            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(numberShouldNotBe5Rule)}:{Environment.NewLine}{numberShouldNotBe5Rule.ExpressionDebugView()}");

            var ruleExecuteResult = numberShouldBe5Rule.IsValid(5);

            ruleExecuteResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"with 5 {nameof(numberShouldBe5Rule)} execute result: {ruleExecuteResult}");

            ruleExecuteResult = numberShouldBe5Rule.IsValid(6);
            ruleExecuteResult.Should().BeFalse();
            _testOutputHelper.WriteLine($"with 6 {nameof(numberShouldBe5Rule)} failed. " +
                                        $"Error code={numberShouldBe5Rule.RuleError.Code}, " +
                                        $"message={numberShouldBe5Rule.RuleError.Message}");

            ruleExecuteResult = numberShouldNotBe5Rule.IsValid(6);
            ruleExecuteResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"with 6 {nameof(numberShouldNotBe5Rule)} execute result: {ruleExecuteResult}");

            ruleExecuteResult = numberShouldNotBe5Rule.IsValid(5);
            ruleExecuteResult.Should().BeFalse();
            _testOutputHelper.WriteLine($"with 5 {nameof(numberShouldNotBe5Rule)} failed. " +
                                        $"Error code={numberShouldNotBe5Rule.RuleError.Code}, " +
                                        $"message={numberShouldNotBe5Rule.RuleError.Message}");
        }
コード例 #11
0
        public void ValidationRuleWithAndAlsoChildrenValidationRules()
        {
            var gameNotNullAndNameIsGreaterThan3CharsRule = new ValidationRule <Game>
            {
                OperatorToUse = "AndAlso",
                RuleError     = new RuleError {
                    Code = "c", Message = "m"
                },
                ChildrenRules =
                {
                    new ValidationRule <Game>
                    {
                        OperatorToUse          = "NotEqual",
                        ValueToValidateAgainst = new ConstantRule <Game>{
                            Value = "null"
                        }
                    },
                    new ValidationRule <Game>
                    {
                        ValueToValidateAgainst = new ConstantRule <string>{
                            Value = "null"
                        },
                        ObjectToValidate = "Name",
                        OperatorToUse    = "NotEqual"
                    },
                    new ValidationRule <Game>
                    {
                        ValueToValidateAgainst = new ConstantRule <int>{
                            Value = "3"
                        },
                        ObjectToValidate = "Name.Length",
                        OperatorToUse    = "GreaterThan"
                    }
                }
            };

            var compileResult = gameNotNullAndNameIsGreaterThan3CharsRule.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(gameNotNullAndNameIsGreaterThan3CharsRule)}:{Environment.NewLine}{gameNotNullAndNameIsGreaterThan3CharsRule.ExpressionDebugView()}");


            var validationResult = gameNotNullAndNameIsGreaterThan3CharsRule.IsValid(_game);

            validationResult.Should().BeTrue();

            validationResult = gameNotNullAndNameIsGreaterThan3CharsRule.IsValid(null);
            validationResult.Should().BeFalse();
            _testOutputHelper.WriteLine($"{nameof(gameNotNullAndNameIsGreaterThan3CharsRule)} failed. " +
                                        $"Error code={gameNotNullAndNameIsGreaterThan3CharsRule.RuleError.Code}, " +
                                        $"message={gameNotNullAndNameIsGreaterThan3CharsRule.RuleError.Message}");
        }
コード例 #12
0
        public void ApplyRuleToSubFieldOrProperty()
        {
            var nameLengthGreaterThan3Rule = new ValidationRule <Game>
            {
                OperatorToUse          = "GreaterThan",
                ValueToValidateAgainst = new ConstantRule <int> {
                    Value = "3"
                },
                ObjectToValidate = "Name.Length",
                RuleError        = new RuleError {
                    Code = "c1", Message = "Name length must be greater than 3"
                }
            };

            var compileResult = nameLengthGreaterThan3Rule.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(nameLengthGreaterThan3Rule)}:{Environment.NewLine}{nameLengthGreaterThan3Rule.ExpressionDebugView()}");


            var validationResult = nameLengthGreaterThan3Rule.IsValid(_game);

            validationResult.Should().BeTrue();

            var someGameWithShortName = new Game {
                Name = "foo"
            };

            validationResult = nameLengthGreaterThan3Rule.IsValid(someGameWithShortName);
            validationResult.Should().BeFalse();
            _testOutputHelper.WriteLine($"with {nameof(someGameWithShortName.Name)}={someGameWithShortName.Name} " +
                                        $"{nameof(nameLengthGreaterThan3Rule)} failed. " +
                                        $"Error code={nameLengthGreaterThan3Rule.RuleError.Code}, " +
                                        $"message={nameLengthGreaterThan3Rule.RuleError.Message}");
        }
コード例 #13
0
        public void RuleToCheckIfRootObjectIsNullOrNotToAndFromJson()
        {
            var rule = new ValidationRule <Game>
            {
                ValueToValidateAgainst = new ConstantRule <Game> {
                    Value = "null"
                },
                OperatorToUse = "NotEqual"
            };
            var compileResult = rule.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(rule)}:{Environment.NewLine}{rule.ExpressionDebugView()}");


            var nullRule = new ValidationRule <Game>
            {
                ValueToValidateAgainst = new ConstantRule <Game> {
                    Value = "null"
                },
                OperatorToUse = "Equal"
            };

            compileResult = nullRule.Compile();
            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(nullRule)}:{Environment.NewLine}{nullRule.ExpressionDebugView()}");


            var ruleExecuteResult = rule.IsValid(_game);

            ruleExecuteResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"with non-null parameter validationResult = {ruleExecuteResult}; expecting true");

            ruleExecuteResult = rule.IsValid(null);
            ruleExecuteResult.Should().BeFalse();
            _testOutputHelper.WriteLine($"with null parameter validationResult = {ruleExecuteResult}; expecting false");

            ruleExecuteResult = nullRule.IsValid(_game);
            ruleExecuteResult.Should().BeFalse();
            _testOutputHelper.WriteLine($"with non-null parameter validationResult = {ruleExecuteResult}; expecting false");

            ruleExecuteResult = nullRule.IsValid(null);
            ruleExecuteResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"with null parameter validationResult = {ruleExecuteResult}; expecting true");

            // convert to json
            var ruleJson = JsonConvert.SerializeObject(rule, new JsonConverterForRule());

            _testOutputHelper.WriteLine($"{nameof(ruleJson)}:{Environment.NewLine}{ruleJson}");
            // re-hydrate from json
            var ruleFromJson = JsonConvert.DeserializeObject <ValidationRule <Game> >(ruleJson, new JsonConverterForRule());

            compileResult = ruleFromJson.Compile();
            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(ruleFromJson)}:{Environment.NewLine}" +
                                        $"{ruleFromJson.ExpressionDebugView()}");

            var validationResult2 = ruleFromJson.IsValid(_game);

            validationResult2.Should().BeTrue();
            validationResult2 = ruleFromJson.IsValid(null);
            validationResult2.Should().BeFalse();
        }
コード例 #14
0
        public void ValidationRuleWithOrElseChildrenValidationRulesToAndFromJson()
        {
            var rule = new ValidationRule <Game> {
                OperatorToUse = "OrElse"
            };

            rule.ChildrenRules.Add(new ValidationRule <Game>
            {
                OperatorToUse          = "Equal",
                ValueToValidateAgainst = new ConstantRule <Game> {
                    Value = "null"
                }
            });
            rule.ChildrenRules.Add
            (
                new ValidationRule <Game>
            {
                OperatorToUse = "AndAlso",
                ChildrenRules =
                {
                    new ValidationRule <Game>
                    {
                        ValueToValidateAgainst = new ConstantRule <string>{
                            Value = "null"
                        },
                        ObjectToValidate = "Name",
                        OperatorToUse    = "NotEqual"
                    },
                    new ValidationRule <Game>
                    {
                        ValueToValidateAgainst = new ConstantRule <int>{
                            Value = "3"
                        },
                        ObjectToValidate = "Name.Length",
                        OperatorToUse    = "GreaterThan"
                    }
                }
            }
            );

            var compileResult = rule.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(rule)}:{Environment.NewLine}{rule.ExpressionDebugView()}");

            var validationResult = rule.IsValid(_game);

            validationResult.Should().BeTrue();

            validationResult = rule.IsValid(null);
            validationResult.Should().BeTrue();

            validationResult = rule.IsValid(new Game {
                Name = null
            });
            validationResult.Should().BeFalse();

            validationResult = rule.IsValid(new Game {
                Name = "a"
            });
            validationResult.Should().BeFalse();

            // convert to json
            var ruleJson = JsonConvert.SerializeObject(rule, new JsonConverterForRule());

            _testOutputHelper.WriteLine($"{nameof(ruleJson)}:{Environment.NewLine}{ruleJson}");
            // re-hydrate from json
            var ruleFromJson = JsonConvert.DeserializeObject <ValidationRule <Game> >(ruleJson, new JsonConverterForRule());

            compileResult = ruleFromJson.Compile();
            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(ruleFromJson)}:{Environment.NewLine}" +
                                        $"{ruleFromJson.ExpressionDebugView()}");

            var validationResult2 = ruleFromJson.IsValid(_game);

            validationResult2.Should().BeTrue();

            validationResult2 = ruleFromJson.IsValid(null);
            validationResult2.Should().BeTrue();

            validationResult2 = ruleFromJson.IsValid(new Game {
                Name = null
            });
            validationResult2.Should().BeFalse();

            validationResult2 = ruleFromJson.IsValid(new Game {
                Name = "a"
            });
            validationResult2.Should().BeFalse();
        }
コード例 #15
0
        public void RuleToCheckIfAnIntegerMatchesRuleValueOrNotToAndFromJson()
        {
            var rule = new ValidationRule <int>
            {
                ValueToValidateAgainst = new ConstantRule <int> {
                    Value = "5"
                },
                OperatorToUse = "Equal",
                RuleError     = new RuleError {
                    Code = "c1", Message = "number is not 5"
                }
            };
            var compileResult = rule.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(rule)}:{Environment.NewLine}" +
                                        $"{rule.ExpressionDebugView()}");

            var not5Rule = new ValidationRule <int>
            {
                ValueToValidateAgainst = new ConstantRule <int> {
                    Value = "5"
                },
                OperatorToUse = "NotEqual",
                RuleError     = new RuleError {
                    Code = "c2", Message = "number is 5"
                }
            };

            compileResult = not5Rule.Compile();
            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(not5Rule)}:{Environment.NewLine}" +
                                        $"{not5Rule.ExpressionDebugView()}");

            var ruleExecuteResult = rule.IsValid(5);

            ruleExecuteResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"with 5 {nameof(rule)} execute result: {ruleExecuteResult}");

            ruleExecuteResult = rule.IsValid(6);
            ruleExecuteResult.Should().BeFalse();
            _testOutputHelper.WriteLine($"with 6 {nameof(rule)} failed. " +
                                        $"Error code={rule.RuleError.Code}, " +
                                        $"message={rule.RuleError.Message}");

            ruleExecuteResult = not5Rule.IsValid(6);
            ruleExecuteResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"with 6 {nameof(not5Rule)} execute result: {ruleExecuteResult}");

            ruleExecuteResult = not5Rule.IsValid(5);
            ruleExecuteResult.Should().BeFalse();
            _testOutputHelper.WriteLine($"with 5 {nameof(not5Rule)} failed. " +
                                        $"Error code={not5Rule.RuleError.Code}, " +
                                        $"message={not5Rule.RuleError.Message}");

            // convert to json
            var ruleJson = JsonConvert.SerializeObject(rule, new JsonConverterForRule());

            _testOutputHelper.WriteLine($"{nameof(ruleJson)}:{Environment.NewLine}{ruleJson}");
            // re-hydrate from json
            var ruleFromJson = JsonConvert.DeserializeObject <ValidationRule <int> >(ruleJson, new JsonConverterForRule());

            compileResult = ruleFromJson.Compile();
            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(ruleFromJson)}:{Environment.NewLine}" +
                                        $"{ruleFromJson.ExpressionDebugView()}");

            var validationResult2 = ruleFromJson.IsValid(5);

            validationResult2.Should().BeTrue();
            validationResult2 = ruleFromJson.IsValid(6);
            validationResult2.Should().BeFalse();
        }