hono-superstruct-validator
v0.2.7
Published
Validator middleware using Superstruct.
Downloads
11
Readme
Superstruct validator middleware for Hono
Validator middleware using Superstruct for Hono applications.
You can write a schema with Superstruct and validate the incoming values.
Usage
import { number, object, string } from "superstruct";
import { sValidator } from "hono-superstruct-validator";
const schema = object({
name: string(),
age: number()
});
app.post("/author", sValidator("json", schema), c => {
const data = c.req.valid("json");
return c.json({
success: true,
message: `${data.name} is ${data.age}`
});
});
By default, if the incoming data does not match the given schema, a JSON object will be returned to the caller, with a status of 400. The response will have the following interface:
interface ResponseBody {
message: string;
}
The message will, by default, be the message
property of the relevant StructError
.
If you wish to handle errors differently, you may include a callback function:
app.post(
"/post",
sValidator("json", schema, (result, c) => {
return c.text("Invalid!", 400);
})
//...
);
At the moment, there is no opportunity to recover from the error. If the input data is bad, the middleware will fail out and respond to the caller.
Author
Average Helper https://git.average.name/AverageHelper
Much of this work is based on Yusuke Wada's wonderful work on @hono/zod-validator
.
License
MIT
Contributing
This project lives primarily at git.average.name. Read-only mirrors also exist on Codeberg and GitHub. Issues or pull requests should be filed at git.average.name. You may sign in or create an account directly, or use one of several OAuth 2.0 providers.