Skip to content

Commit 117dcc9

Browse files
authored
Disable the validation badge for those who do not want it (#5994)
* disabled on string values: "127.0.0.1", "localhost", "none"
1 parent 21f5149 commit 117dcc9

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

docs/usage/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Parameter name | Docker variable | Description
7070
<a name="responseInterceptor"></a>`responseInterceptor` | _Unavailable_ | `Function=(a => a)`. MUST be a function. Function to intercept remote definition, "Try it out", and OAuth 2.0 responses. Accepts one argument responseInterceptor(response) and must return the modified response, or a Promise that resolves to the modified response.
7171
<a name="showMutatedRequest"></a>`showMutatedRequest` | `SHOW_MUTATED_REQUEST` | `Boolean=true`. If set to `true`, uses the mutated request returned from a requestInterceptor to produce the curl command in the UI, otherwise the request before the requestInterceptor was applied is used.
7272
<a name="supportedSubmitMethods"></a>`supportedSubmitMethods` | `SUPPORTED_SUBMIT_METHODS` | `Array=["get", "put", "post", "delete", "options", "head", "patch", "trace"]`. List of HTTP methods that have the "Try it out" feature enabled. An empty array disables "Try it out" for all operations. This does not filter the operations from the display.
73-
<a name="validatorUrl"></a>`validatorUrl` | `VALIDATOR_URL` | `String="https://validator.swagger.io/validator" OR null`. By default, Swagger UI attempts to validate specs against swagger.io's online validator. You can use this parameter to set a different validator URL, for example for locally deployed validators ([Validator Badge](https://github.com/swagger-api/validator-badge)). Setting it to `null` will disable validation.
73+
<a name="validatorUrl"></a>`validatorUrl` | `VALIDATOR_URL` | `String="https://validator.swagger.io/validator" OR null`. By default, Swagger UI attempts to validate specs against swagger.io's online validator. You can use this parameter to set a different validator URL, for example for locally deployed validators ([Validator Badge](https://github.com/swagger-api/validator-badge)). Setting it to either `none`, `127.0.0.1` or `localhost` will disable validation.
7474
<a name="withCredentials"></a>`withCredentials` | `WITH_CREDENTIALS` | `Boolean=false` If set to `true`, enables passing credentials, [as defined in the Fetch standard](https://fetch.spec.whatwg.org/#credentials), in CORS requests that are sent by the browser. Note that Swagger UI cannot currently set cookies cross-domain (see [swagger-js#1163](https://github.com/swagger-api/swagger-js/issues/1163)) - as a result, you will have to rely on browser-supplied cookies (which this setting enables sending) that Swagger UI cannot control.
7575

7676
##### Macros

src/core/components/online-validator-badge.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from "react"
22
import URL from "url-parse"
33

44
import PropTypes from "prop-types"
5-
import { sanitizeUrl } from "core/utils"
5+
import { sanitizeUrl, requiresValidationURL } from "core/utils"
66
import win from "core/window"
77

88
export default class OnlineValidatorBadge extends React.Component {
@@ -48,8 +48,8 @@ export default class OnlineValidatorBadge extends React.Component {
4848

4949
if ( typeof spec === "object" && Object.keys(spec).length) return null
5050

51-
if (!this.state.url || !this.state.validatorUrl || this.state.url.indexOf("localhost") >= 0
52-
|| this.state.url.indexOf("127.0.0.1") >= 0) {
51+
if (!this.state.url || !requiresValidationURL(this.state.validatorUrl)
52+
|| !requiresValidationURL(this.state.url)) {
5353
return null
5454
}
5555

src/core/utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,15 @@ export function sanitizeUrl(url) {
800800
return braintreeSanitizeUrl(url)
801801
}
802802

803+
804+
export function requiresValidationURL(uri) {
805+
if (!uri || uri.indexOf("localhost") >= 0 || uri.indexOf("127.0.0.1") >= 0 || uri === "none") {
806+
return false
807+
}
808+
return true
809+
}
810+
811+
803812
export function getAcceptControllingResponse(responses) {
804813
if(!Im.OrderedMap.isOrderedMap(responses)) {
805814
// wrong type!

test/mocha/core/utils.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
getExtensions,
2424
getCommonExtensions,
2525
sanitizeUrl,
26+
requiresValidationURL,
2627
extractFileNameFromContentDispositionHeader,
2728
deeplyStripKey,
2829
getSampleSchema,
@@ -1339,7 +1340,52 @@ describe("utils", function() {
13391340
expect(sanitizeUrl({})).toEqual("")
13401341
})
13411342
})
1342-
1343+
1344+
describe("requiresValidationURL", function() {
1345+
it("Should tell us if we require a ValidationURL", function() {
1346+
const res = requiresValidationURL("https://example.com")
1347+
1348+
expect(res).toBe(true)
1349+
})
1350+
1351+
it(".. and localhost is not", function() {
1352+
const res = requiresValidationURL("http://localhost")
1353+
1354+
expect(res).toBe(false)
1355+
})
1356+
1357+
it(".. and neither does 127.0.0.1", function() {
1358+
const res = requiresValidationURL("http://127.0.0.1")
1359+
1360+
expect(res).toBe(false)
1361+
})
1362+
1363+
it(".. even without the proto", function() {
1364+
const res = requiresValidationURL("127.0.0.1")
1365+
1366+
expect(res).toBe(false)
1367+
})
1368+
1369+
it(".. and also not with 'none'", function() {
1370+
const res = requiresValidationURL("none")
1371+
1372+
expect(res).toBe(false)
1373+
})
1374+
1375+
it(".. and also not with 'none'", function() {
1376+
const res = requiresValidationURL("none")
1377+
1378+
expect(res).toBe(false)
1379+
})
1380+
1381+
it(".. and also not with ''", function() {
1382+
const res = requiresValidationURL("")
1383+
1384+
expect(res).toBe(false)
1385+
})
1386+
1387+
})
1388+
13431389
describe("getSampleSchema", function() {
13441390
const oriDate = Date
13451391

0 commit comments

Comments
 (0)