fastify 5.0.0 → 5.2.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/.borp.yaml +3 -0
- package/.vscode/settings.json +22 -0
- package/README.md +12 -7
- package/docs/Guides/Database.md +15 -15
- package/docs/Guides/Detecting-When-Clients-Abort.md +28 -28
- package/docs/Guides/Ecosystem.md +14 -15
- package/docs/Guides/Index.md +1 -1
- package/docs/Guides/Migration-Guide-V4.md +11 -11
- package/docs/Guides/Migration-Guide-V5.md +133 -9
- package/docs/Guides/Plugins-Guide.md +1 -1
- package/docs/Guides/Prototype-Poisoning.md +3 -3
- package/docs/Guides/Recommendations.md +9 -9
- package/docs/Guides/Serverless.md +5 -5
- package/docs/Guides/Testing.md +58 -57
- package/docs/Guides/Write-Plugin.md +2 -2
- package/docs/Guides/Write-Type-Provider.md +3 -3
- package/docs/Reference/ContentTypeParser.md +4 -4
- package/docs/Reference/Decorators.md +2 -2
- package/docs/Reference/Errors.md +3 -3
- package/docs/Reference/Hooks.md +7 -7
- package/docs/Reference/LTS.md +8 -0
- package/docs/Reference/Logging.md +5 -4
- package/docs/Reference/Reply.md +55 -58
- package/docs/Reference/Request.md +49 -42
- package/docs/Reference/Routes.md +16 -13
- package/docs/Reference/Server.md +32 -28
- package/docs/Reference/TypeScript.md +9 -9
- package/docs/Reference/Validation-and-Serialization.md +5 -5
- package/examples/typescript-server.ts +1 -1
- package/fastify.d.ts +14 -5
- package/fastify.js +8 -6
- package/lib/contentTypeParser.js +9 -7
- package/lib/context.js +1 -2
- package/lib/error-handler.js +9 -9
- package/lib/errors.js +1 -1
- package/lib/fourOhFour.js +1 -1
- package/lib/hooks.js +4 -1
- package/lib/{logger.js → logger-factory.js} +70 -122
- package/lib/logger-pino.js +68 -0
- package/lib/pluginOverride.js +1 -1
- package/lib/pluginUtils.js +2 -2
- package/lib/reply.js +4 -5
- package/lib/request.js +16 -9
- package/lib/route.js +23 -22
- package/lib/validation.js +2 -2
- package/package.json +13 -15
- package/test/404s.test.js +675 -629
- package/test/500s.test.js +72 -63
- package/test/{allowUnsafeRegex.test.js → allow-unsafe-regex.test.js} +30 -26
- package/test/als.test.js +48 -45
- package/test/async-await.test.js +148 -134
- package/test/async-dispose.test.js +4 -5
- package/test/async_hooks.test.js +30 -28
- package/test/{bodyLimit.test.js → body-limit.test.js} +61 -58
- package/test/buffer.test.js +9 -10
- package/test/build/error-serializer.test.js +3 -4
- package/test/build/version.test.js +2 -3
- package/test/build-certificate.js +1 -1
- package/test/bundler/README.md +5 -5
- package/test/bundler/esbuild/bundler-test.js +10 -9
- package/test/bundler/webpack/bundler-test.js +10 -9
- package/test/case-insensitive.test.js +31 -28
- package/test/chainable.test.js +4 -5
- package/test/check.test.js +8 -10
- package/test/{childLoggerFactory.test.js → child-logger-factory.test.js} +56 -19
- package/test/client-timeout.test.js +5 -5
- package/test/close-pipelining.test.js +6 -8
- package/test/conditional-pino.test.js +47 -0
- package/test/{connectionTimeout.test.js → connection-timeout.test.js} +10 -11
- package/test/constrained-routes.test.js +243 -236
- package/test/content-length.test.js +53 -68
- package/test/content-parser.test.js +186 -158
- package/test/content-type.test.js +8 -9
- package/test/context-config.test.js +44 -54
- package/test/custom-http-server.test.js +16 -20
- package/test/custom-parser.5.test.js +32 -32
- package/test/diagnostics-channel/404.test.js +15 -15
- package/test/diagnostics-channel/async-delay-request.test.js +25 -25
- package/test/diagnostics-channel/async-request.test.js +24 -24
- package/test/diagnostics-channel/error-before-handler.test.js +4 -5
- package/test/diagnostics-channel/error-request.test.js +19 -19
- package/test/diagnostics-channel/error-status.test.js +8 -8
- package/test/diagnostics-channel/init.test.js +6 -7
- package/test/diagnostics-channel/sync-delay-request.test.js +16 -16
- package/test/diagnostics-channel/sync-request-reply.test.js +16 -16
- package/test/diagnostics-channel/sync-request.test.js +19 -19
- package/test/encapsulated-child-logger-factory.test.js +8 -8
- package/test/encapsulated-error-handler.test.js +20 -20
- package/test/esm/errorCodes.test.mjs +5 -5
- package/test/esm/esm.test.mjs +3 -3
- package/test/esm/named-exports.mjs +3 -3
- package/test/esm/other.mjs +2 -2
- package/test/fastify-instance.test.js +33 -34
- package/test/{findRoute.test.js → find-route.test.js} +11 -10
- package/test/fluent-schema.test.js +33 -36
- package/test/handler-context.test.js +11 -11
- package/test/has-route.test.js +12 -15
- package/test/header-overflow.test.js +13 -12
- package/test/hooks.on-ready.test.js +2 -2
- package/test/hooks.test.js +25 -25
- package/test/http-methods/copy.test.js +22 -24
- package/test/http-methods/custom-http-methods.test.js +24 -21
- package/test/http-methods/get.test.js +97 -84
- package/test/http-methods/head.test.js +63 -57
- package/test/http-methods/lock.test.js +21 -20
- package/test/http-methods/mkcalendar.test.js +31 -27
- package/test/http-methods/mkcol.test.js +10 -10
- package/test/http-methods/move.test.js +11 -11
- package/test/http-methods/propfind.test.js +32 -27
- package/test/http-methods/proppatch.test.js +21 -19
- package/test/http-methods/report.test.js +32 -27
- package/test/http-methods/search.test.js +52 -47
- package/test/http-methods/trace.test.js +3 -4
- package/test/http-methods/unlock.test.js +10 -10
- package/test/http2/closing.test.js +50 -58
- package/test/http2/constraint.test.js +47 -50
- package/test/http2/head.test.js +18 -19
- package/test/http2/missing-http2-module.test.js +4 -5
- package/test/http2/plain.test.js +31 -31
- package/test/http2/secure-with-fallback.test.js +61 -61
- package/test/http2/secure.test.js +28 -31
- package/test/http2/unknown-http-method.test.js +13 -14
- package/test/https/custom-https-server.test.js +6 -7
- package/test/https/https.test.js +78 -78
- package/test/imports.test.js +5 -6
- package/test/internals/all.test.js +8 -11
- package/test/internals/{contentTypeParser.test.js → content-type-parser.test.js} +5 -6
- package/test/internals/context.test.js +9 -11
- package/test/internals/decorator.test.js +20 -21
- package/test/internals/errors.test.js +427 -427
- package/test/internals/{handleRequest.test.js → handle-request.test.js} +53 -42
- package/test/internals/{hookRunner.test.js → hook-runner.test.js} +99 -100
- package/test/internals/hooks.test.js +31 -35
- package/test/internals/{initialConfig.test.js → initial-config.test.js} +92 -80
- package/test/internals/logger.test.js +28 -28
- package/test/internals/plugin.test.js +17 -18
- package/test/internals/reply-serialize.test.js +106 -106
- package/test/internals/reply.test.js +620 -585
- package/test/internals/{reqIdGenFactory.test.js → req-id-gen-factory.test.js} +31 -31
- package/test/internals/request-validate.test.js +218 -221
- package/test/internals/request.test.js +225 -107
- package/test/internals/server.test.js +15 -12
- package/test/internals/validation.test.js +35 -36
- package/test/{keepAliveTimeout.test.js → keep-alive-timeout.test.js} +9 -10
- package/test/listen.5.test.js +9 -9
- package/test/{maxRequestsPerSocket.test.js → max-requests-per-socket.test.js} +30 -30
- package/test/middleware.test.js +4 -5
- package/test/noop-set.test.js +5 -5
- package/test/post-empty-body.test.js +18 -11
- package/test/pretty-print.test.js +59 -49
- package/test/proto-poisoning.test.js +42 -37
- package/test/reply-code.test.js +34 -32
- package/test/{reply-earlyHints.test.js → reply-early-hints.test.js} +21 -19
- package/test/request-error.test.js +122 -0
- package/test/request-header-host.test.js +339 -0
- package/test/request-id.test.js +31 -25
- package/test/{requestTimeout.test.js → request-timeout.test.js} +11 -11
- package/test/route.1.test.js +79 -72
- package/test/route.2.test.js +17 -16
- package/test/route.3.test.js +32 -27
- package/test/route.4.test.js +21 -25
- package/test/route.5.test.js +45 -64
- package/test/route.6.test.js +70 -89
- package/test/route.7.test.js +61 -65
- package/test/route.8.test.js +80 -18
- package/test/router-options.test.js +80 -77
- package/test/same-shape.test.js +5 -5
- package/test/schema-examples.test.js +72 -38
- package/test/serialize-response.test.js +9 -10
- package/test/server.test.js +75 -78
- package/test/set-error-handler.test.js +2 -3
- package/test/stream-serializers.test.js +10 -7
- package/test/sync-routes.test.js +18 -18
- package/test/test-reporter.mjs +68 -0
- package/test/trust-proxy.test.js +51 -45
- package/test/type-provider.test.js +8 -6
- package/test/types/content-type-parser.test-d.ts +1 -1
- package/test/types/fastify.test-d.ts +16 -4
- package/test/types/hooks.test-d.ts +2 -1
- package/test/types/instance.test-d.ts +13 -13
- package/test/types/logger.test-d.ts +2 -2
- package/test/types/plugin.test-d.ts +17 -9
- package/test/types/register.test-d.ts +22 -6
- package/test/types/reply.test-d.ts +1 -1
- package/test/types/route.test-d.ts +34 -4
- package/test/types/serverFactory.test-d.ts +1 -1
- package/test/types/type-provider.test-d.ts +1 -1
- package/test/url-rewriting.test.js +35 -38
- package/test/{useSemicolonDelimiter.test.js → use-semicolon-delimiter.test.js} +30 -30
- package/test/validation-error-handling.test.js +259 -285
- package/test/versioned-routes.test.js +126 -113
- package/test/web-api.test.js +48 -37
- package/test/{wrapThenable.test.js → wrap-thenable.test.js} +10 -9
- package/types/hooks.d.ts +2 -1
- package/types/instance.d.ts +9 -2
- package/types/register.d.ts +12 -3
- package/types/reply.d.ts +1 -1
- package/types/request.d.ts +2 -6
- package/types/serverFactory.d.ts +3 -3
- package/types/utils.d.ts +13 -5
- package/test/types/import.js +0 -2
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { test } = require('
|
|
3
|
+
const { test } = require('node:test')
|
|
4
4
|
const Ajv = require('ajv')
|
|
5
5
|
const { kRequestCacheValidateFns, kRouteContext } = require('../../lib/symbols')
|
|
6
6
|
const Fastify = require('../../fastify')
|
|
@@ -44,10 +44,10 @@ const requestSchema = {
|
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
test('#compileValidationSchema', subtest => {
|
|
47
|
+
test('#compileValidationSchema', async subtest => {
|
|
48
48
|
subtest.plan(7)
|
|
49
49
|
|
|
50
|
-
subtest.test('Should return a function - Route without schema', async t => {
|
|
50
|
+
await subtest.test('Should return a function - Route without schema', async t => {
|
|
51
51
|
const fastify = Fastify()
|
|
52
52
|
|
|
53
53
|
t.plan(3)
|
|
@@ -55,9 +55,9 @@ test('#compileValidationSchema', subtest => {
|
|
|
55
55
|
fastify.get('/', (req, reply) => {
|
|
56
56
|
const validate = req.compileValidationSchema(defaultSchema)
|
|
57
57
|
|
|
58
|
-
t.
|
|
59
|
-
t.ok(validate({ hello: 'world' }))
|
|
60
|
-
t.
|
|
58
|
+
t.assert.ok(validate instanceof Function)
|
|
59
|
+
t.assert.ok(validate({ hello: 'world' }))
|
|
60
|
+
t.assert.ok(!validate({ world: 'foo' }))
|
|
61
61
|
|
|
62
62
|
reply.send({ hello: 'world' })
|
|
63
63
|
})
|
|
@@ -68,7 +68,7 @@ test('#compileValidationSchema', subtest => {
|
|
|
68
68
|
})
|
|
69
69
|
})
|
|
70
70
|
|
|
71
|
-
subtest.test('Validate function errors property should be null after validation when input is valid', async t => {
|
|
71
|
+
await subtest.test('Validate function errors property should be null after validation when input is valid', async t => {
|
|
72
72
|
const fastify = Fastify()
|
|
73
73
|
|
|
74
74
|
t.plan(3)
|
|
@@ -76,9 +76,9 @@ test('#compileValidationSchema', subtest => {
|
|
|
76
76
|
fastify.get('/', (req, reply) => {
|
|
77
77
|
const validate = req.compileValidationSchema(defaultSchema)
|
|
78
78
|
|
|
79
|
-
t.ok(validate({ hello: 'world' }))
|
|
80
|
-
t.ok(Object.hasOwn(validate, 'errors'))
|
|
81
|
-
t.
|
|
79
|
+
t.assert.ok(validate({ hello: 'world' }))
|
|
80
|
+
t.assert.ok(Object.hasOwn(validate, 'errors'))
|
|
81
|
+
t.assert.strictEqual(validate.errors, null)
|
|
82
82
|
|
|
83
83
|
reply.send({ hello: 'world' })
|
|
84
84
|
})
|
|
@@ -89,7 +89,7 @@ test('#compileValidationSchema', subtest => {
|
|
|
89
89
|
})
|
|
90
90
|
})
|
|
91
91
|
|
|
92
|
-
subtest.test('Validate function errors property should be an array of errors after validation when input is valid', async t => {
|
|
92
|
+
await subtest.test('Validate function errors property should be an array of errors after validation when input is valid', async t => {
|
|
93
93
|
const fastify = Fastify()
|
|
94
94
|
|
|
95
95
|
t.plan(4)
|
|
@@ -97,10 +97,10 @@ test('#compileValidationSchema', subtest => {
|
|
|
97
97
|
fastify.get('/', (req, reply) => {
|
|
98
98
|
const validate = req.compileValidationSchema(defaultSchema)
|
|
99
99
|
|
|
100
|
-
t.
|
|
101
|
-
t.ok(Object.hasOwn(validate, 'errors'))
|
|
102
|
-
t.ok(Array.isArray(validate.errors))
|
|
103
|
-
t.ok(validate.errors.length > 0)
|
|
100
|
+
t.assert.ok(!validate({ world: 'foo' }))
|
|
101
|
+
t.assert.ok(Object.hasOwn(validate, 'errors'))
|
|
102
|
+
t.assert.ok(Array.isArray(validate.errors))
|
|
103
|
+
t.assert.ok(validate.errors.length > 0)
|
|
104
104
|
|
|
105
105
|
reply.send({ hello: 'world' })
|
|
106
106
|
})
|
|
@@ -111,7 +111,7 @@ test('#compileValidationSchema', subtest => {
|
|
|
111
111
|
})
|
|
112
112
|
})
|
|
113
113
|
|
|
114
|
-
subtest.test(
|
|
114
|
+
await subtest.test(
|
|
115
115
|
'Should reuse the validate fn across multiple invocations - Route without schema',
|
|
116
116
|
async t => {
|
|
117
117
|
const fastify = Fastify()
|
|
@@ -124,15 +124,15 @@ test('#compileValidationSchema', subtest => {
|
|
|
124
124
|
counter++
|
|
125
125
|
if (counter > 1) {
|
|
126
126
|
const newValidate = req.compileValidationSchema(defaultSchema)
|
|
127
|
-
t.
|
|
127
|
+
t.assert.strictEqual(validate, newValidate, 'Are the same validate function')
|
|
128
128
|
validate = newValidate
|
|
129
129
|
} else {
|
|
130
130
|
validate = req.compileValidationSchema(defaultSchema)
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
-
t.
|
|
134
|
-
t.ok(validate({ hello: 'world' }))
|
|
135
|
-
t.
|
|
133
|
+
t.assert.ok(validate instanceof Function)
|
|
134
|
+
t.assert.ok(validate({ hello: 'world' }))
|
|
135
|
+
t.assert.ok(!validate({ world: 'foo' }))
|
|
136
136
|
|
|
137
137
|
reply.send({ hello: 'world' })
|
|
138
138
|
})
|
|
@@ -156,11 +156,11 @@ test('#compileValidationSchema', subtest => {
|
|
|
156
156
|
})
|
|
157
157
|
])
|
|
158
158
|
|
|
159
|
-
t.
|
|
159
|
+
t.assert.strictEqual(counter, 4)
|
|
160
160
|
}
|
|
161
161
|
)
|
|
162
162
|
|
|
163
|
-
subtest.test('Should return a function - Route with schema', async t => {
|
|
163
|
+
await subtest.test('Should return a function - Route with schema', async t => {
|
|
164
164
|
const fastify = Fastify()
|
|
165
165
|
|
|
166
166
|
t.plan(3)
|
|
@@ -175,9 +175,9 @@ test('#compileValidationSchema', subtest => {
|
|
|
175
175
|
(req, reply) => {
|
|
176
176
|
const validate = req.compileValidationSchema(defaultSchema)
|
|
177
177
|
|
|
178
|
-
t.
|
|
179
|
-
t.ok(validate({ hello: 'world' }))
|
|
180
|
-
t.
|
|
178
|
+
t.assert.ok(validate instanceof Function)
|
|
179
|
+
t.assert.ok(validate({ hello: 'world' }))
|
|
180
|
+
t.assert.ok(!validate({ world: 'foo' }))
|
|
181
181
|
|
|
182
182
|
reply.send({ hello: 'world' })
|
|
183
183
|
}
|
|
@@ -193,20 +193,20 @@ test('#compileValidationSchema', subtest => {
|
|
|
193
193
|
})
|
|
194
194
|
})
|
|
195
195
|
|
|
196
|
-
subtest.test(
|
|
196
|
+
await subtest.test(
|
|
197
197
|
'Should use the custom validator compiler for the route',
|
|
198
198
|
async t => {
|
|
199
199
|
const fastify = Fastify()
|
|
200
200
|
let called = 0
|
|
201
201
|
const custom = ({ schema, httpPart, url, method }) => {
|
|
202
|
-
t.
|
|
203
|
-
t.
|
|
204
|
-
t.
|
|
205
|
-
t.
|
|
202
|
+
t.assert.strictEqual(schema, defaultSchema)
|
|
203
|
+
t.assert.strictEqual(url, '/')
|
|
204
|
+
t.assert.strictEqual(method, 'GET')
|
|
205
|
+
t.assert.strictEqual(httpPart, 'querystring')
|
|
206
206
|
|
|
207
207
|
return input => {
|
|
208
208
|
called++
|
|
209
|
-
t.
|
|
209
|
+
t.assert.deepStrictEqual(input, { hello: 'world' })
|
|
210
210
|
return true
|
|
211
211
|
}
|
|
212
212
|
}
|
|
@@ -217,10 +217,10 @@ test('#compileValidationSchema', subtest => {
|
|
|
217
217
|
const first = req.compileValidationSchema(defaultSchema, 'querystring')
|
|
218
218
|
const second = req.compileValidationSchema(defaultSchema, 'querystring')
|
|
219
219
|
|
|
220
|
-
t.
|
|
221
|
-
t.ok(first({ hello: 'world' }))
|
|
222
|
-
t.ok(second({ hello: 'world' }))
|
|
223
|
-
t.
|
|
220
|
+
t.assert.strictEqual(first, second)
|
|
221
|
+
t.assert.ok(first({ hello: 'world' }))
|
|
222
|
+
t.assert.ok(second({ hello: 'world' }))
|
|
223
|
+
t.assert.strictEqual(called, 2)
|
|
224
224
|
|
|
225
225
|
reply.send({ hello: 'world' })
|
|
226
226
|
})
|
|
@@ -232,7 +232,7 @@ test('#compileValidationSchema', subtest => {
|
|
|
232
232
|
}
|
|
233
233
|
)
|
|
234
234
|
|
|
235
|
-
subtest.test(
|
|
235
|
+
await subtest.test(
|
|
236
236
|
'Should instantiate a WeakMap when executed for first time',
|
|
237
237
|
async t => {
|
|
238
238
|
const fastify = Fastify()
|
|
@@ -240,11 +240,11 @@ test('#compileValidationSchema', subtest => {
|
|
|
240
240
|
t.plan(5)
|
|
241
241
|
|
|
242
242
|
fastify.get('/', (req, reply) => {
|
|
243
|
-
t.
|
|
244
|
-
t.
|
|
245
|
-
t.
|
|
246
|
-
t.
|
|
247
|
-
t.
|
|
243
|
+
t.assert.strictEqual(req[kRouteContext][kRequestCacheValidateFns], null)
|
|
244
|
+
t.assert.ok(req.compileValidationSchema(defaultSchema) instanceof Function)
|
|
245
|
+
t.assert.ok(req[kRouteContext][kRequestCacheValidateFns] instanceof WeakMap)
|
|
246
|
+
t.assert.ok(req.compileValidationSchema(Object.assign({}, defaultSchema)) instanceof Function)
|
|
247
|
+
t.assert.ok(req[kRouteContext][kRequestCacheValidateFns] instanceof WeakMap)
|
|
248
248
|
|
|
249
249
|
reply.send({ hello: 'world' })
|
|
250
250
|
})
|
|
@@ -257,10 +257,10 @@ test('#compileValidationSchema', subtest => {
|
|
|
257
257
|
)
|
|
258
258
|
})
|
|
259
259
|
|
|
260
|
-
test('#getValidationFunction', subtest => {
|
|
260
|
+
test('#getValidationFunction', async subtest => {
|
|
261
261
|
subtest.plan(6)
|
|
262
262
|
|
|
263
|
-
subtest.test('Should return a validation function', async t => {
|
|
263
|
+
await subtest.test('Should return a validation function', async t => {
|
|
264
264
|
const fastify = Fastify()
|
|
265
265
|
|
|
266
266
|
t.plan(1)
|
|
@@ -269,7 +269,7 @@ test('#getValidationFunction', subtest => {
|
|
|
269
269
|
const original = req.compileValidationSchema(defaultSchema)
|
|
270
270
|
const referenced = req.getValidationFunction(defaultSchema)
|
|
271
271
|
|
|
272
|
-
t.
|
|
272
|
+
t.assert.strictEqual(original, referenced)
|
|
273
273
|
|
|
274
274
|
reply.send({ hello: 'world' })
|
|
275
275
|
})
|
|
@@ -280,7 +280,7 @@ test('#getValidationFunction', subtest => {
|
|
|
280
280
|
})
|
|
281
281
|
})
|
|
282
282
|
|
|
283
|
-
subtest.test('Validate function errors property should be null after validation when input is valid', async t => {
|
|
283
|
+
await subtest.test('Validate function errors property should be null after validation when input is valid', async t => {
|
|
284
284
|
const fastify = Fastify()
|
|
285
285
|
|
|
286
286
|
t.plan(3)
|
|
@@ -289,9 +289,9 @@ test('#getValidationFunction', subtest => {
|
|
|
289
289
|
req.compileValidationSchema(defaultSchema)
|
|
290
290
|
const validate = req.getValidationFunction(defaultSchema)
|
|
291
291
|
|
|
292
|
-
t.ok(validate({ hello: 'world' }))
|
|
293
|
-
t.ok(Object.hasOwn(validate, 'errors'))
|
|
294
|
-
t.
|
|
292
|
+
t.assert.ok(validate({ hello: 'world' }))
|
|
293
|
+
t.assert.ok(Object.hasOwn(validate, 'errors'))
|
|
294
|
+
t.assert.strictEqual(validate.errors, null)
|
|
295
295
|
|
|
296
296
|
reply.send({ hello: 'world' })
|
|
297
297
|
})
|
|
@@ -302,7 +302,7 @@ test('#getValidationFunction', subtest => {
|
|
|
302
302
|
})
|
|
303
303
|
})
|
|
304
304
|
|
|
305
|
-
subtest.test('Validate function errors property should be an array of errors after validation when input is valid', async t => {
|
|
305
|
+
await subtest.test('Validate function errors property should be an array of errors after validation when input is valid', async t => {
|
|
306
306
|
const fastify = Fastify()
|
|
307
307
|
|
|
308
308
|
t.plan(4)
|
|
@@ -311,10 +311,10 @@ test('#getValidationFunction', subtest => {
|
|
|
311
311
|
req.compileValidationSchema(defaultSchema)
|
|
312
312
|
const validate = req.getValidationFunction(defaultSchema)
|
|
313
313
|
|
|
314
|
-
t.
|
|
315
|
-
t.ok(Object.hasOwn(validate, 'errors'))
|
|
316
|
-
t.ok(Array.isArray(validate.errors))
|
|
317
|
-
t.ok(validate.errors.length > 0)
|
|
314
|
+
t.assert.ok(!validate({ world: 'foo' }))
|
|
315
|
+
t.assert.ok(Object.hasOwn(validate, 'errors'))
|
|
316
|
+
t.assert.ok(Array.isArray(validate.errors))
|
|
317
|
+
t.assert.ok(validate.errors.length > 0)
|
|
318
318
|
|
|
319
319
|
reply.send({ hello: 'world' })
|
|
320
320
|
})
|
|
@@ -325,17 +325,17 @@ test('#getValidationFunction', subtest => {
|
|
|
325
325
|
})
|
|
326
326
|
})
|
|
327
327
|
|
|
328
|
-
subtest.test('Should return undefined if no schema compiled', async t => {
|
|
328
|
+
await subtest.test('Should return undefined if no schema compiled', async t => {
|
|
329
329
|
const fastify = Fastify()
|
|
330
330
|
|
|
331
331
|
t.plan(2)
|
|
332
332
|
|
|
333
333
|
fastify.get('/', (req, reply) => {
|
|
334
334
|
const validate = req.getValidationFunction(defaultSchema)
|
|
335
|
-
t.
|
|
335
|
+
t.assert.ok(!validate)
|
|
336
336
|
|
|
337
337
|
const validateFn = req.getValidationFunction(42)
|
|
338
|
-
t.
|
|
338
|
+
t.assert.ok(!validateFn)
|
|
339
339
|
|
|
340
340
|
reply.send({ hello: 'world' })
|
|
341
341
|
})
|
|
@@ -343,7 +343,7 @@ test('#getValidationFunction', subtest => {
|
|
|
343
343
|
await fastify.inject('/')
|
|
344
344
|
})
|
|
345
345
|
|
|
346
|
-
subtest.test(
|
|
346
|
+
await subtest.test(
|
|
347
347
|
'Should return the validation function from each HTTP part',
|
|
348
348
|
async t => {
|
|
349
349
|
const fastify = Fastify()
|
|
@@ -363,39 +363,38 @@ test('#getValidationFunction', subtest => {
|
|
|
363
363
|
switch (params.id) {
|
|
364
364
|
case 1:
|
|
365
365
|
customValidation = req.compileValidationSchema(defaultSchema)
|
|
366
|
-
t.ok(req.getValidationFunction('body'))
|
|
367
|
-
t.ok(req.getValidationFunction('body')({ hello: 'world' }))
|
|
368
|
-
t.
|
|
366
|
+
t.assert.ok(req.getValidationFunction('body'))
|
|
367
|
+
t.assert.ok(req.getValidationFunction('body')({ hello: 'world' }))
|
|
368
|
+
t.assert.ok(!req.getValidationFunction('body')({ world: 'hello' }))
|
|
369
369
|
break
|
|
370
370
|
case 2:
|
|
371
371
|
headerValidation = req.getValidationFunction('headers')
|
|
372
|
-
t.ok(headerValidation)
|
|
373
|
-
t.ok(headerValidation({ 'x-foo': 'world' }))
|
|
374
|
-
t.
|
|
372
|
+
t.assert.ok(headerValidation)
|
|
373
|
+
t.assert.ok(headerValidation({ 'x-foo': 'world' }))
|
|
374
|
+
t.assert.ok(!headerValidation({ 'x-foo': [] }))
|
|
375
375
|
break
|
|
376
376
|
case 3:
|
|
377
|
-
t.ok(req.getValidationFunction('params'))
|
|
378
|
-
t.ok(req.getValidationFunction('params')({ id: 123 }))
|
|
379
|
-
t.
|
|
377
|
+
t.assert.ok(req.getValidationFunction('params'))
|
|
378
|
+
t.assert.ok(req.getValidationFunction('params')({ id: 123 }))
|
|
379
|
+
t.assert.ok(!req.getValidationFunction('params'({ id: 1.2 })))
|
|
380
380
|
break
|
|
381
381
|
case 4:
|
|
382
|
-
t.ok(req.getValidationFunction('querystring'))
|
|
383
|
-
t.ok(req.getValidationFunction('querystring')({ foo: 'bar' }))
|
|
384
|
-
t.
|
|
385
|
-
req.getValidationFunction('querystring')({ foo: 'not-bar' })
|
|
382
|
+
t.assert.ok(req.getValidationFunction('querystring'))
|
|
383
|
+
t.assert.ok(req.getValidationFunction('querystring')({ foo: 'bar' }))
|
|
384
|
+
t.assert.ok(!req.getValidationFunction('querystring')({ foo: 'not-bar' })
|
|
386
385
|
)
|
|
387
386
|
break
|
|
388
387
|
case 5:
|
|
389
|
-
t.
|
|
388
|
+
t.assert.strictEqual(
|
|
390
389
|
customValidation,
|
|
391
390
|
req.getValidationFunction(defaultSchema)
|
|
392
391
|
)
|
|
393
|
-
t.ok(customValidation({ hello: 'world' }))
|
|
394
|
-
t.
|
|
395
|
-
t.
|
|
392
|
+
t.assert.ok(customValidation({ hello: 'world' }))
|
|
393
|
+
t.assert.ok(!customValidation({}))
|
|
394
|
+
t.assert.strictEqual(headerValidation, req.getValidationFunction('headers'))
|
|
396
395
|
break
|
|
397
396
|
default:
|
|
398
|
-
t.fail('Invalid id')
|
|
397
|
+
t.assert.fail('Invalid id')
|
|
399
398
|
}
|
|
400
399
|
|
|
401
400
|
reply.send({ hello: 'world' })
|
|
@@ -424,7 +423,7 @@ test('#getValidationFunction', subtest => {
|
|
|
424
423
|
}
|
|
425
424
|
)
|
|
426
425
|
|
|
427
|
-
subtest.test('Should not set a WeakMap if there is no schema', async t => {
|
|
426
|
+
await subtest.test('Should not set a WeakMap if there is no schema', async t => {
|
|
428
427
|
const fastify = Fastify()
|
|
429
428
|
|
|
430
429
|
t.plan(1)
|
|
@@ -433,7 +432,7 @@ test('#getValidationFunction', subtest => {
|
|
|
433
432
|
req.getValidationFunction(defaultSchema)
|
|
434
433
|
req.getValidationFunction('body')
|
|
435
434
|
|
|
436
|
-
t.
|
|
435
|
+
t.assert.strictEqual(req[kRouteContext][kRequestCacheValidateFns], null)
|
|
437
436
|
reply.send({ hello: 'world' })
|
|
438
437
|
})
|
|
439
438
|
|
|
@@ -444,10 +443,10 @@ test('#getValidationFunction', subtest => {
|
|
|
444
443
|
})
|
|
445
444
|
})
|
|
446
445
|
|
|
447
|
-
test('#validate', subtest => {
|
|
446
|
+
test('#validate', async subtest => {
|
|
448
447
|
subtest.plan(7)
|
|
449
448
|
|
|
450
|
-
subtest.test(
|
|
449
|
+
await subtest.test(
|
|
451
450
|
'Should return true/false if input valid - Route without schema',
|
|
452
451
|
async t => {
|
|
453
452
|
const fastify = Fastify()
|
|
@@ -458,8 +457,8 @@ test('#validate', subtest => {
|
|
|
458
457
|
const isNotValid = req.validateInput({ world: 'string' }, defaultSchema)
|
|
459
458
|
const isValid = req.validateInput({ hello: 'string' }, defaultSchema)
|
|
460
459
|
|
|
461
|
-
t.
|
|
462
|
-
t.ok(isValid)
|
|
460
|
+
t.assert.ok(!isNotValid)
|
|
461
|
+
t.assert.ok(isValid)
|
|
463
462
|
|
|
464
463
|
reply.send({ hello: 'world' })
|
|
465
464
|
})
|
|
@@ -471,20 +470,20 @@ test('#validate', subtest => {
|
|
|
471
470
|
}
|
|
472
471
|
)
|
|
473
472
|
|
|
474
|
-
subtest.test(
|
|
473
|
+
await subtest.test(
|
|
475
474
|
'Should use the custom validator compiler for the route',
|
|
476
475
|
async t => {
|
|
477
476
|
const fastify = Fastify()
|
|
478
477
|
let called = 0
|
|
479
478
|
const custom = ({ schema, httpPart, url, method }) => {
|
|
480
|
-
t.
|
|
481
|
-
t.
|
|
482
|
-
t.
|
|
483
|
-
t.
|
|
479
|
+
t.assert.strictEqual(schema, defaultSchema)
|
|
480
|
+
t.assert.strictEqual(url, '/')
|
|
481
|
+
t.assert.strictEqual(method, 'GET')
|
|
482
|
+
t.assert.strictEqual(httpPart, 'querystring')
|
|
484
483
|
|
|
485
484
|
return input => {
|
|
486
485
|
called++
|
|
487
|
-
t.
|
|
486
|
+
t.assert.deepStrictEqual(input, { hello: 'world' })
|
|
488
487
|
return true
|
|
489
488
|
}
|
|
490
489
|
}
|
|
@@ -499,9 +498,9 @@ test('#validate', subtest => {
|
|
|
499
498
|
)
|
|
500
499
|
const ok2 = req.validateInput({ hello: 'world' }, defaultSchema)
|
|
501
500
|
|
|
502
|
-
t.ok(ok)
|
|
503
|
-
t.ok(ok2)
|
|
504
|
-
t.
|
|
501
|
+
t.assert.ok(ok)
|
|
502
|
+
t.assert.ok(ok2)
|
|
503
|
+
t.assert.strictEqual(called, 2)
|
|
505
504
|
|
|
506
505
|
reply.send({ hello: 'world' })
|
|
507
506
|
})
|
|
@@ -513,7 +512,7 @@ test('#validate', subtest => {
|
|
|
513
512
|
}
|
|
514
513
|
)
|
|
515
514
|
|
|
516
|
-
subtest.test(
|
|
515
|
+
await subtest.test(
|
|
517
516
|
'Should return true/false if input valid - With Schema for Route defined',
|
|
518
517
|
async t => {
|
|
519
518
|
const fastify = Fastify()
|
|
@@ -530,23 +529,23 @@ test('#validate', subtest => {
|
|
|
530
529
|
|
|
531
530
|
switch (params.id) {
|
|
532
531
|
case 1:
|
|
533
|
-
t.ok(req.validateInput({ hello: 'world' }, 'body'))
|
|
534
|
-
t.
|
|
532
|
+
t.assert.ok(req.validateInput({ hello: 'world' }, 'body'))
|
|
533
|
+
t.assert.ok(!req.validateInput({ hello: [], world: 'foo' }, 'body'))
|
|
535
534
|
break
|
|
536
535
|
case 2:
|
|
537
|
-
t.
|
|
538
|
-
t.ok(req.validateInput({ foo: 'bar' }, 'querystring'))
|
|
536
|
+
t.assert.ok(!req.validateInput({ foo: 'something' }, 'querystring'))
|
|
537
|
+
t.assert.ok(req.validateInput({ foo: 'bar' }, 'querystring'))
|
|
539
538
|
break
|
|
540
539
|
case 3:
|
|
541
|
-
t.
|
|
542
|
-
t.ok(req.validateInput({ 'x-foo': 'something' }, 'headers'))
|
|
540
|
+
t.assert.ok(!req.validateInput({ 'x-foo': [] }, 'headers'))
|
|
541
|
+
t.assert.ok(req.validateInput({ 'x-foo': 'something' }, 'headers'))
|
|
543
542
|
break
|
|
544
543
|
case 4:
|
|
545
|
-
t.ok(req.validateInput({ id: params.id }, 'params'))
|
|
546
|
-
t.
|
|
544
|
+
t.assert.ok(req.validateInput({ id: params.id }, 'params'))
|
|
545
|
+
t.assert.ok(!req.validateInput({ id: 0 }, 'params'))
|
|
547
546
|
break
|
|
548
547
|
default:
|
|
549
|
-
t.fail('Invalid id')
|
|
548
|
+
t.assert.fail('Invalid id')
|
|
550
549
|
}
|
|
551
550
|
|
|
552
551
|
reply.send({ hello: 'world' })
|
|
@@ -575,7 +574,7 @@ test('#validate', subtest => {
|
|
|
575
574
|
}
|
|
576
575
|
)
|
|
577
576
|
|
|
578
|
-
subtest.test(
|
|
577
|
+
await subtest.test(
|
|
579
578
|
'Should throw if missing validation fn for HTTP part and not schema provided',
|
|
580
579
|
async t => {
|
|
581
580
|
const fastify = Fastify()
|
|
@@ -602,7 +601,7 @@ test('#validate', subtest => {
|
|
|
602
601
|
req.validateInput({ id: 0 }, 'params')
|
|
603
602
|
break
|
|
604
603
|
default:
|
|
605
|
-
t.fail('Invalid id')
|
|
604
|
+
t.assert.fail('Invalid id')
|
|
606
605
|
}
|
|
607
606
|
})
|
|
608
607
|
|
|
@@ -614,8 +613,8 @@ test('#validate', subtest => {
|
|
|
614
613
|
const response = await fastify.inject(`/${j}`)
|
|
615
614
|
|
|
616
615
|
const result = response.json()
|
|
617
|
-
t.
|
|
618
|
-
t.
|
|
616
|
+
t.assert.strictEqual(result.statusCode, 500)
|
|
617
|
+
t.assert.strictEqual(result.code, 'FST_ERR_REQ_INVALID_VALIDATION_INVOCATION')
|
|
619
618
|
})(i)
|
|
620
619
|
)
|
|
621
620
|
}
|
|
@@ -624,7 +623,7 @@ test('#validate', subtest => {
|
|
|
624
623
|
}
|
|
625
624
|
)
|
|
626
625
|
|
|
627
|
-
subtest.test(
|
|
626
|
+
await subtest.test(
|
|
628
627
|
'Should throw if missing validation fn for HTTP part and not valid schema provided',
|
|
629
628
|
async t => {
|
|
630
629
|
const fastify = Fastify()
|
|
@@ -651,7 +650,7 @@ test('#validate', subtest => {
|
|
|
651
650
|
req.validateInput({ id: 0 }, () => {}, 'params')
|
|
652
651
|
break
|
|
653
652
|
default:
|
|
654
|
-
t.fail('Invalid id')
|
|
653
|
+
t.assert.fail('Invalid id')
|
|
655
654
|
}
|
|
656
655
|
})
|
|
657
656
|
|
|
@@ -666,8 +665,8 @@ test('#validate', subtest => {
|
|
|
666
665
|
})
|
|
667
666
|
|
|
668
667
|
const result = response.json()
|
|
669
|
-
t.
|
|
670
|
-
t.
|
|
668
|
+
t.assert.strictEqual(result.statusCode, 500)
|
|
669
|
+
t.assert.strictEqual(result.code, 'FST_ERR_REQ_INVALID_VALIDATION_INVOCATION')
|
|
671
670
|
})(i)
|
|
672
671
|
)
|
|
673
672
|
}
|
|
@@ -676,7 +675,7 @@ test('#validate', subtest => {
|
|
|
676
675
|
}
|
|
677
676
|
)
|
|
678
677
|
|
|
679
|
-
subtest.test('Should throw if invalid schema passed', async t => {
|
|
678
|
+
await subtest.test('Should throw if invalid schema passed', async t => {
|
|
680
679
|
const fastify = Fastify()
|
|
681
680
|
|
|
682
681
|
t.plan(10)
|
|
@@ -701,7 +700,7 @@ test('#validate', subtest => {
|
|
|
701
700
|
req.validateInput({ id: 0 }, () => {})
|
|
702
701
|
break
|
|
703
702
|
default:
|
|
704
|
-
t.fail('Invalid id')
|
|
703
|
+
t.assert.fail('Invalid id')
|
|
705
704
|
}
|
|
706
705
|
})
|
|
707
706
|
|
|
@@ -716,8 +715,8 @@ test('#validate', subtest => {
|
|
|
716
715
|
})
|
|
717
716
|
|
|
718
717
|
const result = response.json()
|
|
719
|
-
t.
|
|
720
|
-
t.
|
|
718
|
+
t.assert.strictEqual(result.statusCode, 500)
|
|
719
|
+
t.assert.strictEqual(result.code, 'FST_ERR_REQ_INVALID_VALIDATION_INVOCATION')
|
|
721
720
|
})(i)
|
|
722
721
|
)
|
|
723
722
|
}
|
|
@@ -725,7 +724,7 @@ test('#validate', subtest => {
|
|
|
725
724
|
await Promise.all(promises)
|
|
726
725
|
})
|
|
727
726
|
|
|
728
|
-
subtest.test(
|
|
727
|
+
await subtest.test(
|
|
729
728
|
'Should set a WeakMap if compiling the very first schema',
|
|
730
729
|
async t => {
|
|
731
730
|
const fastify = Fastify()
|
|
@@ -733,9 +732,9 @@ test('#validate', subtest => {
|
|
|
733
732
|
t.plan(3)
|
|
734
733
|
|
|
735
734
|
fastify.get('/', (req, reply) => {
|
|
736
|
-
t.
|
|
737
|
-
t.
|
|
738
|
-
t.
|
|
735
|
+
t.assert.strictEqual(req[kRouteContext][kRequestCacheValidateFns], null)
|
|
736
|
+
t.assert.strictEqual(req.validateInput({ hello: 'world' }, defaultSchema), true)
|
|
737
|
+
t.assert.ok(req[kRouteContext][kRequestCacheValidateFns] instanceof WeakMap)
|
|
739
738
|
|
|
740
739
|
reply.send({ hello: 'world' })
|
|
741
740
|
})
|
|
@@ -748,24 +747,24 @@ test('#validate', subtest => {
|
|
|
748
747
|
)
|
|
749
748
|
})
|
|
750
749
|
|
|
751
|
-
test('Nested Context', subtest => {
|
|
750
|
+
test('Nested Context', async subtest => {
|
|
752
751
|
subtest.plan(1)
|
|
753
752
|
|
|
754
|
-
subtest.test('Level_1', tst => {
|
|
753
|
+
await subtest.test('Level_1', async tst => {
|
|
755
754
|
tst.plan(3)
|
|
756
|
-
tst.test('#compileValidationSchema', ntst => {
|
|
755
|
+
await tst.test('#compileValidationSchema', async ntst => {
|
|
757
756
|
ntst.plan(5)
|
|
758
757
|
|
|
759
|
-
ntst.test('Should return a function - Route without schema', async t => {
|
|
758
|
+
await ntst.test('Should return a function - Route without schema', async t => {
|
|
760
759
|
const fastify = Fastify()
|
|
761
760
|
|
|
762
761
|
fastify.register((instance, opts, next) => {
|
|
763
762
|
instance.get('/', (req, reply) => {
|
|
764
763
|
const validate = req.compileValidationSchema(defaultSchema)
|
|
765
764
|
|
|
766
|
-
t.
|
|
767
|
-
t.ok(validate({ hello: 'world' }))
|
|
768
|
-
t.
|
|
765
|
+
t.assert.ok(validate, Function)
|
|
766
|
+
t.assert.ok(validate({ hello: 'world' }))
|
|
767
|
+
t.assert.ok(!validate({ world: 'foo' }))
|
|
769
768
|
|
|
770
769
|
reply.send({ hello: 'world' })
|
|
771
770
|
})
|
|
@@ -781,7 +780,7 @@ test('Nested Context', subtest => {
|
|
|
781
780
|
})
|
|
782
781
|
})
|
|
783
782
|
|
|
784
|
-
ntst.test(
|
|
783
|
+
await ntst.test(
|
|
785
784
|
'Should reuse the validate fn across multiple invocations - Route without schema',
|
|
786
785
|
async t => {
|
|
787
786
|
const fastify = Fastify()
|
|
@@ -795,15 +794,15 @@ test('Nested Context', subtest => {
|
|
|
795
794
|
counter++
|
|
796
795
|
if (counter > 1) {
|
|
797
796
|
const newValidate = req.compileValidationSchema(defaultSchema)
|
|
798
|
-
t.
|
|
797
|
+
t.assert.strictEqual(validate, newValidate, 'Are the same validate function')
|
|
799
798
|
validate = newValidate
|
|
800
799
|
} else {
|
|
801
800
|
validate = req.compileValidationSchema(defaultSchema)
|
|
802
801
|
}
|
|
803
802
|
|
|
804
|
-
t.
|
|
805
|
-
t.ok(validate({ hello: 'world' }))
|
|
806
|
-
t.
|
|
803
|
+
t.assert.ok(validate, Function)
|
|
804
|
+
t.assert.ok(validate({ hello: 'world' }))
|
|
805
|
+
t.assert.ok(!validate({ world: 'foo' }))
|
|
807
806
|
|
|
808
807
|
reply.send({ hello: 'world' })
|
|
809
808
|
})
|
|
@@ -818,11 +817,11 @@ test('Nested Context', subtest => {
|
|
|
818
817
|
fastify.inject('/')
|
|
819
818
|
])
|
|
820
819
|
|
|
821
|
-
t.
|
|
820
|
+
t.assert.strictEqual(counter, 4)
|
|
822
821
|
}
|
|
823
822
|
)
|
|
824
823
|
|
|
825
|
-
ntst.test('Should return a function - Route with schema', async t => {
|
|
824
|
+
await ntst.test('Should return a function - Route with schema', async t => {
|
|
826
825
|
const fastify = Fastify()
|
|
827
826
|
|
|
828
827
|
t.plan(3)
|
|
@@ -838,9 +837,9 @@ test('Nested Context', subtest => {
|
|
|
838
837
|
(req, reply) => {
|
|
839
838
|
const validate = req.compileValidationSchema(defaultSchema)
|
|
840
839
|
|
|
841
|
-
t.
|
|
842
|
-
t.ok(validate({ hello: 'world' }))
|
|
843
|
-
t.
|
|
840
|
+
t.assert.ok(validate, Function)
|
|
841
|
+
t.assert.ok(validate({ hello: 'world' }))
|
|
842
|
+
t.assert.ok(!validate({ world: 'foo' }))
|
|
844
843
|
|
|
845
844
|
reply.send({ hello: 'world' })
|
|
846
845
|
}
|
|
@@ -859,7 +858,7 @@ test('Nested Context', subtest => {
|
|
|
859
858
|
})
|
|
860
859
|
})
|
|
861
860
|
|
|
862
|
-
ntst.test(
|
|
861
|
+
await ntst.test(
|
|
863
862
|
'Should use the custom validator compiler for the route',
|
|
864
863
|
async t => {
|
|
865
864
|
const fastify = Fastify()
|
|
@@ -869,14 +868,14 @@ test('Nested Context', subtest => {
|
|
|
869
868
|
|
|
870
869
|
fastify.register((instance, opts, next) => {
|
|
871
870
|
const custom = ({ schema, httpPart, url, method }) => {
|
|
872
|
-
t.
|
|
873
|
-
t.
|
|
874
|
-
t.
|
|
875
|
-
t.
|
|
871
|
+
t.assert.strictEqual(schema, defaultSchema)
|
|
872
|
+
t.assert.strictEqual(url, '/')
|
|
873
|
+
t.assert.strictEqual(method, 'GET')
|
|
874
|
+
t.assert.strictEqual(httpPart, 'querystring')
|
|
876
875
|
|
|
877
876
|
return input => {
|
|
878
877
|
called++
|
|
879
|
-
t.
|
|
878
|
+
t.assert.deepStrictEqual(input, { hello: 'world' })
|
|
880
879
|
return true
|
|
881
880
|
}
|
|
882
881
|
}
|
|
@@ -891,10 +890,10 @@ test('Nested Context', subtest => {
|
|
|
891
890
|
'querystring'
|
|
892
891
|
)
|
|
893
892
|
|
|
894
|
-
t.
|
|
895
|
-
t.ok(first({ hello: 'world' }))
|
|
896
|
-
t.ok(second({ hello: 'world' }))
|
|
897
|
-
t.
|
|
893
|
+
t.assert.strictEqual(first, second)
|
|
894
|
+
t.assert.ok(first({ hello: 'world' }))
|
|
895
|
+
t.assert.ok(second({ hello: 'world' }))
|
|
896
|
+
t.assert.strictEqual(called, 2)
|
|
898
897
|
|
|
899
898
|
reply.send({ hello: 'world' })
|
|
900
899
|
})
|
|
@@ -906,7 +905,7 @@ test('Nested Context', subtest => {
|
|
|
906
905
|
}
|
|
907
906
|
)
|
|
908
907
|
|
|
909
|
-
ntst.test('Should compile the custom validation - nested with schema.headers', async t => {
|
|
908
|
+
await ntst.test('Should compile the custom validation - nested with schema.headers', async t => {
|
|
910
909
|
const fastify = Fastify()
|
|
911
910
|
let called = false
|
|
912
911
|
|
|
@@ -921,9 +920,9 @@ test('Nested Context', subtest => {
|
|
|
921
920
|
const custom = ({ schema, httpPart, url, method }) => {
|
|
922
921
|
if (called) return () => true
|
|
923
922
|
// only custom validators keep the same headers object
|
|
924
|
-
t.
|
|
925
|
-
t.
|
|
926
|
-
t.
|
|
923
|
+
t.assert.strictEqual(schema, schemaWithHeaders.headers)
|
|
924
|
+
t.assert.strictEqual(url, '/')
|
|
925
|
+
t.assert.strictEqual(httpPart, 'headers')
|
|
927
926
|
called = true
|
|
928
927
|
return () => true
|
|
929
928
|
}
|
|
@@ -934,7 +933,7 @@ test('Nested Context', subtest => {
|
|
|
934
933
|
|
|
935
934
|
fastify.register((instance, opts, next) => {
|
|
936
935
|
instance.get('/', { schema: schemaWithHeaders }, (req, reply) => {
|
|
937
|
-
t.
|
|
936
|
+
t.assert.strictEqual(called, true)
|
|
938
937
|
|
|
939
938
|
reply.send({ hello: 'world' })
|
|
940
939
|
})
|
|
@@ -946,10 +945,10 @@ test('Nested Context', subtest => {
|
|
|
946
945
|
})
|
|
947
946
|
})
|
|
948
947
|
|
|
949
|
-
tst.test('#getValidationFunction', ntst => {
|
|
948
|
+
await tst.test('#getValidationFunction', async ntst => {
|
|
950
949
|
ntst.plan(6)
|
|
951
950
|
|
|
952
|
-
ntst.test('Should return a validation function', async t => {
|
|
951
|
+
await ntst.test('Should return a validation function', async t => {
|
|
953
952
|
const fastify = Fastify()
|
|
954
953
|
|
|
955
954
|
t.plan(1)
|
|
@@ -959,7 +958,7 @@ test('Nested Context', subtest => {
|
|
|
959
958
|
const original = req.compileValidationSchema(defaultSchema)
|
|
960
959
|
const referenced = req.getValidationFunction(defaultSchema)
|
|
961
960
|
|
|
962
|
-
t.
|
|
961
|
+
t.assert.strictEqual(original, referenced)
|
|
963
962
|
|
|
964
963
|
reply.send({ hello: 'world' })
|
|
965
964
|
})
|
|
@@ -970,7 +969,7 @@ test('Nested Context', subtest => {
|
|
|
970
969
|
await fastify.inject('/')
|
|
971
970
|
})
|
|
972
971
|
|
|
973
|
-
ntst.test('Should return undefined if no schema compiled', async t => {
|
|
972
|
+
await ntst.test('Should return undefined if no schema compiled', async t => {
|
|
974
973
|
const fastify = Fastify()
|
|
975
974
|
|
|
976
975
|
t.plan(1)
|
|
@@ -979,7 +978,7 @@ test('Nested Context', subtest => {
|
|
|
979
978
|
instance.get('/', (req, reply) => {
|
|
980
979
|
const validate = req.getValidationFunction(defaultSchema)
|
|
981
980
|
|
|
982
|
-
t.
|
|
981
|
+
t.assert.ok(!validate)
|
|
983
982
|
|
|
984
983
|
reply.send({ hello: 'world' })
|
|
985
984
|
})
|
|
@@ -990,7 +989,7 @@ test('Nested Context', subtest => {
|
|
|
990
989
|
await fastify.inject('/')
|
|
991
990
|
})
|
|
992
991
|
|
|
993
|
-
ntst.test(
|
|
992
|
+
await ntst.test(
|
|
994
993
|
'Should return the validation function from each HTTP part',
|
|
995
994
|
async t => {
|
|
996
995
|
const fastify = Fastify()
|
|
@@ -1013,48 +1012,46 @@ test('Nested Context', subtest => {
|
|
|
1013
1012
|
customValidation = req.compileValidationSchema(
|
|
1014
1013
|
defaultSchema
|
|
1015
1014
|
)
|
|
1016
|
-
t.ok(req.getValidationFunction('body'))
|
|
1017
|
-
t.ok(req.getValidationFunction('body')({ hello: 'world' }))
|
|
1018
|
-
t.
|
|
1019
|
-
req.getValidationFunction('body')({ world: 'hello' })
|
|
1015
|
+
t.assert.ok(req.getValidationFunction('body'))
|
|
1016
|
+
t.assert.ok(req.getValidationFunction('body')({ hello: 'world' }))
|
|
1017
|
+
t.assert.ok(!req.getValidationFunction('body')({ world: 'hello' })
|
|
1020
1018
|
)
|
|
1021
1019
|
break
|
|
1022
1020
|
case 2:
|
|
1023
1021
|
headerValidation = req.getValidationFunction('headers')
|
|
1024
|
-
t.ok(headerValidation)
|
|
1025
|
-
t.ok(headerValidation({ 'x-foo': 'world' }))
|
|
1026
|
-
t.
|
|
1022
|
+
t.assert.ok(headerValidation)
|
|
1023
|
+
t.assert.ok(headerValidation({ 'x-foo': 'world' }))
|
|
1024
|
+
t.assert.ok(!headerValidation({ 'x-foo': [] }))
|
|
1027
1025
|
break
|
|
1028
1026
|
case 3:
|
|
1029
|
-
t.ok(req.getValidationFunction('params'))
|
|
1030
|
-
t.ok(req.getValidationFunction('params')({ id: 123 }))
|
|
1031
|
-
t.
|
|
1027
|
+
t.assert.ok(req.getValidationFunction('params'))
|
|
1028
|
+
t.assert.ok(req.getValidationFunction('params')({ id: 123 }))
|
|
1029
|
+
t.assert.ok(!req.getValidationFunction('params'({ id: 1.2 })))
|
|
1032
1030
|
break
|
|
1033
1031
|
case 4:
|
|
1034
|
-
t.ok(req.getValidationFunction('querystring'))
|
|
1035
|
-
t.ok(
|
|
1032
|
+
t.assert.ok(req.getValidationFunction('querystring'))
|
|
1033
|
+
t.assert.ok(
|
|
1036
1034
|
req.getValidationFunction('querystring')({ foo: 'bar' })
|
|
1037
1035
|
)
|
|
1038
|
-
t.
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
})
|
|
1036
|
+
t.assert.ok(!req.getValidationFunction('querystring')({
|
|
1037
|
+
foo: 'not-bar'
|
|
1038
|
+
})
|
|
1042
1039
|
)
|
|
1043
1040
|
break
|
|
1044
1041
|
case 5:
|
|
1045
|
-
t.
|
|
1042
|
+
t.assert.strictEqual(
|
|
1046
1043
|
customValidation,
|
|
1047
1044
|
req.getValidationFunction(defaultSchema)
|
|
1048
1045
|
)
|
|
1049
|
-
t.ok(customValidation({ hello: 'world' }))
|
|
1050
|
-
t.
|
|
1051
|
-
t.
|
|
1046
|
+
t.assert.ok(customValidation({ hello: 'world' }))
|
|
1047
|
+
t.assert.ok(!customValidation({}))
|
|
1048
|
+
t.assert.strictEqual(
|
|
1052
1049
|
headerValidation,
|
|
1053
1050
|
req.getValidationFunction('headers')
|
|
1054
1051
|
)
|
|
1055
1052
|
break
|
|
1056
1053
|
default:
|
|
1057
|
-
t.fail('Invalid id')
|
|
1054
|
+
t.assert.fail('Invalid id')
|
|
1058
1055
|
}
|
|
1059
1056
|
|
|
1060
1057
|
reply.send({ hello: 'world' })
|
|
@@ -1085,14 +1082,14 @@ test('Nested Context', subtest => {
|
|
|
1085
1082
|
}
|
|
1086
1083
|
)
|
|
1087
1084
|
|
|
1088
|
-
ntst.test('Should return a validation function - nested', async t => {
|
|
1085
|
+
await ntst.test('Should return a validation function - nested', async t => {
|
|
1089
1086
|
const fastify = Fastify()
|
|
1090
1087
|
let called = false
|
|
1091
1088
|
const custom = ({ schema, httpPart, url, method }) => {
|
|
1092
|
-
t.
|
|
1093
|
-
t.
|
|
1094
|
-
t.
|
|
1095
|
-
t.
|
|
1089
|
+
t.assert.strictEqual(schema, defaultSchema)
|
|
1090
|
+
t.assert.strictEqual(url, '/')
|
|
1091
|
+
t.assert.strictEqual(method, 'GET')
|
|
1092
|
+
t.assert.ok(!httpPart)
|
|
1096
1093
|
|
|
1097
1094
|
called = true
|
|
1098
1095
|
return () => true
|
|
@@ -1107,8 +1104,8 @@ test('Nested Context', subtest => {
|
|
|
1107
1104
|
const original = req.compileValidationSchema(defaultSchema)
|
|
1108
1105
|
const referenced = req.getValidationFunction(defaultSchema)
|
|
1109
1106
|
|
|
1110
|
-
t.
|
|
1111
|
-
t.
|
|
1107
|
+
t.assert.strictEqual(original, referenced)
|
|
1108
|
+
t.assert.strictEqual(called, true)
|
|
1112
1109
|
|
|
1113
1110
|
reply.send({ hello: 'world' })
|
|
1114
1111
|
})
|
|
@@ -1119,7 +1116,7 @@ test('Nested Context', subtest => {
|
|
|
1119
1116
|
await fastify.inject('/')
|
|
1120
1117
|
})
|
|
1121
1118
|
|
|
1122
|
-
ntst.test(
|
|
1119
|
+
await ntst.test(
|
|
1123
1120
|
'Should return undefined if no schema compiled - nested',
|
|
1124
1121
|
async t => {
|
|
1125
1122
|
const fastify = Fastify()
|
|
@@ -1136,7 +1133,7 @@ test('Nested Context', subtest => {
|
|
|
1136
1133
|
fastify.get('/', (req, reply) => {
|
|
1137
1134
|
const validate = req.compileValidationSchema(defaultSchema)
|
|
1138
1135
|
|
|
1139
|
-
t.
|
|
1136
|
+
t.assert.strictEqual(typeof validate, 'function')
|
|
1140
1137
|
|
|
1141
1138
|
reply.send({ hello: 'world' })
|
|
1142
1139
|
})
|
|
@@ -1146,8 +1143,8 @@ test('Nested Context', subtest => {
|
|
|
1146
1143
|
instance.get('/', (req, reply) => {
|
|
1147
1144
|
const validate = req.getValidationFunction(defaultSchema)
|
|
1148
1145
|
|
|
1149
|
-
t.
|
|
1150
|
-
t.
|
|
1146
|
+
t.assert.ok(!validate)
|
|
1147
|
+
t.assert.strictEqual(called, 1)
|
|
1151
1148
|
|
|
1152
1149
|
reply.send({ hello: 'world' })
|
|
1153
1150
|
})
|
|
@@ -1162,7 +1159,7 @@ test('Nested Context', subtest => {
|
|
|
1162
1159
|
}
|
|
1163
1160
|
)
|
|
1164
1161
|
|
|
1165
|
-
ntst.test('Should per-route defined validation compiler', async t => {
|
|
1162
|
+
await ntst.test('Should per-route defined validation compiler', async t => {
|
|
1166
1163
|
const fastify = Fastify()
|
|
1167
1164
|
let validateParent
|
|
1168
1165
|
let validateChild
|
|
@@ -1185,7 +1182,7 @@ test('Nested Context', subtest => {
|
|
|
1185
1182
|
fastify.get('/', (req, reply) => {
|
|
1186
1183
|
validateParent = req.compileValidationSchema(defaultSchema)
|
|
1187
1184
|
|
|
1188
|
-
t.
|
|
1185
|
+
t.assert.strictEqual(typeof validateParent, 'function')
|
|
1189
1186
|
|
|
1190
1187
|
reply.send({ hello: 'world' })
|
|
1191
1188
|
})
|
|
@@ -1201,10 +1198,10 @@ test('Nested Context', subtest => {
|
|
|
1201
1198
|
const validate1 = req.compileValidationSchema(defaultSchema)
|
|
1202
1199
|
validateChild = req.getValidationFunction(defaultSchema)
|
|
1203
1200
|
|
|
1204
|
-
t.
|
|
1205
|
-
t.
|
|
1206
|
-
t.
|
|
1207
|
-
t.
|
|
1201
|
+
t.assert.strictEqual(validate1, validateChild)
|
|
1202
|
+
t.assert.notStrictEqual(validateParent, validateChild)
|
|
1203
|
+
t.assert.strictEqual(calledParent, 1)
|
|
1204
|
+
t.assert.strictEqual(calledChild, 1)
|
|
1208
1205
|
|
|
1209
1206
|
reply.send({ hello: 'world' })
|
|
1210
1207
|
}
|
|
@@ -1220,10 +1217,10 @@ test('Nested Context', subtest => {
|
|
|
1220
1217
|
})
|
|
1221
1218
|
})
|
|
1222
1219
|
|
|
1223
|
-
tst.test('#validate', ntst => {
|
|
1220
|
+
await tst.test('#validate', async ntst => {
|
|
1224
1221
|
ntst.plan(3)
|
|
1225
1222
|
|
|
1226
|
-
ntst.test(
|
|
1223
|
+
await ntst.test(
|
|
1227
1224
|
'Should return true/false if input valid - Route without schema',
|
|
1228
1225
|
async t => {
|
|
1229
1226
|
const fastify = Fastify()
|
|
@@ -1238,8 +1235,8 @@ test('Nested Context', subtest => {
|
|
|
1238
1235
|
)
|
|
1239
1236
|
const isValid = req.validateInput({ hello: 'string' }, defaultSchema)
|
|
1240
1237
|
|
|
1241
|
-
t.
|
|
1242
|
-
t.ok(isValid)
|
|
1238
|
+
t.assert.ok(!isNotValid)
|
|
1239
|
+
t.assert.ok(isValid)
|
|
1243
1240
|
|
|
1244
1241
|
reply.send({ hello: 'world' })
|
|
1245
1242
|
})
|
|
@@ -1251,7 +1248,7 @@ test('Nested Context', subtest => {
|
|
|
1251
1248
|
}
|
|
1252
1249
|
)
|
|
1253
1250
|
|
|
1254
|
-
ntst.test(
|
|
1251
|
+
await ntst.test(
|
|
1255
1252
|
'Should use the custom validator compiler for the route',
|
|
1256
1253
|
async t => {
|
|
1257
1254
|
const fastify = Fastify()
|
|
@@ -1264,14 +1261,14 @@ test('Nested Context', subtest => {
|
|
|
1264
1261
|
}
|
|
1265
1262
|
|
|
1266
1263
|
const customChild = ({ schema, httpPart, url, method }) => {
|
|
1267
|
-
t.
|
|
1268
|
-
t.
|
|
1269
|
-
t.
|
|
1270
|
-
t.
|
|
1264
|
+
t.assert.strictEqual(schema, defaultSchema)
|
|
1265
|
+
t.assert.strictEqual(url, '/')
|
|
1266
|
+
t.assert.strictEqual(method, 'GET')
|
|
1267
|
+
t.assert.strictEqual(httpPart, 'querystring')
|
|
1271
1268
|
|
|
1272
1269
|
return input => {
|
|
1273
1270
|
childCalled++
|
|
1274
|
-
t.
|
|
1271
|
+
t.assert.deepStrictEqual(input, { hello: 'world' })
|
|
1275
1272
|
return true
|
|
1276
1273
|
}
|
|
1277
1274
|
}
|
|
@@ -1292,10 +1289,10 @@ test('Nested Context', subtest => {
|
|
|
1292
1289
|
)
|
|
1293
1290
|
const ok2 = req.validateInput({ hello: 'world' }, defaultSchema)
|
|
1294
1291
|
|
|
1295
|
-
t.ok(ok)
|
|
1296
|
-
t.ok(ok2)
|
|
1297
|
-
t.
|
|
1298
|
-
t.
|
|
1292
|
+
t.assert.ok(ok)
|
|
1293
|
+
t.assert.ok(ok2)
|
|
1294
|
+
t.assert.strictEqual(childCalled, 2)
|
|
1295
|
+
t.assert.strictEqual(parentCalled, 0)
|
|
1299
1296
|
|
|
1300
1297
|
reply.send({ hello: 'world' })
|
|
1301
1298
|
}
|
|
@@ -1308,7 +1305,7 @@ test('Nested Context', subtest => {
|
|
|
1308
1305
|
}
|
|
1309
1306
|
)
|
|
1310
1307
|
|
|
1311
|
-
ntst.test(
|
|
1308
|
+
await ntst.test(
|
|
1312
1309
|
'Should return true/false if input valid - With Schema for Route defined and scoped validator compiler',
|
|
1313
1310
|
async t => {
|
|
1314
1311
|
const validator = new Ajv()
|
|
@@ -1350,23 +1347,23 @@ test('Nested Context', subtest => {
|
|
|
1350
1347
|
|
|
1351
1348
|
switch (parseInt(params.id)) {
|
|
1352
1349
|
case 1:
|
|
1353
|
-
t.ok(req.validateInput({ hello: 'world' }, 'body'))
|
|
1354
|
-
t.
|
|
1350
|
+
t.assert.ok(req.validateInput({ hello: 'world' }, 'body'))
|
|
1351
|
+
t.assert.ok(!req.validateInput({ hello: [], world: 'foo' }, 'body'))
|
|
1355
1352
|
break
|
|
1356
1353
|
case 2:
|
|
1357
|
-
t.
|
|
1358
|
-
t.ok(req.validateInput({ foo: 'bar' }, 'querystring'))
|
|
1354
|
+
t.assert.ok(!req.validateInput({ foo: 'something' }, 'querystring'))
|
|
1355
|
+
t.assert.ok(req.validateInput({ foo: 'bar' }, 'querystring'))
|
|
1359
1356
|
break
|
|
1360
1357
|
case 3:
|
|
1361
|
-
t.
|
|
1362
|
-
t.ok(req.validateInput({ 'x-foo': 'something' }, 'headers'))
|
|
1358
|
+
t.assert.ok(!req.validateInput({ 'x-foo': [] }, 'headers'))
|
|
1359
|
+
t.assert.ok(req.validateInput({ 'x-foo': 'something' }, 'headers'))
|
|
1363
1360
|
break
|
|
1364
1361
|
case 4:
|
|
1365
|
-
t.ok(req.validateInput({ id: 1 }, 'params'))
|
|
1366
|
-
t.
|
|
1362
|
+
t.assert.ok(req.validateInput({ id: 1 }, 'params'))
|
|
1363
|
+
t.assert.ok(!req.validateInput({ id: params.id }, 'params'))
|
|
1367
1364
|
break
|
|
1368
1365
|
default:
|
|
1369
|
-
t.fail('Invalid id')
|
|
1366
|
+
t.assert.fail('Invalid id')
|
|
1370
1367
|
}
|
|
1371
1368
|
|
|
1372
1369
|
reply.send({ hello: 'world' })
|
|
@@ -1393,11 +1390,11 @@ test('Nested Context', subtest => {
|
|
|
1393
1390
|
|
|
1394
1391
|
await Promise.all(promises)
|
|
1395
1392
|
|
|
1396
|
-
t.
|
|
1397
|
-
t.
|
|
1398
|
-
t.
|
|
1399
|
-
t.
|
|
1400
|
-
t.
|
|
1393
|
+
t.assert.strictEqual(childCounter.query, 6) // 4 calls made + 2 custom validations
|
|
1394
|
+
t.assert.strictEqual(childCounter.headers, 6) // 4 calls made + 2 custom validations
|
|
1395
|
+
t.assert.strictEqual(childCounter.body, 6) // 4 calls made + 2 custom validations
|
|
1396
|
+
t.assert.strictEqual(childCounter.params, 6) // 4 calls made + 2 custom validations
|
|
1397
|
+
t.assert.strictEqual(parentCalled, 0)
|
|
1401
1398
|
}
|
|
1402
1399
|
)
|
|
1403
1400
|
})
|