node-fastify 5.8.3
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/.markdownlint-cli2.yaml +22 -0
- package/.prettierignore +1 -0
- package/GOVERNANCE.md +4 -0
- package/LICENSE +21 -0
- package/PROJECT_CHARTER.md +126 -0
- package/README.md +423 -0
- package/SECURITY.md +220 -0
- package/SPONSORS.md +24 -0
- package/build/build-error-serializer.js +35 -0
- package/build/build-validation.js +169 -0
- package/build/sync-version.js +11 -0
- package/docs/Guides/Benchmarking.md +60 -0
- package/docs/Guides/Database.md +321 -0
- package/docs/Guides/Delay-Accepting-Requests.md +608 -0
- package/docs/Guides/Detecting-When-Clients-Abort.md +172 -0
- package/docs/Guides/Ecosystem.md +726 -0
- package/docs/Guides/Fluent-Schema.md +127 -0
- package/docs/Guides/Getting-Started.md +620 -0
- package/docs/Guides/Index.md +43 -0
- package/docs/Guides/Migration-Guide-V3.md +287 -0
- package/docs/Guides/Migration-Guide-V4.md +267 -0
- package/docs/Guides/Migration-Guide-V5.md +727 -0
- package/docs/Guides/Plugins-Guide.md +520 -0
- package/docs/Guides/Prototype-Poisoning.md +383 -0
- package/docs/Guides/Recommendations.md +378 -0
- package/docs/Guides/Serverless.md +604 -0
- package/docs/Guides/Style-Guide.md +246 -0
- package/docs/Guides/Testing.md +481 -0
- package/docs/Guides/Write-Plugin.md +103 -0
- package/docs/Guides/Write-Type-Provider.md +34 -0
- package/docs/Reference/ContentTypeParser.md +271 -0
- package/docs/Reference/Decorators.md +436 -0
- package/docs/Reference/Encapsulation.md +194 -0
- package/docs/Reference/Errors.md +377 -0
- package/docs/Reference/HTTP2.md +94 -0
- package/docs/Reference/Hooks.md +958 -0
- package/docs/Reference/Index.md +73 -0
- package/docs/Reference/LTS.md +86 -0
- package/docs/Reference/Lifecycle.md +99 -0
- package/docs/Reference/Logging.md +268 -0
- package/docs/Reference/Middleware.md +79 -0
- package/docs/Reference/Plugins.md +245 -0
- package/docs/Reference/Principles.md +73 -0
- package/docs/Reference/Reply.md +1001 -0
- package/docs/Reference/Request.md +295 -0
- package/docs/Reference/Routes.md +802 -0
- package/docs/Reference/Server.md +2389 -0
- package/docs/Reference/Type-Providers.md +256 -0
- package/docs/Reference/TypeScript.md +1729 -0
- package/docs/Reference/Validation-and-Serialization.md +1130 -0
- package/docs/Reference/Warnings.md +58 -0
- package/docs/index.md +24 -0
- package/docs/resources/encapsulation_context.drawio +1 -0
- package/docs/resources/encapsulation_context.svg +3 -0
- package/eslint.config.js +35 -0
- package/examples/asyncawait.js +38 -0
- package/examples/benchmark/body.json +3 -0
- package/examples/benchmark/hooks-benchmark-async-await.js +44 -0
- package/examples/benchmark/hooks-benchmark.js +52 -0
- package/examples/benchmark/parser.js +47 -0
- package/examples/benchmark/simple.js +30 -0
- package/examples/benchmark/webstream.js +27 -0
- package/examples/hooks.js +91 -0
- package/examples/http2.js +39 -0
- package/examples/https.js +38 -0
- package/examples/parser.js +53 -0
- package/examples/plugin.js +12 -0
- package/examples/route-prefix.js +38 -0
- package/examples/shared-schema.js +38 -0
- package/examples/simple-stream.js +20 -0
- package/examples/simple.js +32 -0
- package/examples/simple.mjs +27 -0
- package/examples/typescript-server.ts +79 -0
- package/examples/use-plugin.js +29 -0
- package/fastify.d.ts +253 -0
- package/fastify.js +985 -0
- package/integration/server.js +29 -0
- package/integration/test.sh +23 -0
- package/lib/config-validator.js +1266 -0
- package/lib/content-type-parser.js +413 -0
- package/lib/content-type.js +160 -0
- package/lib/context.js +98 -0
- package/lib/decorate.js +152 -0
- package/lib/error-handler.js +173 -0
- package/lib/error-serializer.js +134 -0
- package/lib/error-status.js +14 -0
- package/lib/errors.js +516 -0
- package/lib/four-oh-four.js +190 -0
- package/lib/handle-request.js +195 -0
- package/lib/head-route.js +45 -0
- package/lib/hooks.js +429 -0
- package/lib/initial-config-validation.js +37 -0
- package/lib/logger-factory.js +136 -0
- package/lib/logger-pino.js +68 -0
- package/lib/noop-set.js +10 -0
- package/lib/plugin-override.js +90 -0
- package/lib/plugin-utils.js +169 -0
- package/lib/promise.js +23 -0
- package/lib/reply.js +1030 -0
- package/lib/req-id-gen-factory.js +52 -0
- package/lib/request.js +391 -0
- package/lib/route.js +686 -0
- package/lib/schema-controller.js +164 -0
- package/lib/schemas.js +207 -0
- package/lib/server.js +441 -0
- package/lib/symbols.js +71 -0
- package/lib/validation.js +280 -0
- package/lib/warnings.js +57 -0
- package/lib/wrap-thenable.js +84 -0
- package/package.json +225 -0
- package/scripts/validate-ecosystem-links.js +179 -0
- package/test/404s.test.js +2035 -0
- package/test/500s.test.js +422 -0
- package/test/allow-unsafe-regex.test.js +92 -0
- package/test/als.test.js +65 -0
- package/test/async-await.test.js +705 -0
- package/test/async-dispose.test.js +20 -0
- package/test/async_hooks.test.js +52 -0
- package/test/body-limit.test.js +224 -0
- package/test/buffer.test.js +74 -0
- package/test/build/error-serializer.test.js +36 -0
- package/test/build/version.test.js +14 -0
- package/test/build-certificate.js +109 -0
- package/test/bundler/README.md +29 -0
- package/test/bundler/esbuild/bundler-test.js +32 -0
- package/test/bundler/esbuild/package.json +10 -0
- package/test/bundler/esbuild/src/fail-plugin-version.js +14 -0
- package/test/bundler/esbuild/src/index.js +9 -0
- package/test/bundler/webpack/bundler-test.js +32 -0
- package/test/bundler/webpack/package.json +11 -0
- package/test/bundler/webpack/src/fail-plugin-version.js +14 -0
- package/test/bundler/webpack/src/index.js +9 -0
- package/test/bundler/webpack/webpack.config.js +15 -0
- package/test/case-insensitive.test.js +102 -0
- package/test/chainable.test.js +40 -0
- package/test/child-logger-factory.test.js +128 -0
- package/test/client-timeout.test.js +38 -0
- package/test/close-pipelining.test.js +78 -0
- package/test/close.test.js +706 -0
- package/test/conditional-pino.test.js +47 -0
- package/test/connection-timeout.test.js +42 -0
- package/test/constrained-routes.test.js +1138 -0
- package/test/content-length.test.js +174 -0
- package/test/content-parser.test.js +739 -0
- package/test/content-type.test.js +181 -0
- package/test/context-config.test.js +164 -0
- package/test/custom-http-server.test.js +118 -0
- package/test/custom-parser-async.test.js +59 -0
- package/test/custom-parser.0.test.js +701 -0
- package/test/custom-parser.1.test.js +266 -0
- package/test/custom-parser.2.test.js +91 -0
- package/test/custom-parser.3.test.js +208 -0
- package/test/custom-parser.4.test.js +218 -0
- package/test/custom-parser.5.test.js +130 -0
- package/test/custom-querystring-parser.test.js +129 -0
- package/test/decorator.test.js +1330 -0
- package/test/delete.test.js +344 -0
- package/test/diagnostics-channel/404.test.js +49 -0
- package/test/diagnostics-channel/async-delay-request.test.js +65 -0
- package/test/diagnostics-channel/async-request.test.js +64 -0
- package/test/diagnostics-channel/error-before-handler.test.js +35 -0
- package/test/diagnostics-channel/error-request.test.js +53 -0
- package/test/diagnostics-channel/error-status.test.js +123 -0
- package/test/diagnostics-channel/init.test.js +50 -0
- package/test/diagnostics-channel/sync-delay-request.test.js +49 -0
- package/test/diagnostics-channel/sync-request-reply.test.js +51 -0
- package/test/diagnostics-channel/sync-request.test.js +54 -0
- package/test/encapsulated-child-logger-factory.test.js +69 -0
- package/test/encapsulated-error-handler.test.js +237 -0
- package/test/esm/errorCodes.test.mjs +10 -0
- package/test/esm/esm.test.mjs +13 -0
- package/test/esm/index.test.js +8 -0
- package/test/esm/named-exports.mjs +14 -0
- package/test/esm/other.mjs +8 -0
- package/test/esm/plugin.mjs +8 -0
- package/test/fastify-instance.test.js +300 -0
- package/test/find-route.test.js +152 -0
- package/test/fluent-schema.test.js +209 -0
- package/test/genReqId.test.js +426 -0
- package/test/handler-context.test.js +45 -0
- package/test/handler-timeout.test.js +367 -0
- package/test/has-route.test.js +88 -0
- package/test/header-overflow.test.js +55 -0
- package/test/helper.js +496 -0
- package/test/hooks-async.test.js +1099 -0
- package/test/hooks.on-listen.test.js +1162 -0
- package/test/hooks.on-ready.test.js +421 -0
- package/test/hooks.test.js +3578 -0
- package/test/http-methods/copy.test.js +35 -0
- package/test/http-methods/custom-http-methods.test.js +114 -0
- package/test/http-methods/get.test.js +412 -0
- package/test/http-methods/head.test.js +263 -0
- package/test/http-methods/lock.test.js +108 -0
- package/test/http-methods/mkcalendar.test.js +143 -0
- package/test/http-methods/mkcol.test.js +35 -0
- package/test/http-methods/move.test.js +42 -0
- package/test/http-methods/propfind.test.js +136 -0
- package/test/http-methods/proppatch.test.js +105 -0
- package/test/http-methods/report.test.js +142 -0
- package/test/http-methods/search.test.js +233 -0
- package/test/http-methods/trace.test.js +21 -0
- package/test/http-methods/unlock.test.js +38 -0
- package/test/http2/closing.test.js +270 -0
- package/test/http2/constraint.test.js +109 -0
- package/test/http2/head.test.js +34 -0
- package/test/http2/plain.test.js +68 -0
- package/test/http2/secure-with-fallback.test.js +113 -0
- package/test/http2/secure.test.js +67 -0
- package/test/http2/unknown-http-method.test.js +34 -0
- package/test/https/custom-https-server.test.js +58 -0
- package/test/https/https.test.js +136 -0
- package/test/imports.test.js +17 -0
- package/test/inject.test.js +502 -0
- package/test/input-validation.js +335 -0
- package/test/internals/all.test.js +38 -0
- package/test/internals/content-type-parser.test.js +111 -0
- package/test/internals/context.test.js +31 -0
- package/test/internals/decorator.test.js +156 -0
- package/test/internals/errors.test.js +982 -0
- package/test/internals/handle-request.test.js +270 -0
- package/test/internals/hook-runner.test.js +449 -0
- package/test/internals/hooks.test.js +96 -0
- package/test/internals/initial-config.test.js +383 -0
- package/test/internals/logger.test.js +163 -0
- package/test/internals/plugin.test.js +170 -0
- package/test/internals/promise.test.js +63 -0
- package/test/internals/reply-serialize.test.js +714 -0
- package/test/internals/reply.test.js +1920 -0
- package/test/internals/req-id-gen-factory.test.js +133 -0
- package/test/internals/request-validate.test.js +1402 -0
- package/test/internals/request.test.js +506 -0
- package/test/internals/schema-controller-perf.test.js +40 -0
- package/test/internals/server.test.js +91 -0
- package/test/internals/validation.test.js +352 -0
- package/test/issue-4959.test.js +118 -0
- package/test/keep-alive-timeout.test.js +42 -0
- package/test/listen.1.test.js +154 -0
- package/test/listen.2.test.js +113 -0
- package/test/listen.3.test.js +83 -0
- package/test/listen.4.test.js +168 -0
- package/test/listen.5.test.js +122 -0
- package/test/logger/instantiation.test.js +341 -0
- package/test/logger/logger-test-utils.js +47 -0
- package/test/logger/logging.test.js +460 -0
- package/test/logger/options.test.js +579 -0
- package/test/logger/request.test.js +292 -0
- package/test/logger/response.test.js +183 -0
- package/test/logger/tap-parallel-not-ok +0 -0
- package/test/max-requests-per-socket.test.js +113 -0
- package/test/middleware.test.js +37 -0
- package/test/noop-set.test.js +19 -0
- package/test/nullable-validation.test.js +187 -0
- package/test/options.error-handler.test.js +5 -0
- package/test/options.test.js +5 -0
- package/test/output-validation.test.js +140 -0
- package/test/patch.error-handler.test.js +5 -0
- package/test/patch.test.js +5 -0
- package/test/plugin.1.test.js +230 -0
- package/test/plugin.2.test.js +314 -0
- package/test/plugin.3.test.js +287 -0
- package/test/plugin.4.test.js +504 -0
- package/test/plugin.helper.js +8 -0
- package/test/plugin.name.display.js +10 -0
- package/test/post-empty-body.test.js +38 -0
- package/test/pretty-print.test.js +366 -0
- package/test/promises.test.js +125 -0
- package/test/proto-poisoning.test.js +145 -0
- package/test/put.error-handler.test.js +5 -0
- package/test/put.test.js +5 -0
- package/test/register.test.js +184 -0
- package/test/reply-code.test.js +148 -0
- package/test/reply-early-hints.test.js +100 -0
- package/test/reply-error.test.js +815 -0
- package/test/reply-trailers.test.js +445 -0
- package/test/reply-web-stream-locked.test.js +37 -0
- package/test/request-error.test.js +624 -0
- package/test/request-header-host.test.js +339 -0
- package/test/request-id.test.js +118 -0
- package/test/request-timeout.test.js +53 -0
- package/test/route-hooks.test.js +635 -0
- package/test/route-prefix.test.js +904 -0
- package/test/route-shorthand.test.js +48 -0
- package/test/route.1.test.js +259 -0
- package/test/route.2.test.js +100 -0
- package/test/route.3.test.js +213 -0
- package/test/route.4.test.js +127 -0
- package/test/route.5.test.js +211 -0
- package/test/route.6.test.js +306 -0
- package/test/route.7.test.js +406 -0
- package/test/route.8.test.js +225 -0
- package/test/router-options.test.js +1108 -0
- package/test/same-shape.test.js +124 -0
- package/test/schema-examples.test.js +661 -0
- package/test/schema-feature.test.js +2198 -0
- package/test/schema-serialization.test.js +1171 -0
- package/test/schema-special-usage.test.js +1348 -0
- package/test/schema-validation.test.js +1572 -0
- package/test/scripts/validate-ecosystem-links.test.js +339 -0
- package/test/serialize-response.test.js +186 -0
- package/test/server.test.js +347 -0
- package/test/set-error-handler.test.js +69 -0
- package/test/skip-reply-send.test.js +317 -0
- package/test/stream-serializers.test.js +40 -0
- package/test/stream.1.test.js +94 -0
- package/test/stream.2.test.js +129 -0
- package/test/stream.3.test.js +198 -0
- package/test/stream.4.test.js +176 -0
- package/test/stream.5.test.js +188 -0
- package/test/sync-routes.test.js +32 -0
- package/test/throw.test.js +359 -0
- package/test/toolkit.js +63 -0
- package/test/trust-proxy.test.js +162 -0
- package/test/type-provider.test.js +22 -0
- package/test/types/content-type-parser.test-d.ts +72 -0
- package/test/types/decorate-request-reply.test-d.ts +18 -0
- package/test/types/dummy-plugin.ts +9 -0
- package/test/types/errors.test-d.ts +90 -0
- package/test/types/fastify.test-d.ts +352 -0
- package/test/types/hooks.test-d.ts +550 -0
- package/test/types/import.ts +2 -0
- package/test/types/instance.test-d.ts +588 -0
- package/test/types/logger.test-d.ts +277 -0
- package/test/types/plugin.test-d.ts +97 -0
- package/test/types/register.test-d.ts +237 -0
- package/test/types/reply.test-d.ts +254 -0
- package/test/types/request.test-d.ts +188 -0
- package/test/types/route.test-d.ts +553 -0
- package/test/types/schema.test-d.ts +135 -0
- package/test/types/serverFactory.test-d.ts +37 -0
- package/test/types/type-provider.test-d.ts +1213 -0
- package/test/types/using.test-d.ts +17 -0
- package/test/upgrade.test.js +52 -0
- package/test/url-rewriting.test.js +122 -0
- package/test/use-semicolon-delimiter.test.js +168 -0
- package/test/validation-error-handling.test.js +900 -0
- package/test/versioned-routes.test.js +603 -0
- package/test/web-api.test.js +616 -0
- package/test/wrap-thenable.test.js +30 -0
- package/types/content-type-parser.d.ts +75 -0
- package/types/context.d.ts +22 -0
- package/types/errors.d.ts +92 -0
- package/types/hooks.d.ts +875 -0
- package/types/instance.d.ts +609 -0
- package/types/logger.d.ts +107 -0
- package/types/plugin.d.ts +44 -0
- package/types/register.d.ts +42 -0
- package/types/reply.d.ts +81 -0
- package/types/request.d.ts +95 -0
- package/types/route.d.ts +199 -0
- package/types/schema.d.ts +61 -0
- package/types/server-factory.d.ts +19 -0
- package/types/type-provider.d.ts +130 -0
- package/types/utils.d.ts +98 -0
|
@@ -0,0 +1,2198 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('node:test')
|
|
4
|
+
const Fastify = require('..')
|
|
5
|
+
const fp = require('fastify-plugin')
|
|
6
|
+
const deepClone = require('rfdc')({ circles: true, proto: false })
|
|
7
|
+
const Ajv = require('ajv')
|
|
8
|
+
const { kSchemaController } = require('../lib/symbols.js')
|
|
9
|
+
const { FSTWRN001 } = require('../lib/warnings')
|
|
10
|
+
const { waitForCb } = require('./toolkit')
|
|
11
|
+
|
|
12
|
+
const echoParams = (req, reply) => { reply.send(req.params) }
|
|
13
|
+
const echoBody = (req, reply) => { reply.send(req.body) }
|
|
14
|
+
|
|
15
|
+
;['addSchema', 'getSchema', 'getSchemas', 'setValidatorCompiler', 'setSerializerCompiler'].forEach(f => {
|
|
16
|
+
test(`Should expose ${f} function`, t => {
|
|
17
|
+
t.plan(1)
|
|
18
|
+
const fastify = Fastify()
|
|
19
|
+
t.assert.strictEqual(typeof fastify[f], 'function')
|
|
20
|
+
})
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
;['setValidatorCompiler', 'setSerializerCompiler'].forEach(f => {
|
|
24
|
+
test(`cannot call ${f} after binding`, (t, testDone) => {
|
|
25
|
+
t.plan(2)
|
|
26
|
+
const fastify = Fastify()
|
|
27
|
+
t.after(() => fastify.close())
|
|
28
|
+
fastify.listen({ port: 0 }, err => {
|
|
29
|
+
t.assert.ifError(err)
|
|
30
|
+
try {
|
|
31
|
+
fastify[f](() => { })
|
|
32
|
+
t.assert.fail()
|
|
33
|
+
} catch (e) {
|
|
34
|
+
t.assert.ok(true)
|
|
35
|
+
testDone()
|
|
36
|
+
}
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
test('The schemas should be added to an internal storage', t => {
|
|
42
|
+
t.plan(1)
|
|
43
|
+
const fastify = Fastify()
|
|
44
|
+
const schema = { $id: 'id', my: 'schema' }
|
|
45
|
+
fastify.addSchema(schema)
|
|
46
|
+
t.assert.deepStrictEqual(fastify[kSchemaController].schemaBucket.store, { id: schema })
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
test('The schemas should be accessible via getSchemas', t => {
|
|
50
|
+
t.plan(1)
|
|
51
|
+
const fastify = Fastify()
|
|
52
|
+
|
|
53
|
+
const schemas = {
|
|
54
|
+
id: { $id: 'id', my: 'schema' },
|
|
55
|
+
abc: { $id: 'abc', my: 'schema' },
|
|
56
|
+
bcd: { $id: 'bcd', my: 'schema', properties: { a: 'a', b: 1 } }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
Object.values(schemas).forEach(schema => { fastify.addSchema(schema) })
|
|
60
|
+
t.assert.deepStrictEqual(fastify.getSchemas(), schemas)
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
test('The schema should be accessible by id via getSchema', (t, testDone) => {
|
|
64
|
+
t.plan(5)
|
|
65
|
+
const fastify = Fastify()
|
|
66
|
+
|
|
67
|
+
const schemas = [
|
|
68
|
+
{ $id: 'id', my: 'schema' },
|
|
69
|
+
{ $id: 'abc', my: 'schema' },
|
|
70
|
+
{ $id: 'bcd', my: 'schema', properties: { a: 'a', b: 1 } }
|
|
71
|
+
]
|
|
72
|
+
schemas.forEach(schema => { fastify.addSchema(schema) })
|
|
73
|
+
t.assert.deepStrictEqual(fastify.getSchema('abc'), schemas[1])
|
|
74
|
+
t.assert.deepStrictEqual(fastify.getSchema('id'), schemas[0])
|
|
75
|
+
t.assert.deepStrictEqual(fastify.getSchema('foo'), undefined)
|
|
76
|
+
|
|
77
|
+
fastify.register((instance, opts, done) => {
|
|
78
|
+
const pluginSchema = { $id: 'cde', my: 'schema' }
|
|
79
|
+
instance.addSchema(pluginSchema)
|
|
80
|
+
t.assert.deepStrictEqual(instance.getSchema('cde'), pluginSchema)
|
|
81
|
+
done()
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
fastify.ready(err => {
|
|
85
|
+
t.assert.ifError(err)
|
|
86
|
+
testDone()
|
|
87
|
+
})
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test('Get validatorCompiler after setValidatorCompiler', (t, testDone) => {
|
|
91
|
+
t.plan(2)
|
|
92
|
+
const myCompiler = () => { }
|
|
93
|
+
const fastify = Fastify()
|
|
94
|
+
fastify.setValidatorCompiler(myCompiler)
|
|
95
|
+
const sc = fastify.validatorCompiler
|
|
96
|
+
t.assert.ok(Object.is(myCompiler, sc))
|
|
97
|
+
fastify.ready(err => {
|
|
98
|
+
t.assert.ifError(err)
|
|
99
|
+
testDone()
|
|
100
|
+
})
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
test('Get serializerCompiler after setSerializerCompiler', (t, testDone) => {
|
|
104
|
+
t.plan(2)
|
|
105
|
+
const myCompiler = () => { }
|
|
106
|
+
const fastify = Fastify()
|
|
107
|
+
fastify.setSerializerCompiler(myCompiler)
|
|
108
|
+
const sc = fastify.serializerCompiler
|
|
109
|
+
t.assert.ok(Object.is(myCompiler, sc))
|
|
110
|
+
fastify.ready(err => {
|
|
111
|
+
t.assert.ifError(err)
|
|
112
|
+
testDone()
|
|
113
|
+
})
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
test('Get compilers is empty when settle on routes', (t, testDone) => {
|
|
117
|
+
t.plan(3)
|
|
118
|
+
|
|
119
|
+
const fastify = Fastify()
|
|
120
|
+
|
|
121
|
+
fastify.post('/', {
|
|
122
|
+
schema: {
|
|
123
|
+
body: { type: 'object', properties: { hello: { type: 'string' } } },
|
|
124
|
+
response: {
|
|
125
|
+
'2xx': {
|
|
126
|
+
type: 'object',
|
|
127
|
+
properties: {
|
|
128
|
+
foo: { type: 'array', items: { type: 'string' } }
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
validatorCompiler: ({ schema, method, url, httpPart }) => { },
|
|
134
|
+
serializerCompiler: ({ schema, method, url, httpPart }) => { }
|
|
135
|
+
}, function (req, reply) {
|
|
136
|
+
reply.send('ok')
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
fastify.inject({
|
|
140
|
+
method: 'POST',
|
|
141
|
+
payload: {},
|
|
142
|
+
url: '/'
|
|
143
|
+
}, (err, res) => {
|
|
144
|
+
t.assert.ifError(err)
|
|
145
|
+
t.assert.strictEqual(fastify.validatorCompiler, undefined)
|
|
146
|
+
t.assert.strictEqual(fastify.serializerCompiler, undefined)
|
|
147
|
+
testDone()
|
|
148
|
+
})
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
test('Should throw if the $id property is missing', t => {
|
|
152
|
+
t.plan(1)
|
|
153
|
+
const fastify = Fastify()
|
|
154
|
+
try {
|
|
155
|
+
fastify.addSchema({ type: 'string' })
|
|
156
|
+
t.assert.fail()
|
|
157
|
+
} catch (err) {
|
|
158
|
+
t.assert.strictEqual(err.code, 'FST_ERR_SCH_MISSING_ID')
|
|
159
|
+
}
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
test('Cannot add multiple times the same id', t => {
|
|
163
|
+
t.plan(2)
|
|
164
|
+
const fastify = Fastify()
|
|
165
|
+
|
|
166
|
+
fastify.addSchema({ $id: 'id' })
|
|
167
|
+
try {
|
|
168
|
+
fastify.addSchema({ $id: 'id' })
|
|
169
|
+
} catch (err) {
|
|
170
|
+
t.assert.strictEqual(err.code, 'FST_ERR_SCH_ALREADY_PRESENT')
|
|
171
|
+
t.assert.strictEqual(err.message, 'Schema with id \'id\' already declared!')
|
|
172
|
+
}
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
test('Cannot add schema for query and querystring', (t, testDone) => {
|
|
176
|
+
t.plan(2)
|
|
177
|
+
const fastify = Fastify()
|
|
178
|
+
|
|
179
|
+
fastify.get('/', {
|
|
180
|
+
handler: () => { },
|
|
181
|
+
schema: {
|
|
182
|
+
query: {
|
|
183
|
+
type: 'object',
|
|
184
|
+
properties: {
|
|
185
|
+
foo: { type: 'string' }
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
querystring: {
|
|
189
|
+
type: 'object',
|
|
190
|
+
properties: {
|
|
191
|
+
foo: { type: 'string' }
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
fastify.ready(err => {
|
|
198
|
+
t.assert.strictEqual(err.code, 'FST_ERR_SCH_DUPLICATE')
|
|
199
|
+
t.assert.strictEqual(err.message, 'Schema with \'querystring\' already present!')
|
|
200
|
+
testDone()
|
|
201
|
+
})
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
test('Should throw of the schema does not exists in input', (t, testDone) => {
|
|
205
|
+
t.plan(2)
|
|
206
|
+
const fastify = Fastify()
|
|
207
|
+
|
|
208
|
+
fastify.get('/:id', {
|
|
209
|
+
handler: echoParams,
|
|
210
|
+
schema: {
|
|
211
|
+
params: {
|
|
212
|
+
type: 'object',
|
|
213
|
+
properties: {
|
|
214
|
+
name: { $ref: '#notExist' }
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
fastify.ready(err => {
|
|
221
|
+
t.assert.strictEqual(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
|
|
222
|
+
t.assert.strictEqual(err.message, "Failed building the validation schema for GET: /:id, due to error can't resolve reference #notExist from id #")
|
|
223
|
+
testDone()
|
|
224
|
+
})
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
test('Should throw if schema is missing for content type', (t, testDone) => {
|
|
228
|
+
t.plan(2)
|
|
229
|
+
|
|
230
|
+
const fastify = Fastify()
|
|
231
|
+
fastify.post('/', {
|
|
232
|
+
handler: echoBody,
|
|
233
|
+
schema: {
|
|
234
|
+
body: {
|
|
235
|
+
content: {
|
|
236
|
+
'application/json': {}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
fastify.ready(err => {
|
|
243
|
+
t.assert.strictEqual(err.code, 'FST_ERR_SCH_CONTENT_MISSING_SCHEMA')
|
|
244
|
+
t.assert.strictEqual(err.message, "Schema is missing for the content type 'application/json'")
|
|
245
|
+
testDone()
|
|
246
|
+
})
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
test('Should throw of the schema does not exists in output', (t, testDone) => {
|
|
250
|
+
t.plan(2)
|
|
251
|
+
const fastify = Fastify()
|
|
252
|
+
|
|
253
|
+
fastify.get('/:id', {
|
|
254
|
+
handler: echoParams,
|
|
255
|
+
schema: {
|
|
256
|
+
response: {
|
|
257
|
+
'2xx': {
|
|
258
|
+
type: 'object',
|
|
259
|
+
properties: {
|
|
260
|
+
name: { $ref: '#notExist' }
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
})
|
|
266
|
+
|
|
267
|
+
fastify.ready(err => {
|
|
268
|
+
t.assert.strictEqual(err.code, 'FST_ERR_SCH_SERIALIZATION_BUILD')
|
|
269
|
+
t.assert.match(err.message, /^Failed building the serialization schema for GET: \/:id, due to error Cannot find reference.*/) // error from fast-json-stringify
|
|
270
|
+
testDone()
|
|
271
|
+
})
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
test('Should not change the input schemas', (t, testDone) => {
|
|
275
|
+
t.plan(4)
|
|
276
|
+
|
|
277
|
+
const theSchema = {
|
|
278
|
+
$id: 'helloSchema',
|
|
279
|
+
type: 'object',
|
|
280
|
+
definitions: {
|
|
281
|
+
hello: { type: 'string' }
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const fastify = Fastify()
|
|
286
|
+
fastify.post('/', {
|
|
287
|
+
handler: echoBody,
|
|
288
|
+
schema: {
|
|
289
|
+
body: {
|
|
290
|
+
type: 'object',
|
|
291
|
+
additionalProperties: false,
|
|
292
|
+
properties: {
|
|
293
|
+
name: { $ref: 'helloSchema#/definitions/hello' }
|
|
294
|
+
}
|
|
295
|
+
},
|
|
296
|
+
response: {
|
|
297
|
+
'2xx': {
|
|
298
|
+
type: 'object',
|
|
299
|
+
properties: {
|
|
300
|
+
name: { $ref: 'helloSchema#/definitions/hello' }
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
})
|
|
306
|
+
fastify.addSchema(theSchema)
|
|
307
|
+
|
|
308
|
+
fastify.inject({
|
|
309
|
+
url: '/',
|
|
310
|
+
method: 'POST',
|
|
311
|
+
payload: { name: 'Foo', surname: 'Bar' }
|
|
312
|
+
}, (err, res) => {
|
|
313
|
+
t.assert.ifError(err)
|
|
314
|
+
t.assert.deepStrictEqual(res.json(), { name: 'Foo' })
|
|
315
|
+
t.assert.ok(theSchema.$id, 'the $id is not removed')
|
|
316
|
+
t.assert.deepStrictEqual(fastify.getSchema('helloSchema'), theSchema)
|
|
317
|
+
testDone()
|
|
318
|
+
})
|
|
319
|
+
})
|
|
320
|
+
|
|
321
|
+
test('Should emit warning if the schema headers is undefined', (t, testDone) => {
|
|
322
|
+
t.plan(4)
|
|
323
|
+
const fastify = Fastify()
|
|
324
|
+
|
|
325
|
+
process.on('warning', onWarning)
|
|
326
|
+
function onWarning (warning) {
|
|
327
|
+
t.assert.strictEqual(warning.name, 'FastifyWarning')
|
|
328
|
+
t.assert.strictEqual(warning.code, FSTWRN001.code)
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
t.after(() => {
|
|
332
|
+
process.removeListener('warning', onWarning)
|
|
333
|
+
FSTWRN001.emitted = false
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
fastify.post('/:id', {
|
|
337
|
+
handler: echoParams,
|
|
338
|
+
schema: {
|
|
339
|
+
headers: undefined
|
|
340
|
+
}
|
|
341
|
+
})
|
|
342
|
+
|
|
343
|
+
fastify.inject({
|
|
344
|
+
method: 'POST',
|
|
345
|
+
url: '/123'
|
|
346
|
+
}, (error, res) => {
|
|
347
|
+
t.assert.ifError(error)
|
|
348
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
349
|
+
testDone()
|
|
350
|
+
})
|
|
351
|
+
})
|
|
352
|
+
|
|
353
|
+
test('Should emit warning if the schema body is undefined', (t, testDone) => {
|
|
354
|
+
t.plan(4)
|
|
355
|
+
const fastify = Fastify()
|
|
356
|
+
|
|
357
|
+
process.on('warning', onWarning)
|
|
358
|
+
function onWarning (warning) {
|
|
359
|
+
t.assert.strictEqual(warning.name, 'FastifyWarning')
|
|
360
|
+
t.assert.strictEqual(warning.code, FSTWRN001.code)
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
t.after(() => {
|
|
364
|
+
process.removeListener('warning', onWarning)
|
|
365
|
+
FSTWRN001.emitted = false
|
|
366
|
+
})
|
|
367
|
+
|
|
368
|
+
fastify.post('/:id', {
|
|
369
|
+
handler: echoParams,
|
|
370
|
+
schema: {
|
|
371
|
+
body: undefined
|
|
372
|
+
}
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
fastify.inject({
|
|
376
|
+
method: 'POST',
|
|
377
|
+
url: '/123'
|
|
378
|
+
}, (error, res) => {
|
|
379
|
+
t.assert.ifError(error)
|
|
380
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
381
|
+
testDone()
|
|
382
|
+
})
|
|
383
|
+
})
|
|
384
|
+
|
|
385
|
+
test('Should emit warning if the schema query is undefined', (t, testDone) => {
|
|
386
|
+
t.plan(4)
|
|
387
|
+
const fastify = Fastify()
|
|
388
|
+
|
|
389
|
+
process.on('warning', onWarning)
|
|
390
|
+
function onWarning (warning) {
|
|
391
|
+
t.assert.strictEqual(warning.name, 'FastifyWarning')
|
|
392
|
+
t.assert.strictEqual(warning.code, FSTWRN001.code)
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
t.after(() => {
|
|
396
|
+
process.removeListener('warning', onWarning)
|
|
397
|
+
FSTWRN001.emitted = false
|
|
398
|
+
})
|
|
399
|
+
|
|
400
|
+
fastify.post('/:id', {
|
|
401
|
+
handler: echoParams,
|
|
402
|
+
schema: {
|
|
403
|
+
querystring: undefined
|
|
404
|
+
}
|
|
405
|
+
})
|
|
406
|
+
|
|
407
|
+
fastify.inject({
|
|
408
|
+
method: 'POST',
|
|
409
|
+
url: '/123'
|
|
410
|
+
}, (error, res) => {
|
|
411
|
+
t.assert.ifError(error)
|
|
412
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
413
|
+
testDone()
|
|
414
|
+
})
|
|
415
|
+
})
|
|
416
|
+
|
|
417
|
+
test('Should emit warning if the schema params is undefined', (t, testDone) => {
|
|
418
|
+
t.plan(4)
|
|
419
|
+
const fastify = Fastify()
|
|
420
|
+
|
|
421
|
+
process.on('warning', onWarning)
|
|
422
|
+
function onWarning (warning) {
|
|
423
|
+
t.assert.strictEqual(warning.name, 'FastifyWarning')
|
|
424
|
+
t.assert.strictEqual(warning.code, FSTWRN001.code)
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
t.after(() => {
|
|
428
|
+
process.removeListener('warning', onWarning)
|
|
429
|
+
FSTWRN001.emitted = false
|
|
430
|
+
})
|
|
431
|
+
|
|
432
|
+
fastify.post('/:id', {
|
|
433
|
+
handler: echoParams,
|
|
434
|
+
schema: {
|
|
435
|
+
params: undefined
|
|
436
|
+
}
|
|
437
|
+
})
|
|
438
|
+
|
|
439
|
+
fastify.inject({
|
|
440
|
+
method: 'POST',
|
|
441
|
+
url: '/123'
|
|
442
|
+
}, (error, res) => {
|
|
443
|
+
t.assert.ifError(error)
|
|
444
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
445
|
+
testDone()
|
|
446
|
+
})
|
|
447
|
+
})
|
|
448
|
+
|
|
449
|
+
test('Should emit a warning for every route with undefined schema', (t, testDone) => {
|
|
450
|
+
t.plan(16)
|
|
451
|
+
const fastify = Fastify()
|
|
452
|
+
|
|
453
|
+
let runs = 0
|
|
454
|
+
const expectedWarningEmitted = [0, 1, 2, 3]
|
|
455
|
+
// It emits 4 warnings:
|
|
456
|
+
// - 2 - GET and HEAD for /undefinedParams/:id
|
|
457
|
+
// - 2 - GET and HEAD for /undefinedBody/:id
|
|
458
|
+
// => 3 x 4 assertions = 12 assertions
|
|
459
|
+
function onWarning (warning) {
|
|
460
|
+
t.assert.strictEqual(warning.name, 'FastifyWarning')
|
|
461
|
+
t.assert.strictEqual(warning.code, FSTWRN001.code)
|
|
462
|
+
t.assert.strictEqual(runs++, expectedWarningEmitted.shift())
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
process.on('warning', onWarning)
|
|
466
|
+
t.after(() => {
|
|
467
|
+
process.removeListener('warning', onWarning)
|
|
468
|
+
FSTWRN001.emitted = false
|
|
469
|
+
})
|
|
470
|
+
|
|
471
|
+
fastify.get('/undefinedParams/:id', {
|
|
472
|
+
handler: echoParams,
|
|
473
|
+
schema: {
|
|
474
|
+
params: undefined
|
|
475
|
+
}
|
|
476
|
+
})
|
|
477
|
+
|
|
478
|
+
fastify.get('/undefinedBody/:id', {
|
|
479
|
+
handler: echoParams,
|
|
480
|
+
schema: {
|
|
481
|
+
body: undefined
|
|
482
|
+
}
|
|
483
|
+
})
|
|
484
|
+
|
|
485
|
+
fastify.inject({
|
|
486
|
+
method: 'GET',
|
|
487
|
+
url: '/undefinedParams/123'
|
|
488
|
+
}, (error, res) => {
|
|
489
|
+
t.assert.ifError(error)
|
|
490
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
491
|
+
})
|
|
492
|
+
|
|
493
|
+
fastify.inject({
|
|
494
|
+
method: 'GET',
|
|
495
|
+
url: '/undefinedBody/123'
|
|
496
|
+
}, (error, res) => {
|
|
497
|
+
t.assert.ifError(error)
|
|
498
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
499
|
+
testDone()
|
|
500
|
+
})
|
|
501
|
+
})
|
|
502
|
+
|
|
503
|
+
test('First level $ref', (t, testDone) => {
|
|
504
|
+
t.plan(2)
|
|
505
|
+
const fastify = Fastify()
|
|
506
|
+
|
|
507
|
+
fastify.addSchema({
|
|
508
|
+
$id: 'test',
|
|
509
|
+
type: 'object',
|
|
510
|
+
properties: {
|
|
511
|
+
id: { type: 'number' }
|
|
512
|
+
}
|
|
513
|
+
})
|
|
514
|
+
|
|
515
|
+
fastify.get('/:id', {
|
|
516
|
+
handler: (req, reply) => {
|
|
517
|
+
reply.send({ id: req.params.id * 2, ignore: 'it' })
|
|
518
|
+
},
|
|
519
|
+
schema: {
|
|
520
|
+
params: { $ref: 'test#' },
|
|
521
|
+
response: {
|
|
522
|
+
200: { $ref: 'test#' }
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
})
|
|
526
|
+
|
|
527
|
+
fastify.inject({
|
|
528
|
+
method: 'GET',
|
|
529
|
+
url: '/123'
|
|
530
|
+
}, (err, res) => {
|
|
531
|
+
t.assert.ifError(err)
|
|
532
|
+
t.assert.deepStrictEqual(res.json(), { id: 246 })
|
|
533
|
+
testDone()
|
|
534
|
+
})
|
|
535
|
+
})
|
|
536
|
+
|
|
537
|
+
test('Customize validator compiler in instance and route', t => {
|
|
538
|
+
t.plan(28)
|
|
539
|
+
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
540
|
+
|
|
541
|
+
fastify.setValidatorCompiler(({ schema, method, url, httpPart }) => {
|
|
542
|
+
t.assert.strictEqual(method, 'POST') // run 4 times
|
|
543
|
+
t.assert.strictEqual(url, '/:id') // run 4 times
|
|
544
|
+
switch (httpPart) {
|
|
545
|
+
case 'body':
|
|
546
|
+
t.assert.ok('body evaluated')
|
|
547
|
+
return body => {
|
|
548
|
+
t.assert.deepStrictEqual(body, { foo: ['bar', 'BAR'] })
|
|
549
|
+
return true
|
|
550
|
+
}
|
|
551
|
+
case 'params':
|
|
552
|
+
t.assert.ok('params evaluated')
|
|
553
|
+
return params => {
|
|
554
|
+
t.assert.strictEqual(params.id, '1234')
|
|
555
|
+
return true
|
|
556
|
+
}
|
|
557
|
+
case 'querystring':
|
|
558
|
+
t.assert.ok('querystring evaluated')
|
|
559
|
+
return query => {
|
|
560
|
+
t.assert.strictEqual(query.lang, 'en')
|
|
561
|
+
return true
|
|
562
|
+
}
|
|
563
|
+
case 'headers':
|
|
564
|
+
t.assert.ok('headers evaluated')
|
|
565
|
+
return headers => {
|
|
566
|
+
t.assert.strictEqual(headers.x, 'hello')
|
|
567
|
+
return true
|
|
568
|
+
}
|
|
569
|
+
case '2xx':
|
|
570
|
+
t.assert.fail('the validator doesn\'t process the response')
|
|
571
|
+
break
|
|
572
|
+
default:
|
|
573
|
+
t.assert.fail(`unknown httpPart ${httpPart}`)
|
|
574
|
+
}
|
|
575
|
+
})
|
|
576
|
+
|
|
577
|
+
fastify.post('/:id', {
|
|
578
|
+
handler: echoBody,
|
|
579
|
+
schema: {
|
|
580
|
+
query: {
|
|
581
|
+
type: 'object',
|
|
582
|
+
properties: {
|
|
583
|
+
lang: { type: 'string', enum: ['it', 'en'] }
|
|
584
|
+
}
|
|
585
|
+
},
|
|
586
|
+
headers: {
|
|
587
|
+
type: 'object',
|
|
588
|
+
properties: {
|
|
589
|
+
x: { type: 'string' }
|
|
590
|
+
}
|
|
591
|
+
},
|
|
592
|
+
params: {
|
|
593
|
+
type: 'object',
|
|
594
|
+
properties: {
|
|
595
|
+
id: { type: 'number' }
|
|
596
|
+
}
|
|
597
|
+
},
|
|
598
|
+
body: {
|
|
599
|
+
type: 'object',
|
|
600
|
+
properties: {
|
|
601
|
+
foo: { type: 'array' }
|
|
602
|
+
}
|
|
603
|
+
},
|
|
604
|
+
response: {
|
|
605
|
+
'2xx': {
|
|
606
|
+
type: 'object',
|
|
607
|
+
properties: {
|
|
608
|
+
foo: { type: 'array', items: { type: 'string' } }
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
})
|
|
614
|
+
|
|
615
|
+
fastify.get('/wow/:id', {
|
|
616
|
+
handler: echoParams,
|
|
617
|
+
validatorCompiler: ({ schema, method, url, httpPart }) => {
|
|
618
|
+
t.assert.strictEqual(method, 'GET') // run 3 times (params, headers, query)
|
|
619
|
+
t.assert.strictEqual(url, '/wow/:id') // run 4 times
|
|
620
|
+
return () => { return true } // ignore the validation
|
|
621
|
+
},
|
|
622
|
+
schema: {
|
|
623
|
+
query: {
|
|
624
|
+
type: 'object',
|
|
625
|
+
properties: {
|
|
626
|
+
lang: { type: 'string', enum: ['it', 'en'] }
|
|
627
|
+
}
|
|
628
|
+
},
|
|
629
|
+
headers: {
|
|
630
|
+
type: 'object',
|
|
631
|
+
properties: {
|
|
632
|
+
x: { type: 'string' }
|
|
633
|
+
}
|
|
634
|
+
},
|
|
635
|
+
params: {
|
|
636
|
+
type: 'object',
|
|
637
|
+
properties: {
|
|
638
|
+
id: { type: 'number' }
|
|
639
|
+
}
|
|
640
|
+
},
|
|
641
|
+
response: {
|
|
642
|
+
'2xx': {
|
|
643
|
+
type: 'object',
|
|
644
|
+
properties: {
|
|
645
|
+
foo: { type: 'array', items: { type: 'string' } }
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
})
|
|
651
|
+
|
|
652
|
+
const { stepIn, patience } = waitForCb({ steps: 2 })
|
|
653
|
+
|
|
654
|
+
fastify.inject({
|
|
655
|
+
url: '/1234',
|
|
656
|
+
method: 'POST',
|
|
657
|
+
headers: { x: 'hello' },
|
|
658
|
+
query: { lang: 'en' },
|
|
659
|
+
payload: { foo: ['bar', 'BAR'] }
|
|
660
|
+
}, (err, res) => {
|
|
661
|
+
t.assert.ifError(err)
|
|
662
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
663
|
+
t.assert.deepStrictEqual(res.json(), { foo: ['bar', 'BAR'] })
|
|
664
|
+
stepIn()
|
|
665
|
+
})
|
|
666
|
+
|
|
667
|
+
fastify.inject({
|
|
668
|
+
url: '/wow/should-be-a-num',
|
|
669
|
+
method: 'GET',
|
|
670
|
+
headers: { x: 'hello' },
|
|
671
|
+
query: { lang: 'jp' } // not in the enum
|
|
672
|
+
}, (err, res) => {
|
|
673
|
+
t.assert.ifError(err)
|
|
674
|
+
t.assert.strictEqual(res.statusCode, 200) // the validation is always true
|
|
675
|
+
t.assert.deepStrictEqual(res.json(), {})
|
|
676
|
+
stepIn()
|
|
677
|
+
})
|
|
678
|
+
|
|
679
|
+
return patience
|
|
680
|
+
})
|
|
681
|
+
|
|
682
|
+
test('Use the same schema across multiple routes', (t, testDone) => {
|
|
683
|
+
t.plan(4)
|
|
684
|
+
const fastify = Fastify()
|
|
685
|
+
|
|
686
|
+
fastify.addSchema({
|
|
687
|
+
$id: 'test',
|
|
688
|
+
type: 'object',
|
|
689
|
+
properties: {
|
|
690
|
+
id: { type: 'number' }
|
|
691
|
+
}
|
|
692
|
+
})
|
|
693
|
+
|
|
694
|
+
fastify.get('/first/:id', {
|
|
695
|
+
schema: {
|
|
696
|
+
params: {
|
|
697
|
+
type: 'object',
|
|
698
|
+
properties: {
|
|
699
|
+
id: { $ref: 'test#/properties/id' }
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
},
|
|
703
|
+
handler: (req, reply) => {
|
|
704
|
+
reply.send(typeof req.params.id)
|
|
705
|
+
}
|
|
706
|
+
})
|
|
707
|
+
|
|
708
|
+
fastify.get('/second/:id', {
|
|
709
|
+
schema: {
|
|
710
|
+
params: {
|
|
711
|
+
type: 'object',
|
|
712
|
+
properties: {
|
|
713
|
+
id: { $ref: 'test#/properties/id' }
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
},
|
|
717
|
+
handler: (req, reply) => {
|
|
718
|
+
reply.send(typeof req.params.id)
|
|
719
|
+
}
|
|
720
|
+
})
|
|
721
|
+
|
|
722
|
+
fastify.inject({
|
|
723
|
+
method: 'GET',
|
|
724
|
+
url: '/first/123'
|
|
725
|
+
}, (err, res) => {
|
|
726
|
+
t.assert.ifError(err)
|
|
727
|
+
t.assert.strictEqual(res.payload, 'number')
|
|
728
|
+
})
|
|
729
|
+
|
|
730
|
+
fastify.inject({
|
|
731
|
+
method: 'GET',
|
|
732
|
+
url: '/second/123'
|
|
733
|
+
}, (err, res) => {
|
|
734
|
+
t.assert.ifError(err)
|
|
735
|
+
t.assert.strictEqual(res.payload, 'number')
|
|
736
|
+
testDone()
|
|
737
|
+
})
|
|
738
|
+
})
|
|
739
|
+
|
|
740
|
+
test('Encapsulation should intervene', (t, testDone) => {
|
|
741
|
+
t.plan(2)
|
|
742
|
+
const fastify = Fastify()
|
|
743
|
+
|
|
744
|
+
fastify.register((instance, opts, done) => {
|
|
745
|
+
instance.addSchema({
|
|
746
|
+
$id: 'encapsulation',
|
|
747
|
+
type: 'object',
|
|
748
|
+
properties: {
|
|
749
|
+
id: { type: 'number' }
|
|
750
|
+
}
|
|
751
|
+
})
|
|
752
|
+
done()
|
|
753
|
+
})
|
|
754
|
+
|
|
755
|
+
fastify.register((instance, opts, done) => {
|
|
756
|
+
instance.get('/:id', {
|
|
757
|
+
handler: echoParams,
|
|
758
|
+
schema: {
|
|
759
|
+
params: {
|
|
760
|
+
type: 'object',
|
|
761
|
+
properties: {
|
|
762
|
+
id: { $ref: 'encapsulation#/properties/id' }
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
})
|
|
767
|
+
done()
|
|
768
|
+
})
|
|
769
|
+
|
|
770
|
+
fastify.ready(err => {
|
|
771
|
+
t.assert.strictEqual(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
|
|
772
|
+
t.assert.strictEqual(err.message, "Failed building the validation schema for GET: /:id, due to error can't resolve reference encapsulation#/properties/id from id #")
|
|
773
|
+
testDone()
|
|
774
|
+
})
|
|
775
|
+
})
|
|
776
|
+
|
|
777
|
+
test('Encapsulation isolation', (t, testDone) => {
|
|
778
|
+
t.plan(1)
|
|
779
|
+
const fastify = Fastify()
|
|
780
|
+
|
|
781
|
+
fastify.register((instance, opts, done) => {
|
|
782
|
+
instance.addSchema({ $id: 'id' })
|
|
783
|
+
done()
|
|
784
|
+
})
|
|
785
|
+
|
|
786
|
+
fastify.register((instance, opts, done) => {
|
|
787
|
+
instance.addSchema({ $id: 'id' })
|
|
788
|
+
done()
|
|
789
|
+
})
|
|
790
|
+
|
|
791
|
+
fastify.ready(err => {
|
|
792
|
+
t.assert.ifError(err)
|
|
793
|
+
testDone()
|
|
794
|
+
})
|
|
795
|
+
})
|
|
796
|
+
|
|
797
|
+
test('Add schema after register', (t, testDone) => {
|
|
798
|
+
t.plan(5)
|
|
799
|
+
|
|
800
|
+
const fastify = Fastify()
|
|
801
|
+
fastify.register((instance, opts, done) => {
|
|
802
|
+
instance.get('/:id', {
|
|
803
|
+
handler: echoParams,
|
|
804
|
+
schema: {
|
|
805
|
+
params: { $ref: 'test#' }
|
|
806
|
+
}
|
|
807
|
+
})
|
|
808
|
+
|
|
809
|
+
// add it to the parent instance
|
|
810
|
+
fastify.addSchema({
|
|
811
|
+
$id: 'test',
|
|
812
|
+
type: 'object',
|
|
813
|
+
properties: {
|
|
814
|
+
id: { type: 'number' }
|
|
815
|
+
}
|
|
816
|
+
})
|
|
817
|
+
|
|
818
|
+
try {
|
|
819
|
+
instance.addSchema({ $id: 'test' })
|
|
820
|
+
} catch (err) {
|
|
821
|
+
t.assert.strictEqual(err.code, 'FST_ERR_SCH_ALREADY_PRESENT')
|
|
822
|
+
t.assert.strictEqual(err.message, 'Schema with id \'test\' already declared!')
|
|
823
|
+
}
|
|
824
|
+
done()
|
|
825
|
+
})
|
|
826
|
+
|
|
827
|
+
fastify.inject({
|
|
828
|
+
method: 'GET',
|
|
829
|
+
url: '/4242'
|
|
830
|
+
}, (err, res) => {
|
|
831
|
+
t.assert.ifError(err)
|
|
832
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
833
|
+
t.assert.deepStrictEqual(res.json(), { id: 4242 })
|
|
834
|
+
testDone()
|
|
835
|
+
})
|
|
836
|
+
})
|
|
837
|
+
|
|
838
|
+
test('Encapsulation isolation for getSchemas', (t, testDone) => {
|
|
839
|
+
t.plan(5)
|
|
840
|
+
const fastify = Fastify()
|
|
841
|
+
|
|
842
|
+
let pluginDeepOneSide
|
|
843
|
+
let pluginDeepOne
|
|
844
|
+
let pluginDeepTwo
|
|
845
|
+
|
|
846
|
+
const schemas = {
|
|
847
|
+
z: { $id: 'z', my: 'schema' },
|
|
848
|
+
a: { $id: 'a', my: 'schema' },
|
|
849
|
+
b: { $id: 'b', my: 'schema' },
|
|
850
|
+
c: { $id: 'c', my: 'schema', properties: { a: 'a', b: 1 } }
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
fastify.addSchema(schemas.z)
|
|
854
|
+
|
|
855
|
+
fastify.register((instance, opts, done) => {
|
|
856
|
+
instance.addSchema(schemas.a)
|
|
857
|
+
pluginDeepOneSide = instance
|
|
858
|
+
done()
|
|
859
|
+
})
|
|
860
|
+
|
|
861
|
+
fastify.register((instance, opts, done) => {
|
|
862
|
+
instance.addSchema(schemas.b)
|
|
863
|
+
instance.register((subinstance, opts, done) => {
|
|
864
|
+
subinstance.addSchema(schemas.c)
|
|
865
|
+
pluginDeepTwo = subinstance
|
|
866
|
+
done()
|
|
867
|
+
})
|
|
868
|
+
pluginDeepOne = instance
|
|
869
|
+
done()
|
|
870
|
+
})
|
|
871
|
+
|
|
872
|
+
fastify.ready(err => {
|
|
873
|
+
t.assert.ifError(err)
|
|
874
|
+
t.assert.deepStrictEqual(fastify.getSchemas(), { z: schemas.z })
|
|
875
|
+
t.assert.deepStrictEqual(pluginDeepOneSide.getSchemas(), { z: schemas.z, a: schemas.a })
|
|
876
|
+
t.assert.deepStrictEqual(pluginDeepOne.getSchemas(), { z: schemas.z, b: schemas.b })
|
|
877
|
+
t.assert.deepStrictEqual(pluginDeepTwo.getSchemas(), { z: schemas.z, b: schemas.b, c: schemas.c })
|
|
878
|
+
testDone()
|
|
879
|
+
})
|
|
880
|
+
})
|
|
881
|
+
|
|
882
|
+
test('Use the same schema id in different places', (t, testDone) => {
|
|
883
|
+
t.plan(1)
|
|
884
|
+
const fastify = Fastify()
|
|
885
|
+
|
|
886
|
+
fastify.addSchema({
|
|
887
|
+
$id: 'test',
|
|
888
|
+
type: 'object',
|
|
889
|
+
properties: {
|
|
890
|
+
id: { type: 'number' }
|
|
891
|
+
}
|
|
892
|
+
})
|
|
893
|
+
|
|
894
|
+
fastify.get('/:id', {
|
|
895
|
+
handler: echoParams,
|
|
896
|
+
schema: {
|
|
897
|
+
response: {
|
|
898
|
+
200: {
|
|
899
|
+
type: 'array',
|
|
900
|
+
items: { $ref: 'test#/properties/id' }
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
})
|
|
905
|
+
|
|
906
|
+
fastify.post('/:id', {
|
|
907
|
+
handler: echoBody,
|
|
908
|
+
schema: {
|
|
909
|
+
body: {
|
|
910
|
+
type: 'object',
|
|
911
|
+
properties: {
|
|
912
|
+
id: { $ref: 'test#/properties/id' }
|
|
913
|
+
}
|
|
914
|
+
},
|
|
915
|
+
response: {
|
|
916
|
+
200: {
|
|
917
|
+
type: 'object',
|
|
918
|
+
properties: {
|
|
919
|
+
id: { $ref: 'test#/properties/id' }
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
})
|
|
925
|
+
|
|
926
|
+
fastify.ready(err => {
|
|
927
|
+
t.assert.ifError(err)
|
|
928
|
+
testDone()
|
|
929
|
+
})
|
|
930
|
+
})
|
|
931
|
+
|
|
932
|
+
test('Get schema anyway should not add `properties` if allOf is present', (t, testDone) => {
|
|
933
|
+
t.plan(1)
|
|
934
|
+
const fastify = Fastify()
|
|
935
|
+
|
|
936
|
+
fastify.addSchema({
|
|
937
|
+
$id: 'first',
|
|
938
|
+
type: 'object',
|
|
939
|
+
properties: {
|
|
940
|
+
first: { type: 'number' }
|
|
941
|
+
}
|
|
942
|
+
})
|
|
943
|
+
|
|
944
|
+
fastify.addSchema({
|
|
945
|
+
$id: 'second',
|
|
946
|
+
type: 'object',
|
|
947
|
+
allOf: [
|
|
948
|
+
{
|
|
949
|
+
type: 'object',
|
|
950
|
+
properties: {
|
|
951
|
+
second: { type: 'number' }
|
|
952
|
+
}
|
|
953
|
+
},
|
|
954
|
+
fastify.getSchema('first')
|
|
955
|
+
]
|
|
956
|
+
})
|
|
957
|
+
|
|
958
|
+
fastify.get('/', {
|
|
959
|
+
handler: () => { },
|
|
960
|
+
schema: {
|
|
961
|
+
querystring: fastify.getSchema('second'),
|
|
962
|
+
response: { 200: fastify.getSchema('second') }
|
|
963
|
+
}
|
|
964
|
+
})
|
|
965
|
+
|
|
966
|
+
fastify.ready(err => {
|
|
967
|
+
t.assert.ifError(err)
|
|
968
|
+
testDone()
|
|
969
|
+
})
|
|
970
|
+
})
|
|
971
|
+
|
|
972
|
+
test('Get schema anyway should not add `properties` if oneOf is present', (t, testDone) => {
|
|
973
|
+
t.plan(1)
|
|
974
|
+
const fastify = Fastify()
|
|
975
|
+
|
|
976
|
+
fastify.addSchema({
|
|
977
|
+
$id: 'first',
|
|
978
|
+
type: 'object',
|
|
979
|
+
properties: {
|
|
980
|
+
first: { type: 'number' }
|
|
981
|
+
}
|
|
982
|
+
})
|
|
983
|
+
|
|
984
|
+
fastify.addSchema({
|
|
985
|
+
$id: 'second',
|
|
986
|
+
type: 'object',
|
|
987
|
+
oneOf: [
|
|
988
|
+
{
|
|
989
|
+
type: 'object',
|
|
990
|
+
properties: {
|
|
991
|
+
second: { type: 'number' }
|
|
992
|
+
}
|
|
993
|
+
},
|
|
994
|
+
fastify.getSchema('first')
|
|
995
|
+
]
|
|
996
|
+
})
|
|
997
|
+
|
|
998
|
+
fastify.get('/', {
|
|
999
|
+
handler: () => { },
|
|
1000
|
+
schema: {
|
|
1001
|
+
querystring: fastify.getSchema('second'),
|
|
1002
|
+
response: { 200: fastify.getSchema('second') }
|
|
1003
|
+
}
|
|
1004
|
+
})
|
|
1005
|
+
|
|
1006
|
+
fastify.ready(err => {
|
|
1007
|
+
t.assert.ifError(err)
|
|
1008
|
+
testDone()
|
|
1009
|
+
})
|
|
1010
|
+
})
|
|
1011
|
+
|
|
1012
|
+
test('Get schema anyway should not add `properties` if anyOf is present', (t, testDone) => {
|
|
1013
|
+
t.plan(1)
|
|
1014
|
+
const fastify = Fastify()
|
|
1015
|
+
|
|
1016
|
+
fastify.addSchema({
|
|
1017
|
+
$id: 'first',
|
|
1018
|
+
type: 'object',
|
|
1019
|
+
properties: {
|
|
1020
|
+
first: { type: 'number' }
|
|
1021
|
+
}
|
|
1022
|
+
})
|
|
1023
|
+
|
|
1024
|
+
fastify.addSchema({
|
|
1025
|
+
$id: 'second',
|
|
1026
|
+
type: 'object',
|
|
1027
|
+
anyOf: [
|
|
1028
|
+
{
|
|
1029
|
+
type: 'object',
|
|
1030
|
+
properties: {
|
|
1031
|
+
second: { type: 'number' }
|
|
1032
|
+
}
|
|
1033
|
+
},
|
|
1034
|
+
fastify.getSchema('first')
|
|
1035
|
+
]
|
|
1036
|
+
})
|
|
1037
|
+
|
|
1038
|
+
fastify.get('/', {
|
|
1039
|
+
handler: () => { },
|
|
1040
|
+
schema: {
|
|
1041
|
+
querystring: fastify.getSchema('second'),
|
|
1042
|
+
response: { 200: fastify.getSchema('second') }
|
|
1043
|
+
}
|
|
1044
|
+
})
|
|
1045
|
+
|
|
1046
|
+
fastify.ready(err => {
|
|
1047
|
+
t.assert.ifError(err)
|
|
1048
|
+
testDone()
|
|
1049
|
+
})
|
|
1050
|
+
})
|
|
1051
|
+
|
|
1052
|
+
test('Shared schema should be ignored in string enum', (t, testDone) => {
|
|
1053
|
+
t.plan(2)
|
|
1054
|
+
const fastify = Fastify()
|
|
1055
|
+
|
|
1056
|
+
fastify.get('/:lang', {
|
|
1057
|
+
handler: echoParams,
|
|
1058
|
+
schema: {
|
|
1059
|
+
params: {
|
|
1060
|
+
type: 'object',
|
|
1061
|
+
properties: {
|
|
1062
|
+
lang: {
|
|
1063
|
+
type: 'string',
|
|
1064
|
+
enum: ['Javascript', 'C++', 'C#']
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
})
|
|
1070
|
+
|
|
1071
|
+
fastify.inject('/C%23', (err, res) => {
|
|
1072
|
+
t.assert.ifError(err)
|
|
1073
|
+
t.assert.deepStrictEqual(res.json(), { lang: 'C#' })
|
|
1074
|
+
testDone()
|
|
1075
|
+
})
|
|
1076
|
+
})
|
|
1077
|
+
|
|
1078
|
+
test('Shared schema should NOT be ignored in != string enum', (t, testDone) => {
|
|
1079
|
+
t.plan(2)
|
|
1080
|
+
const fastify = Fastify()
|
|
1081
|
+
|
|
1082
|
+
fastify.addSchema({
|
|
1083
|
+
$id: 'C',
|
|
1084
|
+
type: 'object',
|
|
1085
|
+
properties: {
|
|
1086
|
+
lang: {
|
|
1087
|
+
type: 'string',
|
|
1088
|
+
enum: ['Javascript', 'C++', 'C#']
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
})
|
|
1092
|
+
|
|
1093
|
+
fastify.post('/:lang', {
|
|
1094
|
+
handler: echoBody,
|
|
1095
|
+
schema: {
|
|
1096
|
+
body: fastify.getSchema('C')
|
|
1097
|
+
}
|
|
1098
|
+
})
|
|
1099
|
+
|
|
1100
|
+
fastify.inject({
|
|
1101
|
+
url: '/',
|
|
1102
|
+
method: 'POST',
|
|
1103
|
+
payload: { lang: 'C#' }
|
|
1104
|
+
}, (err, res) => {
|
|
1105
|
+
t.assert.ifError(err)
|
|
1106
|
+
t.assert.deepStrictEqual(res.json(), { lang: 'C#' })
|
|
1107
|
+
testDone()
|
|
1108
|
+
})
|
|
1109
|
+
})
|
|
1110
|
+
|
|
1111
|
+
test('Case insensitive header validation', (t, testDone) => {
|
|
1112
|
+
t.plan(2)
|
|
1113
|
+
const fastify = Fastify()
|
|
1114
|
+
fastify.get('/', {
|
|
1115
|
+
handler: (req, reply) => {
|
|
1116
|
+
reply.code(200).send(req.headers.foobar)
|
|
1117
|
+
},
|
|
1118
|
+
schema: {
|
|
1119
|
+
headers: {
|
|
1120
|
+
type: 'object',
|
|
1121
|
+
required: ['FooBar'],
|
|
1122
|
+
properties: {
|
|
1123
|
+
FooBar: { type: 'string' }
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
})
|
|
1128
|
+
fastify.inject({
|
|
1129
|
+
url: '/',
|
|
1130
|
+
method: 'GET',
|
|
1131
|
+
headers: {
|
|
1132
|
+
FooBar: 'Baz'
|
|
1133
|
+
}
|
|
1134
|
+
}, (err, res) => {
|
|
1135
|
+
t.assert.ifError(err)
|
|
1136
|
+
t.assert.strictEqual(res.payload, 'Baz')
|
|
1137
|
+
testDone()
|
|
1138
|
+
})
|
|
1139
|
+
})
|
|
1140
|
+
|
|
1141
|
+
test('Not evaluate json-schema $schema keyword', (t, testDone) => {
|
|
1142
|
+
t.plan(2)
|
|
1143
|
+
const fastify = Fastify()
|
|
1144
|
+
fastify.post('/', {
|
|
1145
|
+
handler: echoBody,
|
|
1146
|
+
schema: {
|
|
1147
|
+
body: {
|
|
1148
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
1149
|
+
type: 'object',
|
|
1150
|
+
additionalProperties: false,
|
|
1151
|
+
properties: {
|
|
1152
|
+
hello: {
|
|
1153
|
+
type: 'string'
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
})
|
|
1159
|
+
fastify.inject({
|
|
1160
|
+
url: '/',
|
|
1161
|
+
method: 'POST',
|
|
1162
|
+
body: { hello: 'world', foo: 'bar' }
|
|
1163
|
+
}, (err, res) => {
|
|
1164
|
+
t.assert.ifError(err)
|
|
1165
|
+
t.assert.deepStrictEqual(res.json(), { hello: 'world' })
|
|
1166
|
+
testDone()
|
|
1167
|
+
})
|
|
1168
|
+
})
|
|
1169
|
+
|
|
1170
|
+
test('Validation context in validation result', (t, testDone) => {
|
|
1171
|
+
t.plan(5)
|
|
1172
|
+
const fastify = Fastify()
|
|
1173
|
+
// custom error handler to expose validation context in response, so we can test it later
|
|
1174
|
+
fastify.setErrorHandler((err, request, reply) => {
|
|
1175
|
+
t.assert.strictEqual(err instanceof Error, true)
|
|
1176
|
+
t.assert.ok(err.validation, 'detailed errors')
|
|
1177
|
+
t.assert.strictEqual(err.validationContext, 'body')
|
|
1178
|
+
reply.code(400).send()
|
|
1179
|
+
})
|
|
1180
|
+
fastify.post('/', {
|
|
1181
|
+
handler: echoParams,
|
|
1182
|
+
schema: {
|
|
1183
|
+
body: {
|
|
1184
|
+
type: 'object',
|
|
1185
|
+
required: ['hello'],
|
|
1186
|
+
properties: {
|
|
1187
|
+
hello: { type: 'string' }
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
})
|
|
1192
|
+
fastify.inject({
|
|
1193
|
+
method: 'POST',
|
|
1194
|
+
url: '/',
|
|
1195
|
+
payload: {} // body lacks required field, will fail validation
|
|
1196
|
+
}, (err, res) => {
|
|
1197
|
+
t.assert.ifError(err)
|
|
1198
|
+
t.assert.strictEqual(res.statusCode, 400)
|
|
1199
|
+
testDone()
|
|
1200
|
+
})
|
|
1201
|
+
})
|
|
1202
|
+
|
|
1203
|
+
test('The schema build should not modify the input', (t, testDone) => {
|
|
1204
|
+
t.plan(3)
|
|
1205
|
+
const fastify = Fastify()
|
|
1206
|
+
|
|
1207
|
+
const first = {
|
|
1208
|
+
$id: 'first',
|
|
1209
|
+
type: 'object',
|
|
1210
|
+
properties: {
|
|
1211
|
+
first: {
|
|
1212
|
+
type: 'number'
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
fastify.addSchema(first)
|
|
1218
|
+
|
|
1219
|
+
fastify.addSchema({
|
|
1220
|
+
$id: 'second',
|
|
1221
|
+
type: 'object',
|
|
1222
|
+
allOf: [
|
|
1223
|
+
{
|
|
1224
|
+
type: 'object',
|
|
1225
|
+
properties: {
|
|
1226
|
+
second: {
|
|
1227
|
+
type: 'number'
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
},
|
|
1231
|
+
{ $ref: 'first#' }
|
|
1232
|
+
]
|
|
1233
|
+
})
|
|
1234
|
+
|
|
1235
|
+
fastify.post('/', {
|
|
1236
|
+
schema: {
|
|
1237
|
+
description: 'get',
|
|
1238
|
+
body: { $ref: 'second#' },
|
|
1239
|
+
response: {
|
|
1240
|
+
200: { $ref: 'second#' }
|
|
1241
|
+
}
|
|
1242
|
+
},
|
|
1243
|
+
handler: (request, reply) => {
|
|
1244
|
+
reply.send({ hello: 'world' })
|
|
1245
|
+
}
|
|
1246
|
+
})
|
|
1247
|
+
|
|
1248
|
+
fastify.patch('/', {
|
|
1249
|
+
schema: {
|
|
1250
|
+
description: 'patch',
|
|
1251
|
+
body: { $ref: 'first#' },
|
|
1252
|
+
response: {
|
|
1253
|
+
200: { $ref: 'first#' }
|
|
1254
|
+
}
|
|
1255
|
+
},
|
|
1256
|
+
handler: (request, reply) => {
|
|
1257
|
+
reply.send({ hello: 'world' })
|
|
1258
|
+
}
|
|
1259
|
+
})
|
|
1260
|
+
|
|
1261
|
+
t.assert.ok(first.$id)
|
|
1262
|
+
fastify.ready(err => {
|
|
1263
|
+
t.assert.ifError(err)
|
|
1264
|
+
t.assert.ok(first.$id)
|
|
1265
|
+
testDone()
|
|
1266
|
+
})
|
|
1267
|
+
})
|
|
1268
|
+
|
|
1269
|
+
test('Cross schema reference with encapsulation references', (t, testDone) => {
|
|
1270
|
+
t.plan(1)
|
|
1271
|
+
|
|
1272
|
+
const fastify = Fastify()
|
|
1273
|
+
fastify.addSchema({
|
|
1274
|
+
$id: 'http://foo/item',
|
|
1275
|
+
type: 'object',
|
|
1276
|
+
properties: { foo: { type: 'string' } }
|
|
1277
|
+
})
|
|
1278
|
+
|
|
1279
|
+
const refItem = { $ref: 'http://foo/item#' }
|
|
1280
|
+
|
|
1281
|
+
fastify.addSchema({
|
|
1282
|
+
$id: 'itemList',
|
|
1283
|
+
type: 'array',
|
|
1284
|
+
items: refItem
|
|
1285
|
+
})
|
|
1286
|
+
|
|
1287
|
+
fastify.register((instance, opts, done) => {
|
|
1288
|
+
instance.addSchema({
|
|
1289
|
+
$id: 'encapsulation',
|
|
1290
|
+
type: 'object',
|
|
1291
|
+
properties: {
|
|
1292
|
+
id: { type: 'number' },
|
|
1293
|
+
item: refItem,
|
|
1294
|
+
secondItem: refItem
|
|
1295
|
+
}
|
|
1296
|
+
})
|
|
1297
|
+
|
|
1298
|
+
const multipleRef = {
|
|
1299
|
+
type: 'object',
|
|
1300
|
+
properties: {
|
|
1301
|
+
a: { $ref: 'itemList#' },
|
|
1302
|
+
b: refItem,
|
|
1303
|
+
c: refItem,
|
|
1304
|
+
d: refItem
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
instance.get('/get', { schema: { response: { 200: deepClone(multipleRef) } } }, () => { })
|
|
1309
|
+
instance.get('/double-get', { schema: { querystring: multipleRef, response: { 200: multipleRef } } }, () => { })
|
|
1310
|
+
instance.post('/post', { schema: { body: multipleRef, response: { 200: multipleRef } } }, () => { })
|
|
1311
|
+
instance.post('/double', { schema: { response: { 200: { $ref: 'encapsulation' } } } }, () => { })
|
|
1312
|
+
done()
|
|
1313
|
+
}, { prefix: '/foo' })
|
|
1314
|
+
|
|
1315
|
+
fastify.post('/post', { schema: { body: refItem, response: { 200: refItem } } }, () => { })
|
|
1316
|
+
fastify.get('/get', { schema: { params: refItem, response: { 200: refItem } } }, () => { })
|
|
1317
|
+
|
|
1318
|
+
fastify.ready(err => {
|
|
1319
|
+
t.assert.ifError(err)
|
|
1320
|
+
testDone()
|
|
1321
|
+
})
|
|
1322
|
+
})
|
|
1323
|
+
|
|
1324
|
+
test('Check how many AJV instances are built #1', (t, testDone) => {
|
|
1325
|
+
t.plan(12)
|
|
1326
|
+
const fastify = Fastify()
|
|
1327
|
+
addRandomRoute(fastify) // this trigger the schema validation creation
|
|
1328
|
+
t.assert.ok(!fastify.validatorCompiler, 'validator not initialized')
|
|
1329
|
+
|
|
1330
|
+
const instances = []
|
|
1331
|
+
fastify.register((instance, opts, done) => {
|
|
1332
|
+
t.assert.ok(!fastify.validatorCompiler, 'validator not initialized')
|
|
1333
|
+
instances.push(instance)
|
|
1334
|
+
done()
|
|
1335
|
+
})
|
|
1336
|
+
fastify.register((instance, opts, done) => {
|
|
1337
|
+
t.assert.ok(!fastify.validatorCompiler, 'validator not initialized')
|
|
1338
|
+
addRandomRoute(instance)
|
|
1339
|
+
instances.push(instance)
|
|
1340
|
+
done()
|
|
1341
|
+
instance.register((instance, opts, done) => {
|
|
1342
|
+
t.assert.ok(!fastify.validatorCompiler, 'validator not initialized')
|
|
1343
|
+
addRandomRoute(instance)
|
|
1344
|
+
instances.push(instance)
|
|
1345
|
+
done()
|
|
1346
|
+
})
|
|
1347
|
+
})
|
|
1348
|
+
|
|
1349
|
+
fastify.ready(err => {
|
|
1350
|
+
t.assert.ifError(err)
|
|
1351
|
+
|
|
1352
|
+
t.assert.ok(fastify.validatorCompiler, 'validator initialized on preReady')
|
|
1353
|
+
fastify.validatorCompiler.checkPointer = true
|
|
1354
|
+
instances.forEach(i => {
|
|
1355
|
+
t.assert.ok(i.validatorCompiler, 'validator initialized on preReady')
|
|
1356
|
+
t.assert.strictEqual(i.validatorCompiler.checkPointer, true, 'validator is only one for all the instances')
|
|
1357
|
+
})
|
|
1358
|
+
testDone()
|
|
1359
|
+
})
|
|
1360
|
+
})
|
|
1361
|
+
|
|
1362
|
+
test('onReady hook has the compilers ready', (t, testDone) => {
|
|
1363
|
+
t.plan(6)
|
|
1364
|
+
|
|
1365
|
+
const fastify = Fastify()
|
|
1366
|
+
|
|
1367
|
+
fastify.get(`/${Math.random()}`, {
|
|
1368
|
+
handler: (req, reply) => reply.send(),
|
|
1369
|
+
schema: {
|
|
1370
|
+
headers: { type: 'object' },
|
|
1371
|
+
response: { 200: { type: 'object' } }
|
|
1372
|
+
}
|
|
1373
|
+
})
|
|
1374
|
+
|
|
1375
|
+
fastify.addHook('onReady', function (done) {
|
|
1376
|
+
t.assert.ok(this.validatorCompiler)
|
|
1377
|
+
t.assert.ok(this.serializerCompiler)
|
|
1378
|
+
done()
|
|
1379
|
+
})
|
|
1380
|
+
|
|
1381
|
+
let hookCallCounter = 0
|
|
1382
|
+
fastify.register(async (i, o) => {
|
|
1383
|
+
i.addHook('onReady', function (done) {
|
|
1384
|
+
t.assert.ok(this.validatorCompiler)
|
|
1385
|
+
t.assert.ok(this.serializerCompiler)
|
|
1386
|
+
done()
|
|
1387
|
+
})
|
|
1388
|
+
|
|
1389
|
+
i.register(async (i, o) => { })
|
|
1390
|
+
|
|
1391
|
+
i.addHook('onReady', function (done) {
|
|
1392
|
+
hookCallCounter++
|
|
1393
|
+
done()
|
|
1394
|
+
})
|
|
1395
|
+
})
|
|
1396
|
+
|
|
1397
|
+
fastify.ready(err => {
|
|
1398
|
+
t.assert.ifError(err)
|
|
1399
|
+
t.assert.strictEqual(hookCallCounter, 1, 'it is called once')
|
|
1400
|
+
testDone()
|
|
1401
|
+
})
|
|
1402
|
+
})
|
|
1403
|
+
|
|
1404
|
+
test('Check how many AJV instances are built #2 - verify validatorPool', (t, testDone) => {
|
|
1405
|
+
t.plan(13)
|
|
1406
|
+
const fastify = Fastify()
|
|
1407
|
+
t.assert.ok(!fastify.validatorCompiler, 'validator not initialized')
|
|
1408
|
+
|
|
1409
|
+
fastify.register(function sibling1 (instance, opts, done) {
|
|
1410
|
+
addRandomRoute(instance)
|
|
1411
|
+
t.assert.ok(!instance.validatorCompiler, 'validator not initialized')
|
|
1412
|
+
instance.ready(() => {
|
|
1413
|
+
t.assert.ok(instance.validatorCompiler, 'validator is initialized')
|
|
1414
|
+
instance.validatorCompiler.sharedPool = 1
|
|
1415
|
+
})
|
|
1416
|
+
instance.after(() => {
|
|
1417
|
+
t.assert.ok(!instance.validatorCompiler, 'validator not initialized')
|
|
1418
|
+
})
|
|
1419
|
+
done()
|
|
1420
|
+
})
|
|
1421
|
+
|
|
1422
|
+
fastify.register(function sibling2 (instance, opts, done) {
|
|
1423
|
+
addRandomRoute(instance)
|
|
1424
|
+
t.assert.ok(!instance.validatorCompiler, 'validator not initialized')
|
|
1425
|
+
instance.ready(() => {
|
|
1426
|
+
t.assert.strictEqual(instance.validatorCompiler.sharedPool, 1, 'this context must share the validator with the same schemas')
|
|
1427
|
+
instance.validatorCompiler.sharedPool = 2
|
|
1428
|
+
})
|
|
1429
|
+
instance.after(() => {
|
|
1430
|
+
t.assert.ok(!instance.validatorCompiler, 'validator not initialized')
|
|
1431
|
+
})
|
|
1432
|
+
|
|
1433
|
+
instance.register((instance, opts, done) => {
|
|
1434
|
+
t.assert.ok(!instance.validatorCompiler, 'validator not initialized')
|
|
1435
|
+
instance.ready(() => {
|
|
1436
|
+
t.assert.strictEqual(instance.validatorCompiler.sharedPool, 2, 'this context must share the validator of the parent')
|
|
1437
|
+
})
|
|
1438
|
+
done()
|
|
1439
|
+
})
|
|
1440
|
+
done()
|
|
1441
|
+
})
|
|
1442
|
+
|
|
1443
|
+
fastify.register(function sibling3 (instance, opts, done) {
|
|
1444
|
+
addRandomRoute(instance)
|
|
1445
|
+
|
|
1446
|
+
// this trigger to don't reuse the same compiler pool
|
|
1447
|
+
instance.addSchema({ $id: 'diff', type: 'object' })
|
|
1448
|
+
|
|
1449
|
+
t.assert.ok(!instance.validatorCompiler, 'validator not initialized')
|
|
1450
|
+
instance.ready(() => {
|
|
1451
|
+
t.assert.ok(instance.validatorCompiler, 'validator is initialized')
|
|
1452
|
+
t.assert.ok(!instance.validatorCompiler.sharedPool, 'this context has its own compiler')
|
|
1453
|
+
})
|
|
1454
|
+
done()
|
|
1455
|
+
})
|
|
1456
|
+
|
|
1457
|
+
fastify.ready(err => {
|
|
1458
|
+
t.assert.ifError(err)
|
|
1459
|
+
testDone()
|
|
1460
|
+
})
|
|
1461
|
+
})
|
|
1462
|
+
|
|
1463
|
+
function addRandomRoute (server) {
|
|
1464
|
+
server.post(`/${Math.random()}`,
|
|
1465
|
+
{ schema: { body: { type: 'object' } } },
|
|
1466
|
+
(req, reply) => reply.send()
|
|
1467
|
+
)
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
test('Add schema order should not break the startup', (t, testDone) => {
|
|
1471
|
+
t.plan(1)
|
|
1472
|
+
const fastify = Fastify()
|
|
1473
|
+
|
|
1474
|
+
fastify.get('/', { schema: { random: 'options' } }, () => { })
|
|
1475
|
+
|
|
1476
|
+
fastify.register(fp((f, opts) => {
|
|
1477
|
+
f.addSchema({
|
|
1478
|
+
$id: 'https://fastify.test/bson/objectId',
|
|
1479
|
+
type: 'string',
|
|
1480
|
+
pattern: '\\b[0-9A-Fa-f]{24}\\b'
|
|
1481
|
+
})
|
|
1482
|
+
return Promise.resolve() // avoid async for node 6
|
|
1483
|
+
}))
|
|
1484
|
+
|
|
1485
|
+
fastify.get('/:id', {
|
|
1486
|
+
schema: {
|
|
1487
|
+
params: {
|
|
1488
|
+
type: 'object',
|
|
1489
|
+
properties: {
|
|
1490
|
+
id: { $ref: 'https://fastify.test/bson/objectId#' }
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
}, () => { })
|
|
1495
|
+
|
|
1496
|
+
fastify.ready(err => {
|
|
1497
|
+
t.assert.ifError(err)
|
|
1498
|
+
testDone()
|
|
1499
|
+
})
|
|
1500
|
+
})
|
|
1501
|
+
|
|
1502
|
+
test('The schema compiler recreate itself if needed', (t, testDone) => {
|
|
1503
|
+
t.plan(1)
|
|
1504
|
+
const fastify = Fastify()
|
|
1505
|
+
|
|
1506
|
+
fastify.options('/', { schema: { hide: true } }, echoBody)
|
|
1507
|
+
|
|
1508
|
+
fastify.register(function (fastify, options, done) {
|
|
1509
|
+
fastify.addSchema({
|
|
1510
|
+
$id: 'identifier',
|
|
1511
|
+
type: 'string',
|
|
1512
|
+
format: 'uuid'
|
|
1513
|
+
})
|
|
1514
|
+
|
|
1515
|
+
fastify.get('/:foobarId', {
|
|
1516
|
+
schema: {
|
|
1517
|
+
params: {
|
|
1518
|
+
type: 'object',
|
|
1519
|
+
properties: {
|
|
1520
|
+
foobarId: { $ref: 'identifier#' }
|
|
1521
|
+
}
|
|
1522
|
+
}
|
|
1523
|
+
}
|
|
1524
|
+
}, echoBody)
|
|
1525
|
+
|
|
1526
|
+
done()
|
|
1527
|
+
})
|
|
1528
|
+
|
|
1529
|
+
fastify.ready(err => {
|
|
1530
|
+
t.assert.ifError(err)
|
|
1531
|
+
testDone()
|
|
1532
|
+
})
|
|
1533
|
+
})
|
|
1534
|
+
|
|
1535
|
+
test('Schema controller setter', t => {
|
|
1536
|
+
t.plan(2)
|
|
1537
|
+
Fastify({ schemaController: {} })
|
|
1538
|
+
t.assert.ok('allow empty object')
|
|
1539
|
+
|
|
1540
|
+
try {
|
|
1541
|
+
Fastify({ schemaController: { bucket: {} } })
|
|
1542
|
+
t.fail('the bucket option must be a function')
|
|
1543
|
+
} catch (err) {
|
|
1544
|
+
t.assert.strictEqual(err.message, "schemaController.bucket option should be a function, instead got 'object'")
|
|
1545
|
+
}
|
|
1546
|
+
})
|
|
1547
|
+
|
|
1548
|
+
test('Schema controller bucket', (t, testDone) => {
|
|
1549
|
+
t.plan(10)
|
|
1550
|
+
|
|
1551
|
+
let added = 0
|
|
1552
|
+
let builtBucket = 0
|
|
1553
|
+
|
|
1554
|
+
const initStoreQueue = []
|
|
1555
|
+
|
|
1556
|
+
function factoryBucket (storeInit) {
|
|
1557
|
+
builtBucket++
|
|
1558
|
+
t.assert.deepStrictEqual(initStoreQueue.pop(), storeInit)
|
|
1559
|
+
const store = new Map(storeInit)
|
|
1560
|
+
return {
|
|
1561
|
+
add (schema) {
|
|
1562
|
+
added++
|
|
1563
|
+
store.set(schema.$id, schema)
|
|
1564
|
+
},
|
|
1565
|
+
getSchema (id) {
|
|
1566
|
+
return store.get(id)
|
|
1567
|
+
},
|
|
1568
|
+
getSchemas () {
|
|
1569
|
+
// what is returned by this function, will be the `storeInit` parameter
|
|
1570
|
+
initStoreQueue.push(store)
|
|
1571
|
+
return store
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
const fastify = Fastify({
|
|
1577
|
+
schemaController: {
|
|
1578
|
+
bucket: factoryBucket
|
|
1579
|
+
}
|
|
1580
|
+
})
|
|
1581
|
+
|
|
1582
|
+
fastify.register(async (instance) => {
|
|
1583
|
+
instance.addSchema({ $id: 'b', type: 'string' })
|
|
1584
|
+
instance.addHook('onReady', function (done) {
|
|
1585
|
+
t.assert.strictEqual(instance.getSchemas().size, 2)
|
|
1586
|
+
done()
|
|
1587
|
+
})
|
|
1588
|
+
instance.register(async (subinstance) => {
|
|
1589
|
+
subinstance.addSchema({ $id: 'c', type: 'string' })
|
|
1590
|
+
subinstance.addHook('onReady', function (done) {
|
|
1591
|
+
t.assert.strictEqual(subinstance.getSchemas().size, 3)
|
|
1592
|
+
done()
|
|
1593
|
+
})
|
|
1594
|
+
})
|
|
1595
|
+
})
|
|
1596
|
+
|
|
1597
|
+
fastify.register(async (instance) => {
|
|
1598
|
+
instance.addHook('onReady', function (done) {
|
|
1599
|
+
t.assert.strictEqual(instance.getSchemas().size, 1)
|
|
1600
|
+
done()
|
|
1601
|
+
})
|
|
1602
|
+
})
|
|
1603
|
+
|
|
1604
|
+
fastify.addSchema({ $id: 'a', type: 'string' })
|
|
1605
|
+
|
|
1606
|
+
fastify.ready(err => {
|
|
1607
|
+
t.assert.ifError(err)
|
|
1608
|
+
t.assert.strictEqual(added, 3, 'three schema added')
|
|
1609
|
+
t.assert.strictEqual(builtBucket, 4, 'one bucket built for every register call + 1 for the root instance')
|
|
1610
|
+
testDone()
|
|
1611
|
+
})
|
|
1612
|
+
})
|
|
1613
|
+
|
|
1614
|
+
test('setSchemaController per instance', (t, testDone) => {
|
|
1615
|
+
t.plan(7)
|
|
1616
|
+
const fastify = Fastify({})
|
|
1617
|
+
|
|
1618
|
+
fastify.register(async (instance1) => {
|
|
1619
|
+
instance1.setSchemaController({
|
|
1620
|
+
bucket: function factoryBucket (storeInit) {
|
|
1621
|
+
t.assert.ok('instance1 has created the bucket')
|
|
1622
|
+
return {
|
|
1623
|
+
add (schema) { t.fail('add is not called') },
|
|
1624
|
+
getSchema (id) { t.fail('getSchema is not called') },
|
|
1625
|
+
getSchemas () { t.fail('getSchemas is not called') }
|
|
1626
|
+
}
|
|
1627
|
+
}
|
|
1628
|
+
})
|
|
1629
|
+
})
|
|
1630
|
+
|
|
1631
|
+
fastify.register(async (instance2) => {
|
|
1632
|
+
const bSchema = { $id: 'b', type: 'string' }
|
|
1633
|
+
|
|
1634
|
+
instance2.setSchemaController({
|
|
1635
|
+
bucket: function factoryBucket (storeInit) {
|
|
1636
|
+
t.assert.ok('instance2 has created the bucket')
|
|
1637
|
+
const map = {}
|
|
1638
|
+
return {
|
|
1639
|
+
add (schema) {
|
|
1640
|
+
t.assert.strictEqual(schema.$id, bSchema.$id, 'add is called')
|
|
1641
|
+
map[schema.$id] = schema
|
|
1642
|
+
},
|
|
1643
|
+
getSchema (id) {
|
|
1644
|
+
t.assert.ok('getSchema is called')
|
|
1645
|
+
return map[id]
|
|
1646
|
+
},
|
|
1647
|
+
getSchemas () {
|
|
1648
|
+
t.assert.ok('getSchemas is called')
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
}
|
|
1652
|
+
})
|
|
1653
|
+
|
|
1654
|
+
instance2.addSchema(bSchema)
|
|
1655
|
+
|
|
1656
|
+
instance2.addHook('onReady', function (done) {
|
|
1657
|
+
instance2.getSchemas()
|
|
1658
|
+
t.assert.deepStrictEqual(instance2.getSchema('b'), bSchema, 'the schema are loaded')
|
|
1659
|
+
done()
|
|
1660
|
+
})
|
|
1661
|
+
})
|
|
1662
|
+
|
|
1663
|
+
fastify.ready(err => {
|
|
1664
|
+
t.assert.ifError(err)
|
|
1665
|
+
testDone()
|
|
1666
|
+
})
|
|
1667
|
+
})
|
|
1668
|
+
|
|
1669
|
+
test('setSchemaController: Inherits correctly parent schemas with a customized validator instance', async t => {
|
|
1670
|
+
t.plan(5)
|
|
1671
|
+
const customAjv = new Ajv({ coerceTypes: false })
|
|
1672
|
+
const server = Fastify()
|
|
1673
|
+
const someSchema = {
|
|
1674
|
+
$id: 'some',
|
|
1675
|
+
type: 'array',
|
|
1676
|
+
items: {
|
|
1677
|
+
type: 'string'
|
|
1678
|
+
}
|
|
1679
|
+
}
|
|
1680
|
+
const errorResponseSchema = {
|
|
1681
|
+
$id: 'error_response',
|
|
1682
|
+
type: 'object',
|
|
1683
|
+
properties: {
|
|
1684
|
+
statusCode: {
|
|
1685
|
+
type: 'integer'
|
|
1686
|
+
},
|
|
1687
|
+
message: {
|
|
1688
|
+
type: 'string'
|
|
1689
|
+
}
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1693
|
+
server.addSchema(someSchema)
|
|
1694
|
+
server.addSchema(errorResponseSchema)
|
|
1695
|
+
|
|
1696
|
+
server.register((instance, _, done) => {
|
|
1697
|
+
instance.setSchemaController({
|
|
1698
|
+
compilersFactory: {
|
|
1699
|
+
buildValidator: function (externalSchemas) {
|
|
1700
|
+
const schemaKeys = Object.keys(externalSchemas)
|
|
1701
|
+
t.assert.strictEqual(schemaKeys.length, 2, 'Contains same number of schemas')
|
|
1702
|
+
t.assert.deepStrictEqual([someSchema, errorResponseSchema], Object.values(externalSchemas), 'Contains expected schemas')
|
|
1703
|
+
for (const key of schemaKeys) {
|
|
1704
|
+
if (customAjv.getSchema(key) == null) {
|
|
1705
|
+
customAjv.addSchema(externalSchemas[key], key)
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
return function validatorCompiler ({ schema }) {
|
|
1709
|
+
return customAjv.compile(schema)
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1712
|
+
}
|
|
1713
|
+
})
|
|
1714
|
+
|
|
1715
|
+
instance.get(
|
|
1716
|
+
'/',
|
|
1717
|
+
{
|
|
1718
|
+
schema: {
|
|
1719
|
+
querystring: {
|
|
1720
|
+
type: 'object',
|
|
1721
|
+
properties: {
|
|
1722
|
+
msg: {
|
|
1723
|
+
$ref: 'some#'
|
|
1724
|
+
}
|
|
1725
|
+
}
|
|
1726
|
+
},
|
|
1727
|
+
response: {
|
|
1728
|
+
'4xx': {
|
|
1729
|
+
$ref: 'error_response#'
|
|
1730
|
+
}
|
|
1731
|
+
}
|
|
1732
|
+
}
|
|
1733
|
+
},
|
|
1734
|
+
(req, reply) => {
|
|
1735
|
+
reply.send({ noop: 'noop' })
|
|
1736
|
+
}
|
|
1737
|
+
)
|
|
1738
|
+
|
|
1739
|
+
done()
|
|
1740
|
+
})
|
|
1741
|
+
|
|
1742
|
+
const res = await server.inject({
|
|
1743
|
+
method: 'GET',
|
|
1744
|
+
url: '/',
|
|
1745
|
+
query: {
|
|
1746
|
+
msg: 'string'
|
|
1747
|
+
}
|
|
1748
|
+
})
|
|
1749
|
+
const json = res.json()
|
|
1750
|
+
|
|
1751
|
+
t.assert.strictEqual(json.message, 'querystring/msg must be array')
|
|
1752
|
+
t.assert.strictEqual(json.statusCode, 400)
|
|
1753
|
+
t.assert.strictEqual(res.statusCode, 400, 'Should not coerce the string into array')
|
|
1754
|
+
})
|
|
1755
|
+
|
|
1756
|
+
test('setSchemaController: Inherits buildSerializer from parent if not present within the instance', async t => {
|
|
1757
|
+
t.plan(6)
|
|
1758
|
+
const customAjv = new Ajv({ coerceTypes: false })
|
|
1759
|
+
const someSchema = {
|
|
1760
|
+
$id: 'some',
|
|
1761
|
+
type: 'array',
|
|
1762
|
+
items: {
|
|
1763
|
+
type: 'string'
|
|
1764
|
+
}
|
|
1765
|
+
}
|
|
1766
|
+
const errorResponseSchema = {
|
|
1767
|
+
$id: 'error_response',
|
|
1768
|
+
type: 'object',
|
|
1769
|
+
properties: {
|
|
1770
|
+
statusCode: {
|
|
1771
|
+
type: 'integer'
|
|
1772
|
+
},
|
|
1773
|
+
message: {
|
|
1774
|
+
type: 'string'
|
|
1775
|
+
}
|
|
1776
|
+
}
|
|
1777
|
+
}
|
|
1778
|
+
let rootSerializerCalled = 0
|
|
1779
|
+
let rootValidatorCalled = 0
|
|
1780
|
+
let childValidatorCalled = 0
|
|
1781
|
+
const rootBuildSerializer = function (externalSchemas) {
|
|
1782
|
+
rootSerializerCalled++
|
|
1783
|
+
return function serializer () {
|
|
1784
|
+
return data => {
|
|
1785
|
+
return JSON.stringify({
|
|
1786
|
+
statusCode: data.statusCode,
|
|
1787
|
+
message: data.message
|
|
1788
|
+
})
|
|
1789
|
+
}
|
|
1790
|
+
}
|
|
1791
|
+
}
|
|
1792
|
+
const rootBuildValidator = function (externalSchemas) {
|
|
1793
|
+
rootValidatorCalled++
|
|
1794
|
+
return function validatorCompiler ({ schema }) {
|
|
1795
|
+
return customAjv.compile(schema)
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
1798
|
+
const server = Fastify({
|
|
1799
|
+
schemaController: {
|
|
1800
|
+
compilersFactory: {
|
|
1801
|
+
buildValidator: rootBuildValidator,
|
|
1802
|
+
buildSerializer: rootBuildSerializer
|
|
1803
|
+
}
|
|
1804
|
+
}
|
|
1805
|
+
})
|
|
1806
|
+
|
|
1807
|
+
server.addSchema(someSchema)
|
|
1808
|
+
server.addSchema(errorResponseSchema)
|
|
1809
|
+
|
|
1810
|
+
server.register((instance, _, done) => {
|
|
1811
|
+
instance.setSchemaController({
|
|
1812
|
+
compilersFactory: {
|
|
1813
|
+
buildValidator: function (externalSchemas) {
|
|
1814
|
+
childValidatorCalled++
|
|
1815
|
+
const schemaKeys = Object.keys(externalSchemas)
|
|
1816
|
+
for (const key of schemaKeys) {
|
|
1817
|
+
if (customAjv.getSchema(key) == null) {
|
|
1818
|
+
customAjv.addSchema(externalSchemas[key], key)
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
return function validatorCompiler ({ schema }) {
|
|
1822
|
+
return customAjv.compile(schema)
|
|
1823
|
+
}
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
})
|
|
1827
|
+
|
|
1828
|
+
instance.get(
|
|
1829
|
+
'/',
|
|
1830
|
+
{
|
|
1831
|
+
schema: {
|
|
1832
|
+
querystring: {
|
|
1833
|
+
type: 'object',
|
|
1834
|
+
properties: {
|
|
1835
|
+
msg: {
|
|
1836
|
+
$ref: 'some#'
|
|
1837
|
+
}
|
|
1838
|
+
}
|
|
1839
|
+
},
|
|
1840
|
+
response: {
|
|
1841
|
+
'4xx': {
|
|
1842
|
+
$ref: 'error_response#'
|
|
1843
|
+
}
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1846
|
+
},
|
|
1847
|
+
(req, reply) => {
|
|
1848
|
+
reply.send({ noop: 'noop' })
|
|
1849
|
+
}
|
|
1850
|
+
)
|
|
1851
|
+
|
|
1852
|
+
done()
|
|
1853
|
+
})
|
|
1854
|
+
|
|
1855
|
+
const res = await server.inject({
|
|
1856
|
+
method: 'GET',
|
|
1857
|
+
url: '/',
|
|
1858
|
+
query: {
|
|
1859
|
+
msg: ['string']
|
|
1860
|
+
}
|
|
1861
|
+
})
|
|
1862
|
+
const json = res.json()
|
|
1863
|
+
|
|
1864
|
+
t.assert.strictEqual(json.statusCode, 400)
|
|
1865
|
+
t.assert.strictEqual(json.message, 'querystring/msg must be array')
|
|
1866
|
+
t.assert.strictEqual(rootSerializerCalled, 1, 'Should be called from the child')
|
|
1867
|
+
t.assert.strictEqual(rootValidatorCalled, 0, 'Should not be called from the child')
|
|
1868
|
+
t.assert.strictEqual(childValidatorCalled, 1, 'Should be called from the child')
|
|
1869
|
+
t.assert.strictEqual(res.statusCode, 400, 'Should not coerce the string into array')
|
|
1870
|
+
})
|
|
1871
|
+
|
|
1872
|
+
test('setSchemaController: Inherits buildValidator from parent if not present within the instance', async t => {
|
|
1873
|
+
t.plan(6)
|
|
1874
|
+
const customAjv = new Ajv({ coerceTypes: false })
|
|
1875
|
+
const someSchema = {
|
|
1876
|
+
$id: 'some',
|
|
1877
|
+
type: 'array',
|
|
1878
|
+
items: {
|
|
1879
|
+
type: 'string'
|
|
1880
|
+
}
|
|
1881
|
+
}
|
|
1882
|
+
const errorResponseSchema = {
|
|
1883
|
+
$id: 'error_response',
|
|
1884
|
+
type: 'object',
|
|
1885
|
+
properties: {
|
|
1886
|
+
statusCode: {
|
|
1887
|
+
type: 'integer'
|
|
1888
|
+
},
|
|
1889
|
+
message: {
|
|
1890
|
+
type: 'string'
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
}
|
|
1894
|
+
let rootSerializerCalled = 0
|
|
1895
|
+
let rootValidatorCalled = 0
|
|
1896
|
+
let childSerializerCalled = 0
|
|
1897
|
+
const rootBuildSerializer = function (externalSchemas) {
|
|
1898
|
+
rootSerializerCalled++
|
|
1899
|
+
return function serializer () {
|
|
1900
|
+
return data => JSON.stringify(data)
|
|
1901
|
+
}
|
|
1902
|
+
}
|
|
1903
|
+
const rootBuildValidator = function (externalSchemas) {
|
|
1904
|
+
rootValidatorCalled++
|
|
1905
|
+
const schemaKeys = Object.keys(externalSchemas)
|
|
1906
|
+
for (const key of schemaKeys) {
|
|
1907
|
+
if (customAjv.getSchema(key) == null) {
|
|
1908
|
+
customAjv.addSchema(externalSchemas[key], key)
|
|
1909
|
+
}
|
|
1910
|
+
}
|
|
1911
|
+
return function validatorCompiler ({ schema }) {
|
|
1912
|
+
return customAjv.compile(schema)
|
|
1913
|
+
}
|
|
1914
|
+
}
|
|
1915
|
+
const server = Fastify({
|
|
1916
|
+
schemaController: {
|
|
1917
|
+
compilersFactory: {
|
|
1918
|
+
buildValidator: rootBuildValidator,
|
|
1919
|
+
buildSerializer: rootBuildSerializer
|
|
1920
|
+
}
|
|
1921
|
+
}
|
|
1922
|
+
})
|
|
1923
|
+
|
|
1924
|
+
server.register((instance, _, done) => {
|
|
1925
|
+
instance.register((subInstance, _, subDone) => {
|
|
1926
|
+
subInstance.setSchemaController({
|
|
1927
|
+
compilersFactory: {
|
|
1928
|
+
buildSerializer: function (externalSchemas) {
|
|
1929
|
+
childSerializerCalled++
|
|
1930
|
+
return function serializerCompiler () {
|
|
1931
|
+
return data => {
|
|
1932
|
+
return JSON.stringify({
|
|
1933
|
+
statusCode: data.statusCode,
|
|
1934
|
+
message: data.message
|
|
1935
|
+
})
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
}
|
|
1939
|
+
}
|
|
1940
|
+
})
|
|
1941
|
+
|
|
1942
|
+
subInstance.get(
|
|
1943
|
+
'/',
|
|
1944
|
+
{
|
|
1945
|
+
schema: {
|
|
1946
|
+
querystring: {
|
|
1947
|
+
type: 'object',
|
|
1948
|
+
properties: {
|
|
1949
|
+
msg: {
|
|
1950
|
+
$ref: 'some#'
|
|
1951
|
+
}
|
|
1952
|
+
}
|
|
1953
|
+
},
|
|
1954
|
+
response: {
|
|
1955
|
+
'4xx': {
|
|
1956
|
+
$ref: 'error_response#'
|
|
1957
|
+
}
|
|
1958
|
+
}
|
|
1959
|
+
}
|
|
1960
|
+
},
|
|
1961
|
+
(req, reply) => {
|
|
1962
|
+
reply.send({ noop: 'noop' })
|
|
1963
|
+
}
|
|
1964
|
+
)
|
|
1965
|
+
|
|
1966
|
+
subDone()
|
|
1967
|
+
})
|
|
1968
|
+
|
|
1969
|
+
done()
|
|
1970
|
+
})
|
|
1971
|
+
|
|
1972
|
+
server.addSchema(someSchema)
|
|
1973
|
+
server.addSchema(errorResponseSchema)
|
|
1974
|
+
|
|
1975
|
+
const res = await server.inject({
|
|
1976
|
+
method: 'GET',
|
|
1977
|
+
url: '/',
|
|
1978
|
+
query: {
|
|
1979
|
+
msg: ['string']
|
|
1980
|
+
}
|
|
1981
|
+
})
|
|
1982
|
+
const json = res.json()
|
|
1983
|
+
|
|
1984
|
+
t.assert.strictEqual(json.statusCode, 400)
|
|
1985
|
+
t.assert.strictEqual(json.message, 'querystring/msg must be array')
|
|
1986
|
+
t.assert.strictEqual(rootSerializerCalled, 0, 'Should be called from the child')
|
|
1987
|
+
t.assert.strictEqual(rootValidatorCalled, 1, 'Should not be called from the child')
|
|
1988
|
+
t.assert.strictEqual(childSerializerCalled, 1, 'Should be called from the child')
|
|
1989
|
+
t.assert.strictEqual(res.statusCode, 400, 'Should not coerce the string into array')
|
|
1990
|
+
})
|
|
1991
|
+
|
|
1992
|
+
test('Should throw if not default validator passed', async t => {
|
|
1993
|
+
t.plan(4)
|
|
1994
|
+
const customAjv = new Ajv({ coerceTypes: false })
|
|
1995
|
+
const someSchema = {
|
|
1996
|
+
$id: 'some',
|
|
1997
|
+
type: 'array',
|
|
1998
|
+
items: {
|
|
1999
|
+
type: 'string'
|
|
2000
|
+
}
|
|
2001
|
+
}
|
|
2002
|
+
const anotherSchema = {
|
|
2003
|
+
$id: 'another',
|
|
2004
|
+
type: 'integer'
|
|
2005
|
+
}
|
|
2006
|
+
const plugin = fp(function (pluginInstance, _, pluginDone) {
|
|
2007
|
+
pluginInstance.setSchemaController({
|
|
2008
|
+
compilersFactory: {
|
|
2009
|
+
buildValidator: function (externalSchemas) {
|
|
2010
|
+
const schemaKeys = Object.keys(externalSchemas)
|
|
2011
|
+
t.assert.strictEqual(schemaKeys.length, 2)
|
|
2012
|
+
t.assert.deepStrictEqual(schemaKeys, ['some', 'another'])
|
|
2013
|
+
|
|
2014
|
+
for (const key of schemaKeys) {
|
|
2015
|
+
if (customAjv.getSchema(key) == null) {
|
|
2016
|
+
customAjv.addSchema(externalSchemas[key], key)
|
|
2017
|
+
}
|
|
2018
|
+
}
|
|
2019
|
+
return function validatorCompiler ({ schema }) {
|
|
2020
|
+
return customAjv.compile(schema)
|
|
2021
|
+
}
|
|
2022
|
+
}
|
|
2023
|
+
}
|
|
2024
|
+
})
|
|
2025
|
+
|
|
2026
|
+
pluginDone()
|
|
2027
|
+
})
|
|
2028
|
+
const server = Fastify()
|
|
2029
|
+
|
|
2030
|
+
server.addSchema(someSchema)
|
|
2031
|
+
|
|
2032
|
+
server.register((instance, opts, done) => {
|
|
2033
|
+
instance.addSchema(anotherSchema)
|
|
2034
|
+
|
|
2035
|
+
instance.register(plugin, {})
|
|
2036
|
+
|
|
2037
|
+
instance.post(
|
|
2038
|
+
'/',
|
|
2039
|
+
{
|
|
2040
|
+
schema: {
|
|
2041
|
+
query: {
|
|
2042
|
+
type: 'object',
|
|
2043
|
+
properties: {
|
|
2044
|
+
msg: {
|
|
2045
|
+
$ref: 'some#'
|
|
2046
|
+
}
|
|
2047
|
+
}
|
|
2048
|
+
},
|
|
2049
|
+
headers: {
|
|
2050
|
+
type: 'object',
|
|
2051
|
+
properties: {
|
|
2052
|
+
'x-another': {
|
|
2053
|
+
$ref: 'another#'
|
|
2054
|
+
}
|
|
2055
|
+
}
|
|
2056
|
+
}
|
|
2057
|
+
}
|
|
2058
|
+
},
|
|
2059
|
+
(req, reply) => {
|
|
2060
|
+
reply.send({ noop: 'noop' })
|
|
2061
|
+
}
|
|
2062
|
+
)
|
|
2063
|
+
|
|
2064
|
+
done()
|
|
2065
|
+
})
|
|
2066
|
+
|
|
2067
|
+
try {
|
|
2068
|
+
const res = await server.inject({
|
|
2069
|
+
method: 'POST',
|
|
2070
|
+
url: '/',
|
|
2071
|
+
query: {
|
|
2072
|
+
msg: ['string']
|
|
2073
|
+
}
|
|
2074
|
+
})
|
|
2075
|
+
|
|
2076
|
+
t.assert.strictEqual(res.json().message, 'querystring/msg must be array')
|
|
2077
|
+
t.assert.strictEqual(res.statusCode, 400, 'Should not coerce the string into array')
|
|
2078
|
+
} catch (err) {
|
|
2079
|
+
t.assert.ifError(err)
|
|
2080
|
+
}
|
|
2081
|
+
})
|
|
2082
|
+
|
|
2083
|
+
test('Should coerce the array if the default validator is used', async t => {
|
|
2084
|
+
t.plan(2)
|
|
2085
|
+
const someSchema = {
|
|
2086
|
+
$id: 'some',
|
|
2087
|
+
type: 'array',
|
|
2088
|
+
items: {
|
|
2089
|
+
type: 'string'
|
|
2090
|
+
}
|
|
2091
|
+
}
|
|
2092
|
+
const anotherSchema = {
|
|
2093
|
+
$id: 'another',
|
|
2094
|
+
type: 'integer'
|
|
2095
|
+
}
|
|
2096
|
+
|
|
2097
|
+
const server = Fastify()
|
|
2098
|
+
|
|
2099
|
+
server.addSchema(someSchema)
|
|
2100
|
+
|
|
2101
|
+
server.register((instance, opts, done) => {
|
|
2102
|
+
instance.addSchema(anotherSchema)
|
|
2103
|
+
|
|
2104
|
+
instance.post(
|
|
2105
|
+
'/',
|
|
2106
|
+
{
|
|
2107
|
+
schema: {
|
|
2108
|
+
query: {
|
|
2109
|
+
type: 'object',
|
|
2110
|
+
properties: {
|
|
2111
|
+
msg: {
|
|
2112
|
+
$ref: 'some#'
|
|
2113
|
+
}
|
|
2114
|
+
}
|
|
2115
|
+
},
|
|
2116
|
+
headers: {
|
|
2117
|
+
type: 'object',
|
|
2118
|
+
properties: {
|
|
2119
|
+
'x-another': {
|
|
2120
|
+
$ref: 'another#'
|
|
2121
|
+
}
|
|
2122
|
+
}
|
|
2123
|
+
}
|
|
2124
|
+
}
|
|
2125
|
+
},
|
|
2126
|
+
(req, reply) => {
|
|
2127
|
+
reply.send(req.query)
|
|
2128
|
+
}
|
|
2129
|
+
)
|
|
2130
|
+
|
|
2131
|
+
done()
|
|
2132
|
+
})
|
|
2133
|
+
|
|
2134
|
+
try {
|
|
2135
|
+
const res = await server.inject({
|
|
2136
|
+
method: 'POST',
|
|
2137
|
+
url: '/',
|
|
2138
|
+
query: {
|
|
2139
|
+
msg: 'string'
|
|
2140
|
+
}
|
|
2141
|
+
})
|
|
2142
|
+
|
|
2143
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
2144
|
+
t.assert.deepStrictEqual(res.json(), { msg: ['string'] }, 'Should coerce the string into array')
|
|
2145
|
+
} catch (err) {
|
|
2146
|
+
t.assert.ifError(err)
|
|
2147
|
+
}
|
|
2148
|
+
})
|
|
2149
|
+
|
|
2150
|
+
test('Should return a human-friendly error if response status codes are not specified', (t, testDone) => {
|
|
2151
|
+
t.plan(2)
|
|
2152
|
+
const fastify = Fastify()
|
|
2153
|
+
|
|
2154
|
+
fastify.route({
|
|
2155
|
+
url: '/',
|
|
2156
|
+
method: 'GET',
|
|
2157
|
+
schema: {
|
|
2158
|
+
response: {
|
|
2159
|
+
// This should be nested under a status code key, e.g { 200: { type: 'array' } }
|
|
2160
|
+
type: 'array'
|
|
2161
|
+
}
|
|
2162
|
+
},
|
|
2163
|
+
handler: (req, reply) => {
|
|
2164
|
+
reply.send([])
|
|
2165
|
+
}
|
|
2166
|
+
})
|
|
2167
|
+
|
|
2168
|
+
fastify.ready(err => {
|
|
2169
|
+
t.assert.strictEqual(err.code, 'FST_ERR_SCH_SERIALIZATION_BUILD')
|
|
2170
|
+
t.assert.strictEqual(err.message, 'Failed building the serialization schema for GET: /, due to error response schemas should be nested under a valid status code, e.g { 2xx: { type: "object" } }')
|
|
2171
|
+
testDone()
|
|
2172
|
+
})
|
|
2173
|
+
})
|
|
2174
|
+
|
|
2175
|
+
test('setSchemaController: custom validator instance should not mutate headers schema', async t => {
|
|
2176
|
+
t.plan(2)
|
|
2177
|
+
class Headers { }
|
|
2178
|
+
const fastify = Fastify()
|
|
2179
|
+
|
|
2180
|
+
fastify.setSchemaController({
|
|
2181
|
+
compilersFactory: {
|
|
2182
|
+
buildValidator: function () {
|
|
2183
|
+
return ({ schema, method, url, httpPart }) => {
|
|
2184
|
+
t.assert.ok(schema instanceof Headers)
|
|
2185
|
+
return () => { }
|
|
2186
|
+
}
|
|
2187
|
+
}
|
|
2188
|
+
}
|
|
2189
|
+
})
|
|
2190
|
+
|
|
2191
|
+
fastify.get('/', {
|
|
2192
|
+
schema: {
|
|
2193
|
+
headers: new Headers()
|
|
2194
|
+
}
|
|
2195
|
+
}, () => { })
|
|
2196
|
+
|
|
2197
|
+
await fastify.ready()
|
|
2198
|
+
})
|