簡體   English   中英

Mongoose 異步自定義驗證無法按預期工作

[英]Mongoose async custom validation not working as expected

在我的架構中,我正在執行大量異步的自定義驗證。 但是,驗證的行為並不像我期望的那樣。 即使承諾以“false”解決,貓鼬仍繼續驗證。 根據他們的文檔,情況不應該是這樣。

示例架構:

    var questSchema = mongoose.Schema({
      questCategory: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        validate: {
          validator: async function (v) {
            await data_verificator.checkIfQuestCategoryExists(v);
          },
          message: (props) => `${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) =>
            `${props.value} is an invalid value as it violates the limitations set by the quest category.`,
        },
      },
    })

請注意,這兩個架構字段的自定義驗證是異步發生的。 questCategory字段的驗證工作得很好。 如果承諾解析為false ,則驗證失敗。 但是, fulfillmentPeriod字段並非如此。 即使 promise 解析為false ,驗證也會成功。

我不確定為什么會出現這種奇怪的行為。 如果我將fulfillmentPeriod驗證重寫為如下所示,一切都會再次按預期工作。 解析為false的承諾會導致驗證失敗。 這是為什么? 為什么它適用於下面的代碼而不適用於我粘貼在上面的初始代碼? 那是因為我引用了另一個異步驗證的模式字段嗎?

validator: async function (v) {
  const result = await data_verificator.checkFulfillmentPeriod(this.questCategory, v);
  return result;
},

以防萬一這很重要, checkFulfillmentPeriod函數如下所示:

const checkFulfillmentPeriod = async function (categoryId, period) {
  const connectionManager = require("../configuration").connectionManager;

  var category = await connectionManager.QuestCategoryModel.findOne({
    _id: categoryId,
    availableFulfillmentPeriods: {
      $elemMatch: {
        period: period,
      },
    },
  });

  if (!category) return false;

  return true;
};

該函數只是檢查是否有符合條件的類別。 如果是,則返回 true。 否則為假。 從我發現的情況來看,問題並非源於此函數,而是與 mongoose 的驗證有關。

checkIfQuestCategoryExists函數看起來完全一樣,只是查詢設置不同。

我已經在這個問題上花費了幾個小時,此時我再也看不到任何錯誤了。

如果我能得到任何幫助/建議,我將不勝感激!

您的驗證器缺少 return 語句,因此就像您返回Promise<void> ,這不會觸發 mongo 的驗證。 您可以添加 return 或重寫您的函數,承諾后者不太優雅。

new Promise( (resolve,reject) => {
  .....
  resolve(true/false);
});

你能試試這個代碼嗎:

var questSchema = mongoose.Schema({
      questCategory: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        validate: {
          validator: async function (v) {
            return await data_verificator.checkIfQuestCategoryExists(v);
          },
          message: (props) => `${props.value} is not a valid quest category id.`,
        },
      },
      fulfillmentPeriod: {
        type: String,
        required: true,
        validate: {
          validator: async function (v) {
            return await data_verificator.checkFulfillmentPeriod(this.questCategory, v);
          },
          message: (props) =>
            `${props.value} is an invalid value as it violates the limitations set by the quest category.`,
        },
      },
    })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM