-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
feat(fastify): add Fastify native schema Validation and Serialization #14987
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Pull Request Test Coverage Report for Build f798fff2-b2fb-4ae8-aaa2-264a76cd5ac4Details
💛 - Coveralls |
Any chance a sample could be added to the samples directory? Probably something like https://github.com/nestjs/nest/tree/master/sample/01-cats-app . I ended up using fastify for the first time with nest so i never got into how things might work with plain fastify app. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any chance a sample could be added to the samples directory? Probably something like https://github.com/nestjs/nest/tree/master/sample/01-cats-app . I ended up using fastify for the first time with nest so i never got into how things might work with plain fastify app.
That should be easily doable. but then in the sample/10-fastify example.
Duplicate #14789 |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Currently, when using the
@nestjs/platform-fastify
package, route schemas (e.g.,body
,querystring
,params
) cannot be defined via metadata or decorators in a way that integrates with Fastify’s native validation system.This means:
schema.body
,schema.querystring
, etc.Reflect.getMetadata(...)
) does not natively support passing Fastify schemas into route options.Issue Number: 14986
What is the new behavior?
This change introduces support for Fastify-native schema validation by enabling developers to annotate their route handlers with a new
@Schema()
decorator.The
FastifyAdapter
is extended to read schema metadata from the route handler and inject it into Fastify’s route options using theschema
field. This enables full compatibility with Fastify’s schema-based validation, serialization, and tooling.Example usage:
his allows seamless integration with libraries like TypeBox, Zod, and others—while remaining agnostic of the schema definition format—and keeps the validation logic cleanly separated from business logic.
Does this PR introduce a breaking change?
No, this PR does not introduce any breaking changes.
The schema metadata is entirely optional and only applied when the
@Schema()
decorator is used. Existing applications using theFastifyAdapter
without this decorator will continue to behave exactly as before.Other information
This feature enables native Fastify schema validation support in NestJS by allowing schemas to be defined via a
@Schema()
decorator and injected into the route options through a modifiedFastifyAdapter
.It integrates seamlessly into the current architecture, does not interfere with existing applications, and opens the door for using TypeBox or any other schema definition library without coupling to a specific one.
A corresponding issue was opened to describe the motivation and context of this PR: https://github.com/nestjs/nest/issues/