Skip to content

Commit 846d35a

Browse files
feat(fastify): add decorator for custom schema
Add an ability to attach json schema to request with a RequestSchema decorator
1 parent 2e1ae5a commit 846d35a

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

packages/platform-fastify/adapters/fastify-adapter.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import { parse as querystringParse } from 'fast-querystring';
5353
import {
5454
FASTIFY_ROUTE_CONFIG_METADATA,
5555
FASTIFY_ROUTE_CONSTRAINTS_METADATA,
56+
FASTIFY_ROUTE_SCHEMA_METADATA,
5657
} from '../constants';
5758
import { NestFastifyBodyParserOptions } from '../interfaces';
5859
import {
@@ -752,17 +753,22 @@ export class FastifyAdapter<
752753
handlerRef,
753754
);
754755

756+
const routeSchema = Reflect.getMetadata(
757+
FASTIFY_ROUTE_SCHEMA_METADATA,
758+
handlerRef,
759+
);
760+
755761
const hasConfig = !isUndefined(routeConfig);
756762
const hasConstraints = !isUndefined(routeConstraints);
757-
763+
const hasSchema = !isUndefined(routeSchema);
758764
const routeToInject: RouteOptions<TServer, TRawRequest, TRawResponse> &
759765
RouteShorthandOptions = {
760766
method: routerMethodKey,
761767
url: args[0],
762768
handler: handlerRef,
763769
};
764770

765-
if (isVersioned || hasConstraints || hasConfig) {
771+
if (isVersioned || hasConstraints || hasConfig || hasSchema) {
766772
const isPathAndRouteTuple = args.length === 2;
767773
if (isPathAndRouteTuple) {
768774
const constraints = {
@@ -779,6 +785,9 @@ export class FastifyAdapter<
779785
...routeConfig,
780786
},
781787
}),
788+
...(hasSchema && {
789+
schema: routeSchema,
790+
}),
782791
};
783792

784793
const routeToInjectWithOptions = { ...routeToInject, ...options };
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const FASTIFY_ROUTE_CONFIG_METADATA = '__fastify_route_config__';
22
export const FASTIFY_ROUTE_CONSTRAINTS_METADATA =
33
'__fastify_route_constraints__';
4+
export const FASTIFY_ROUTE_SCHEMA_METADATA = '__fastify_route_schema__';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './route-config.decorator';
22
export * from './route-constraints.decorator';
3+
export * from './route-schema.decorator';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { SetMetadata } from '@nestjs/common';
2+
import { FASTIFY_ROUTE_SCHEMA_METADATA } from '../constants';
3+
import { FastifySchema } from 'fastify';
4+
5+
/**
6+
* @publicApi
7+
* Allows setting the schema for the route. Schema is an object that can contain the following properties:
8+
* - body: JsonSchema
9+
* - querystring or query: JsonSchema
10+
* - params: JsonSchema
11+
* - response: Record<HttpStatusCode, JsonSchema>
12+
* @param schema See {@link https://fastify.dev/docs/latest/Reference/Routes/#routes-options}
13+
*/
14+
export const RouteSchema = (schema: FastifySchema) =>
15+
SetMetadata(FASTIFY_ROUTE_SCHEMA_METADATA, schema);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { expect } from 'chai';
2+
import { FASTIFY_ROUTE_SCHEMA_METADATA } from '../../constants';
3+
import { RouteSchema } from '../../decorators/route-schema.decorator';
4+
5+
describe('@RouteSchema', () => {
6+
const routeSchema = { body: 'testValue' };
7+
class Test {
8+
config;
9+
@RouteSchema(routeSchema)
10+
public static test() {}
11+
}
12+
13+
it('should enhance method with expected fastify route schema', () => {
14+
const path = Reflect.getMetadata(FASTIFY_ROUTE_SCHEMA_METADATA, Test.test);
15+
expect(path).to.be.eql(routeSchema);
16+
});
17+
});

0 commit comments

Comments
 (0)