From ced7f1664574ae351d884fa18da943fdd51c3f8a Mon Sep 17 00:00:00 2001 From: Kevin Locke Date: Fri, 6 Jan 2017 00:01:20 -0700 Subject: [PATCH] [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 --- server.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ try.html | 4 ++++ 2 files changed, 55 insertions(+) diff --git a/server.js b/server.js index 5ddaafa..1474ad7 100644 --- a/server.js +++ b/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) { diff --git a/try.html b/try.html index b6ce6d3..b1799ee 100644 --- a/try.html +++ b/try.html @@ -902,6 +902,10 @@ Pixel-perfect   Retina-ready   Fast   Consistent   Hackable https://img.shields.io/amo/stars/dustman.svg + Swagger Validator: + + https://img.shields.io/swagger/valid/2.0/https/bitbucket.org/api/swagger.json.svg +

Longer Miscellaneous