
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@honojs/validator
Advanced tools
Validator middleware for Hono. This middleware that wraps validator.js validates form body, queries, headers, and JSON body.
IT'S BETA QUALITY. DO NOT USE IT FOR PRODUCTION USAGE.
npm:
npm install @honojs/validator
Deno:
import { validator } from 'https://deno.land/x/hono_validator/mod.ts'
import { Hono } from 'hono'
import { validation } from '@honojs/validator'
const app = new Hono()
app.post(
'/post',
validation((v, message) => ({
body: {
title: [v.required, message('Title is required!!')],
body: [v.isLength, { max: 400 }],
},
})),
(c) => c.text('Created!', 201)
)
export default app
Validator Middleware wraps validator.js. There are a lot of rules in the library. You can validate four types of targets: form body, request headers, search params, and JSON body.
app.post(
'*',
validation((v) => ({
body: {
// Pass the parameters to the validator using array:
name: [v.isAlpha, [v.contains, 'abc']],
},
header: {
'x-custom-header': v.isAlphanumeric,
},
query: {
q: v.required,
},
json: {
// You can specify the key using JSON Path:
'post.author.email': [v.required, v.isEmail],
},
}))
)
You can sanitize the values before passing the theme to the validator.
app.post(
'/post',
validation((v) => ({
body: {
email: [v.trim, v.isEmail],
},
}))
)
If it's invalid, it will return "400" response with the messages set automatically.
You can set custom error messages for each rule.
app.post(
'/post',
validation((v, message) => ({
body: {
title: [v.required, message('Please set the title! Please!')],
},
}))
)
Making custom validator is easy.
const passwordValidator = (value: string) => {
return value.match(/[a-zA-Z0-9+=]+/) ? true : false
}
app.post(
'/custom-validator',
validation((_, message) => ({
body: {
password: [passwordValidator, message('password is wrong')],
},
}))
)
You can handle the errors more flexibly using validatorResult
method.
import { validation, validationResult } from '@honojs/validator'
//...
app.get('/search', (c) => {
const result = validationResult(c)
if (result.hasError) {
return c.json(
{
messages: result.messages,
},
404
)
}
return c.json({
messages: ['success!'],
})
})
Yusuke Wada https://github.com/yusukebe
MIT
FAQs
Validator Middleware for Hono
The npm package @honojs/validator receives a total of 11 weekly downloads. As such, @honojs/validator popularity was classified as not popular.
We found that @honojs/validator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.