Intersection of Discriminated Unions
First of all, I love Convex - great job team ❤️
I have a use case where I need to create a convex schema that is an intersection of two discriminated unions. I have the following toy example that works with Zod but I can't see an easy way of implementing the same behavior using Convex.
In the example below we start with two unions:
1. People vs animals
2. Countries vs cities
Ideally, in convex, I would be able to easily define the intersection of those two, allowing me to store objects like this:
I know I can write a function to get all NxM possible convex objects, but I was just wondering if there is cleaner approach, similar to what I can do with Zod
4 Replies
Thanks for posting in <#1088161997662724167>.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.
- Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
- Use search.convex.dev to search Docs, Stack, and Discord all at once.
- Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI.
- Avoid tagging staff unless specifically instructed.
Thank you!
I'm not aware of a cleaner approach than writing a function that does this yourself. Generic support could probably be added to the
zodToConvex
helper in convex-helpers
, so that you could just use the above Zod validator directly, if you were interested in contributing.
Other options that don't accomplish exactly what you're asking for include not flattening these but keeping everything in the same table still:
Or having separate entity and location tables:
Thanks for the reply @RJ, I assumed that might be the case. Unfortunately I have to keep them flat as i'm modelling the schema off of an external API, but the function approach will work okay for now. I'll take a look at
zodToConvex
and make a PR if I come up with a workable solutionNo problem! And makes sense