title: Fluent validation Vary object validator according to the class it's used in
body: <p>I have a class which gets reused in multiple places. It has a default validator applied to it, which has the following attribute,  [Validator(typeof(ChildValidator))]. </p>
  
  <p>I wanted to be to override the validator that's been applied to Child class depending on the parent class that's using it. Below is a simplified version of my model. So instead of validate Child with ChildValidator, I want to apply rules in ChildValidator2.  Can this be done?</p>
  
  <p>Note: the follow code will always fail the validation because it will always apply rule in ChildValidator </p>
  
  <p>Models:</p>
  
  <pre><code>    [Validator(typeof(ParentValidator))]
      public class Parent
      {
          public bool IgnoreChild { get; set; }
          public DateTime Birthdate { get; set; }
  
          public Child Children { get; set; }
      }
  
      [Validator(typeof(ChildValidator))]
      public class Child
      {
          public string ChildProperty{ get; set; }
          public DateTime Birthdate { get; set; }
      }
  </code></pre>
  
  <p>Validators: </p>
  
  <pre><code>public class ParentValidator : AbstractValidator&lt;Parent&gt;
  {
      public ParentValidator()
      {
           RuleFor(model =&gt; model.Name).NotEmpty();
  
           When(x=&gt;x.IgnoreChild, () =&gt;
           {
               RuleForEach(model =&gt; model.Child).SetValidator(new ChildValidator2());
           });
      }
  }
  
  public class ChildValidator : AbstractValidator&lt;Child&gt;
  {
      public ChildValidator()
      {
          RuleFor(model =&gt; model.ChildProperty).NotEmpty();
          //Compare birthday to make sure date is &lt; Parents birthday
      }
  }
  
  
  
  public class ChildValidator2 : AbstractValidator&lt;Child&gt;
  {
      public ChildValidator2()
      {
          RuleFor(model =&gt; model.ChildProperty).Empty();
      }
  }
  </code></pre>