From c5ac1833b2ab1904da5330ada8a64120d4c1e8a7 Mon Sep 17 00:00:00 2001 From: Black-Hole <158blackhole@gmail.com> Date: Fri, 1 Jul 2022 15:19:34 +0800 Subject: [PATCH] fix: FastifySchemaValidationError type insufficient (#4094) --- test/types/fastify.test-d.ts | 13 ++++++++++++- test/validation-error-handling.test.js | 7 ++++++- types/schema.d.ts | 5 ++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/test/types/fastify.test-d.ts b/test/types/fastify.test-d.ts index d43ca5a14f..66c4d64d17 100644 --- a/test/types/fastify.test-d.ts +++ b/test/types/fastify.test-d.ts @@ -194,7 +194,18 @@ expectAssignable(fastify({ frameworkErrors: () => { } })) expectAssignable(fastify({ rewriteUrl: (req) => req.url === '/hi' ? '/hello' : req.url! })) -expectAssignable(fastify({ schemaErrorFormatter: (errors, dataVar) => new Error() })) +expectAssignable(fastify({ + schemaErrorFormatter: (errors, dataVar) => { + console.log( + errors[0].keyword.toLowerCase(), + errors[0].message?.toLowerCase(), + errors[0].params, + errors[0].instancePath.toLowerCase(), + errors[0].schemaPath.toLowerCase() + ) + return new Error() + } +})) expectAssignable(fastify({ clientErrorHandler: (err, socket) => { expectType(err) diff --git a/test/validation-error-handling.test.js b/test/validation-error-handling.test.js index 360af81927..534258af74 100644 --- a/test/validation-error-handling.test.js +++ b/test/validation-error-handling.test.js @@ -494,12 +494,17 @@ test('the custom error formatter context must be the server instance in options' }) test('should call custom error formatter', t => { - t.plan(6) + t.plan(9) const fastify = Fastify({ schemaErrorFormatter: (errors, dataVar) => { t.equal(errors.length, 1) t.equal(errors[0].message, "must have required property 'name'") + t.equal(errors[0].keyword, 'required') + t.equal(errors[0].schemaPath, '#/required') + t.same(errors[0].params, { + missingProperty: 'name' + }) t.equal(dataVar, 'body') return new Error('my error') } diff --git a/types/schema.d.ts b/types/schema.d.ts index 42d35a7945..bb0fd70916 100644 --- a/types/schema.d.ts +++ b/types/schema.d.ts @@ -23,8 +23,11 @@ export interface FastifyRouteSchemaDef { } export interface FastifySchemaValidationError { - message?: string; + keyword: string; instancePath: string; + schemaPath: string; + params: Record; + message?: string; } export interface FastifyValidationResult {