[PATCH v2] Add support for swagger.io validator
The OpenAPI Specification (formerly known as the Swagger RESTful API Documentation Specification) defines a format for describing RESTful APIs. The Swagger project provides a set of tools for working with this format, including a hosted validator which provides a validation badge and JSON result.[1] This commit adds shields.io support for this badge. The service currently only provides validation of files conforming to version 2.0 of the OpenAPI Specification. This commit adds a path component for specifying the version under the assumption that the validator may support version 3.0 or later as they are released. It accepts the URL to validate as two path components, a scheme followed by the rest of the URL, to match the convention used for the JIRA host and webpage online shields. Changes in v2: - Use bitbucket in try.html example for clarity. - Change /v/ in URL to /valid/ to avoid conflict with v=version. 1. https://github.com/swagger-api/validator-badge Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
This commit is contained in:
parent
84e2d6d7e3
commit
ced7f16645
51
server.js
51
server.js
|
@ -5769,6 +5769,57 @@ cache(function(data, match, sendBadge, request) {
|
|||
});
|
||||
}));
|
||||
|
||||
// Swagger Validator integration.
|
||||
camp.route(/^\/swagger\/(valid)\/(2\.0)\/(https?)\/(.+)\.(svg|png|gif|jpg|json)$/,
|
||||
cache(function(data, match, sendBadge, request) {
|
||||
var type = match[1]; // e.g. `valid` for validate
|
||||
var specVer = match[2]; // e.g. `2.0` for OpenAPI 2.0
|
||||
var scheme = match[3]; // e.g. `https`
|
||||
var swaggerUrl = match[4]; // e.g. `api.example.com/swagger.yaml`
|
||||
var format = match[5];
|
||||
|
||||
var badgeData = getBadgeData('swagger', data);
|
||||
|
||||
var urlParam = encodeURIComponent(scheme + '://' + swaggerUrl);
|
||||
var url = 'http://online.swagger.io/validator/debug?url=' + urlParam;
|
||||
var options = {
|
||||
method: 'GET',
|
||||
url: url,
|
||||
gzip: true,
|
||||
json: true
|
||||
};
|
||||
request(options, function(err, res, json) {
|
||||
try {
|
||||
if (err != null || res.statusCode >= 500 || typeof json !== 'object') {
|
||||
badgeData.text[1] = 'inaccessible';
|
||||
sendBadge(format, badgeData);
|
||||
return;
|
||||
}
|
||||
|
||||
var messages = json.schemaValidationMessages;
|
||||
if (messages == null || messages.length === 0) {
|
||||
badgeData.colorscheme = 'brightgreen';
|
||||
badgeData.text[1] = 'valid';
|
||||
} else {
|
||||
badgeData.colorscheme = 'red';
|
||||
|
||||
var firstMessage = messages[0];
|
||||
if (messages.length === 1 &&
|
||||
firstMessage.level === 'error' &&
|
||||
/^Can't read from/.test(firstMessage.message)) {
|
||||
badgeData.text[1] = 'not found';
|
||||
} else {
|
||||
badgeData.text[1] = 'invalid';
|
||||
}
|
||||
}
|
||||
sendBadge(format, badgeData);
|
||||
} catch (e) {
|
||||
badgeData.text[1] = 'inaccessible';
|
||||
sendBadge(format, badgeData);
|
||||
}
|
||||
});
|
||||
}));
|
||||
|
||||
// Any badge.
|
||||
camp.route(/^\/(:|badge\/)(([^-]|--)*?)-(([^-]|--)*)-(([^-]|--)+)\.(svg|png|gif|jpg)$/,
|
||||
function(data, match, end, ask) {
|
||||
|
|
4
try.html
4
try.html
|
@ -902,6 +902,10 @@ Pixel-perfect Retina-ready Fast Consistent Hackable
|
|||
<td><img src='/amo/stars/dustman.svg' alt=''/></td>
|
||||
<td><code>https://img.shields.io/amo/stars/dustman.svg</code></td>
|
||||
</tr>
|
||||
<tr><th> Swagger Validator: </th>
|
||||
<td><img src='/swagger/valid/2.0/https/bitbucket.org/api/swagger.json.svg' alt=''/></td>
|
||||
<td><code>https://img.shields.io/swagger/valid/2.0/https/bitbucket.org/api/swagger.json.svg</code></td>
|
||||
</tr>
|
||||
</tbody></table>
|
||||
|
||||
<h3 id="miscellaneous"> Longer Miscellaneous </h3>
|
||||
|
|
Loading…
Reference in New Issue
Block a user