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);