title: Knockout custom computed looses validation rules
body: <p>I've created a custom function as seen below. It works perfekt while storing data and updating the observable <em>MetaData</em> values etc, but it breaks when it comes to validation.</p>
  
  <p>I am using Knockout validation and have been debugging for a few hours and what I THINK i found out is the fact that the validation is run twice and the second time, all the rules of my observable has dropped, so every time the observable is valid, since there are no rules. The later code is copied from the source code here: <a href="https://github.com/Knockout-Contrib/Knockout-Validation/blob/master/Dist/knockout.validation.js" rel="nofollow">https://github.com/Knockout-Contrib/Knockout-Validation/blob/master/Dist/knockout.validation.js</a></p>
  
  <p>Why are my custom function making the observable dropping the validation rules?</p>
  
  <p><strong>My custom function</strong></p>
  
  <pre><code>ko.observable.fn.valueByKey = function (key) {
      return ko.computed({
          read: function () {
              var md = ko.utils.arrayFirst(ko.unwrap(this), function (item) {
                  return item.Key() == key;
              });
  
              if (md === null) {
                  md = new MetaData({ Key: key });
                  this.push(md);
              }
  
              return md.Value();
          },
          write: function (value) {
              var md = ko.utils.arrayFirst(ko.unwrap(this), function (item) {
                  return item.Key() == key;
              });
  
              md.Value(value);
          }
      }, this);
  };
  </code></pre>
  
  <p><strong>Code that runs twice</strong></p>
  
  <pre><code>var h_obsValidationTrigger = ko.computed(function () {
                  var obs = observable(),
                      ruleContexts = observable.rules();
                  console.log(ruleContexts);
                  exports.validateObservable(observable);
                  return true;
              });
  </code></pre>
  
  <p><strong>Another important part of knockout validation js</strong></p>
  
  <pre><code>addRule: function (observable, rule) {
                  observable.extend({ validatable: true });
  
                  //push a Rule Context to the observables local array of Rule Contexts
                  observable.rules.push(rule);
  
                  return observable;
              },
  </code></pre>
  
  <p><strong>UPDATE 1:</strong></p>
  
  <p>I've come up with a simple solution that almost seems to work.</p>
  
  <pre><code>ko.observable.fn.valueByKey = function (key) {
      var md = ko.utils.arrayFirst(ko.unwrap(this), function (item) {
          return item.Key() == key;
      });
  
      if (md === null) {
          md = new MetaData({ Key: key });
          this.push(md);
      }
      return md.Value;
  }
  </code></pre>
  
  <p>When using this, I get validation message on the element, but error-count does not rise on my view model, so the viewmodel it self is still valid, even though i get validation error.</p>