{"action":"created","issue":{"url":"https://api.github.com/repos/JeremySkinner/FluentValidation/issues/177","labels_url":"https://api.github.com/repos/JeremySkinner/FluentValidation/issues/177/labels{/name}","comments_url":"https://api.github.com/repos/JeremySkinner/FluentValidation/issues/177/comments","events_url":"https://api.github.com/repos/JeremySkinner/FluentValidation/issues/177/events","html_url":"https://github.com/JeremySkinner/FluentValidation/issues/177","id":122414890,"number":177,"title":"SetValidator inside the RuleSet","user":{"login":"infinitimods","id":15639698,"avatar_url":"https://avatars.githubusercontent.com/u/15639698?v=3","gravatar_id":"","url":"https://api.github.com/users/infinitimods","html_url":"https://github.com/infinitimods","followers_url":"https://api.github.com/users/infinitimods/followers","following_url":"https://api.github.com/users/infinitimods/following{/other_user}","gists_url":"https://api.github.com/users/infinitimods/gists{/gist_id}","starred_url":"https://api.github.com/users/infinitimods/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/infinitimods/subscriptions","organizations_url":"https://api.github.com/users/infinitimods/orgs","repos_url":"https://api.github.com/users/infinitimods/repos","events_url":"https://api.github.com/users/infinitimods/events{/privacy}","received_events_url":"https://api.github.com/users/infinitimods/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-12-16T02:45:19Z","updated_at":"2015-12-17T05:35:14Z","closed_at":null,"body":"Hi Jeremey,\r\n\r\nI have a RuleFor defined for a BillingAddress property of the customer object. I am trying to use your SetValidator but it only works outside of the rule set. Is there a way to make it work inside the rule set?\r\n\r\n\tinternal class CustomerValidator : AbstractValidator<Customer>\r\n\t{\r\n\t\tinternal CustomerValidator ()\r\n\t\t{\r\n\t\t\t// Works here\r\n\t\t\t//RuleFor(o => o.BillingAddress).SetValidator(new BillingAddressValidator());\r\n\r\n\t\t\tRuleSet(\"Add\", () =>\r\n\t\t\t{\t\r\n\t\t\t\t// Does not work here\r\n\t\t\t\tRuleFor(o => o.BillingAddress).SetValidator(new BillingAddressValidator());\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\nThank you Jeremy"},"comment":{"url":"https://api.github.com/repos/JeremySkinner/FluentValidation/issues/comments/165342349","html_url":"https://github.com/JeremySkinner/FluentValidation/issues/177#issuecomment-165342349","issue_url":"https://api.github.com/repos/JeremySkinner/FluentValidation/issues/177","id":165342349,"user":{"login":"infinitimods","id":15639698,"avatar_url":"https://avatars.githubusercontent.com/u/15639698?v=3","gravatar_id":"","url":"https://api.github.com/users/infinitimods","html_url":"https://github.com/infinitimods","followers_url":"https://api.github.com/users/infinitimods/followers","following_url":"https://api.github.com/users/infinitimods/following{/other_user}","gists_url":"https://api.github.com/users/infinitimods/gists{/gist_id}","starred_url":"https://api.github.com/users/infinitimods/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/infinitimods/subscriptions","organizations_url":"https://api.github.com/users/infinitimods/orgs","repos_url":"https://api.github.com/users/infinitimods/repos","events_url":"https://api.github.com/users/infinitimods/events{/privacy}","received_events_url":"https://api.github.com/users/infinitimods/received_events","type":"User","site_admin":false},"created_at":"2015-12-17T05:35:14Z","updated_at":"2015-12-17T05:35:14Z","body":"Hi Jeremy,\r\n\r\nTrying to run the following test with with Address property of the Person object set to null and get \"Address is required\" error message, but I don't. As you can see, I am using SetValidator on the address property of the person object. AddressValidator is then supposed to validate if the Address is valid. Is there an easy fix for this? In one of my previous questions, you have indicated that \"(Additionally, FluentValidation requires that the root-level object being validated is not-null, so your example using RuleFor(o => o).NotNull() won't actually work, but the concept is right)\". As you can see, it actually works, if I was to run AddressValidator on the address object directly, but it does not work if I use SetValidator. Is this just a bad practice to run a null check on the object? I've put a unit test for you so you can see what I am trying to achieve. What are your thoughts? (RulesetTests.cs)\r\n\r\n\t\t[Fact]\r\n\t\tpublic void Ruleset_cascades_to_child_validator_with_null_check()\r\n\t\t{\r\n\t\t\tvar addressValidator = new InlineValidator<Address>();\r\n\t\t\taddressValidator.RuleSet(\"Test\", () =>\r\n\t\t\t{\r\n\t\t\t\taddressValidator.RuleFor(x => x.Line1).NotNull();\r\n\t\t\t});\r\n\r\n\t\t\tvar validator = new TestValidator();\r\n\r\n\t\t\tvalidator.RuleSet(\"Test\", () =>\r\n\t\t\t{\r\n\t\t\t\tvalidator.RuleFor(o => o)\r\n\t\t\t\t\t.Cascade(CascadeMode.StopOnFirstFailure)\r\n\t\t\t\t\t.NotNull().WithMessage(\"Person is required.\")\r\n\t\t\t\t\t.DependentRules(x =>\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tthis.BaseValidation(x);\r\n\t\t\t\t\t});\r\n\t\t\t});\r\n\r\n\t\t\t// Works. Error message is \"Person is required.\"\r\n\t\t\t// var person = null;\r\n\r\n\t\t\tvar person = new Person\r\n\t\t\t{\r\n\t\t\t\t//Works. Error message is \"Country name is required.\"\r\n\t\t\t\t//Address = new Address()\r\n\r\n\t\t\t\t//Does not work. Skips validation altogether. I was expecting to get \"Address is required\", but did not\r\n\t\t\t\tAddress = null\r\n\t\t\t};\r\n\r\n\t\t\tvar result = validator.Validate(new ValidationContext<Person>(person, new PropertyChain(), new RulesetValidatorSelector(\"Test\")));\r\n\r\n\t\t\tresult.Errors.Count.ShouldEqual(1);\r\n\t\t}\r\n\r\n\t\tprivate void BaseValidation(AbstractValidator<Person> validator)\r\n\t\t{\r\n\t\t\tvalidator.RuleFor(o => o.Address).SetValidator(new AddressValidator());\r\n\t\t}\r\n\r\n\t\tinternal class AddressValidator : AbstractValidator<Address>\r\n\t\t{\r\n\t\t\tinternal AddressValidator()\r\n\t\t\t{\r\n\t\t\t\tRuleSet(\"Test\", () =>\r\n\t\t\t\t{\r\n\t\t\t\t\tRuleFor(o => o)\r\n\t\t\t\t\t\t.Cascade(CascadeMode.StopOnFirstFailure)\r\n\t\t\t\t\t\t.NotNull().WithMessage(\"Address is required.\")\r\n\t\t\t\t\t\t.DependentRules(x =>\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tthis.BaseValidation(x);\r\n\t\t\t\t\t\t});\r\n\t\t\t\t});\r\n\t\t\t}\r\n\r\n\t\t\tprivate void BaseValidation(AbstractValidator<Address> validator)\r\n\t\t\t{\r\n\t\t\t\tvalidator.RuleFor(o => o.Country).NotEmpty().WithMessage(\"Country name is required.\");\r\n\t\t\t}\r\n\t\t}"}}