Skip to content

plugin is applied to subdocuments as well #48

@AlbertHambardzumyan

Description

@AlbertHambardzumyan

We have the following schema:

const DocumentSchema = new mongoose.Schema({
  ...
  account: { type: mongoose.Schema.Types.ObjectId, ref: 'Accounts', required: true, index: true }
  items: [SubDocumentSchema]
}, { timestamps: true })

DocumentSchema.plugin(mongooseIdValidator)
const SubDocumentSchema = new mongoose.Schema({
  ...
  user: { type: mongoose.Schema.Types.ObjectId, ref: 'Users' }
  product: { type: mongoose.Schema.Types.ObjectId, ref: 'Products' }
}, { timestamps: true })

As you can see, we try to verify the references in the DocumentSchema but NOT in the SubDocumentSchema, thus we set the plugin only on DocumentSchema. Based on the business requirements, we need to store a subdocument having a product id no matter that product id is valid or not (might not exist).

However, as we apply the plugin on DocumentSchema (to make sure the account field is valid), it also verifies all references in SubDocumentSchema, which we really do NOT want and for that reason we have NOT set the plugin on SubDocumentSchema.

Any way to stop propagation ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions