Skip to content
This repository was archived by the owner on May 5, 2018. It is now read-only.

adding openAPI 3.0.0 support #1

Merged
merged 1 commit into from
May 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
# 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.
* Performant.
* Currently supports type definitions included in the `definitions` property of the
provided openapi document.
* Extensively tested.
* 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

**Huge thank you to the [gnostic](https://github.com/googleapis/gnostic) project for building up a 3.0.0 JSON schema.**


## Example

See `./test/data-driven/*.js` for more examples.

```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])

`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).
## API
### .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`

## LICENSE
``````
Expand Down
20 changes: 16 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,27 @@ var fs = require('fs');
var Validator = require('jsonschema').Validator;
var v = new Validator();
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);

module.exports = {
validate: validate
};

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 = typeof version === "number" ? version : 2; // default to swagger 2.0 validation
return v.validate(openapiDoc, version === 2 || version === 2.0 ? swagger2Schema : swagger3Schema);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "openapi-schema-validation",
"version": "0.2.1",
"version": "0.3.0",
"description": "Validate openapi documents.",
"scripts": {
"cover": "istanbul cover _mocha -- ./test/*.js",
Expand Down
Loading