Skip to content

Commit

Permalink
Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
andylockran committed Apr 3, 2023
1 parent 46775bd commit a0a89ca
Show file tree
Hide file tree
Showing 7 changed files with 2,895 additions and 142 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"search.exclude": {
"**/node_modules": false
}
}
2,853 changes: 2,753 additions & 100 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
},
"devDependencies": {
"@babel/preset-env": "^7.14.1",
"@stoplight/spectral-core": "^1.16.1",
"@stoplight/spectral-parsers": "^1.0.2",
"@stoplight/spectral-ruleset-bundler": "^1.5.1",
"@stoplight/spectral-runtime": "^1.1.2",
"apigw-model-validator": "^1.1.0",
"babel-jest": "^29.5.0",
"jest": "^29.5.0",
Expand All @@ -52,6 +56,6 @@
"^.+\\.js$": "babel-jest",
"^.+\\.mjs$": "babel-jest"
},
"testRegex": "((\\.|/*.)(spec))\\.js?$"
"testRegex": "((\\.|/*.)(spec))\\.(mjs|js)?$"
}
}
3 changes: 3 additions & 0 deletions src/__tests__/.spectral.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extends:
- spectral:oas
- spectral-aws-apigateway-ruleset
91 changes: 91 additions & 0 deletions src/__tests__/rules.spec.mjs
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([]);
});
});
});
63 changes: 30 additions & 33 deletions src/draft4.mjs
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;
};
16 changes: 8 additions & 8 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ module.exports = {
},
mode: "production",
// experiments: {
// executeModule: false,
// outputModule: true,
// syncWebAssembly: false,
// topLevelAwait: false,
// asyncWebAssembly: true,
// layers: false,
// lazyCompilation: false,
// // executeModule: true,
// // outputModule: true,
// // syncWebAssembly: false,
// // topLevelAwait: false,
// // asyncWebAssembly: true,
// // layers: false,
// // lazyCompilation: false,
// },
output: {
filename: "[name].js",
path: path.resolve(__dirname, "functions"),
// library: {
// type: "module",
// export: ["validate_json"]
//}
// },
},
// externals: {
// "ajv": "require('ajv')"
Expand Down

0 comments on commit a0a89ca

Please sign in to comment.