diff --git a/test/types/instance.test-d.ts b/test/types/instance.test-d.ts index 0ff544a3f1..7517c4d49a 100644 --- a/test/types/instance.test-d.ts +++ b/test/types/instance.test-d.ts @@ -10,6 +10,7 @@ import { expectAssignable, expectError, expectType } from 'tsd' import { FastifyRequest } from '../../types/request' import { FastifyReply } from '../../types/reply' import { HookHandlerDoneFunction } from '../../types/hooks' +import { FastifySchemaControllerOptions } from '../../types/schema' const server = fastify() @@ -112,6 +113,29 @@ server.setNotFoundHandler({ preHandler: notFoundpreHandlerHandler, preValidation function invalidErrorHandler (error: number) {} expectError(server.setErrorHandler(invalidErrorHandler)) +server.setSchemaController({ + bucket: (parentSchemas: unknown) => { + return { + addSchema (schema: unknown) { + expectType(schema) + expectType(server.addSchema({ type: 'null' })) + return server.addSchema({ type: 'null' }) + }, + getSchema (schemaId: string) { + expectType(schemaId) + return server.getSchema('SchemaId') + }, + getSchemas () { + expectType>(server.getSchemas()) + return server.getSchemas() + } + } + } +}) + +function invalidSchemaController (schemaControllerOptions: FastifySchemaControllerOptions) {} +expectError(server.setSchemaController(invalidSchemaController)) + server.setReplySerializer(function (payload, statusCode) { expectType(payload) expectType(statusCode) diff --git a/types/instance.d.ts b/types/instance.d.ts index 5d5d3de80a..5d5ef68101 100644 --- a/types/instance.d.ts +++ b/types/instance.d.ts @@ -1,6 +1,12 @@ import { Chain as LightMyRequestChain, InjectOptions, Response as LightMyRequestResponse, CallbackFunc as LightMyRequestCallback } from 'light-my-request' import { RouteOptions, RouteShorthandMethod, RouteGenericInterface, DefaultRoute } from './route' -import { FastifySchema, FastifySchemaCompiler, FastifySchemaValidationError, FastifySerializerCompiler } from './schema' +import { + FastifySchema, + FastifySchemaCompiler, + FastifySchemaValidationError, + FastifySerializerCompiler, + FastifySchemaControllerOptions +} from './schema' import { RawServerBase, RawRequestDefaultExpression, RawServerDefault, RawReplyDefaultExpression, ContextConfigDefault } from './utils' import { FastifyLoggerInstance } from './logger' import { FastifyRegister } from './register' @@ -380,6 +386,11 @@ export interface FastifyInstance< */ setSerializerCompiler(schemaCompiler: FastifySerializerCompiler): FastifyInstance; + /** + * Set the schema controller for all routes. + */ + setSchemaController(schemaControllerOpts: FastifySchemaControllerOptions): FastifyInstance; + /** * Set the reply serializer for all routes. */ diff --git a/types/schema.d.ts b/types/schema.d.ts index ce2f49cf68..88fcfc8ffb 100644 --- a/types/schema.d.ts +++ b/types/schema.d.ts @@ -1,3 +1,5 @@ +import { ValidatorCompiler } from '@fastify/ajv-compiler' +import { FastifyInstance, FastifyServerOptions } from '../fastify' /** * Schemas in Fastify follow the JSON-Schema standard. For this reason * we have opted to not ship strict schema based types. Instead we provide @@ -36,3 +38,15 @@ export interface FastifyValidationResult { export type FastifySchemaCompiler = (routeSchema: FastifyRouteSchemaDef) => FastifyValidationResult export type FastifySerializerCompiler = (routeSchema: FastifyRouteSchemaDef) => (data: any) => string + +export interface FastifySchemaControllerOptions{ + bucket?: (parentSchemas?: unknown) => { + addSchema(schema: unknown): FastifyInstance; + getSchema(schemaId: string): unknown; + getSchemas(): Record; + }; + compilersFactory?: { + buildValidator?: ValidatorCompiler; + buildSerializer?: (externalSchemas: unknown, serializerOptsServerOption: FastifyServerOptions['serializerOpts']) => FastifySerializerCompiler; + }; +}