title: Why can't I validate an embedded document in mongoose? What is the right way to do this?
body: <p>I have an schema like this:</p>
  
  <pre><code>var testSchema = new Schema({
          foo: { type: String, required: true, trim: true },
          bar: {
              fooBar: { type: String },
              barFoo: { type: String }
          }
  });
  </code></pre>
  
  <p>And I must validate values of <code>bar</code> based on <code>foo</code> values, something like this:</p>
  
  <pre><code>testSchema.path("bar").validate(function(bar){
      if(this.foo === "someValue")
          //return custom validation logic 1
      else if(this.foo === "anotherString")
          //return custom validation logic 2  
      else
          return false;
  });
  </code></pre>
  
  <p>But when I try to strat my app I get the following error:</p>
  
  <pre><code>/Users/Renato/github/local/prv/domain/models/testModel.js:34
  testSchema.path("bar").validate(function(bar){
                         ^
  TypeError: Cannot call method 'validate' of undefined
  </code></pre>
  
  <p>What am I doing wrong here? Whats the correct way to validate this object??? I googled for it but I could not seem to find anything out! Even updated my mongoose version to <code>~3.5.5</code></p>