title: Mongoose async custom validation not working as expected
body: <p>in my schema, I'm performing a lot of asynchronous, custom validations. However, validation is not behaving as I expect it to behave. <strong>Even though promises resolve with &quot;false&quot;, mongoose continues validation.</strong> According to <a href="https://mongoosejs.com/docs/validation.html#async-custom-validators" rel="nofollow noreferrer">their documentation</a>, that shouldn't be the case.</p>
  <p><strong>Sample schema:</strong></p>
  <pre><code>    var questSchema = mongoose.Schema({
        questCategory: {
          type: mongoose.Schema.Types.ObjectId,
          required: true,
          validate: {
            validator: async function (v) {
              await data_verificator.checkIfQuestCategoryExists(v);
            },
            message: (props) =&gt; `${props.value} is not a valid quest category id.`,
          },
        },
        fulfillmentPeriod: {
          type: String,
          required: true,
          validate: {
            validator: async function (v) {
              await data_verificator.checkFulfillmentPeriod(this.questCategory, v);
            },
            message: (props) =&gt;
              `${props.value} is an invalid value as it violates the limitations set by the quest category.`,
          },
        },
      })
  </code></pre>
  <p>Note that the custom validation happens <strong>asynchronously</strong> for those two schema fields. The validation of the <code>questCategory</code> field works perfectly fine. If the promise resolves to <code>false</code>, validation fails. However, that's not the case for the <code>fulfillmentPeriod</code> field. Even though the promise resolves to <code>false</code>, the validation succeeds.</p>
  <p>I am not sure why I get this weird behavior. If I were to rewrite validation of the <code>fulfillmentPeriod</code> to look like the following, everything works as expected again. The promise resolving to <code>false</code> causes the validation to fail. Why is that? Why does it work with below code but not with the initial code I pasted above? Is that because I'm referencing another schema field that is validated asynchronously?</p>
  <pre><code>validator: async function (v) {
    const result = await data_verificator.checkFulfillmentPeriod(this.questCategory, v);
    return result;
  },
  </code></pre>
  <p><strong>Just in case this is important, the <code>checkFulfillmentPeriod</code> function looks like this:</strong></p>
  <pre><code>const checkFulfillmentPeriod = async function (categoryId, period) {
    const connectionManager = require(&quot;../configuration&quot;).connectionManager;
  
    var category = await connectionManager.QuestCategoryModel.findOne({
      _id: categoryId,
      availableFulfillmentPeriods: {
        $elemMatch: {
          period: period,
        },
      },
    });
  
    if (!category) return false;
  
    return true;
  };
  </code></pre>
  <p>The function simply checks if there's a category matching the criteria. If so, true is returned. Otherwise false. From what I've found out, the issue doesn't originate in this function but has something to do with mongoose's validation.</p>
  <p><em><strong>The <code>checkIfQuestCategoryExists</code>function looks exactly the same, just with different query settings.</strong></em></p>
  <p>I've spent hours on this issue and at this point I just don't see any errors anymore.</p>
  <p><strong>I would appreciate any help/advice I could get!</strong></p>