H
Honoβ€’2mo ago
JustUseFirefox

How to use Validator ? Context is not finalized

As soon as I try to use validator, Hono complains. api-hono:dev: error: Context is not finalized. Did you forget to return a Response object or await next()?
app.post(
'/',
validator('form', (value, c) => {
console.log("VALIDATOR")

const body = value['body'];
// if (!body || typeof body !== 'string') {
// return c.text('Invalid!', 400);
// }

// const parsed = customerFormSchema.safeParse(body);
// if (!parsed.success) {
// console.log(401)
// return c.text('Invalid!', 401);
// }
// // parsed.data for body;

return {
body
};
}),
...
app.post(
'/',
validator('form', (value, c) => {
console.log("VALIDATOR")

const body = value['body'];
// if (!body || typeof body !== 'string') {
// return c.text('Invalid!', 400);
// }

// const parsed = customerFormSchema.safeParse(body);
// if (!parsed.success) {
// console.log(401)
// return c.text('Invalid!', 401);
// }
// // parsed.data for body;

return {
body
};
}),
...
Any idea? :\ EDIT: I'm using Superforms to send the request. Devtool says the data it is indeed Form Data. If I replace form with json, it doesnt throw the error, but the value is empty
9 Replies
Aditya Mathur
Aditya Mathurβ€’2mo ago
Just do await next(); If you want to save the updated body you will have to do to C.req.setValdate something On phone so can’t really type
JustUseFirefox
JustUseFirefoxβ€’2mo ago
Shit, really ? I didn't see that in any example I think... πŸ˜‚
Aditya Mathur
Aditya Mathurβ€’2mo ago
Interesting can share the docs page where you say it If there is an issue I will update them
JustUseFirefox
JustUseFirefoxβ€’2mo ago
Thank you very much for taking the time, you're amazing! I'll do that and report back ❀️ https://hono.dev/docs/guides/validation#manual-validator
Aditya Mathur
Aditya Mathurβ€’2mo ago
Interesting, I will take a look at this and will update it
JustUseFirefox
JustUseFirefoxβ€’2mo ago
don't do anxiety to reply lol, just leaving this here for when you're home and have time πŸ˜‡ Post form test in the repo: https://github.com/honojs/hono/blob/73ff6c0e82d66468e28ed439481220f56ab03882/src/validator/validator.test.ts#L217 But it doesnt work on my end. On my end, I tried adding next, but it doesnt like it.
validator('form', async (c, next) => {

await next();
}),
validator('form', async (c, next) => {

await next();
}),
This expression is not callable.
Type 'Context<any, string, {}>' has no call signatures.
This expression is not callable.
Type 'Context<any, string, {}>' has no call signatures.
package.json: "hono": "^4.5.11"
GitHub
hono/src/validator/validator.test.ts at 73ff6c0e82d66468e28ed439481...
Web framework built on Web Standards. Contribute to honojs/hono development by creating an account on GitHub.
Aditya Mathur
Aditya Mathurβ€’2mo ago
Thanks I will take a look πŸ‘€
JustUseFirefox
JustUseFirefoxβ€’2mo ago
I might have found the underlying issue... I'm using const body = await c.req.parseBody() so I need an async function in the main handler. and then I try to return values:
return c.json({
hello: true
// id: insert.id,
});
return c.json({
hello: true
// id: insert.id,
});
https://hono.dev/docs/api/request#parsebody This throws an error api-hono:dev: error: Context is not finalized. Did you forget to return a Response object or await next()? Found the culprit !!!
app.use(async (c, next) => {
c.set('dbClient', createClient());
c.set('e', e);
next();
});
app.use(async (c, next) => {
c.set('dbClient', createClient());
c.set('e', e);
next();
});
The first middleware that is suppose to make my db connection inside context causes this. Strange as I was succesful into inserting requests but I must have changed something somewhere. πŸ˜‚ adding await before next() solves the issue 🀦
Aditya Mathur
Aditya Mathurβ€’2mo ago
Great! πŸ˜‚
Want results from more Discord servers?
Add your server