Skip to content

Commit

Permalink
Add validatorUrl option (#105)
Browse files Browse the repository at this point in the history
* Add validatorUrl option

Signed-off-by: Matteo Collina <[email protected]>

* Set it to false by default

Signed-off-by: Matteo Collina <[email protected]>

* Update README.md

Co-authored-by: Igor Savin <[email protected]>

---------

Signed-off-by: Matteo Collina <[email protected]>
Co-authored-by: Aras Abbasi <[email protected]>
Co-authored-by: Igor Savin <[email protected]>
  • Loading branch information
3 people authored Dec 6, 2023
1 parent 1ab87ea commit 23ad7ff
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
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 specification using an online validator.
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

0 comments on commit 23ad7ff

Please sign in to comment.