Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin is applied to subdocuments as well #48

Open
AlbertHambardzumyan opened this issue Dec 12, 2023 · 0 comments
Open

plugin is applied to subdocuments as well #48

AlbertHambardzumyan opened this issue Dec 12, 2023 · 0 comments

Comments

@AlbertHambardzumyan
Copy link

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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant