'Check if a document has already been created in mongoose validator?

In one of my conditional schema validation I need to know if the current document has already been created before returning true or false.
This is the kind of logic I'm looking for (in pseudo code):

  password: {
    type: String,
    trim: true,
    required: function () { 
        if (this.password == '') {
            // Pseudo code:
            return document.exists ? false : true;
        }
         
    }
  },

How can I do that with real code and is it the correct way or should I look for a different approach ?
note: I'm aware of the exists() method but I don't know how to use it in a schema validation situation.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source