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
Make Model.validate()
respect discriminators when discriminator key is set
#12621
Comments
This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days |
Short answer is yes.
https://mongoosejs.com/docs/discriminators.html#discriminators-save-to-the-event-models-collection |
Hey, @IslandRhythms I appreciate the reply but I am afraid that doesn't really help me much. May I ask you for a minimal example of how this can be achieved? As I mentioned I looked for days online and in the docs but I couldn't find anything, perhaps I am missing the obvious?
What I am asking is if you can use just a single model with discriminators. Going with the demo I made in the post above, basically I only ever want to have a single Cheers! |
Then no. Its creating a new model for each discriminator. |
I understand, thanks for clarifying. Just in case you guys decide to add this as a feature in the future, my motivation for this was (re)-exports and code structure. Because suppose you have 20 different animals, and you don't know ahead of time what animal a 3rd party API might return. You would have to export all 20 models, then import them somewhere else, so it would've been that much easier to just use a single model. But this is non-crucial for me anyway. Cheers again. |
@barca-reddit the following seems to work fine: (async () => {
const validPet = new Animal({
id: '123',
kind: 'cat',
meows: true
});
const invalidPet1 = new Animal({
id: '123',
kind: 'dog',
meows: true // <= a dog can't meow
});
const invalidPet2 = new Animal({
id: '123',
kind: 'INVALID',
meows: 'INVALID'
});
await validPet.validate(); // ✅ valid
await invalidPet1.validate(); // ❌ invalid
await invalidPet2.validate(); // ❌ invalid
})(); This just seems like it might be an issue with |
Model.validate()
respect discriminators when discriminator key is set
fix(model): respect discriminators with `Model.validate()`
Prerequisites
Mongoose version
5.12.8
Node.js version
v16.18.0
MongoDB version
5.0.13
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
22.04
Issue
Hello. I have a simple question, and please don't consider this a feature request or something similar, I am merely asking for help. I've searched the docs and online, but I couldn't find an example of this, so I thought I'd ask here.
Is it possible to have discriminators while only using a single model. I think this is best illustrated with an example. Let's say we have 3 schemas,
AnimalSchema
- the main schema, and two types of animalsCatSchema
andDogSchema
which all have distinct properties, and the discriminator key inAnimalSchema
iskind
. Finally, we have anAnimal
model.What I want to do is to instantiate
new Animal
s with theirkind
, and be able to check their validity like this:I am currently using v
5.12.8
but the code example provided in this post was made with v6.latest
as I thought that might work and I noticed some new syntax and features, but sadly mongoose didn't pick up any errors withinvalidPet1
andinvalidPet2
when I tried to.validate()
against theAnimals
model.So yep, don't want to make this any longer than necessary, just wanted to ask if it's possible to do this on v5, v6 or at all. I have done all of this a few years ago with nested paths, but it's not an option in this particular use case.
Thanks.
The text was updated successfully, but these errors were encountered: