Files

47 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2025-09-02 21:43:00 +10:00
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";
import $RefParser from "@apidevtools/json-schema-ref-parser";
2024-10-09 18:05:15 +10:00
2025-09-02 21:43:00 +10:00
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
2024-10-09 18:05:15 +10:00
2025-09-02 21:43:00 +10:00
let compiledSchema = null;
2024-10-09 18:05:15 +10:00
2025-09-02 21:43:00 +10:00
/**
* Compiles the schema, by dereferencing it, only once
* and returns the memory cached value
*/
const getCompiledSchema = async () => {
if (compiledSchema === null) {
compiledSchema = await $RefParser.dereference(`${__dirname}/swagger.json`, {
mutateInputSchema: false,
});
}
return compiledSchema;
};
2024-10-09 18:05:15 +10:00
2025-09-02 21:43:00 +10:00
/**
* Scans the schema for the validation schema for the given path and method
* and returns it.
*
* @param {string} path
* @param {string} method
* @returns string|null
*/
const getValidationSchema = (path, method) => {
if (
compiledSchema !== null &&
typeof compiledSchema.paths[path] !== "undefined" &&
typeof compiledSchema.paths[path][method] !== "undefined" &&
typeof compiledSchema.paths[path][method].requestBody !== "undefined" &&
typeof compiledSchema.paths[path][method].requestBody.content !== "undefined" &&
typeof compiledSchema.paths[path][method].requestBody.content["application/json"] !== "undefined" &&
typeof compiledSchema.paths[path][method].requestBody.content["application/json"].schema !== "undefined"
) {
return compiledSchema.paths[path][method].requestBody.content["application/json"].schema;
2024-10-09 18:05:15 +10:00
}
2025-09-02 21:43:00 +10:00
return null;
2024-10-09 18:05:15 +10:00
};
2025-09-02 21:43:00 +10:00
export { getCompiledSchema, getValidationSchema };