-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
46775bd
commit a0a89ca
Showing
7 changed files
with
2,895 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"search.exclude": { | ||
"**/node_modules": false | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
extends: | ||
- spectral:oas | ||
- spectral-aws-apigateway-ruleset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
const path = require("path"); | ||
const fs = require("fs"); | ||
|
||
const { Spectral } = require("@stoplight/spectral-core"); | ||
const { fetch } = require("@stoplight/spectral-runtime"); // can also use isomorphic-fetch, etc.. If you ruleset does not reference any external assets, you can provide some stub instead. | ||
const { | ||
bundleAndLoadRuleset, | ||
} = require("@stoplight/spectral-ruleset-bundler/with-loader"); | ||
const { | ||
commonjs, | ||
} = require("@stoplight/spectral-ruleset-bundler/plugins/commonjs"); // needed if you want to use CommonJS | ||
|
||
const spectral = new Spectral(); | ||
|
||
beforeAll(async () => { | ||
const rulesetFilepath = path.join(__dirname, ".spectral.yaml"); | ||
spectral.setRuleset( | ||
await bundleAndLoadRuleset(rulesetFilepath, { fs, fetch }, [commonjs()]) | ||
); | ||
return spectral; | ||
}); | ||
|
||
describe("Testing each rule against example documents", () => { | ||
test("testing the openapi rule", async () => { | ||
const badDocument = { | ||
description: "", | ||
openapi: "3.1.1", | ||
}; | ||
|
||
// we lint our document using the ruleset we passed to the Spectral object | ||
spectral.run(badDocument).then((spectralResultArray) => { | ||
expect(spectralResultArray).not.toEqual([]); | ||
}); | ||
|
||
const correctDocument = { | ||
description: "", | ||
openapi: "3.0.1", | ||
}; | ||
|
||
// we lint our document using the ruleset we passed to the Spectral object | ||
spectral.run(correctDocument).then((spectralResultArray) => { | ||
expect(spectralResultArray).toEqual([]); | ||
}); | ||
}); | ||
|
||
xtest("aws-path-segments", async () => { | ||
const goodDocument = { | ||
description: "", | ||
openapi: "3.0.1", | ||
paths: { | ||
"/": { | ||
post: { | ||
tags: ["pet"], | ||
summary: "uploads an image", | ||
description: "", | ||
operationId: "uploadFile", | ||
consumes: ["multipart/form-data"], | ||
produces: ["application/json"], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
// we lint our document using the ruleset we passed to the Spectral object | ||
spectral.run(goodDocument).then((spectralResultArray) => { | ||
expect(spectralResultArray).toEqual([]); | ||
}); | ||
|
||
const badDocument = { | ||
description: "", | ||
openapi: "3.0.1", | ||
paths: { | ||
"this should not work": { | ||
post: { | ||
tags: ["pet"], | ||
summary: "uploads an image", | ||
description: "", | ||
operationId: "uploadFile", | ||
consumes: ["multipart/form-data"], | ||
produces: ["application/json"], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
// we lint our document using the ruleset we passed to the Spectral object | ||
spectral.run(badDocument).then((spectralResultArray) => { | ||
expect(spectralResultArray).not.toEqual([]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,46 @@ | ||
import Ajv from "ajv-draft-04" | ||
import Ajv from "ajv-draft-04"; | ||
|
||
const ajv = new Ajv({ | ||
"strict": true, | ||
"validateFormats": false | ||
strict: true, | ||
validateFormats: false, | ||
}); | ||
|
||
// Removed the checking of formats, as AWS API Gateway has additional formats, and | ||
// will delegate to specific rules to make easier to spot compatible/incompatible formats. | ||
//addFormats(ajv, ["date-time","email","hostname","ipv4","ipv6","uri"]) | ||
|
||
ajv.addKeyword("example"); // Added 'example' keyword as picked up by `aws-example-tag` rule. | ||
|
||
// function validate_json() { | ||
|
||
// }; | ||
|
||
module.exports = (targetVal, _opts, context) => { | ||
let openapi = JSON.parse(JSON.stringify(targetVal)) | ||
const results = [] | ||
let openapi = JSON.parse(JSON.stringify(targetVal)); | ||
const results = []; | ||
|
||
const schemaList = openapi.components.schemas | ||
for (const property in schemaList) { | ||
if (Object.prototype.hasOwnProperty.call(schemaList, property)) { | ||
const schema_model = schemaList[property] | ||
schema_model.id = `#/components/schemas/${property}` | ||
try { | ||
let schema = schemaList[property] | ||
ajv.addSchema(schema) | ||
} | ||
catch (error) { | ||
console.log(`${property} failed to add due to ${error}`) | ||
} | ||
|
||
} | ||
const schemaList = openapi.components.schemas; | ||
for (const property in schemaList) { | ||
if (Object.prototype.hasOwnProperty.call(schemaList, property)) { | ||
const schema_model = schemaList[property]; | ||
schema_model.id = `#/components/schemas/${property}`; | ||
try { | ||
let schema = schemaList[property]; | ||
ajv.addSchema(schema); | ||
} catch (error) { | ||
console.log(`${property} failed to add due to ${error}`); | ||
} | ||
} | ||
for (const property in schemaList) { | ||
try { | ||
ajv.compile(schemaList[property]) | ||
} | ||
catch (error) { | ||
results.push({ | ||
"message": `${error}`, | ||
"path": ['components', 'schemas', property] | ||
}) | ||
} | ||
} | ||
for (const property in schemaList) { | ||
try { | ||
ajv.compile(schemaList[property]); | ||
} catch (error) { | ||
results.push({ | ||
message: `${error}`, | ||
path: ["components", "schemas", property], | ||
}); | ||
} | ||
return results | ||
} | ||
} | ||
return results; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters