
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
@detools/type-provider-zod
Advanced tools
A Type Provider for Typebox
npm i @fastify/type-provider-typebox
import Fastify from 'fastify'
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'
const fastify = Fastify().withTypeProvider<TypeBoxTypeProvider>()
import Fastify from 'fastify'
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'
import { Type } from '@sinclair/typebox'
const fastify = Fastify().withTypeProvider<TypeBoxTypeProvider>()
fastify.get('/', {
schema: {
body: Type.Object({
x: Type.String(),
y: Type.Number(),
z: Type.Boolean()
})
}
}, (req) => {
// The `x`, `y`, `z` types are automatically inferred
const { x, y, z } = req.body
})
import {
FastifyReply,
FastifyRequest,
RawRequestDefaultExpression,
RawServerDefault,
} from 'fastify';
import { Type } from '@sinclair/typebox';
import { RouteGenericInterface } from 'fastify/types/route';
import { FastifySchema } from 'fastify/types/schema';
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
export type FastifyRequestTypebox<TSchema extends FastifySchema> = FastifyRequest<
RouteGenericInterface,
RawServerDefault,
RawRequestDefaultExpression<RawServerDefault>,
TSchema,
TypeBoxTypeProvider
>;
export const CreateProductSchema = {
body: Type.Object({
name: Type.String(),
price: Type.Number(),
}),
};
export const CreateProductHandler = (
req: FastifyRequestTypebox<typeof CreateProductSchema>
) => {
// The `name` and `price` types are automatically inferred
const { name, price } = req.body;
};
Note When using plugin types, withTypeProvider is not required in order to register the plugin
import { Type } from '@sinclair/typebox';
import { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox'
const plugin: FastifyPluginAsyncTypebox = async function(fastify, _opts) {
fastify.get('/', {
schema: {
body: Type.Object({
x: Type.String(),
y: Type.Number(),
z: Type.Boolean()
})
}
}, (req) => {
/// The `x`, `y`, and `z` types are automatically inferred
const { x, y, z } = req.body
});
}
TypeBox provides an optional type compiler that perform very fast runtime type checking for data received on routes. Note this compiler is limited to types expressable through the TypeBox Type.*
namespace only. To enable this compiler, you can call .setValidatorCompiler(...)
with the TypeBoxValidatorCompiler
export provided by this package.
import { TypeBoxTypeProvider, TypeBoxValidatorCompiler } from '@fastify/type-provider-typebox'
import { Type } from '@sinclair/typebox'
import Fastify from 'fastify'
const fastify = Fastify().setValidatorCompiler(TypeBoxValidatorCompiler)
fastify.withTypeProvider<TypeBoxTypeProvider>().get('/', {
schema: {
querystring: Type.Object({
x: Type.String(),
y: Type.String(),
z: Type.String()
})
}
}, (req) => {
const { x, y, z } = req.query
})
For additional information on this compiler, please refer to the TypeBox documentation located here
FAQs
A Type Provider for Zod over Fastify
We found that @detools/type-provider-zod demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.