Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rare-swans-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/arri-validator': major
---

Initial release
98 changes: 98 additions & 0 deletions packages/arri-validator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Arri validator middleware for Hono

Validator middleware for [Hono](https://honojs.dev) applications which uses [Arri Schema](https://github.com/modiimedia/arri). You can write a schema with Arri and validate the incoming values.

## Usage

```ts
import { a } from '@arrirpc/schema'
import { aValidator } from '@hono/arri-validator'

const schema = a.object({
name: a.string(),
age: a.number(),
})

app.post('/author', aValidator('json', schema), (c) => {
const data = c.req.valid('json')
return c.json({
success: true,
message: `${data.name} is ${data.age}`,
})
})
```

Hook:

```ts
app.post(
'/post',
aValidator('json', schema, (result, c) => {
if (!result.success) {
return c.text('Invalid!', 400)
}
})
//...
)
```

Throw Error:

To throw an error instead of directly returning an error response, you can create a custom wrapper for the validator. You could also create a custom validation function which uses `a.parseUnsafe`.

```ts
// file: validator-wrapper.ts
import type { ASchema } from '@arrirpc/schema'
import type { ValidationTargets } from 'hono'
import { aValidator as av } from '@hono/arri-validator'

export const aValidator = <T extends ASchema, Target extends keyof ValidationTargets>(
target: Target,
schema: T
) =>
av(target, schema, (result, c) => {
if (!result.success) {
throw new HTTPException(400, { cause: result.errors })
}
})

// usage
import { aValidator } from './validator-wrapper'
app.post(
'/post',
aValidator('json', schema)
//...
)
```

### Custom validation function

By default, this validation is done using `a.parse`.

```ts
await a.parse(schema, value)
```

If you want to use the [`a.coerce`](https://github.com/modiimedia/arri/blob/master/languages/ts/ts-schema/README.md#coerce), you can specify your own function in `validationFunction`.

```ts
app.post(
'/',
aValidator('json', schema, undefined, {
validationFunction: (schema, value) => {
return a.coerce(schema, value)
},
}),
(c) => {
// ...
}
)
```

## Author

kalucky0 <https://github.com/kalucky0>

## License

MIT
54 changes: 54 additions & 0 deletions packages/arri-validator/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "@hono/arri-validator",
"version": "0.1.0",
"description": "Validator middleware using Arri Schema",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
}
},
"files": [
"dist"
],
"scripts": {
"build": "tsup ./src/index.ts",
"prepack": "yarn build",
"publint": "attw --pack && publint",
"typecheck": "tsc -b tsconfig.json",
"test": "vitest"
},
"license": "MIT",
"publishConfig": {
"registry": "https://registry.npmjs.org",
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/honojs/middleware.git",
"directory": "packages/arri-validator"
},
"homepage": "https://github.com/honojs/middleware",
"peerDependencies": {
"@arrirpc/schema": "^0.79.0",
"hono": ">=4.0.0"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.18.1",
"@arrirpc/schema": "^0.79.0",
"publint": "^0.3.12",
"tsup": "^8.5.0",
"typescript": "^5.8.3",
"vitest": "^3.1.3"
}
}
Loading