https://github.com/tinchoz49/fastify-typebox-module
A Fastify plugin that enables the use of TypeBox module types by registering them as standard AJV schemas.
https://github.com/tinchoz49/fastify-typebox-module
fastify fastify-plugin plugin typebox
Last synced: 29 days ago
JSON representation
A Fastify plugin that enables the use of TypeBox module types by registering them as standard AJV schemas.
- Host: GitHub
- URL: https://github.com/tinchoz49/fastify-typebox-module
- Owner: tinchoz49
- License: mit
- Created: 2025-02-09T19:36:51.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-07-08T12:39:13.000Z (11 months ago)
- Last Synced: 2025-10-14T15:23:31.892Z (8 months ago)
- Topics: fastify, fastify-plugin, plugin, typebox
- Language: TypeScript
- Homepage:
- Size: 56.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# fastify-typebox-module
> A plugin for Fastify that allows you to use TypeBox module schemas and register them as normal ajv schemas.

[](https://github.com/tinchoz49/eslint-config-standard-ext)
[](https://github.com/RichardLitt/standard-readme)
## Install
```bash
$ npm install fastify-typebox-module
```
## Usage
```ts
import { type TypeBoxTypeProvider } from '@fastify/type-provider-typebox'
import { Type } from '@sinclair/typebox'
import fastify from 'fastify'
import fastifyTypeboxModule, { type SchemaType } from 'fastify-typebox-module'
const schemas = {
UserParams: Type.Object({
id: Type.String(),
}),
User: Type.Object({
id: Type.String(),
name: Type.String(),
address: Type.Ref('Address'),
phones: Type.Array(Type.Ref('Phone')),
}),
Address: Type.Object({
street: Type.String(),
city: Type.String(),
zip: Type.String(),
}),
Phone: Type.Object({
number: Type.String(),
}),
}
declare module 'fastify-typebox-module' {
interface FastifyTypeboxModule {
schemas: typeof schemas
}
}
const app = fastify().withTypeProvider()
// this is important to wait for the schemas to be registered before using them in the routes
await app.register(fastifyTypeboxModule, {
schemas,
})
// You can also access the TypeBox schema type directly with autocompletion
type User = SchemaType<'User'>
app.get('/user/:id', {
schema: {
params: app.typeboxModule.ref('UserParams'),
response: {
200: app.typeboxModule.ref('User'),
},
},
handler: async (request, reply) => {
const { id } = request.params
const user = {
id,
name: 'John Doe',
address: { street: '123 Main St', city: 'Anytown', zip: '12345' },
phones: [{ number: '123-456-7890' }],
}
return user
},
})
```
The schemas are going to be register, compiled and be available as normal ajv schemas, then you can reference any schema by using `app.typeboxModule.ref` and it will work as expected validating the request schemas, response schemas and their types.
## Important
This plugin is not compatible with the `TypeCompiler` of `@fastify/type-provider-typebox`, it only works with the built-in AJV compiler.
## Issues
:bug: If you found an issue we encourage you to report it on [github](https://github.com/tinchoz49/fastify-typebox-module/issues). Please specify your OS and the actions to reproduce it.
## Contributing
:busts_in_silhouette: Ideas and contributions to the project are welcome. You must follow this [guideline](https://github.com/tinchoz49/fastify-typebox-module/blob/main/CONTRIBUTING.md).
## License
MIT © 2025 Martin Acosta