Skip to content

Commit 9dd9c6a

Browse files
authored
fix: Schema error formatter type (fastify#4570)
* refactor: type SchemaErrorFormatter * fix: refactor and fix type SchemaErrorDataVar * fix: type test for validationContext
1 parent b09fa76 commit 9dd9c6a

File tree

5 files changed

+13
-10
lines changed

5 files changed

+13
-10
lines changed

fastify.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ import { FastifyRegister, FastifyRegisterOptions, RegisterOptions } from './type
2020
import { FastifyReply } from './types/reply'
2121
import { FastifyRequest, RequestGenericInterface } from './types/request'
2222
import { RouteHandler, RouteHandlerMethod, RouteOptions, RouteShorthandMethod, RouteShorthandOptions, RouteShorthandOptionsWithHandler, RouteGenericInterface } from './types/route'
23-
import { FastifySchema, FastifySchemaCompiler, FastifySchemaValidationError } from './types/schema'
23+
import { FastifySchema, FastifySchemaCompiler, SchemaErrorDataVar, SchemaErrorFormatter } from './types/schema'
2424
import { FastifyServerFactory, FastifyServerFactoryHandler } from './types/serverFactory'
2525
import { FastifyTypeProvider, FastifyTypeProviderDefault } from './types/type-provider'
2626
import { HTTPMethods, RawServerBase, RawRequestDefaultExpression, RawReplyDefaultExpression, RawServerDefault, ContextConfigDefault, RequestBodyDefault, RequestQuerystringDefault, RequestParamsDefault, RequestHeadersDefault } from './types/utils'
2727

2828
declare module '@fastify/error' {
2929
interface FastifyError {
3030
validation?: fastify.ValidationResult[];
31-
validationContext?: 'body' | 'headers' | 'parameters' | 'querystring';
31+
validationContext?: SchemaErrorDataVar;
3232
}
3333
}
3434

@@ -149,7 +149,7 @@ declare namespace fastify {
149149
res: FastifyReply<RawServer, RawRequestDefaultExpression<RawServer>, RawReplyDefaultExpression<RawServer>, RequestGeneric, FastifyContextConfig, SchemaCompiler, TypeProvider>
150150
) => void,
151151
rewriteUrl?: (req: RawRequestDefaultExpression<RawServer>) => string,
152-
schemaErrorFormatter?: (errors: FastifySchemaValidationError[], dataVar: string) => Error,
152+
schemaErrorFormatter?: SchemaErrorFormatter,
153153
/**
154154
* listener to error events emitted by client connections
155155
*/

test/types/fastify.test-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ expectAssignable<ValidationResult>(ajvErrorObject)
240240
expectAssignable<FastifyError['validation']>([ajvErrorObject])
241241
expectAssignable<FastifyError['validationContext']>('body')
242242
expectAssignable<FastifyError['validationContext']>('headers')
243-
expectAssignable<FastifyError['validationContext']>('parameters')
243+
expectAssignable<FastifyError['validationContext']>('params')
244244
expectAssignable<FastifyError['validationContext']>('querystring')
245245

246246
const routeGeneric: RouteGenericInterface = {}

types/instance.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import {
1313
FastifySchema,
1414
FastifySchemaCompiler,
1515
FastifySchemaControllerOptions,
16-
FastifySchemaValidationError,
17-
FastifySerializerCompiler
16+
FastifySerializerCompiler,
17+
SchemaErrorFormatter
1818
} from './schema'
1919
import {
2020
FastifyTypeProvider,
@@ -530,7 +530,7 @@ export interface FastifyInstance<
530530
/*
531531
* Set the schema error formatter for all routes.
532532
*/
533-
setSchemaErrorFormatter(errorFormatter: (errors: FastifySchemaValidationError[], dataVar: string) => Error): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
533+
setSchemaErrorFormatter(errorFormatter: SchemaErrorFormatter): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
534534
/**
535535
* Add a content type parser
536536
*/

types/route.d.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { FastifyInstance } from './instance'
22
import { FastifyRequest, RequestGenericInterface } from './request'
33
import { FastifyReply, ReplyGenericInterface } from './reply'
4-
import { FastifySchema, FastifySchemaCompiler, FastifySchemaValidationError, FastifySerializerCompiler } from './schema'
4+
import { FastifySchema, FastifySchemaCompiler, FastifySerializerCompiler, SchemaErrorFormatter } from './schema'
55
import { HTTPMethods, RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
66
import { preValidationHookHandler, preHandlerHookHandler, preSerializationHookHandler, onRequestHookHandler, preParsingHookHandler, onResponseHookHandler, onSendHookHandler, onErrorHookHandler, onTimeoutHookHandler } from './hooks'
77
import { FastifyError } from '@fastify/error'
@@ -41,8 +41,7 @@ export interface RouteShorthandOptions<
4141
constraints?: { [name: string]: any },
4242
prefixTrailingSlash?: 'slash'|'no-slash'|'both';
4343
errorHandler?: (this: FastifyInstance, error: FastifyError, request: FastifyRequest, reply: FastifyReply) => void;
44-
// TODO: Change to actual type.
45-
schemaErrorFormatter?: (errors: FastifySchemaValidationError[], dataVar: string) => Error;
44+
schemaErrorFormatter?: SchemaErrorFormatter;
4645

4746
// hooks
4847
onRequest?: onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger> | onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];

types/schema.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,7 @@ export interface FastifySchemaControllerOptions{
5454
buildSerializer?: (externalSchemas: unknown, serializerOptsServerOption: FastifyServerOptions['serializerOpts']) => FastifySerializerCompiler<unknown>;
5555
};
5656
}
57+
58+
export type SchemaErrorDataVar = 'body' | 'headers' | 'params' | 'querystring'
59+
60+
export type SchemaErrorFormatter = (errors: FastifySchemaValidationError[], dataVar: SchemaErrorDataVar) => Error

0 commit comments

Comments
 (0)