title: Move validation from observable to computed
body: <p>I have a custom KO extension that formats numbers, this extender wraps the original observable in a computed and return the new reference to the computed. </p>
  
  <p>There is a problem, lets say down the chain before the number extender is called someone adds a validation to the observable, then this validation will not work when the computed is exposed by the number extender. </p>
  
  <p>Can I somehow move the validation from the observable to the computed? </p>
  
  <p>Off course I can make sure that the validation is added after the number extender, but that does not feel very solid, especially not in a large project </p>
  
  <p>The extender looks like</p>
  
  <pre><code>ko.extenders.asNumber = function (target) {
      var result = ko.computed({
          read: function () {
              return Globalize.format(target(), "N2");
          },
          write: function (value) {
              if (value != null &amp;&amp; value.substr) {
                  var parsedValue = Globalize.parseFloat(value);
                  if (isNaN(parsedValue)) {
                      parsedValue = value;
                  }
                  value = parsedValue;
  
                  if (!String.hasValue(value))
                      value = null;
              }
  
              target(value);
              target.valueHasMutated();
          }
      });
  
      target.hasValue = function () {
          return target() != null &amp;&amp; target() != 0;
      };
      result.raw = target;
      return result;
  };
  </code></pre>
  
  <p><strong>update:</strong></p>
  
  <p>One way of solving it is</p>
  
  <pre><code>function copyValidation(observable, newObservable) {
      if (observable.__valid__) {
          newObservable.extend({ validatable: true });
          ko.utils.arrayForEach(observable.rules(), function(rule) {
              newObservable.rules.push(rule);
          });
          observable.rules([]);
      }
  }
  </code></pre>
  
  <p><strong>Final version</strong></p>
  
  <p>If no one has a better way I will go for this</p>
  
  <pre><code>function moveValidation(observable, newObservable) {
      if (observable.__valid__) {
          newObservable.extend({ validatable: true });
          ko.utils.arrayForEach(observable.rules(), function(rule) {
              newObservable.rules.push(rule);
          });
          observable.extend({ validatable: false });
      }
  }
  </code></pre>
  
  <p><strong>update</strong></p>
  
  <p>Above solution with <code>observable.extend({ validatable: false });</code> does for some reason not work once you bundle and minify your scripts. <code>observable.rules([]);</code> works</p>