oas-normalize
Advanced tools
Comparing version 3.0.5 to 4.0.0
@@ -0,1 +1,13 @@ | ||
## 4.0.0 (2021-08-26) | ||
> 🚨 **Breaking change!** | ||
> | ||
> `.validate()` will no longer dereference schemas. If you need to dereference before or after validating, you shoudl use the `.deref()` method instead. | ||
* fix: stop dereferencing when running spec validation (#153) ([7b706db](https://github.com/readmeio/oas-normalize/commit/7b706db)), closes [#153](https://github.com/readmeio/oas-normalize/issues/153) | ||
* docs: adding a pull request template ([16da316](https://github.com/readmeio/oas-normalize/commit/16da316)) | ||
* docs: make docs more OpenAPI friendly (#152) ([68cfc34](https://github.com/readmeio/oas-normalize/commit/68cfc34)), closes [#152](https://github.com/readmeio/oas-normalize/issues/152) | ||
## <small>3.0.5 (2021-08-20)</small> | ||
@@ -2,0 +14,0 @@ |
43
index.js
const fetch = require('node-fetch'); | ||
const fs = require('fs'); | ||
const $RefParser = require('@apidevtools/json-schema-ref-parser'); | ||
const converter = require('swagger2openapi'); | ||
const SwaggerParser = require('swagger-parser'); | ||
const swaggerParser = require('@apidevtools/swagger-parser'); | ||
const utils = require('./lib/utils'); | ||
@@ -55,3 +54,3 @@ | ||
return this.load() | ||
.then(schema => $RefParser.bundle(schema)) | ||
.then(schema => swaggerParser.bundle(schema)) | ||
.then(bundle => { | ||
@@ -67,3 +66,3 @@ this.cache.bundle = bundle; | ||
return this.load() | ||
.then(schema => $RefParser.dereference(schema)) | ||
.then(schema => swaggerParser.dereference(schema)) | ||
.then(dereferenced => { | ||
@@ -76,20 +75,27 @@ this.cache.deref = dereferenced; | ||
async validate(convertToLatest) { | ||
return this.deref().then(async schema => { | ||
return this.load().then(async schema => { | ||
const baseVersion = parseInt(utils.version(schema), 10); | ||
const resolve = out => { | ||
if (!convertToLatest) { | ||
return out; | ||
} | ||
return converter.convertObj(out, { anchors: true }).then(options => { | ||
return options.openapi; | ||
}); | ||
}; | ||
if (baseVersion === 1) { | ||
return Promise.reject(new Error('Swagger v1.2 is unsupported.')); | ||
} else if (baseVersion === 2 || baseVersion === 3) { | ||
return resolve( | ||
await SwaggerParser.validate(schema).catch(err => { | ||
// `swaggerParser.validate()` dereferences schemas at the same time as validation and does | ||
// not give us an option to disable this. Since all we already have a dereferencing method | ||
// on this library and our `validate()` method here just needs to tell us if the definition | ||
// is valid or not we need to clone it before passing it over to `swagger-parser` so as to | ||
// not run into pass-by-reference problems. | ||
const clonedSchema = JSON.parse(JSON.stringify(schema)); | ||
return swaggerParser | ||
.validate(clonedSchema) | ||
.then(() => { | ||
if (!convertToLatest) { | ||
return schema; | ||
} | ||
return converter.convertObj(schema, { anchors: true }).then(options => { | ||
return options.openapi; | ||
}); | ||
}) | ||
.catch(err => { | ||
const error = new Error(err.message.replace(/\[object Object\]/g, 'Schema')); | ||
@@ -114,4 +120,3 @@ error.full = err; | ||
return Promise.reject(error); | ||
}) | ||
); | ||
}); | ||
} | ||
@@ -118,0 +123,0 @@ |
{ | ||
"name": "oas-normalize", | ||
"version": "3.0.5", | ||
"description": "Swagger 2 or OAS 3? YAML or JSON? URL, path, string or object? Who cares! It just works.", | ||
"version": "4.0.0", | ||
"description": "OpenAPI 3 or Swagger 2? YAML or JSON? URL, path, string or object? Who cares! It just works.", | ||
"main": "index.js", | ||
@@ -9,2 +9,17 @@ "engines": { | ||
}, | ||
"tags": [ | ||
"api", | ||
"apis", | ||
"openapi", | ||
"swagger", | ||
"openapi initiative", | ||
"openapi specification", | ||
"openapi spec", | ||
"openapi document", | ||
"oai", | ||
"oas", | ||
"apidoc", | ||
"microservice", | ||
"documentation" | ||
], | ||
"repository": { | ||
@@ -26,6 +41,5 @@ "type": "git", | ||
"dependencies": { | ||
"@apidevtools/json-schema-ref-parser": "^9.0.7", | ||
"@apidevtools/swagger-parser": "^10.0.3", | ||
"js-yaml": "^4.1.0", | ||
"node-fetch": "^2.6.1", | ||
"swagger-parser": "^10.0.1", | ||
"swagger2openapi": "^7.0.8" | ||
@@ -32,0 +46,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
Swagger 2 or OAS 3? YAML or JSON? URL, path, string or object? Who cares! It just works. | ||
OpenAPI 3 or Swagger 2? YAML or JSON? URL, path, string or object? Who cares! It just works. | ||
@@ -7,3 +7,3 @@ This module uses a bunch of other great modules to do the heavy lifting, and normalizes everything! | ||
[![](https://cl.ly/1h271F1M1e2T/Untitled-2.png)](http://readme.io) | ||
[![](https://d3vv6lp55qjaqc.cloudfront.net/items/1M3C3j0I0s0j3T362344/Untitled-2.png)](https://readme.com) | ||
@@ -50,3 +50,3 @@ # Install | ||
# Helper functions | ||
# Helper Functions | ||
@@ -64,7 +64,7 @@ > **Note:** All of these functions are promise-driven. | ||
# Other little features | ||
# Other Little Features | ||
### Always return OAS 3 | ||
### Always Return OpenAPI 3.x | ||
If you want `.validate` to always return an OpenAPI 3.0 definition, supply `true` as its argument: | ||
If you want `.validate` to always return an OpenAPI 3.x definition, supply `true` as its argument: | ||
@@ -75,3 +75,3 @@ ```js | ||
### Enable local paths | ||
### Enable Local Paths | ||
@@ -83,2 +83,1 @@ For security reasons, you need to opt into allowing fetching by a local path. To enable it supply the `enablePaths` option to the class instance: | ||
``` | ||
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
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
33790
4
147
79
+ Added@apidevtools/json-schema-ref-parser@9.0.6(transitive)
+ Added@apidevtools/swagger-parser@10.1.0(transitive)
+ Addedajv@8.17.1(transitive)
+ Addedajv-draft-04@1.0.0(transitive)
+ Addedargparse@1.0.10(transitive)
+ Addedesprima@4.0.1(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-uri@3.0.3(transitive)
+ Addedjs-yaml@3.14.1(transitive)
+ Addedjson-schema-traverse@1.0.0(transitive)
+ Addedrequire-from-string@2.0.2(transitive)
+ Addedsprintf-js@1.0.3(transitive)
- Removedswagger-parser@^10.0.1
- Removed@apidevtools/json-schema-ref-parser@9.1.2(transitive)
- Removed@apidevtools/swagger-parser@10.0.3(transitive)
- Removed@types/json-schema@7.0.15(transitive)
- Removedcommander@10.0.1(transitive)
- Removedlodash.get@4.4.2(transitive)
- Removedlodash.isequal@4.5.0(transitive)
- Removedswagger-parser@10.0.3(transitive)
- Removedvalidator@13.12.0(transitive)
- Removedz-schema@5.0.6(transitive)