Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/averagehelper/hono-superstruct-validator
Hono validator middleware using Superstruct. (Read-only Git mirror)
https://github.com/averagehelper/hono-superstruct-validator
mirror
Last synced: 22 days ago
JSON representation
Hono validator middleware using Superstruct. (Read-only Git mirror)
- Host: GitHub
- URL: https://github.com/averagehelper/hono-superstruct-validator
- Owner: AverageHelper
- License: mit
- Created: 2023-09-18T19:58:34.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-12-17T04:59:16.000Z (about 1 year ago)
- Last Synced: 2024-12-12T17:06:04.322Z (about 1 month ago)
- Topics: mirror
- Language: TypeScript
- Homepage: https://git.average.name/AverageHelper/hono-superstruct-validator
- Size: 353 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Superstruct validator middleware for Hono
Validator middleware using [Superstruct](https://docs.superstructjs.org) for [Hono](https://honojs.dev) applications.
You can write a schema with Superstruct and validate the incoming values.
## Usage
```ts
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:
```ts
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:
```ts
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
Much of this work is based on [Yusuke Wada](https://github.com/yusukebe)'s wonderful work on [`@hono/zod-validator`](https://github.com/honojs/middleware/tree/main/packages/zod-validator).
## License
MIT
## Contributing
This project lives primarily at [git.average.name](https://git.average.name/AverageHelper/hono-superstruct-validator). Read-only mirrors also exist on [Codeberg](https://codeberg.org/AverageHelper/hono-superstruct-validator) and [GitHub](https://github.com/AverageHelper/hono-superstruct-validator). Issues or pull requests should be filed at [git.average.name](https://git.average.name/AverageHelper/hono-superstruct-validator). You may sign in or create an account directly, or use one of several OAuth 2.0 providers.