Gobot1234
Gobot12342d ago

Modifying input in a middleware

Is it possible to modify input somewhere so that I can do something like this
const signIn = deskOrAdminProcedure
.input(
z.object({
name: LocationNameSchema,
ucard_number: z
.string()
.regex(/\d{9,}/),
data: CreateSignInSchema.extend({
reason_id: z.string(),
}),
}),
)
.mutation(async ({ input, ctx: { db, logger } }) => {
const { user, name, data } = await ensureUser(input, { db });
const signIn = deskOrAdminProcedure
.input(
z.object({
name: LocationNameSchema,
ucard_number: z
.string()
.regex(/\d{9,}/),
data: CreateSignInSchema.extend({
reason_id: z.string(),
}),
}),
)
.mutation(async ({ input, ctx: { db, logger } }) => {
const { user, name, data } = await ensureUser(input, { db });
as just
const signIn = deskOrAdminProcedure
.input(
z.object({
name: LocationNameSchema,
ucard_number: z
.string()
.regex(/\d{9,}/)
.transform(ldapLibraryToUcardNumber),
data: CreateSignInSchema.extend({
reason_id: z.string(),
}),
}),
)
// ... IDK what but inject ensureUser and db
.mutation(async ({ { user, name, data }, ctx: { db, logger } }) => {
const signIn = deskOrAdminProcedure
.input(
z.object({
name: LocationNameSchema,
ucard_number: z
.string()
.regex(/\d{9,}/)
.transform(ldapLibraryToUcardNumber),
data: CreateSignInSchema.extend({
reason_id: z.string(),
}),
}),
)
// ... IDK what but inject ensureUser and db
.mutation(async ({ { user, name, data }, ctx: { db, logger } }) => {
6 Replies
Alex / KATT 🐱
sorry, what are you trying to do? .transform() should work
Gobot1234
Gobot1234OP2d ago
i originally had .transform but I can't do that cause I don't have access to context
Alex / KATT 🐱
aha, you want access to context in middleware the input that's not possible unfortunately, but you can do a reusable part of it
Alex / KATT 🐱
Alex / KATT 🐱 (@alexdotjs) on X
Middlewares in tRPC 📗 Base procedures Most apps should only have a few base procedures that cover 99% of the app's use cases - In the below example, I have: 1. A public base procedure with logging 2. An authed base procedure that extends ^ 3. An organization procedure that
X
Solution
Gobot1234
Gobot1234OP2d ago
so a procedure with custom context is the best option? i was either thinking that or a wrapper function would do it

Did you find this page helpful?