Skip to content

Commit

Permalink
fix: FastifySchemaValidationError type insufficient (#4094)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackHole1 committed Jul 1, 2022
1 parent 42cf476 commit c5ac183
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
13 changes: 12 additions & 1 deletion test/types/fastify.test-d.ts
Expand Up @@ -194,7 +194,18 @@ expectAssignable<FastifyInstance>(fastify({ frameworkErrors: () => { } }))
expectAssignable<FastifyInstance>(fastify({
rewriteUrl: (req) => req.url === '/hi' ? '/hello' : req.url!
}))
expectAssignable<FastifyInstance>(fastify({ schemaErrorFormatter: (errors, dataVar) => new Error() }))
expectAssignable<FastifyInstance>(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<FastifyInstance>(fastify({
clientErrorHandler: (err, socket) => {
expectType<ConnectionError>(err)
Expand Down
7 changes: 6 additions & 1 deletion test/validation-error-handling.test.js
Expand Up @@ -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')
}
Expand Down
5 changes: 4 additions & 1 deletion types/schema.d.ts
Expand Up @@ -23,8 +23,11 @@ export interface FastifyRouteSchemaDef<T> {
}

export interface FastifySchemaValidationError {
message?: string;
keyword: string;
instancePath: string;
schemaPath: string;
params: Record<string, string | string[]>;
message?: string;
}

export interface FastifyValidationResult {
Expand Down

0 comments on commit c5ac183

Please sign in to comment.