Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from pallakartheekreddy/master
Browse files Browse the repository at this point in the history
Open API validator enhancement
  • Loading branch information
shiva-rakshith authored May 20, 2024
2 parents fdd21c5 + 35337f4 commit 8f67a96
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"uuid": "^8.3.2",
"winston": "^3.7.2",
"winston-daily-rotate-file": "^4.7.1",
"yaml": "^2.4.2",
"zod": "^3.14.2"
}
}
18 changes: 17 additions & 1 deletion src/middlewares/schemaValidator.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,24 @@ import { Locals } from "../interfaces/locals.interface";
import { getConfig } from "../utils/config.utils";
import fs from "fs";
import path from "path";
import { OpenAPIV3 } from 'express-openapi-validator/dist/framework/types';
import YAML from 'yaml';
const protocolServerLevel = `${getConfig().app.mode.toUpperCase()}-${getConfig().app.gateway.mode.toUpperCase()}`;

// Cache object
const apiSpecCache: { [filename: string]: OpenAPIV3.Document } = {};

// Function to load and cache the API spec
const loadApiSpec = (specFile: string): OpenAPIV3.Document => {
if (!apiSpecCache[specFile]) {
console.log("Cache Not found. Loading....", specFile)
const apiSpecYAML = fs.readFileSync(specFile, 'utf8');
const apiSpec = YAML.parse(apiSpecYAML);
apiSpecCache[specFile] = apiSpec;
}
return apiSpecCache[specFile];
};

export const schemaErrorHandler = (
err: any,
req: Request,
Expand Down Expand Up @@ -65,7 +81,7 @@ export const openApiValidatorMiddleware = async (
}

const openApiValidator = OpenApiValidator.middleware({
apiSpec: specFile,
apiSpec: loadApiSpec(specFile),
validateRequests: true,
validateResponses: false,
$refParser: {
Expand Down

0 comments on commit 8f67a96

Please sign in to comment.