swagger-parser
Advanced tools
Comparing version 6.0.5 to 7.0.0
@@ -6,2 +6,19 @@ # Change Log | ||
## [v7.0.0](https://github.com/APIDevTools/swagger-parser/tree/v7.0.0) (2019-06-12) | ||
#### Breaking Changes | ||
- Dropped support for Node 6 | ||
- Updated all code to ES6+ syntax (async/await, template literals, arrow functions, etc.) | ||
- No longer including a pre-built bundle in the package. such as [Webpack](https://webpack.js.org/), [Rollup](https://rollupjs.org/), [Parcel](https://parceljs.org/), or [Browserify](http://browserify.org/) to include Swagger Parser in your app | ||
#### Other Changes | ||
- Added [TypeScript definitions](lib/index.d.ts) | ||
[Full Changelog](https://github.com/APIDevTools/swagger-parser/compare/v6.0.5...v7.0.0) | ||
## [v6.0.0](https://github.com/APIDevTools/swagger-parser/tree/v6.0.0) (2018-10-05) | ||
@@ -8,0 +25,0 @@ |
179
lib/index.js
"use strict"; | ||
var validateSchema = require("./validators/schema"), | ||
validateSpec = require("./validators/spec"), | ||
normalizeArgs = require("json-schema-ref-parser/lib/normalize-args"), | ||
util = require("./util"), | ||
Options = require("./options"), | ||
maybe = require("call-me-maybe"), | ||
ono = require("ono"), | ||
$RefParser = require("json-schema-ref-parser"), | ||
dereference = require("json-schema-ref-parser/lib/dereference"); | ||
const validateSchema = require("./validators/schema"); | ||
const validateSpec = require("./validators/spec"); | ||
const normalizeArgs = require("json-schema-ref-parser/lib/normalize-args"); | ||
const util = require("./util"); | ||
const Options = require("./options"); | ||
const maybe = require("call-me-maybe"); | ||
const { ono } = require("ono"); | ||
const $RefParser = require("json-schema-ref-parser"); | ||
const dereference = require("json-schema-ref-parser/lib/dereference"); | ||
@@ -39,3 +39,3 @@ module.exports = SwaggerParser; | ||
enumerable: true, | ||
get: function () { | ||
get () { | ||
return this.schema; | ||
@@ -56,53 +56,55 @@ } | ||
*/ | ||
SwaggerParser.prototype.parse = function (path, api, options, callback) { | ||
var args = normalizeArgs(arguments); | ||
SwaggerParser.prototype.parse = async function (path, api, options, callback) { | ||
let args = normalizeArgs(arguments); | ||
args.options = new Options(args.options); | ||
return $RefParser.prototype.parse.call(this, args.path, args.schema, args.options) | ||
.then(function (schema) { | ||
if (schema.swagger) { | ||
// Verify that the parsed object is a Swagger API | ||
if (schema.swagger === undefined || schema.info === undefined || schema.paths === undefined) { | ||
throw ono.syntax("%s is not a valid Swagger API definition", args.path || args.schema); | ||
} | ||
else if (typeof schema.swagger === "number") { | ||
// This is a very common mistake, so give a helpful error message | ||
throw ono.syntax('Swagger version number must be a string (e.g. "2.0") not a number.'); | ||
} | ||
else if (typeof schema.info.version === "number") { | ||
// This is a very common mistake, so give a helpful error message | ||
throw ono.syntax('API version number must be a string (e.g. "1.0.0") not a number.'); | ||
} | ||
else if (schema.swagger !== "2.0") { | ||
throw ono.syntax("Unrecognized Swagger version: %d. Expected 2.0", schema.swagger); | ||
} | ||
try { | ||
let schema = await $RefParser.prototype.parse.call(this, args.path, args.schema, args.options); | ||
if (schema.swagger) { | ||
// Verify that the parsed object is a Swagger API | ||
if (schema.swagger === undefined || schema.info === undefined || schema.paths === undefined) { | ||
throw ono.syntax(`${args.path || args.schema} is not a valid Swagger API definition`); | ||
} | ||
else { | ||
var supportedVersions = ["3.0.0", "3.0.1", "3.0.2"]; | ||
else if (typeof schema.swagger === "number") { | ||
// This is a very common mistake, so give a helpful error message | ||
throw ono.syntax('Swagger version number must be a string (e.g. "2.0") not a number.'); | ||
} | ||
else if (typeof schema.info.version === "number") { | ||
// This is a very common mistake, so give a helpful error message | ||
throw ono.syntax('API version number must be a string (e.g. "1.0.0") not a number.'); | ||
} | ||
else if (schema.swagger !== "2.0") { | ||
throw ono.syntax(`Unrecognized Swagger version: ${schema.swagger}. Expected 2.0`); | ||
} | ||
} | ||
else { | ||
let supportedVersions = ["3.0.0", "3.0.1", "3.0.2"]; | ||
// Verify that the parsed object is a Openapi API | ||
if (schema.openapi === undefined || schema.info === undefined || schema.paths === undefined) { | ||
throw ono.syntax("%s is not a valid Openapi API definition", args.path || args.schema); | ||
} | ||
else if (typeof schema.openapi === "number") { | ||
// This is a very common mistake, so give a helpful error message | ||
throw ono.syntax('Openapi version number must be a string (e.g. "3.0.0") not a number.'); | ||
} | ||
else if (typeof schema.info.version === "number") { | ||
// This is a very common mistake, so give a helpful error message | ||
throw ono.syntax('API version number must be a string (e.g. "1.0.0") not a number.'); | ||
} | ||
else if (supportedVersions.indexOf(schema.openapi) === -1) { | ||
throw ono.syntax( | ||
"Unsupported OpenAPI version: %d. Swagger Parser only supports versions %s", | ||
schema.openapi, supportedVersions.join(", ")); | ||
} | ||
// Verify that the parsed object is a Openapi API | ||
if (schema.openapi === undefined || schema.info === undefined || schema.paths === undefined) { | ||
throw ono.syntax(`${args.path || args.schema} is not a valid Openapi API definition`); | ||
} | ||
else if (typeof schema.openapi === "number") { | ||
// This is a very common mistake, so give a helpful error message | ||
throw ono.syntax('Openapi version number must be a string (e.g. "3.0.0") not a number.'); | ||
} | ||
else if (typeof schema.info.version === "number") { | ||
// This is a very common mistake, so give a helpful error message | ||
throw ono.syntax('API version number must be a string (e.g. "1.0.0") not a number.'); | ||
} | ||
else if (supportedVersions.indexOf(schema.openapi) === -1) { | ||
throw ono.syntax( | ||
`Unsupported OpenAPI version: ${schema.openapi}. ` + | ||
`Swagger Parser only supports versions ${supportedVersions.join(", ")}` | ||
); | ||
} | ||
} | ||
// Looks good! | ||
return maybe(args.callback, Promise.resolve(schema)); | ||
}) | ||
.catch(function (err) { | ||
return maybe(args.callback, Promise.reject(err)); | ||
}); | ||
// Looks good! | ||
return maybe(args.callback, Promise.resolve(schema)); | ||
} | ||
catch (err) { | ||
return maybe(args.callback, Promise.reject(err)); | ||
} | ||
}; | ||
@@ -121,4 +123,4 @@ | ||
SwaggerParser.validate = function (path, api, options, callback) { | ||
var Class = this; // eslint-disable-line consistent-this | ||
var instance = new Class(); | ||
let Class = this; // eslint-disable-line consistent-this | ||
let instance = new Class(); | ||
return instance.validate.apply(instance, arguments); | ||
@@ -137,5 +139,5 @@ }; | ||
*/ | ||
SwaggerParser.prototype.validate = function (path, api, options, callback) { | ||
var me = this; | ||
var args = normalizeArgs(arguments); | ||
SwaggerParser.prototype.validate = async function (path, api, options, callback) { | ||
let me = this; | ||
let args = normalizeArgs(arguments); | ||
args.options = new Options(args.options); | ||
@@ -145,38 +147,39 @@ | ||
// (see https://github.com/zaggino/z-schema/issues/137) | ||
var circular$RefOption = args.options.dereference.circular; | ||
let circular$RefOption = args.options.dereference.circular; | ||
args.options.validate.schema && (args.options.dereference.circular = "ignore"); | ||
return this.dereference(args.path, args.schema, args.options) | ||
.then(function () { | ||
// Restore the original options, now that we're done dereferencing | ||
args.options.dereference.circular = circular$RefOption; | ||
try { | ||
await this.dereference(args.path, args.schema, args.options); | ||
if (args.options.validate.schema) { | ||
// Validate the API against the Swagger schema | ||
// NOTE: This is safe to do, because we haven't dereferenced circular $refs yet | ||
validateSchema(me.api); | ||
// Restore the original options, now that we're done dereferencing | ||
args.options.dereference.circular = circular$RefOption; | ||
if (me.$refs.circular) { | ||
if (circular$RefOption === true) { | ||
// The API has circular references, | ||
// so we need to do a second-pass to fully-dereference it | ||
dereference(me, args.options); | ||
} | ||
else if (circular$RefOption === false) { | ||
// The API has circular references, and they're not allowed, so throw an error | ||
throw ono.reference("The API contains circular references"); | ||
} | ||
if (args.options.validate.schema) { | ||
// Validate the API against the Swagger schema | ||
// NOTE: This is safe to do, because we haven't dereferenced circular $refs yet | ||
validateSchema(me.api); | ||
if (me.$refs.circular) { | ||
if (circular$RefOption === true) { | ||
// The API has circular references, | ||
// so we need to do a second-pass to fully-dereference it | ||
dereference(me, args.options); | ||
} | ||
else if (circular$RefOption === false) { | ||
// The API has circular references, and they're not allowed, so throw an error | ||
throw ono.reference("The API contains circular references"); | ||
} | ||
} | ||
} | ||
if (args.options.validate.spec) { | ||
// Validate the API against the Swagger spec | ||
validateSpec(me.api); | ||
} | ||
if (args.options.validate.spec) { | ||
// Validate the API against the Swagger spec | ||
validateSpec(me.api); | ||
} | ||
return maybe(args.callback, Promise.resolve(me.schema)); | ||
}) | ||
.catch(function (err) { | ||
return maybe(args.callback, Promise.reject(err)); | ||
}); | ||
return maybe(args.callback, Promise.resolve(me.schema)); | ||
} | ||
catch (err) { | ||
return maybe(args.callback, Promise.reject(err)); | ||
} | ||
}; | ||
@@ -183,0 +186,0 @@ |
"use strict"; | ||
var $RefParserOptions = require("json-schema-ref-parser/lib/options"), | ||
schemaValidator = require("./validators/schema"), | ||
specValidator = require("./validators/spec"), | ||
util = require("util"); | ||
const $RefParserOptions = require("json-schema-ref-parser/lib/options"); | ||
const schemaValidator = require("./validators/schema"); | ||
const specValidator = require("./validators/spec"); | ||
const util = require("util"); | ||
@@ -8,0 +8,0 @@ module.exports = ParserOptions; |
"use strict"; | ||
var util = require("util"); | ||
const util = require("util"); | ||
@@ -5,0 +5,0 @@ exports.format = util.format; |
"use strict"; | ||
var util = require("../util"), | ||
ono = require("ono"), | ||
ZSchema = require("z-schema"); | ||
const util = require("../util"); | ||
const { ono } = require("ono"); | ||
const ZSchema = require("z-schema"); | ||
module.exports = validateSchema; | ||
initializeZSchema(); | ||
let zSchema = initializeZSchema(); | ||
@@ -18,11 +18,11 @@ /** | ||
// Choose the appropriate schema (Swagger or OpenAPI) | ||
var schema = api.swagger | ||
let schema = api.swagger | ||
? require("swagger-schema-official/schema.json") | ||
: require("openapi-schema-validation/schema/openapi-3.0.json"); | ||
var isValid = ZSchema.validate(api, schema); | ||
let isValid = zSchema.validate(api, schema); | ||
if (!isValid) { | ||
var err = ZSchema.getLastError(); | ||
var message = "Swagger schema validation failed. \n" + formatZSchemaError(err.details); | ||
let err = zSchema.getLastError(); | ||
let message = "Swagger schema validation failed. \n" + formatZSchemaError(err.details); | ||
throw ono.syntax(err, { details: err.details }, message); | ||
@@ -36,3 +36,3 @@ } | ||
function initializeZSchema () { | ||
ZSchema = new ZSchema({ | ||
return new ZSchema({ | ||
breakOnFirstError: true, | ||
@@ -55,10 +55,10 @@ noExtraKeywords: true, | ||
indent = indent || " "; | ||
var message = ""; | ||
errors.forEach(function (error, index) { | ||
message += util.format("%s%s at #/%s\n", indent, error.message, error.path.join("/")); | ||
let message = ""; | ||
for (let error of errors) { | ||
message += util.format(`${indent}${error.message} at #/${error.path.join("/")}\n`); | ||
if (error.inner) { | ||
message += formatZSchemaError(error.inner, indent + " "); | ||
} | ||
}); | ||
} | ||
return message; | ||
} |
"use strict"; | ||
var util = require("../util"), | ||
ono = require("ono"), | ||
swaggerMethods = require("swagger-methods"), | ||
primitiveTypes = ["array", "boolean", "integer", "number", "string"], | ||
schemaTypes = ["array", "boolean", "integer", "number", "string", "object", "null", undefined]; | ||
const util = require("../util"); | ||
const { ono } = require("ono"); | ||
const swaggerMethods = require("swagger-methods"); | ||
const primitiveTypes = ["array", "boolean", "integer", "number", "string"]; | ||
const schemaTypes = ["array", "boolean", "integer", "number", "string", "object", "null", undefined]; | ||
@@ -22,7 +22,7 @@ module.exports = validateSpec; | ||
var paths = Object.keys(api.paths || {}); | ||
var operationIds = []; | ||
paths.forEach(function (pathName) { | ||
var path = api.paths[pathName]; | ||
var pathId = "/paths" + pathName; | ||
let paths = Object.keys(api.paths || {}); | ||
let operationIds = []; | ||
for (let pathName of paths) { | ||
let path = api.paths[pathName]; | ||
let pathId = "/paths" + pathName; | ||
@@ -32,9 +32,10 @@ if (path && pathName.indexOf("/") === 0) { | ||
} | ||
}); | ||
var definitions = Object.keys(api.definitions || {}); | ||
definitions.forEach(function (definitionName) { | ||
var definition = api.definitions[definitionName]; | ||
var definitionId = "/definitions/" + definitionName; | ||
} | ||
let definitions = Object.keys(api.definitions || {}); | ||
for (let definitionName of definitions) { | ||
let definition = api.definitions[definitionName]; | ||
let definitionId = "/definitions/" + definitionName; | ||
validateRequiredPropertiesExist(definition, definitionId); | ||
}); | ||
} | ||
} | ||
@@ -51,8 +52,8 @@ | ||
function validatePath (api, path, pathId, operationIds) { | ||
swaggerMethods.forEach(function (operationName) { | ||
var operation = path[operationName]; | ||
var operationId = pathId + "/" + operationName; | ||
for (let operationName of swaggerMethods) { | ||
let operation = path[operationName]; | ||
let operationId = pathId + "/" + operationName; | ||
if (operation) { | ||
var declaredOperationId = operation.operationId; | ||
let declaredOperationId = operation.operationId; | ||
if (declaredOperationId) { | ||
@@ -63,3 +64,3 @@ if (operationIds.indexOf(declaredOperationId) === -1) { | ||
else { | ||
throw ono.syntax("Validation failed. Duplicate operation id '%s'", declaredOperationId); | ||
throw ono.syntax(`Validation failed. Duplicate operation id '${declaredOperationId}'`); | ||
} | ||
@@ -69,10 +70,10 @@ } | ||
var responses = Object.keys(operation.responses || {}); | ||
responses.forEach(function (responseName) { | ||
var response = operation.responses[responseName]; | ||
var responseId = operationId + "/responses/" + responseName; | ||
let responses = Object.keys(operation.responses || {}); | ||
for (let responseName of responses) { | ||
let response = operation.responses[responseName]; | ||
let responseId = operationId + "/responses/" + responseName; | ||
validateResponse(responseName, (response || {}), responseId); | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
@@ -90,4 +91,4 @@ | ||
function validateParameters (api, path, pathId, operation, operationId) { | ||
var pathParams = path.parameters || []; | ||
var operationParams = operation.parameters || []; | ||
let pathParams = path.parameters || []; | ||
let operationParams = operation.parameters || []; | ||
@@ -99,3 +100,3 @@ // Check for duplicate path parameters | ||
catch (e) { | ||
throw ono.syntax(e, "Validation failed. %s has duplicate parameters", pathId); | ||
throw ono.syntax(e, `Validation failed. ${pathId} has duplicate parameters`); | ||
} | ||
@@ -108,3 +109,3 @@ | ||
catch (e) { | ||
throw ono.syntax(e, "Validation failed. %s has duplicate parameters", operationId); | ||
throw ono.syntax(e, `Validation failed. ${operationId} has duplicate parameters`); | ||
} | ||
@@ -114,4 +115,4 @@ | ||
// with the operation params taking precedence over the path params | ||
var params = pathParams.reduce(function (combinedParams, value) { | ||
var duplicate = combinedParams.some(function (param) { | ||
let params = pathParams.reduce((combinedParams, value) => { | ||
let duplicate = combinedParams.some((param) => { | ||
return param.in === value.in && param.name === value.name; | ||
@@ -137,4 +138,4 @@ }); | ||
function validateBodyParameters (params, operationId) { | ||
var bodyParams = params.filter(function (param) { return param.in === "body"; }); | ||
var formParams = params.filter(function (param) { return param.in === "formData"; }); | ||
let bodyParams = params.filter((param) => { return param.in === "body"; }); | ||
let formParams = params.filter((param) => { return param.in === "formData"; }); | ||
@@ -144,4 +145,3 @@ // There can only be one "body" parameter | ||
throw ono.syntax( | ||
"Validation failed. %s has %d body parameters. Only one is allowed.", | ||
operationId, bodyParams.length | ||
`Validation failed. ${operationId} has ${bodyParams.length} body parameters. Only one is allowed.`, | ||
); | ||
@@ -152,4 +152,3 @@ } | ||
throw ono.syntax( | ||
"Validation failed. %s has body parameters and formData parameters. Only one or the other is allowed.", | ||
operationId | ||
`Validation failed. ${operationId} has body parameters and formData parameters. Only one or the other is allowed.`, | ||
); | ||
@@ -168,10 +167,10 @@ } | ||
// Find all {placeholders} in the path string | ||
var placeholders = pathId.match(util.swaggerParamRegExp) || []; | ||
let placeholders = pathId.match(util.swaggerParamRegExp) || []; | ||
// Check for duplicates | ||
for (var i = 0; i < placeholders.length; i++) { | ||
for (var j = i + 1; j < placeholders.length; j++) { | ||
for (let i = 0; i < placeholders.length; i++) { | ||
for (let j = i + 1; j < placeholders.length; j++) { | ||
if (placeholders[i] === placeholders[j]) { | ||
throw ono.syntax( | ||
"Validation failed. %s has multiple path placeholders named %s", operationId, placeholders[i]); | ||
`Validation failed. ${operationId} has multiple path placeholders named ${placeholders[i]}`); | ||
} | ||
@@ -181,27 +180,23 @@ } | ||
params | ||
.filter(function (param) { return param.in === "path"; }) | ||
.forEach(function (param) { | ||
if (param.required !== true) { | ||
throw ono.syntax( | ||
'Validation failed. Path parameters cannot be optional. Set required=true for the "%s" parameter at %s', | ||
param.name, | ||
operationId | ||
); | ||
} | ||
var match = placeholders.indexOf("{" + param.name + "}"); | ||
if (match === -1) { | ||
throw ono.syntax( | ||
'Validation failed. %s has a path parameter named "%s", ' + | ||
"but there is no corresponding {%s} in the path string", | ||
operationId, | ||
param.name, | ||
param.name | ||
); | ||
} | ||
placeholders.splice(match, 1); | ||
}); | ||
params = params.filter((param) => { return param.in === "path"; }); | ||
for (let param of params) { | ||
if (param.required !== true) { | ||
throw ono.syntax( | ||
"Validation failed. Path parameters cannot be optional. " + | ||
`Set required=true for the "${param.name}" parameter at ${operationId}`, | ||
); | ||
} | ||
let match = placeholders.indexOf("{" + param.name + "}"); | ||
if (match === -1) { | ||
throw ono.syntax( | ||
`Validation failed. ${operationId} has a path parameter named "${param.name}", ` + | ||
`but there is no corresponding {${param.name}} in the path string` | ||
); | ||
} | ||
placeholders.splice(match, 1); | ||
} | ||
if (placeholders.length > 0) { | ||
throw ono.syntax("Validation failed. %s is missing path parameter(s) for %s", operationId, placeholders); | ||
throw ono.syntax(`Validation failed. ${operationId} is missing path parameter(s) for ${placeholders}`); | ||
} | ||
@@ -219,5 +214,5 @@ } | ||
function validateParameterTypes (params, api, operation, operationId) { | ||
params.forEach(function (param) { | ||
var parameterId = operationId + "/parameters/" + param.name; | ||
var schema, validTypes; | ||
for (let param of params) { | ||
let parameterId = operationId + "/parameters/" + param.name; | ||
let schema, validTypes; | ||
@@ -243,8 +238,8 @@ switch (param.in) { | ||
// "file" params must consume at least one of these MIME types | ||
var formData = /multipart\/(.*\+)?form-data/; | ||
var urlEncoded = /application\/(.*\+)?x-www-form-urlencoded/; | ||
let formData = /multipart\/(.*\+)?form-data/; | ||
let urlEncoded = /application\/(.*\+)?x-www-form-urlencoded/; | ||
var consumes = operation.consumes || api.consumes || []; | ||
let consumes = operation.consumes || api.consumes || []; | ||
var hasValidMimeType = consumes.some(function (consume) { | ||
let hasValidMimeType = consumes.some((consume) => { | ||
return formData.test(consume) || urlEncoded.test(consume); | ||
@@ -255,9 +250,8 @@ }); | ||
throw ono.syntax( | ||
"Validation failed. %s has a file parameter, so it must consume multipart/form-data " + | ||
`Validation failed. ${operationId} has a file parameter, so it must consume multipart/form-data ` + | ||
"or application/x-www-form-urlencoded", | ||
operationId | ||
); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
@@ -271,8 +265,8 @@ | ||
function checkForDuplicates (params) { | ||
for (var i = 0; i < params.length - 1; i++) { | ||
var outer = params[i]; | ||
for (var j = i + 1; j < params.length; j++) { | ||
var inner = params[j]; | ||
for (let i = 0; i < params.length - 1; i++) { | ||
let outer = params[i]; | ||
for (let j = i + 1; j < params.length; j++) { | ||
let inner = params[j]; | ||
if (outer.name === inner.name && outer.in === inner.in) { | ||
throw ono.syntax('Validation failed. Found multiple %s parameters named "%s"', outer.in, outer.name); | ||
throw ono.syntax(`Validation failed. Found multiple ${outer.in} parameters named "${outer.name}"`); | ||
} | ||
@@ -292,17 +286,17 @@ } | ||
if (code !== "default" && (code < 100 || code > 599)) { | ||
throw ono.syntax("Validation failed. %s has an invalid response code (%s)", responseId, code); | ||
throw ono.syntax(`Validation failed. ${responseId} has an invalid response code (${code})`); | ||
} | ||
var headers = Object.keys(response.headers || {}); | ||
headers.forEach(function (headerName) { | ||
var header = response.headers[headerName]; | ||
var headerId = responseId + "/headers/" + headerName; | ||
let headers = Object.keys(response.headers || {}); | ||
for (let headerName of headers) { | ||
let header = response.headers[headerName]; | ||
let headerId = responseId + "/headers/" + headerName; | ||
validateSchema(header, headerId, primitiveTypes); | ||
}); | ||
} | ||
if (response.schema) { | ||
var validTypes = schemaTypes.concat("file"); | ||
let validTypes = schemaTypes.concat("file"); | ||
if (validTypes.indexOf(response.schema.type) === -1) { | ||
throw ono.syntax( | ||
"Validation failed. %s has an invalid response schema type (%s)", responseId, response.schema.type); | ||
`Validation failed. ${responseId} has an invalid response schema type (${response.schema.type})`); | ||
} | ||
@@ -325,7 +319,7 @@ else { | ||
throw ono.syntax( | ||
"Validation failed. %s has an invalid type (%s)", schemaId, schema.type); | ||
`Validation failed. ${schemaId} has an invalid type (${schema.type})`); | ||
} | ||
if (schema.type === "array" && !schema.items) { | ||
throw ono.syntax('Validation failed. %s is an array, so it must include an "items" schema', schemaId); | ||
throw ono.syntax(`Validation failed. ${schemaId} is an array, so it must include an "items" schema`); | ||
} | ||
@@ -346,3 +340,3 @@ } | ||
if (schemaObj.properties) { | ||
for (var property in schemaObj.properties) { | ||
for (let property in schemaObj.properties) { | ||
if (schemaObj.properties.hasOwnProperty(property)) { | ||
@@ -354,5 +348,5 @@ props[property] = schemaObj.properties[property]; | ||
if (schemaObj.allOf) { | ||
schemaObj.allOf.forEach(function (parent) { | ||
for (let parent of schemaObj.allOf) { | ||
collectProperties(parent, props); | ||
}); | ||
} | ||
} | ||
@@ -362,11 +356,12 @@ } | ||
if (schema.required && Array.isArray(schema.required)) { | ||
var props = {}; | ||
let props = {}; | ||
collectProperties(schema, props); | ||
schema.required.forEach(function (requiredProperty) { | ||
for (let requiredProperty of schema.required) { | ||
if (!props[requiredProperty]) { | ||
throw ono.syntax("Validation failed. Property '%s' listed as required but does not exist in '%s'", | ||
requiredProperty, schemaId); | ||
throw ono.syntax( | ||
`Validation failed. Property '${requiredProperty}' listed as required but does not exist in '${schemaId}'` | ||
); | ||
} | ||
}); | ||
} | ||
} | ||
} |
{ | ||
"name": "swagger-parser", | ||
"version": "6.0.5", | ||
"version": "7.0.0", | ||
"description": "Swagger 2.0 and OpenAPI 3.0 parser and validator for Node and browsers", | ||
@@ -33,7 +33,4 @@ "keywords": [ | ||
"main": "lib/index.js", | ||
"typings": "lib/index.d.ts", | ||
"files": [ | ||
"dist/swagger-parser.js", | ||
"dist/swagger-parser.js.map", | ||
"dist/swagger-parser.min.js", | ||
"dist/swagger-parser.min.js.map", | ||
"lib" | ||
@@ -43,53 +40,46 @@ ], | ||
"lint": "eslint lib test online/src/js", | ||
"build": "npm run build:browser && npm run build:website && npm run build:sass", | ||
"build:browser": "simplifyify lib/index.js --outfile dist/swagger-parser.js --standalone SwaggerParser --bundle --debug --minify", | ||
"build": "npm run build:website && npm run build:sass", | ||
"build:website": "simplifyify online/src/js/index.js --outfile online/js/bundle.js --bundle --debug --minify", | ||
"build:sass": "node-sass --source-map true --output-style compressed online/src/scss/style.scss online/css/style.min.css", | ||
"test": "npm run test:node && npm run test:browser", | ||
"test": "npm run test:node && npm run test:browser && npm run test:typescript && npm run lint", | ||
"test:node": "mocha", | ||
"test:browser": "karma start --single-run", | ||
"test:typescript": "tsc --noEmit --strict --lib esnext test/specs/typescript-definition.spec.ts", | ||
"coverage": "npm run coverage:node && npm run coverage:browser", | ||
"coverage:node": "nyc --reporter=text --reporter=lcov --report-dir coverage/node mocha", | ||
"coverage:browser": "npm run build:browser -- --coverage && npm run test:browser -- --coverage", | ||
"coverage:browser": "npm run test:browser -- --coverage", | ||
"upgrade": "npm-check -u", | ||
"bump": "bump --prompt --grep dist/* --tag --push --all", | ||
"release": "npm run upgrade && npm run build && npm test && npm run bump", | ||
"start": "http-server -o -c-1" | ||
"bump": "bump --tag --push --all", | ||
"release": "npm run upgrade && npm run build && npm test && npm run bump" | ||
}, | ||
"devDependencies": { | ||
"@babel/polyfill": "^7.4.4", | ||
"@types/node": "^12.0.7", | ||
"chai": "^4.2.0", | ||
"coveralls": "^3.0.2", | ||
"eslint": "^5.12.0", | ||
"eslint-config-modular": "^6.0.0", | ||
"http-server": "^0.11.1", | ||
"karma": "^4.0.0", | ||
"karma-chai": "^0.1.0", | ||
"karma-chrome-launcher": "^2.2.0", | ||
"coveralls": "^3.0.4", | ||
"eslint": "^5.16.0", | ||
"eslint-config-modular": "^7.0.0", | ||
"host-environment": "^1.1.3", | ||
"karma": "^4.1.0", | ||
"karma-cli": "^2.0.0", | ||
"karma-coverage": "^1.1.1", | ||
"karma-edge-launcher": "^0.4.2", | ||
"karma-firefox-launcher": "^1.1.0", | ||
"karma-host-environment": "^1.1.7", | ||
"karma-ie-launcher": "^1.0.0", | ||
"karma-mocha": "^1.3.0", | ||
"karma-safari-launcher": "^1.0.0", | ||
"karma-sauce-launcher": "^2.0.2", | ||
"karma-verbose-reporter": "0.0.6", | ||
"mocha": "^5.0.4", | ||
"node-sass": "^4.11.0", | ||
"karma-config": "^1.5.2", | ||
"mocha": "^6.1.4", | ||
"node-fetch": "^2.6.0", | ||
"node-sass": "^4.12.0", | ||
"npm-check": "^5.9.0", | ||
"nyc": "^13.1.0", | ||
"simplifyify": "^7.0.1", | ||
"superagent": "^4.1.0", | ||
"version-bump-prompt": "^4.2.2" | ||
"nyc": "^14.1.1", | ||
"openapi-types": "^1.3.5", | ||
"simplifyify": "^7.0.2", | ||
"typescript": "^3.5.1", | ||
"version-bump-prompt": "^5.0.2" | ||
}, | ||
"dependencies": { | ||
"call-me-maybe": "^1.0.1", | ||
"json-schema-ref-parser": "^6.0.3", | ||
"ono": "^4.0.11", | ||
"json-schema-ref-parser": "^7.0.1", | ||
"ono": "^5.0.1", | ||
"openapi-schema-validation": "^0.4.2", | ||
"swagger-methods": "^1.0.8", | ||
"swagger-methods": "^2.0.0", | ||
"swagger-schema-official": "2.0.0-bab6bed", | ||
"z-schema": "^3.24.2" | ||
"z-schema": "^4.1.0" | ||
} | ||
} | ||
} |
@@ -23,3 +23,3 @@ Swagger 2.0 and OpenAPI 3.0 parser/validator | ||
- Can [dereference](https://apidevtools.org/swagger-parser/docs/swagger-parser.html#dereferenceapi-options-callback) all `$ref` pointers, giving you a normal JavaScript object that's easy to work with | ||
- **[Tested](https://apidevtools.org/swagger-parser/test/)** in Node.js and all modern web browsers on Mac, Windows, and Linux | ||
- **[Tested](https://travis-ci.com/APIDevTools/swagger-parser)** in Node.js and all modern web browsers on Mac, Windows, and Linux | ||
- Tested on **[over 1,000 real-world APIs](https://apis.guru/browse-apis/)** from Google, Instagram, Spotify, etc. | ||
@@ -40,3 +40,3 @@ - Supports [circular references](https://apidevtools.org/swagger-parser/docs/#circular-refs), nested references, back-references, and cross-references | ||
```javascript | ||
SwaggerParser.validate(myAPI, function(err, api) { | ||
SwaggerParser.validate(myAPI, (err, api) => { | ||
if (err) { | ||
@@ -51,12 +51,12 @@ console.error(err); | ||
Or use [Promises syntax](http://javascriptplayground.com/blog/2015/02/promises/) instead. The following example is the same as above: | ||
Or use `async`/`await` or [Promise](http://javascriptplayground.com/blog/2015/02/promises/) syntax instead. The following example is the same as above: | ||
```javascript | ||
SwaggerParser.validate(myAPI) | ||
.then(function(api) { | ||
console.log("API name: %s, Version: %s", api.info.title, api.info.version); | ||
}) | ||
.catch(function(err) { | ||
console.error(err); | ||
}); | ||
try { | ||
let api = await SwaggerParser.validate(myAPI); | ||
console.log("API name: %s, Version: %s", api.info.title, api.info.version); | ||
} | ||
catch(err) { | ||
console.error(err); | ||
} | ||
``` | ||
@@ -67,5 +67,5 @@ | ||
Installation | ||
-------------------------- | ||
#### Node | ||
Install using [npm](https://docs.npmjs.com/about-npm/): | ||
@@ -77,26 +77,28 @@ | ||
Then require it in your code: | ||
Usage | ||
-------------------------- | ||
When using Swagger Parser in Node.js apps, you'll probably want to use **CommonJS** syntax: | ||
```javascript | ||
var SwaggerParser = require('swagger-parser'); | ||
const SwaggerParser = require("swagger-parser"); | ||
``` | ||
#### Web Browsers | ||
Reference [`swagger-parser.js`](dist/swagger-parser.js) or [`swagger-parser.min.js`](dist/swagger-parser.min.js) in your HTML: | ||
When using a transpiler such as [Babel](https://babeljs.io/) or [TypeScript](https://www.typescriptlang.org/), or a bundler such as [Webpack](https://webpack.js.org/) or [Rollup](https://rollupjs.org/), you can use **ECMAScript modules** syntax instead: | ||
```html | ||
<script src="https://cdn.rawgit.com/JS-DevTools/swagger-parser/dist/swagger-parser.js"></script> | ||
<script> | ||
SwaggerParser.validate(myAPI, function(err, api) { | ||
if (err) { | ||
console.error(err); | ||
} | ||
else { | ||
console.log("API name: %s, Version: %s", api.info.title, api.info.version); | ||
} | ||
}); | ||
</script> | ||
```javascript | ||
import SwaggerParser from "swagger-parser"; | ||
``` | ||
Browser support | ||
-------------------------- | ||
Swagger Parser supports recent versions of every major web browser. Older browsers may require [Babel](https://babeljs.io/) and/or [polyfills](https://babeljs.io/docs/en/next/babel-polyfill). | ||
To use Swagger Parser in a browser, you'll need to use a bundling tool such as [Webpack](https://webpack.js.org/), [Rollup](https://rollupjs.org/), [Parcel](https://parceljs.org/), or [Browserify](http://browserify.org/). Some bundlers may require a bit of configuration, such as setting `browser: true` in [rollup-plugin-resolve](https://github.com/rollup/rollup-plugin-node-resolve). | ||
API Documentation | ||
@@ -126,6 +128,3 @@ -------------------------- | ||
5. __Start the local web server__<br> | ||
`npm start` (then browse to [http://localhost:8080/test/](http://localhost:8080/test/)) | ||
License | ||
@@ -132,0 +131,0 @@ -------------------------- |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
19
1
0
1
70414
10
956
135
+ Addedjson-schema-ref-parser@7.1.4(transitive)
+ Addedono@5.1.06.0.1(transitive)
+ Addedswagger-methods@2.0.2(transitive)
+ Addedvalidator@13.12.0(transitive)
+ Addedz-schema@4.2.4(transitive)
- Removedcore-js@2.6.12(transitive)
- Removedformat-util@1.0.5(transitive)
- Removedjson-schema-ref-parser@6.1.0(transitive)
- Removedono@4.0.11(transitive)
- Removedswagger-methods@1.0.8(transitive)
- Removedvalidator@10.11.0(transitive)
- Removedz-schema@3.25.1(transitive)
Updatedono@^5.0.1
Updatedswagger-methods@^2.0.0
Updatedz-schema@^4.1.0