title: Complex validation extension
body: <p>While writing extensions for Fluent Validation rule builder I came up with the idea to make more complex validation and then wire it with client validation. I've successfully created extensions that validate one property based on another and so on. What I am struggling with is the validation on multiple fields:</p>
  
  <p>The extension method as on <a href="https://fluentvalidation.codeplex.com/wikipage?title=Custom&amp;referringTitle=Documentation" rel="nofollow">https://fluentvalidation.codeplex.com/wikipage?title=Custom&amp;referringTitle=Documentation</a></p>
  
  <pre><code>public static IRuleBuilderOptions&lt;T, TProperty&gt; Required&lt;T, TProperty&gt;(this IRuleBuilder&lt;T, TProperty&gt; ruleBuilder, Action&lt;MyRuleBuilder&lt;T&gt;&gt; configurator)
      {
          MyRuleBuilder&lt;T&gt; builder = new MyRuleBuilder&lt;T&gt;();
  
          configurator(builder);
  
          return ruleBuilder.SetValidator(new MyValidator&lt;T&gt;(builder));
      }
  </code></pre>
  
  <p>The MyRuleBuilder class which allows adding rules fluently:</p>
  
  <pre><code>public class MyRuleBuilder&lt;T&gt;
  {
      public Dictionary&lt;string, object&gt; Rules = new Dictionary&lt;string,object&gt;();
  
      public MyRuleBuilder&lt;T&gt; If(Expression&lt;Func&lt;T, object&gt;&gt; exp, object value)
      {
          Rules.Add(exp.GetMember().Name, value);
  
          return this;
      }
  }
  </code></pre>
  
  <p>Then the view model and view model validator rules look like this:</p>
  
  <pre><code>public class TestViewModel
  {
      public bool DeviceReadAccess { get; set; }
      public string DeviceReadWriteAccess { get; set; }
      public int DeviceEncrypted { get ; set; }
  }
  
  RuleFor(x =&gt; x.HasAgreedToTerms)
      .Required(builder =&gt; builder
          .If(x =&gt; x.DeviceReadAccess, true)
          .If(x =&gt; x.DeviceReadWriteAccess, "yes")
          .If(x =&gt; x.DeviceEncrypted, 1 ));
  </code></pre>
  
  <p><strong>The problem:</strong></p>
  
  <p>The above works fine but what I don't like is the "If" function. it does not enforce the value to be of the selected property type. Example:</p>
  
  <pre><code>RuleFor(x =&gt; x.HasAgreedToTerms)
      .Required(builder =&gt; builder
          .If(x =&gt; x.DeviceReadAccess, true) // I would like the value to be enforced to bool
          .If(x =&gt; x.DeviceReadWriteAccess, "yes") // I would like the value to be enforced to string
  
  // Ideally something like 
  
  // public MyRuleBuilder&lt;T&gt; If(Expression&lt;Func&lt;T, U&gt;&gt; exp, U value) but unfortunately U cannot be automatically inferred
  </code></pre>
  
  <p>Is it possible with this architecture or should i take a different approach?</p>
  
  <p>Thanks.</p>