title: mongoose dynamically attaching schema to validate against on the fly
body: <p>I'd like to dynamically attach a schema to a particular field, based on some business logic in <code>schema.pre('save',function(..){...})</code>. How to do this, if at all possible? </p>
  
  <p>Some (simplified) schemas and background: </p>
  
  <pre><code>var fact= new Schema({  
      name: { type: String, required: true, index: { unique: false }}
      ,value: {type:  {}, required: true}  
      ,moreinfodesc: { type: String, required: false} 
      ,createddate : { type: Date, required: true, default: Date.now, select: false } }
  }, { collection: 'Fact' } );
  
  var factSchema= new Schema({
      name: { type: String, required: true, index: { unique: true }}
      , valueType: { type: {}, required: true}                                        
      ,isMulti: {type: Boolean, required: true }
  
      //ACL-stuff
      ,directChangeRoles: {type: [String]} //i.e: [super, admin,owner]
      ,suggestChangeRoles: {type: [String]} //ie: [editor]
      ,enum: {type: [{}]} 
      ,mixins: {type: [String]} 
  }, { collection: 'FactSchema' });
  </code></pre>
  
  <p>This is a simplified structure to allow "facts' of a particular 'entity' to be edited. </p>
  
  <pre><code>e.g: entityA.facts=[fact]
  </code></pre>
  
  <p>As can be seen from the schema's <code>fact.value</code> can have any type as far as mongoose is concerned. I however, want to constrain it at runtime to the schema as defined in 
  <code>FactSchema.valueType</code> (a String containing "Boolean", "String" or something more complex as "[Tag]") . This might all seem cumbersome, but this is the way i'd like to go for several reasons. </p>
  
  <p>So let's say that for a particular fact with <code>fact.name=tags</code> I want to assign  <code>fact.value</code> the type <code>[Tag]</code> at runtime. For this I would have setup a <code>Tag</code>-schema with validation like usual and have <code>fact.value</code> validate against it.</p>
  
  <p>I was thinking of somehow "attaching" the <code>[Tag]</code>-schema to <code>fact.value</code> in <code>fact.pre('save',function(..){.. //validation here })</code> and hope validation would magically happen as if <code>fact.value</code>was assigned the type <code>[Tag]</code> at design-time instead of run-time. </p>
  
  <p>Finally the question: I've got no idea if it's possible to do that 'attach' and if so, how?</p>
  
  <p>Thanks. </p>