Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validatorUrl option #105

Merged
merged 3 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,20 @@ await fastify.register(require('@fastify/swagger-ui', {

To ensure that models are correctly rendered at the bottom of the Swagger UI page, it's important to define your schemas using $refs through [fastify.addSchema](https://fastify.dev/docs/latest/Reference/Validation-and-Serialization/#adding-a-shared-schema). Directly embedding JSON schemas within the schema property of your route definitions in Fastify may lead to them not being displayed in Swagger UI.

#### validatorUrl

[SwaggerUI](https://github.com/swagger-api/swagger-ui/) can automatically validate the given sepcification using an online validator.
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
To enable this behavior you can pass the [`validatorUrl`](https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md) option
to this plugin which will be forwarded to SwaggerUI.

```js
fastify.register('@fastify/swagger-ui', {
validatorUrl: 'https://validator.swagger.io/validator'
})
```

Note that this behavior is disabled by default in `@fastify/swagger-ui`.

<a name="license"></a>
## License

Expand Down
6 changes: 4 additions & 2 deletions examples/dynamic-openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ fastify.register(require('@fastify/swagger'), {
hideUntagged: true
})

fastify.register(require('../index'))
fastify.register(require('../index'), {
validatorUrl: false
})

fastify.register(async function (fastify) {
fastify.put('/some-route/:id', {
Expand Down Expand Up @@ -113,6 +115,6 @@ fastify.register(async function (fastify) {
}, (req, reply) => { reply.send({ hello: `Hello ${req.body.hello}` }) })
})

fastify.listen({ port: 3000 }, err => {
fastify.listen({ port: 3000, hostname: '0.0.0.0' }, err => {
if (err) throw err
})
3 changes: 2 additions & 1 deletion lib/swagger-initializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ function swaggerInitializer (opts) {
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
layout: "StandaloneLayout",
validatorUrl: ${serialize(opts.validatorUrl)},
}, config, {
url: resolveUrl('./json').replace('static/json', 'json'),
oauth2RedirectUrl: resolveUrl('./oauth2-redirect.html')
Expand Down
Loading