From 6e96bba2e4e38008a72ce943229811b55713ab8f Mon Sep 17 00:00:00 2001 From: Zack_Aayush <60972989+AayushSaini101@users.noreply.github.com> Date: Tue, 19 Sep 2023 11:15:49 +0530 Subject: [PATCH] feat: added case for the extension to validate the file (#807) Co-authored-by: Aayush %0ACo-authored-by: souvik --- src/errors/specification-file.ts | 10 +++++++--- src/models/SpecificationFile.ts | 9 +++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/errors/specification-file.ts b/src/errors/specification-file.ts index 0e36516739d..9df74db9218 100644 --- a/src/errors/specification-file.ts +++ b/src/errors/specification-file.ts @@ -24,7 +24,7 @@ export class SpecificationURLNotFound extends SpecificationFileError { } } -type From = 'file' | 'url' | 'context' +type From = 'file' | 'url' | 'context' | 'invalid file' export class ErrorLoadingSpec extends Error { private readonly errorMessages = { @@ -35,14 +35,18 @@ export class ErrorLoadingSpec extends Error { if (from === 'file') { this.name = 'error loading AsyncAPI document from file'; this.message = `${param} file does not exist.`; - } + } if (from === 'url') { this.name = 'error loading AsyncAPI document from url'; this.message = `Failed to download ${param}.`; - } + } if (from === 'context') { this.name = 'error loading AsyncAPI document from context'; this.message = `${param} context name does not exist.`; + } + if (from === 'invalid file') { + this.name = 'Invalid AsyncAPI file type'; + this.message = 'cli only supports yml ,yaml ,json extension'; } if (!from) { diff --git a/src/models/SpecificationFile.ts b/src/models/SpecificationFile.ts index e33c9118e4d..4d4fe917bf0 100644 --- a/src/models/SpecificationFile.ts +++ b/src/models/SpecificationFile.ts @@ -185,6 +185,15 @@ export async function fileExists(name: string): Promise { if ((await lstat(name)).isFile()) { return true; } + + const extension = name.split('.')[1]; + + const allowedExtenstion=['yml','yaml','json']; + + if (!allowedExtenstion.includes(extension)) { + throw new ErrorLoadingSpec('invalid file',name); + } + throw new ErrorLoadingSpec('file', name); } catch (e) { throw new ErrorLoadingSpec('file', name);