@hono/effect-validator
v1.2.0
Published
Validator middleware using Effect Schema
Downloads
1,270
Readme
Effect Schema Validator Middleware for Hono
This package provides a validator middleware using Effect Schema for Hono applications. With this middleware, you can define schemas using Effect Schema and validate incoming data in your Hono routes.
Why Effect Schema?
Effect Schema offers several advantages over other validation libraries:
- Bidirectional transformations: Effect Schema can both decode and encode data.
- Integration with Effect: It inherits benefits from the Effect ecosystem, such as dependency tracking in transformations.
- Highly customizable: Users can attach meta-information through annotations.
- Functional programming style: Uses combinators and transformations for schema definition.
Usage
import { Hono } from 'hono'
import { Schema as S } from '@effect/schema'
import { effectValidator } from '@hono/effect-validator'
const app = new Hono()
const User = S.Struct({
name: S.String,
age: S.Number,
})
app.post('/user', effectValidator('json', User), (c) => {
const user = c.req.valid('json')
return c.json({
success: true,
message: `${user.name} is ${user.age}`,
})
})
API
effectValidator(target, schema)
target
: The target of validation ('json', 'form', 'query', etc.)schema
: An Effect Schema schema
Author
Günther Brunner https://github.com/gunta
License
MIT