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(); }
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}"); }
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(); }
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}"); }
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(); }
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}"); }
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(); }
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"); }
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(); }
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(); }
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}"); }
private static void LoadAndCompileRules() { var orderRule = new ValidationRule <Order> { OperatorToUse = "NotEqual", ValueToValidateAgainst = new ConstantRule <Order> { Value = null }, RuleError = new RuleError { Code = "c1", Message = "order can't be null" } }; if (orderRule.Compile()) { OrderRules.Add(orderRule); } var orderCustomerAndProductCannotBeNullRule = new ValidationRule <Order> { OperatorToUse = "AndAlso", RuleError = new RuleError { Code = "c2", Message = "Customer and/or Product can't be null" }, ChildrenRules = { new ValidationRule <Order> { OperatorToUse = "NotEqual", ObjectToValidate = "Customer", ValueToValidateAgainst = new ConstantRule <Customer>{ Value = "null" } }, new ValidationRule <Order> { OperatorToUse = "NotEqual", ObjectToValidate = "Product", ValueToValidateAgainst = new ConstantRule <Product>{ Value = "null" } } } }; if (orderCustomerAndProductCannotBeNullRule.Compile()) { OrderRules.Add(orderCustomerAndProductCannotBeNullRule); } var orderCustomerFirstNameRule = new ValidationRule <Order> { OperatorToUse = "AndAlso", RuleError = new RuleError { Code = "c3", Message = "first name can't be null/empty and has to be 3+ chars long" }, ChildrenRules = { new ValidationRule <Order> { OperatorToUse = "NotEqual", ObjectToValidate = "Customer.FirstName", ValueToValidateAgainst = new ConstantRule <string>{ Value = "null" } }, new ValidationRule <Order> { OperatorToUse = "GreaterThan", ObjectToValidate = "Customer.FirstName.Length", ValueToValidateAgainst = new ConstantRule <int>{ Value = "2" } } } }; if (orderCustomerFirstNameRule.Compile()) { OrderRules.Add(orderCustomerFirstNameRule); } var orderCustomerLastNameRule = new ValidationRule <Order> { OperatorToUse = "AndAlso", RuleError = new RuleError { Code = "c4", Message = "last name can't be null/empty and has to be 4+ chars long" }, ChildrenRules = { new ValidationRule <Order> { OperatorToUse = "NotEqual", ObjectToValidate = "Customer.LastName", ValueToValidateAgainst = new ConstantRule <string>{ Value = "null" } }, new ValidationRule <Order> { OperatorToUse = "GreaterThan", ObjectToValidate = "Customer.LastName.Length", ValueToValidateAgainst = new ConstantRule <int>{ Value = "3" } } } }; if (orderCustomerLastNameRule.Compile()) { OrderRules.Add(orderCustomerLastNameRule); } var orderProductIdPositiveOrNameGreaterThan5 = new ValidationRule <Order> { OperatorToUse = "OrElse", RuleError = new RuleError { Code = "c5", Message = "product id must be greater than zero or name has to be 5+ chars" }, ChildrenRules = { new ValidationRule <Order> { OperatorToUse = "GreaterThan", ObjectToValidate = "Product.Id", ValueToValidateAgainst = new ConstantRule <int>{ Value = "0" } }, new ValidationRule <Order> { OperatorToUse = "AndAlso", ChildrenRules = { new ValidationRule <Order> { OperatorToUse = "NotEqual", ObjectToValidate = "Product.Name", ValueToValidateAgainst = new ConstantRule <string>{ Value = "null" } }, new ValidationRule <Order> { OperatorToUse = "GreaterThan", ObjectToValidate = "Product.Name.Length", ValueToValidateAgainst = new ConstantRule <int>{ Value = "4" } } } } } }; if (orderProductIdPositiveOrNameGreaterThan5.Compile()) { OrderRules.Add(orderProductIdPositiveOrNameGreaterThan5); } }
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(); }
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(); }
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(); }
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(); }