openapi-schema-validation
Advanced tools
Comparing version 0.2.1 to 0.4.0
20
index.js
@@ -6,6 +6,8 @@ var loggingKey = require('./package.json').name; | ||
var jsonSchema = require('jsonschema-draft4'); | ||
var swaggerSchema = require('swagger-schema-official/schema.json'); | ||
var swagger2Schema = require('swagger-schema-official/schema.json'); | ||
var swagger3Schema = require('./schema/openapi-3.0.json'); | ||
v.addSchema(jsonSchema); | ||
v.addSchema(swaggerSchema); | ||
v.addSchema(swagger2Schema); | ||
v.addSchema(swagger3Schema); | ||
@@ -16,4 +18,14 @@ module.exports = { | ||
function validate(openapiDoc) { | ||
return v.validate(openapiDoc, swaggerSchema); | ||
/** | ||
* Runs specified validator against the provided openapiDocument and returns the results. | ||
* Previously the second param was unused. To maintain backward compatibility, if anything other than a | ||
* Number is passed, the v2 validator will be used. | ||
* | ||
* @param openapiDoc {object} to be validated | ||
* @param version {Number} defaults to 2 if unspecified or invalid type is passed | ||
* @returns {object} results from the validator | ||
*/ | ||
function validate(openapiDoc, version) { | ||
version = parseInt(version, 10) || 2; // default to swagger 2.0 validation | ||
return v.validate(openapiDoc, version === 2 ? swagger2Schema : swagger3Schema); | ||
} |
{ | ||
"name": "openapi-schema-validation", | ||
"version": "0.2.1", | ||
"version": "0.4.0", | ||
"description": "Validate openapi documents.", | ||
"scripts": { | ||
"cover": "istanbul cover _mocha -- ./test/*.js", | ||
"coveralls": "cat ./coverage/lcov.info | coveralls -v", | ||
"test-watch": "mocha -w ./test/*.js ./test/**/*.js", | ||
"test": "mocha ./test/*.js" | ||
"test": "mocha ./test/*.js", | ||
"travis-test": "npm run cover" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/kogosoftwarellc/openapi-schema-validation.git" | ||
"url": "git+https://github.com/kogosoftwarellc/open-api.git" | ||
}, | ||
@@ -23,8 +23,7 @@ "keywords": [ | ||
"bugs": { | ||
"url": "https://github.com/kogosoftwarellc/openapi-schema-validation/issues" | ||
"url": "https://github.com/kogosoftwarellc/open-api/issues" | ||
}, | ||
"homepage": "https://github.com/kogosoftwarellc/openapi-schema-validation#readme", | ||
"homepage": "https://github.com/kogosoftwarellc/open-api/tree/master/packages/openapi-schema-validation#readme", | ||
"devDependencies": { | ||
"chai": "^3.4.1", | ||
"coveralls": "^2.11.6", | ||
"glob": "^6.0.3", | ||
@@ -31,0 +30,0 @@ "istanbul": "^0.4.1", |
# openapi-schema-validation [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coveralls Status][coveralls-image]][coveralls-url] | ||
> Validate openapi documents. | ||
For OpenAPI v2.0 (a.k.a. swagger 2.0) document examples and documentation, view | ||
[the official docs](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#itemsObject). | ||
##For OpenAPI v2.0 (a.k.a. swagger 2.0) and OpenAPI v3.0.0 | ||
####Document examples and full specs: | ||
* [Official 2.0 docs](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#itemsObject) | ||
* [Official 3.0.0 docs](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md) | ||
## Highlights | ||
* Validate openapi documents against official openapi schema documents. | ||
* Validate openapi documents against openapi schema documents. | ||
* Uses [jsonschema](https://github.com/tdegrunt/jsonschema) under the hood. | ||
@@ -16,22 +18,22 @@ * Performant. | ||
* Small footprint. | ||
* Currently supports openapi 2.0 (a.k.a. swagger 2.0) documents. | ||
* Supports openapi 2.0 (a.k.a. swagger 2.0) documents and openapi 3.0.0 | ||
## Example | ||
**Huge thank you to the [gnostic](https://github.com/googleapis/gnostic) project for building up a 3.0.0 JSON schema.** | ||
See `./test/data-driven/*.js` for more examples. | ||
## Example | ||
```javascript | ||
var validateSchema = require('openapi-schema-validation').validate; | ||
console.log(validateSchema({/* openapi doc */}, {version: 'swagger-2.0'}); | ||
console.log(validateSchema(apiDoc, version)); | ||
``` | ||
[see here](https://github.com/tdegrunt/jsonschema#results) for example results. | ||
## API | ||
### .validate(apiDoc [, args]) | ||
### .validate(apiDoc, version) | ||
* `apiDoc` _object_ is any api document you wish to validate. | ||
* `version` _optional number_ openapi document schema version to use (2 or 3). | ||
* 2 - `swagger-2.0` (default) | ||
* 3 - `openapi-3.0.0` | ||
`apiDoc` is any api document you wish to validate. | ||
`[args]` is an optional object the accepts the following arguments: | ||
* `version` - The openapi document schema version to use. Currently the only supported | ||
version is `swagger-2.0` (the default). | ||
## LICENSE | ||
@@ -66,6 +68,6 @@ `````` | ||
[travis-url]: https://travis-ci.org/kogosoftwarellc/openapi-schema-validation | ||
[travis-image]: http://img.shields.io/travis/kogosoftwarellc/openapi-schema-validation.svg | ||
[travis-url]: https://travis-ci.org/kogosoftwarellc/open-api | ||
[travis-image]: http://img.shields.io/travis/kogosoftwarellc/open-api.svg | ||
[coveralls-url]: https://coveralls.io/r/kogosoftwarellc/openapi-schema-validation | ||
[coveralls-image]: http://img.shields.io/coveralls/kogosoftwarellc/openapi-schema-validation/master.svg | ||
[coveralls-url]: https://coveralls.io/r/kogosoftwarellc/open-api | ||
[coveralls-image]: http://img.shields.io/coveralls/kogosoftwarellc/open-api/master.svg |
@@ -12,5 +12,4 @@ var expect = require('chai').expect; | ||
fixture = require(path.resolve(baseDir, fixture)); | ||
it('should ' + testName, function() { | ||
var errors = sut.validate(fixture.apiDoc).errors; | ||
var errors = sut.validate(fixture.apiDoc, fixture.validator).errors; | ||
expect(JSON.stringify(errors, null, ' ')) | ||
@@ -17,0 +16,0 @@ .to.equal(JSON.stringify(fixture.errors, null, ' ')); |
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
No website
QualityPackage does not have a website.
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
57964
4
10
1905
72
2
1