fastify 4.16.3 → 4.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/fastify.js +2 -2
- package/lib/validation.js +2 -0
- package/package.json +1 -1
- package/test/404s.test.js +4 -2
- package/test/delete.test.js +3 -0
- package/test/fluent-schema.test.js +4 -4
- package/test/get.test.js +3 -0
- package/test/input-validation.js +10 -5
- package/test/schema-examples.test.js +2 -0
- package/test/schema-serialization.test.js +7 -5
- package/test/schema-validation.test.js +8 -4
- package/test/search.test.js +3 -0
- package/test/types/hooks.test-d.ts +42 -0
- package/test/validation-error-handling.test.js +18 -3
- package/types/route.d.ts +11 -31
package/fastify.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const VERSION = '4.
|
|
3
|
+
const VERSION = '4.17.0'
|
|
4
4
|
|
|
5
5
|
const Avvio = require('avvio')
|
|
6
6
|
const http = require('http')
|
|
@@ -689,7 +689,7 @@ function fastify (options) {
|
|
|
689
689
|
const reply = new Reply(res, request, childLogger)
|
|
690
690
|
return frameworkErrors(new FST_ERR_BAD_URL(path), request, reply)
|
|
691
691
|
}
|
|
692
|
-
const body = `{"error":"Bad Request","message":"'${path}' is not a valid url component","statusCode":400}`
|
|
692
|
+
const body = `{"error":"Bad Request","code":"FST_ERR_BAD_URL","message":"'${path}' is not a valid url component","statusCode":400}`
|
|
693
693
|
res.writeHead(400, {
|
|
694
694
|
'Content-Type': 'application/json',
|
|
695
695
|
'Content-Length': body.length
|
package/lib/validation.js
CHANGED
|
@@ -138,12 +138,14 @@ function validate (context, request) {
|
|
|
138
138
|
function wrapValidationError (result, dataVar, schemaErrorFormatter) {
|
|
139
139
|
if (result instanceof Error) {
|
|
140
140
|
result.statusCode = result.statusCode || 400
|
|
141
|
+
result.code = result.code || 'FST_ERR_VALIDATION'
|
|
141
142
|
result.validationContext = result.validationContext || dataVar
|
|
142
143
|
return result
|
|
143
144
|
}
|
|
144
145
|
|
|
145
146
|
const error = schemaErrorFormatter(result, dataVar)
|
|
146
147
|
error.statusCode = error.statusCode || 400
|
|
148
|
+
error.code = error.code || 'FST_ERR_VALIDATION'
|
|
147
149
|
error.validation = result
|
|
148
150
|
error.validationContext = dataVar
|
|
149
151
|
return error
|
package/package.json
CHANGED
package/test/404s.test.js
CHANGED
|
@@ -1831,7 +1831,8 @@ test('400 in case of bad url (pre find-my-way v2.2.0 was a 404)', t => {
|
|
|
1831
1831
|
t.same(JSON.parse(response.payload), {
|
|
1832
1832
|
error: 'Bad Request',
|
|
1833
1833
|
message: "'/hello/%world' is not a valid url component",
|
|
1834
|
-
statusCode: 400
|
|
1834
|
+
statusCode: 400,
|
|
1835
|
+
code: 'FST_ERR_BAD_URL'
|
|
1835
1836
|
})
|
|
1836
1837
|
})
|
|
1837
1838
|
})
|
|
@@ -1849,7 +1850,8 @@ test('400 in case of bad url (pre find-my-way v2.2.0 was a 404)', t => {
|
|
|
1849
1850
|
t.same(JSON.parse(response.payload), {
|
|
1850
1851
|
error: 'Bad Request',
|
|
1851
1852
|
message: "'/hello/%world' is not a valid url component",
|
|
1852
|
-
statusCode: 400
|
|
1853
|
+
statusCode: 400,
|
|
1854
|
+
code: 'FST_ERR_BAD_URL'
|
|
1853
1855
|
})
|
|
1854
1856
|
})
|
|
1855
1857
|
})
|
package/test/delete.test.js
CHANGED
|
@@ -197,6 +197,7 @@ fastify.listen({ port: 0 }, err => {
|
|
|
197
197
|
t.equal(response.statusCode, 400)
|
|
198
198
|
t.same(JSON.parse(body), {
|
|
199
199
|
error: 'Bad Request',
|
|
200
|
+
code: 'FST_ERR_VALIDATION',
|
|
200
201
|
message: 'params/test must be integer',
|
|
201
202
|
statusCode: 400
|
|
202
203
|
})
|
|
@@ -232,6 +233,7 @@ fastify.listen({ port: 0 }, err => {
|
|
|
232
233
|
t.equal(response.statusCode, 400)
|
|
233
234
|
t.same(JSON.parse(body), {
|
|
234
235
|
error: 'Bad Request',
|
|
236
|
+
code: 'FST_ERR_VALIDATION',
|
|
235
237
|
message: 'headers/x-test must be number',
|
|
236
238
|
statusCode: 400
|
|
237
239
|
})
|
|
@@ -261,6 +263,7 @@ fastify.listen({ port: 0 }, err => {
|
|
|
261
263
|
t.equal(response.statusCode, 400)
|
|
262
264
|
t.same(JSON.parse(body), {
|
|
263
265
|
error: 'Bad Request',
|
|
266
|
+
code: 'FST_ERR_VALIDATION',
|
|
264
267
|
message: 'querystring/hello must be integer',
|
|
265
268
|
statusCode: 400
|
|
266
269
|
})
|
|
@@ -34,7 +34,7 @@ test('use fluent-json-schema object', t => {
|
|
|
34
34
|
}, (err, res) => {
|
|
35
35
|
t.error(err)
|
|
36
36
|
t.equal(res.statusCode, 400)
|
|
37
|
-
t.same(res.json(), { statusCode: 400, error: 'Bad Request', message: 'params/id must be >= 42' })
|
|
37
|
+
t.same(res.json(), { statusCode: 400, code: 'FST_ERR_VALIDATION', error: 'Bad Request', message: 'params/id must be >= 42' })
|
|
38
38
|
})
|
|
39
39
|
|
|
40
40
|
// check header
|
|
@@ -47,7 +47,7 @@ test('use fluent-json-schema object', t => {
|
|
|
47
47
|
}, (err, res) => {
|
|
48
48
|
t.error(err)
|
|
49
49
|
t.equal(res.statusCode, 400)
|
|
50
|
-
t.same(res.json(), { statusCode: 400, error: 'Bad Request', message: 'headers/x-custom must match format "email"' })
|
|
50
|
+
t.same(res.json(), { statusCode: 400, code: 'FST_ERR_VALIDATION', error: 'Bad Request', message: 'headers/x-custom must match format "email"' })
|
|
51
51
|
})
|
|
52
52
|
|
|
53
53
|
// check query
|
|
@@ -60,7 +60,7 @@ test('use fluent-json-schema object', t => {
|
|
|
60
60
|
}, (err, res) => {
|
|
61
61
|
t.error(err)
|
|
62
62
|
t.equal(res.statusCode, 400)
|
|
63
|
-
t.same(res.json(), { statusCode: 400, error: 'Bad Request', message: 'querystring must have required property \'surname\'' })
|
|
63
|
+
t.same(res.json(), { statusCode: 400, code: 'FST_ERR_VALIDATION', error: 'Bad Request', message: 'querystring must have required property \'surname\'' })
|
|
64
64
|
})
|
|
65
65
|
|
|
66
66
|
// check body
|
|
@@ -73,7 +73,7 @@ test('use fluent-json-schema object', t => {
|
|
|
73
73
|
}, (err, res) => {
|
|
74
74
|
t.error(err)
|
|
75
75
|
t.equal(res.statusCode, 400)
|
|
76
|
-
t.same(res.json(), { statusCode: 400, error: 'Bad Request', message: 'body/name must be string' })
|
|
76
|
+
t.same(res.json(), { statusCode: 400, code: 'FST_ERR_VALIDATION', error: 'Bad Request', message: 'body/name must be string' })
|
|
77
77
|
})
|
|
78
78
|
|
|
79
79
|
// check response
|
package/test/get.test.js
CHANGED
|
@@ -243,6 +243,7 @@ fastify.listen({ port: 0 }, err => {
|
|
|
243
243
|
t.equal(response.statusCode, 400)
|
|
244
244
|
t.same(JSON.parse(body), {
|
|
245
245
|
error: 'Bad Request',
|
|
246
|
+
code: 'FST_ERR_VALIDATION',
|
|
246
247
|
message: 'params/test must be integer',
|
|
247
248
|
statusCode: 400
|
|
248
249
|
})
|
|
@@ -280,6 +281,7 @@ fastify.listen({ port: 0 }, err => {
|
|
|
280
281
|
t.equal(response.statusCode, 400)
|
|
281
282
|
t.same(JSON.parse(body), {
|
|
282
283
|
error: 'Bad Request',
|
|
284
|
+
code: 'FST_ERR_VALIDATION',
|
|
283
285
|
message: 'headers/x-test must be number',
|
|
284
286
|
statusCode: 400
|
|
285
287
|
})
|
|
@@ -309,6 +311,7 @@ fastify.listen({ port: 0 }, err => {
|
|
|
309
311
|
t.equal(response.statusCode, 400)
|
|
310
312
|
t.same(JSON.parse(body), {
|
|
311
313
|
error: 'Bad Request',
|
|
314
|
+
code: 'FST_ERR_VALIDATION',
|
|
312
315
|
message: 'querystring/hello must be integer',
|
|
313
316
|
statusCode: 400
|
|
314
317
|
})
|
package/test/input-validation.js
CHANGED
|
@@ -182,7 +182,8 @@ module.exports.payloadMethod = function (method, t) {
|
|
|
182
182
|
t.same(body, {
|
|
183
183
|
error: 'Bad Request',
|
|
184
184
|
message: 'body/hello must be integer',
|
|
185
|
-
statusCode: 400
|
|
185
|
+
statusCode: 400,
|
|
186
|
+
code: 'FST_ERR_VALIDATION'
|
|
186
187
|
})
|
|
187
188
|
})
|
|
188
189
|
})
|
|
@@ -251,7 +252,8 @@ module.exports.payloadMethod = function (method, t) {
|
|
|
251
252
|
t.same(body, {
|
|
252
253
|
error: 'Bad Request',
|
|
253
254
|
message: '"hello" must be a string',
|
|
254
|
-
statusCode: 400
|
|
255
|
+
statusCode: 400,
|
|
256
|
+
code: 'FST_ERR_VALIDATION'
|
|
255
257
|
})
|
|
256
258
|
})
|
|
257
259
|
})
|
|
@@ -287,7 +289,8 @@ module.exports.payloadMethod = function (method, t) {
|
|
|
287
289
|
t.same(body, {
|
|
288
290
|
error: 'Bad Request',
|
|
289
291
|
message: 'hello must be a `string` type, but the final value was: `44`.',
|
|
290
|
-
statusCode: 400
|
|
292
|
+
statusCode: 400,
|
|
293
|
+
code: 'FST_ERR_VALIDATION'
|
|
291
294
|
})
|
|
292
295
|
})
|
|
293
296
|
})
|
|
@@ -305,7 +308,8 @@ module.exports.payloadMethod = function (method, t) {
|
|
|
305
308
|
t.same(body, {
|
|
306
309
|
error: 'Bad Request',
|
|
307
310
|
message: 'From custom schema compiler!',
|
|
308
|
-
statusCode: '400'
|
|
311
|
+
statusCode: '400',
|
|
312
|
+
code: 'FST_ERR_VALIDATION'
|
|
309
313
|
})
|
|
310
314
|
})
|
|
311
315
|
})
|
|
@@ -323,7 +327,8 @@ module.exports.payloadMethod = function (method, t) {
|
|
|
323
327
|
t.same(body, {
|
|
324
328
|
error: 'Bad Request',
|
|
325
329
|
message: 'Always fail!',
|
|
326
|
-
statusCode: '400'
|
|
330
|
+
statusCode: '400',
|
|
331
|
+
code: 'FST_ERR_VALIDATION'
|
|
327
332
|
})
|
|
328
333
|
})
|
|
329
334
|
})
|
|
@@ -502,6 +502,7 @@ test('should return custom error messages with ajv-errors', t => {
|
|
|
502
502
|
t.error(err)
|
|
503
503
|
t.same(JSON.parse(res.payload), {
|
|
504
504
|
statusCode: 400,
|
|
505
|
+
code: 'FST_ERR_VALIDATION',
|
|
505
506
|
error: 'Bad Request',
|
|
506
507
|
message: 'body/age bad age - should be num, body name please, body work please'
|
|
507
508
|
})
|
|
@@ -557,6 +558,7 @@ test('should be able to handle formats of ajv-formats when added by plugins opti
|
|
|
557
558
|
}, (_err, res) => {
|
|
558
559
|
t.same(JSON.parse(res.payload), {
|
|
559
560
|
statusCode: 400,
|
|
561
|
+
code: 'FST_ERR_VALIDATION',
|
|
560
562
|
error: 'Bad Request',
|
|
561
563
|
message: 'body/id must match format "uuid"'
|
|
562
564
|
})
|
|
@@ -394,7 +394,8 @@ test('Use shared schema and $ref with $id in response ($ref to $id)', t => {
|
|
|
394
394
|
t.same(res.json(), {
|
|
395
395
|
error: 'Bad Request',
|
|
396
396
|
message: "body must have required property 'address'",
|
|
397
|
-
statusCode: 400
|
|
397
|
+
statusCode: 400,
|
|
398
|
+
code: 'FST_ERR_VALIDATION'
|
|
398
399
|
})
|
|
399
400
|
})
|
|
400
401
|
})
|
|
@@ -503,7 +504,8 @@ test('Shared schema should be pass to serializer and validator ($ref to shared s
|
|
|
503
504
|
t.same(res.json(), {
|
|
504
505
|
error: 'Bad Request',
|
|
505
506
|
message: 'body/0/location/email must match format "email"',
|
|
506
|
-
statusCode: 400
|
|
507
|
+
statusCode: 400,
|
|
508
|
+
code: 'FST_ERR_VALIDATION'
|
|
507
509
|
})
|
|
508
510
|
})
|
|
509
511
|
})
|
|
@@ -806,9 +808,9 @@ test('do not crash if status code serializer errors', async t => {
|
|
|
806
808
|
const someUserErrorType2 = {
|
|
807
809
|
type: 'object',
|
|
808
810
|
properties: {
|
|
809
|
-
|
|
811
|
+
customCode: { type: 'number' }
|
|
810
812
|
},
|
|
811
|
-
required: ['
|
|
813
|
+
required: ['customCode']
|
|
812
814
|
}
|
|
813
815
|
|
|
814
816
|
fastify.get(
|
|
@@ -834,7 +836,7 @@ test('do not crash if status code serializer errors', async t => {
|
|
|
834
836
|
t.same(res.json(), {
|
|
835
837
|
statusCode: 500,
|
|
836
838
|
code: 'FST_ERR_FAILED_ERROR_SERIALIZATION',
|
|
837
|
-
message: 'Failed to serialize an error. Error: "
|
|
839
|
+
message: 'Failed to serialize an error. Error: "customCode" is required!. ' +
|
|
838
840
|
'Original error: querystring must have required property \'foo\''
|
|
839
841
|
})
|
|
840
842
|
})
|
|
@@ -100,7 +100,7 @@ test('Basic validation test', t => {
|
|
|
100
100
|
url: '/'
|
|
101
101
|
}, (err, res) => {
|
|
102
102
|
t.error(err)
|
|
103
|
-
t.same(res.json(), { statusCode: 400, error: 'Bad Request', message: "body must have required property 'work'" })
|
|
103
|
+
t.same(res.json(), { statusCode: 400, code: 'FST_ERR_VALIDATION', error: 'Bad Request', message: "body must have required property 'work'" })
|
|
104
104
|
t.equal(res.statusCode, 400)
|
|
105
105
|
})
|
|
106
106
|
})
|
|
@@ -537,6 +537,7 @@ test('JSON Schema validation keywords', t => {
|
|
|
537
537
|
t.equal(res.statusCode, 400)
|
|
538
538
|
t.same(res.json(), {
|
|
539
539
|
statusCode: 400,
|
|
540
|
+
code: 'FST_ERR_VALIDATION',
|
|
540
541
|
error: 'Bad Request',
|
|
541
542
|
message: 'params/ip must match format "ipv4"'
|
|
542
543
|
})
|
|
@@ -593,7 +594,8 @@ test('Nested id calls', t => {
|
|
|
593
594
|
t.same(res.json(), {
|
|
594
595
|
error: 'Bad Request',
|
|
595
596
|
message: 'body/host/ip must match format "ipv4"',
|
|
596
|
-
statusCode: 400
|
|
597
|
+
statusCode: 400,
|
|
598
|
+
code: 'FST_ERR_VALIDATION'
|
|
597
599
|
})
|
|
598
600
|
})
|
|
599
601
|
})
|
|
@@ -695,7 +697,8 @@ test('Use shared schema and $ref with $id ($ref to $id)', t => {
|
|
|
695
697
|
t.same(res.json(), {
|
|
696
698
|
error: 'Bad Request',
|
|
697
699
|
message: "body must have required property 'address'",
|
|
698
|
-
statusCode: 400
|
|
700
|
+
statusCode: 400,
|
|
701
|
+
code: 'FST_ERR_VALIDATION'
|
|
699
702
|
})
|
|
700
703
|
})
|
|
701
704
|
})
|
|
@@ -811,7 +814,8 @@ test('Use $ref to /definitions', t => {
|
|
|
811
814
|
t.same(res.json(), {
|
|
812
815
|
error: 'Bad Request',
|
|
813
816
|
message: 'body/test/id must be number',
|
|
814
|
-
statusCode: 400
|
|
817
|
+
statusCode: 400,
|
|
818
|
+
code: 'FST_ERR_VALIDATION'
|
|
815
819
|
})
|
|
816
820
|
})
|
|
817
821
|
})
|
package/test/search.test.js
CHANGED
|
@@ -167,6 +167,7 @@ fastify.listen({ port: 0 }, err => {
|
|
|
167
167
|
t.equal(response.statusCode, 400)
|
|
168
168
|
t.same(JSON.parse(body), {
|
|
169
169
|
error: 'Bad Request',
|
|
170
|
+
code: 'FST_ERR_VALIDATION',
|
|
170
171
|
message: 'params/test must be integer',
|
|
171
172
|
statusCode: 400
|
|
172
173
|
})
|
|
@@ -196,6 +197,7 @@ fastify.listen({ port: 0 }, err => {
|
|
|
196
197
|
t.equal(response.statusCode, 400)
|
|
197
198
|
t.same(JSON.parse(body), {
|
|
198
199
|
error: 'Bad Request',
|
|
200
|
+
code: 'FST_ERR_VALIDATION',
|
|
199
201
|
message: 'querystring/hello must be integer',
|
|
200
202
|
statusCode: 400
|
|
201
203
|
})
|
|
@@ -231,6 +233,7 @@ fastify.listen({ port: 0 }, err => {
|
|
|
231
233
|
t.equal(response.headers['content-length'], '' + body.length)
|
|
232
234
|
t.same(JSON.parse(body), {
|
|
233
235
|
error: 'Bad Request',
|
|
236
|
+
code: 'FST_ERR_VALIDATION',
|
|
234
237
|
message: 'body/test must be integer',
|
|
235
238
|
statusCode: 400
|
|
236
239
|
})
|
|
@@ -295,6 +295,48 @@ server.route<RouteGenericInterface, CustomContextConfig>({
|
|
|
295
295
|
}
|
|
296
296
|
})
|
|
297
297
|
|
|
298
|
+
server.get<RouteGenericInterface, CustomContextConfig>('/', {
|
|
299
|
+
onRequest: async (request, reply) => {
|
|
300
|
+
expectType<CustomContextConfig>(request.context.config)
|
|
301
|
+
expectType<CustomContextConfig>(reply.context.config)
|
|
302
|
+
},
|
|
303
|
+
preParsing: async (request, reply) => {
|
|
304
|
+
expectType<CustomContextConfig>(request.context.config)
|
|
305
|
+
expectType<CustomContextConfig>(reply.context.config)
|
|
306
|
+
},
|
|
307
|
+
preValidation: async (request, reply) => {
|
|
308
|
+
expectType<CustomContextConfig>(request.context.config)
|
|
309
|
+
expectType<CustomContextConfig>(reply.context.config)
|
|
310
|
+
},
|
|
311
|
+
preHandler: async (request, reply) => {
|
|
312
|
+
expectType<CustomContextConfig>(request.context.config)
|
|
313
|
+
expectType<CustomContextConfig>(reply.context.config)
|
|
314
|
+
},
|
|
315
|
+
preSerialization: async (request, reply) => {
|
|
316
|
+
expectType<CustomContextConfig>(request.context.config)
|
|
317
|
+
expectType<CustomContextConfig>(reply.context.config)
|
|
318
|
+
},
|
|
319
|
+
onSend: async (request, reply) => {
|
|
320
|
+
expectType<CustomContextConfig>(request.context.config)
|
|
321
|
+
expectType<CustomContextConfig>(reply.context.config)
|
|
322
|
+
},
|
|
323
|
+
onResponse: async (request, reply) => {
|
|
324
|
+
expectType<CustomContextConfig>(request.context.config)
|
|
325
|
+
expectType<CustomContextConfig>(reply.context.config)
|
|
326
|
+
},
|
|
327
|
+
onTimeout: async (request, reply) => {
|
|
328
|
+
expectType<CustomContextConfig>(request.context.config)
|
|
329
|
+
expectType<CustomContextConfig>(reply.context.config)
|
|
330
|
+
},
|
|
331
|
+
onError: async (request, reply) => {
|
|
332
|
+
expectType<CustomContextConfig>(request.context.config)
|
|
333
|
+
expectType<CustomContextConfig>(reply.context.config)
|
|
334
|
+
}
|
|
335
|
+
}, async (request, reply) => {
|
|
336
|
+
expectType<CustomContextConfig>(request.context.config)
|
|
337
|
+
expectType<CustomContextConfig>(reply.context.config)
|
|
338
|
+
})
|
|
339
|
+
|
|
298
340
|
type CustomContextRequest = FastifyRequest<any, any, any, any, any, CustomContextConfig, any>
|
|
299
341
|
type CustomContextReply = FastifyReply<any, any, any, any, CustomContextConfig, any, any>
|
|
300
342
|
server.route<RouteGenericInterface, CustomContextConfig>({
|
|
@@ -57,6 +57,7 @@ test('should fail immediately with invalid payload', t => {
|
|
|
57
57
|
t.error(err)
|
|
58
58
|
t.same(res.json(), {
|
|
59
59
|
statusCode: 400,
|
|
60
|
+
code: 'FST_ERR_VALIDATION',
|
|
60
61
|
error: 'Bad Request',
|
|
61
62
|
message: "body must have required property 'name'"
|
|
62
63
|
})
|
|
@@ -244,6 +245,7 @@ test('should respect when attachValidation is explicitly set to false', t => {
|
|
|
244
245
|
t.error(err)
|
|
245
246
|
t.same(JSON.parse(res.payload), {
|
|
246
247
|
statusCode: 400,
|
|
248
|
+
code: 'FST_ERR_VALIDATION',
|
|
247
249
|
error: 'Bad Request',
|
|
248
250
|
message: "body must have required property 'name'"
|
|
249
251
|
})
|
|
@@ -368,7 +370,7 @@ test('should return a defined output message parsing AJV errors', t => {
|
|
|
368
370
|
url: '/'
|
|
369
371
|
}, (err, res) => {
|
|
370
372
|
t.error(err)
|
|
371
|
-
t.equal(res.payload, '{"statusCode":400,"error":"Bad Request","message":"body must have required property \'name\'"}')
|
|
373
|
+
t.equal(res.payload, '{"statusCode":400,"code":"FST_ERR_VALIDATION","error":"Bad Request","message":"body must have required property \'name\'"}')
|
|
372
374
|
})
|
|
373
375
|
})
|
|
374
376
|
|
|
@@ -398,7 +400,7 @@ test('should return a defined output message parsing JOI errors', t => {
|
|
|
398
400
|
url: '/'
|
|
399
401
|
}, (err, res) => {
|
|
400
402
|
t.error(err)
|
|
401
|
-
t.equal(res.payload, '{"statusCode":400,"error":"Bad Request","message":"\\"name\\" is required"}')
|
|
403
|
+
t.equal(res.payload, '{"statusCode":400,"code":"FST_ERR_VALIDATION","error":"Bad Request","message":"\\"name\\" is required"}')
|
|
402
404
|
})
|
|
403
405
|
})
|
|
404
406
|
|
|
@@ -431,7 +433,7 @@ test('should return a defined output message parsing JOI error details', t => {
|
|
|
431
433
|
url: '/'
|
|
432
434
|
}, (err, res) => {
|
|
433
435
|
t.error(err)
|
|
434
|
-
t.equal(res.payload, '{"statusCode":400,"error":"Bad Request","message":"body \\"name\\" is required"}')
|
|
436
|
+
t.equal(res.payload, '{"statusCode":400,"code":"FST_ERR_VALIDATION","error":"Bad Request","message":"body \\"name\\" is required"}')
|
|
435
437
|
})
|
|
436
438
|
})
|
|
437
439
|
|
|
@@ -457,6 +459,7 @@ test('the custom error formatter context must be the server instance', t => {
|
|
|
457
459
|
t.error(err)
|
|
458
460
|
t.same(res.json(), {
|
|
459
461
|
statusCode: 400,
|
|
462
|
+
code: 'FST_ERR_VALIDATION',
|
|
460
463
|
error: 'Bad Request',
|
|
461
464
|
message: 'my error'
|
|
462
465
|
})
|
|
@@ -486,6 +489,7 @@ test('the custom error formatter context must be the server instance in options'
|
|
|
486
489
|
t.error(err)
|
|
487
490
|
t.same(res.json(), {
|
|
488
491
|
statusCode: 400,
|
|
492
|
+
code: 'FST_ERR_VALIDATION',
|
|
489
493
|
error: 'Bad Request',
|
|
490
494
|
message: 'my error'
|
|
491
495
|
})
|
|
@@ -522,6 +526,7 @@ test('should call custom error formatter', t => {
|
|
|
522
526
|
t.error(err)
|
|
523
527
|
t.same(res.json(), {
|
|
524
528
|
statusCode: 400,
|
|
529
|
+
code: 'FST_ERR_VALIDATION',
|
|
525
530
|
error: 'Bad Request',
|
|
526
531
|
message: 'my error'
|
|
527
532
|
})
|
|
@@ -609,6 +614,7 @@ test('should register a route based schema error formatter', t => {
|
|
|
609
614
|
t.error(err)
|
|
610
615
|
t.same(res.json(), {
|
|
611
616
|
statusCode: 400,
|
|
617
|
+
code: 'FST_ERR_VALIDATION',
|
|
612
618
|
error: 'Bad Request',
|
|
613
619
|
message: 'abc'
|
|
614
620
|
})
|
|
@@ -652,6 +658,7 @@ test('prefer route based error formatter over global one', t => {
|
|
|
652
658
|
t.error(err)
|
|
653
659
|
t.same(res.json(), {
|
|
654
660
|
statusCode: 400,
|
|
661
|
+
code: 'FST_ERR_VALIDATION',
|
|
655
662
|
error: 'Bad Request',
|
|
656
663
|
message: '123'
|
|
657
664
|
})
|
|
@@ -668,6 +675,7 @@ test('prefer route based error formatter over global one', t => {
|
|
|
668
675
|
t.error(err)
|
|
669
676
|
t.same(res.json(), {
|
|
670
677
|
statusCode: 400,
|
|
678
|
+
code: 'FST_ERR_VALIDATION',
|
|
671
679
|
error: 'Bad Request',
|
|
672
680
|
message: 'abc'
|
|
673
681
|
})
|
|
@@ -684,6 +692,7 @@ test('prefer route based error formatter over global one', t => {
|
|
|
684
692
|
t.error(err)
|
|
685
693
|
t.same(res.json(), {
|
|
686
694
|
statusCode: 400,
|
|
695
|
+
code: 'FST_ERR_VALIDATION',
|
|
687
696
|
error: 'Bad Request',
|
|
688
697
|
message: 'abc123'
|
|
689
698
|
})
|
|
@@ -712,6 +721,7 @@ test('adding schemaErrorFormatter', t => {
|
|
|
712
721
|
t.error(err)
|
|
713
722
|
t.same(res.json(), {
|
|
714
723
|
statusCode: 400,
|
|
724
|
+
code: 'FST_ERR_VALIDATION',
|
|
715
725
|
error: 'Bad Request',
|
|
716
726
|
message: 'abc'
|
|
717
727
|
})
|
|
@@ -772,6 +782,7 @@ test('plugin override', t => {
|
|
|
772
782
|
t.error(err)
|
|
773
783
|
t.same(res.json(), {
|
|
774
784
|
statusCode: 400,
|
|
785
|
+
code: 'FST_ERR_VALIDATION',
|
|
775
786
|
error: 'Bad Request',
|
|
776
787
|
message: 'A'
|
|
777
788
|
})
|
|
@@ -788,6 +799,7 @@ test('plugin override', t => {
|
|
|
788
799
|
t.error(err)
|
|
789
800
|
t.same(res.json(), {
|
|
790
801
|
statusCode: 400,
|
|
802
|
+
code: 'FST_ERR_VALIDATION',
|
|
791
803
|
error: 'Bad Request',
|
|
792
804
|
message: 'B'
|
|
793
805
|
})
|
|
@@ -804,6 +816,7 @@ test('plugin override', t => {
|
|
|
804
816
|
t.error(err)
|
|
805
817
|
t.same(res.json(), {
|
|
806
818
|
statusCode: 400,
|
|
819
|
+
code: 'FST_ERR_VALIDATION',
|
|
807
820
|
error: 'Bad Request',
|
|
808
821
|
message: 'C'
|
|
809
822
|
})
|
|
@@ -820,6 +833,7 @@ test('plugin override', t => {
|
|
|
820
833
|
t.error(err)
|
|
821
834
|
t.same(res.json(), {
|
|
822
835
|
statusCode: 400,
|
|
836
|
+
code: 'FST_ERR_VALIDATION',
|
|
823
837
|
error: 'Bad Request',
|
|
824
838
|
message: 'D'
|
|
825
839
|
})
|
|
@@ -836,6 +850,7 @@ test('plugin override', t => {
|
|
|
836
850
|
t.error(err)
|
|
837
851
|
t.same(res.json(), {
|
|
838
852
|
statusCode: 400,
|
|
853
|
+
code: 'FST_ERR_VALIDATION',
|
|
839
854
|
error: 'Bad Request',
|
|
840
855
|
message: 'C'
|
|
841
856
|
})
|
package/types/route.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { FastifyRequest, RequestGenericInterface } from './request'
|
|
|
3
3
|
import { FastifyReply, ReplyGenericInterface } from './reply'
|
|
4
4
|
import { FastifySchema, FastifySchemaCompiler, FastifySerializerCompiler, SchemaErrorFormatter } from './schema'
|
|
5
5
|
import { HTTPMethods, RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
|
|
6
|
-
import { preValidationHookHandler, preHandlerHookHandler, preSerializationHookHandler, onRequestHookHandler, preParsingHookHandler, onResponseHookHandler, onSendHookHandler, onErrorHookHandler, onTimeoutHookHandler, onRequestAbortHookHandler
|
|
6
|
+
import { preValidationHookHandler, preHandlerHookHandler, preSerializationHookHandler, onRequestHookHandler, preParsingHookHandler, onResponseHookHandler, onSendHookHandler, onErrorHookHandler, onTimeoutHookHandler, onRequestAbortHookHandler } from './hooks'
|
|
7
7
|
import { FastifyError } from '@fastify/error'
|
|
8
8
|
import { FastifyContext } from './context'
|
|
9
9
|
import {
|
|
@@ -45,45 +45,25 @@ export interface RouteShorthandOptions<
|
|
|
45
45
|
|
|
46
46
|
// hooks
|
|
47
47
|
onRequest?: onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
|
|
48
|
-
| onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[]
|
|
49
|
-
| onRequestAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
|
|
50
|
-
| onRequestAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
|
|
48
|
+
| onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
|
|
51
49
|
preParsing?: preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
|
|
52
|
-
| preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[]
|
|
53
|
-
| preParsingAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
|
|
54
|
-
| preParsingAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
|
|
50
|
+
| preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
|
|
55
51
|
preValidation?: preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
|
|
56
|
-
| preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[]
|
|
57
|
-
| preValidationAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
|
|
58
|
-
| preValidationAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
|
|
52
|
+
| preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
|
|
59
53
|
preHandler?: preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
|
|
60
|
-
| preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[]
|
|
61
|
-
| preHandlerAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
|
|
62
|
-
| preHandlerAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
|
|
54
|
+
| preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
|
|
63
55
|
preSerialization?: preSerializationHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
|
|
64
|
-
| preSerializationHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[]
|
|
65
|
-
| preSerializationAsyncHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
|
|
66
|
-
| preSerializationAsyncHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
|
|
56
|
+
| preSerializationHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
|
|
67
57
|
onSend?: onSendHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
|
|
68
|
-
| onSendHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[]
|
|
69
|
-
| onSendAsyncHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
|
|
70
|
-
| onSendAsyncHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
|
|
58
|
+
| onSendHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
|
|
71
59
|
onResponse?: onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
|
|
72
|
-
| onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[]
|
|
73
|
-
| onResponseAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
|
|
74
|
-
| onResponseAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
|
|
60
|
+
| onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
|
|
75
61
|
onTimeout?: onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
|
|
76
|
-
| onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[]
|
|
77
|
-
| onTimeoutAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
|
|
78
|
-
| onTimeoutAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
|
|
62
|
+
| onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
|
|
79
63
|
onError?: onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, FastifyError, SchemaCompiler, TypeProvider, Logger>
|
|
80
|
-
| onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, FastifyError, SchemaCompiler, TypeProvider, Logger>[]
|
|
81
|
-
| onErrorAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, FastifyError, SchemaCompiler, TypeProvider, Logger>
|
|
82
|
-
| onErrorAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, FastifyError, SchemaCompiler, TypeProvider, Logger>[];
|
|
64
|
+
| onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, FastifyError, SchemaCompiler, TypeProvider, Logger>[];
|
|
83
65
|
onRequestAbort?: onRequestAbortHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
|
|
84
|
-
| onRequestAbortHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[]
|
|
85
|
-
| onRequestAbortAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
|
|
86
|
-
| onRequestAbortAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
|
|
66
|
+
| onRequestAbortHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
|
|
87
67
|
}
|
|
88
68
|
|
|
89
69
|
/**
|