Skip to content

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

Closed
wants to merge 4 commits into from

Conversation

vrxj81
Copy link

@vrxj81 vrxj81 commented Apr 18, 2025

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • [ x ] Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

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:

  • Developers must manually register schemas when defining routes.
  • There’s no clean or idiomatic NestJS way to leverage Fastify's built-in request validation using schema.body, schema.querystring, etc.
  • Nest’s route metadata system (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 the schema field. This enables full compatibility with Fastify’s schema-based validation, serialization, and tooling.

Example usage:

@Schema({
  body: Type.Object({
    name: Type.String(),
  }),
})
@Post()
create(@Body() dto: any) {
  return dto;
}

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?

  • Yes
  • [ x ] No

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 the FastifyAdapter 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 modified FastifyAdapter.

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/

@coveralls
Copy link

coveralls commented Apr 18, 2025

Pull Request Test Coverage Report for Build f798fff2-b2fb-4ae8-aaa2-264a76cd5ac4

Details

  • 5 of 5 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.007%) to 89.328%

Totals Coverage Status
Change from base Build a2ecd259-4df8-488c-89b4-98c4c90fbc6b: 0.007%
Covered Lines: 7165
Relevant Lines: 8021

💛 - Coveralls

@vrxj81 vrxj81 changed the title Fastify-schema-validation feat(fastify): add Fastify native schema Validation and Serialization Apr 18, 2025
@micalevisk
Copy link
Member

so this is supposed to close the Issue #14986

Will this address the Issue #14931 as well?

@jadejr
Copy link

jadejr commented Apr 20, 2025

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.

Copy link
Author

@vrxj81 vrxj81 left a 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.

@kamilmysliwiec
Copy link
Member

Duplicate #14789

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants