T
tRPC

input using z.or not working properly

input using z.or not working properly

BBarakonda3/4/2023
i have an input like this let input = z.object({ name: z.string().optional() }).or(z.object({ id: z.number().optional() })); when I call the route with { name: "123" } I get in the input { name: "123" } properly like I should, but when I call the same route with { id: 2 } then I get in the input {} - the rawInput is correctly { id: 2 } but the data is not transferred to input, when I switch the order of the input like this let input = z.object({ id: z.number().optional() }).or(z.object({ name: z.string().optional() })); then now, id is the one that works and name gives {} this is probably a bug, should I post this in the git or maybe it wasn't supposed to work at all?
Nnlucas3/4/2023
Think about how it works internally, both properties are optional. It will try to parse each possible type in order, and take the first success as the result. So in both examples, it parses, sees the optional type isn’t there which is fine, and returns the empty result This isn’t exactly a Zod help channel though, you’re best to refer to their own docs and issues for suggestions on workarounds I think there’s an option to make an input with extraneous inputs throw and maybe switching that on will force the wrong type to come up invalid and try the next one
Mmsalsbery3/5/2023
@Barakonda does it work if you remove the optional() calls?
BBarakonda3/5/2023
@msalsbery ill check and get back to you soon @Nick Lucas true, but it works if its a sub object inside the main input schema(from another part in my project) like this let input = z.object({ name: z.object({z.string().optional() }).or(z.object({ id: z.number().optional() })); @msalsbery yes' removing the optional works... pretty weird
Nnlucas3/5/2023
Your example is incomplete by the way, but I’d guess there’s some non-optional part of the that structure which does match fine You might want a discriminatedUnion
Mmsalsbery3/5/2023
Cool, like @Nick Lucas mentioned, if you think about how it works, with the optional, if the property isn’t there you get the “else” which is {} … the other object definition in the union isn’t looked at

Looking for more? Join the community!

T
tRPC

input using z.or not working properly

Join Server
Recommended Posts
How can I disable batching with fastify adapter?I cant seem to find a way to disable batching for my server, and this link doesnt help me much httpsIssue with monorepo architecture ant tRPCHi, we had an issue with batched requests that if we batch some requests they produce a TRPCClientErUsing tRPC in CRON jobsHey everyone, this might be a very stupid question, but is it possible to use tRPC inside a CRON joasync createContext for Express AdapterBeen debugging an odd behavior for the past hour, it seems like that an async function does not workIs it possible to split the router definition with the imlementation?I want to define the server router(input\output\meta) in a separate package from the server package Cache not working for `useQuery`I have a query like this: ```js const { data: article, isFetching } = api.public.getArticle.useQueryZod File Upload Validation with Open-Api Support?Hi guys, anyone know how to validate file upload with zod and get also open-api support?Zod validation and open-api support for File on the server?Hi guys, anyone know how to validate a File upload using zod? and also have open-api support?is context cached?If I put an object on the context that represents the User record from my database... ``` export asyJSON inferred router output not matchingHello. I have a procedure query which is returning Json from a postgresql db using Prisma. The type Best way to implement input based validation on a router procedureHi guys, bit of a noob. I have already created a 'protectedProcedure', ensuring the user is logged [Help] Turbo shared typesI have a turborepo with two apps (web and embed). web is a t3 stack and embed is a create-react-app.Cache SSG helper responseI'm using `createProxySSGHelpers` in GSSP with `ssr: false` in the global config. I trying to cacheInput is too big for a single dispatchI decided to try tRPC for my Crypto analytics dashboard. However, I'm having a hard time passing thetypesafe permissionsHi, So I wanted to infer all the procedures from my router recursively & assign a permission (stringawaiting for procedure & logging the response.Hi, I was wondering if there is a way to handle the return object via the post-middleware's? I know createCaller Dependency Injection in Middleware ctx ?`createCaller` makes it really easy to inject dependencies via anything that's created during the `cbest practices for organizing routes/procedures?i'm trying to find some practices/styles in which people generally define routes with trpc. currentlValidating input inside middleware declaration```js const enforceUserIsCreatorOfEvent = t.middleware(({ ctx, next, input }) => { if (!input.evenFetch errors on stale pagesRecently I have been getting a lot of fetch errors on stale pages, in particular ones that have quer