index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const refParser = require('@apidevtools/json-schema-ref-parser');
  2. let compiledSchema = null;
  3. module.exports = {
  4. /**
  5. * Compiles the schema, by dereferencing it, only once
  6. * and returns the memory cached value
  7. */
  8. getCompiledSchema: async () => {
  9. if (compiledSchema === null) {
  10. compiledSchema = await refParser.dereference(__dirname + '/swagger.json', {
  11. mutateInputSchema: false,
  12. });
  13. }
  14. return compiledSchema;
  15. },
  16. /**
  17. * Scans the schema for the validation schema for the given path and method
  18. * and returns it.
  19. *
  20. * @param {string} path
  21. * @param {string} method
  22. * @returns string|null
  23. */
  24. getValidationSchema: (path, method) => {
  25. if (compiledSchema !== null &&
  26. typeof compiledSchema.paths[path] !== 'undefined' &&
  27. typeof compiledSchema.paths[path][method] !== 'undefined' &&
  28. typeof compiledSchema.paths[path][method].requestBody !== 'undefined' &&
  29. typeof compiledSchema.paths[path][method].requestBody.content !== 'undefined' &&
  30. typeof compiledSchema.paths[path][method].requestBody.content['application/json'] !== 'undefined' &&
  31. typeof compiledSchema.paths[path][method].requestBody.content['application/json'].schema !== 'undefined'
  32. ) {
  33. return compiledSchema.paths[path][method].requestBody.content['application/json'].schema;
  34. }
  35. return null;
  36. }
  37. };