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
DTDrizzle Team
Created by Nacho Elias on 6/18/2024 in #help
Many to many flatten response
I'm struggling to understand why does drizzle work this way and why there isnt an easy way to flatten the response structure. I have 3 tables, Profile, Interest and Language. I also have 2 other join tables, ProfileInterest and ProfileLanguage. As you may have noticed, I have a many-to-many relationship between Profile and Interest and between Profile and Language. All 5 tables are in place (3 "models" and 2 join tables). I also have all the relations created and everything works as expected. To query for a specific profile including its' interests and languages I run the following.
db.query.Profile.findFirst({
where: eq(Profile.id, input.id),
with: {
interests: {
with: {
interest: true,
},
},
languages: {
with: {
language: true,
},
},
},
});
db.query.Profile.findFirst({
where: eq(Profile.id, input.id),
with: {
interests: {
with: {
interest: true,
},
},
languages: {
with: {
language: true,
},
},
},
});
The problem? The response looks like this
const currentOutput = {
...Profile, // All the fields from Profile
interests: [
{
profileId: 1,
interestId: 1,
interest: {
id: 1,
name: "Music",
},
},
],
languages: [
{
profileId: 1,
languageId: 1,
language: {
id: 1,
name: "English",
code: "en",
},
},
],
};
const currentOutput = {
...Profile, // All the fields from Profile
interests: [
{
profileId: 1,
interestId: 1,
interest: {
id: 1,
name: "Music",
},
},
],
languages: [
{
profileId: 1,
languageId: 1,
language: {
id: 1,
name: "English",
code: "en",
},
},
],
};
And of course, I'd like the following instead
const desiredOutput = {
...Profile,
interests: [
{
id: 1,
name: "Music",
},
],
languages: [
{
id: 1,
name: "English",
code: "en",
},
],
};
const desiredOutput = {
...Profile,
interests: [
{
id: 1,
name: "Music",
},
],
languages: [
{
id: 1,
name: "English",
code: "en",
},
],
};
Is there a way to achieve this using the queryAPI? At the moment the only thing I can do is either use it this way, which is not the end of the world but very inconvenient, or I can just flatten the structure in my server, which is also quite inconvenient...
2 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
DTDrizzle Team
Created by Nacho Elias on 6/13/2024 in #help
Error in DB update because of Date
No description
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