What is the right syntax to make required field depend on another field/function in Mongoose
<p>I'm trying to make fields required depending on value in current Mongoose object. 
How can I make customerInfo required depend on the value in status in below? </p>

<p>this reference is not working in that function. </p>

<p>Ideally would prefer to declare the function outside the schema definition too but couldn't get it to work. I also couldn't find the documentation on the feature to make required support a function, but I'm fairly sure it's supported...</p>

<pre><code>var OrderSchema = new Schema(
{

    status: {
      type: String,
      trim: true,
      enum: refdata.orderStatus,
      default: refdata.orderStatus[0]
    },

    createDate: {
      type: Date,
      default: Date.now
    },
    customerInfo: {
      type: OrderCustomer,
      required: function (v){
        return this.status === 'INIT';
      }
    } 
}
</code></pre>