Nacho Elias
Nacho Elias
Explore posts from servers
TtRPC
Created by Nacho Elias on 6/18/2024 in #❓-help
Zod formatting Error
I'm using Zod for validation and whenever I get a validation error I'd like to be able to get the error message properly formatted. In the docs there's a section for this where it's sepcified that we can use the errorFormatter to achieve this. However, it's not doing anything at all. Btw, if I add a console.log within the errorFormatter nothing's being shown in the console, which is kind of weird if you ask me. However superjson is working completely fine, so I dont think its a configuration problem.... Here's my code
const t = initTRPC.context<typeof createTRPCContext>().create({
transformer: superjson,
errorFormatter: ({ shape, error }) => ({
...shape,
data: {
...shape.data,
zodError: error.cause instanceof ZodError ? error.cause.flatten() : null,
},
}),
});
const t = initTRPC.context<typeof createTRPCContext>().create({
transformer: superjson,
errorFormatter: ({ shape, error }) => ({
...shape,
data: {
...shape.data,
zodError: error.cause instanceof ZodError ? error.cause.flatten() : null,
},
}),
});
This is my validation
export const GetProfileSchema = z.object({
id: z.string().uuid({ message: "Invalid Id" }),
});
export const GetProfileSchema = z.object({
id: z.string().uuid({ message: "Invalid Id" }),
});
And this is what I get in my client in error.message instead of "Invalid ID"
[
{
"validation": "uuid",
"code": "invalid_string",
"message": "Invalid Id",
"path": [
"id"
]
}
]
[
{
"validation": "uuid",
"code": "invalid_string",
"message": "Invalid Id",
"path": [
"id"
]
}
]
4 replies
TtRPC
Created by Nacho Elias on 6/15/2024 in #❓-help
Zod error not being formatted
I'm using Zod for validation and whenever I get a validation error I'd like to be able to get the error message properly formatted. In the docs there's a section for this where it's sepcified that we can use the errorFormatter to achieve this. However, it's not doing anything at all. Btw, if I add a console.log within the errorFormatter nothing's being shown in the console, which is kind of weird if you ask me. However superjson is working completely fine, so I dont think its a configuration problem.... Here's my code
const t = initTRPC.context<typeof createTRPCContext>().create({
transformer: superjson,
errorFormatter: ({ shape, error }) => ({
...shape,
data: {
...shape.data,
zodError: error.cause instanceof ZodError ? error.cause.flatten() : null,
},
}),
});
const t = initTRPC.context<typeof createTRPCContext>().create({
transformer: superjson,
errorFormatter: ({ shape, error }) => ({
...shape,
data: {
...shape.data,
zodError: error.cause instanceof ZodError ? error.cause.flatten() : null,
},
}),
});
This is my validation
export const GetProfileSchema = z.object({
id: z.string().uuid({ message: "Invalid Id" }),
});
export const GetProfileSchema = z.object({
id: z.string().uuid({ message: "Invalid Id" }),
});
And this is what I get in my client in error.message instead of "Invalid ID"
[
{
"validation": "uuid",
"code": "invalid_string",
"message": "Invalid Id",
"path": [
"id"
]
}
]
[
{
"validation": "uuid",
"code": "invalid_string",
"message": "Invalid Id",
"path": [
"id"
]
}
]
2 replies
TtRPC
Created by Nacho Elias on 10/17/2022 in #❓-help
Which http library trpc uses in the frontend?
I was wondering iftrpc uses the Fetch API or uses something like Axios to do API calls from the frontend. I recently added supertokens to an app and I need to add a middleware for session refreshment depending on the tool I'd be using
3 replies
TtRPC
Created by Nacho Elias on 10/17/2022 in #❓-help
useQueryClient is not working as expected
So before v10 I was simply using useQueryClient and had my queryClient strongly typed. Now I moved to v10 and for some reason, the documentation says that the queryClient was removed from the context so I should use the useQueryClient hook directly from @tanstack but if I do so then the queryClient returned by that hook is not the queryClient trpc is using, it's just empty. I made it work by using the trpc.useContext hook coming from my client utils but I definitely think that the docs are a bit f*cked up.
71 replies