You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, so when I tried to implement discriminated unions for my app i got an error from zod-mongoose, and via the documentation i found out that they're not supported because of a mongoose limitation.
But when checking out the official mongoose docs i found this: https://mongoosejs.com/docs/discriminators.html
So would it be possible to implement discriminated unions, or is that impossible/waste of time?
The text was updated successfully, but these errors were encountered:
Oi!
I've checked that out and there's a conceptional conflict with zod-mongoose.
For top-level discriminators, it should create two (or more) schemas, which is not an expected behaviour in current scope.
Imagine you have a Union field in schema, it accepts two (or more) types, then you'll have two (and more) output schemas. It's alright. But imagine you have two fields with two or more types each, It's already 4 schemas, and for three fields – it's 9 already. To manage those, you would have to build a whole new mongoose wrapper with it's own API.
Simple way to do discriminators is for user to follow the mongoose docs and modify the output schema.
It could work like this:
constzVariant0=z.object({platform: z.enum(["iOS","Android","Web"]),id: z.string(),});constzVariant1=z.object({type: z.enum(["wearable","beacon"]),mac: z.string(),});constzUser=z.object({name: z.string(),device: z.union([zVariant0,zVariant1]),});extend(z);constuserSchemaV0=newSchema("UserSchema",zodSchemaRaw(zUser));// will consume Union's first typeconstuserSchemaV1=userSchemaV0.discriminator("UserSchemaV1",zodSchemaRaw(z.object({device: zodSchemaRaw(zVariant1),}););
I didn't check it on practice yet, so please consider this answer as just a first thought
Hi, so when I tried to implement discriminated unions for my app i got an error from
zod-mongoose
, and via the documentation i found out that they're not supported because of a mongoose limitation.But when checking out the official mongoose docs i found this: https://mongoosejs.com/docs/discriminators.html
So would it be possible to implement discriminated unions, or is that impossible/waste of time?
The text was updated successfully, but these errors were encountered: