Skip to content

Commit

Permalink
Added case for the extension
Browse files Browse the repository at this point in the history
  • Loading branch information
aayushRedHat committed Sep 14, 2023
1 parent c02b378 commit 58b523d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/errors/specification-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -35,15 +35,19 @@ 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.`;
}
} else
if (from === 'url') {
this.name = 'error loading AsyncAPI document from url';
this.message = `Failed to download ${param}.`;
} else
if (from === 'context') {
this.name = 'error loading AsyncAPI document from context';
this.message = `${param} context name does not exist.`;
} else
if (from === 'invalid file') {
this.name = 'Invalid AsyncAPI file type';
this.message = 'cli only supports yml ,yaml ,json extension';
}

if (!from) {
this.name = 'error locating AsyncAPI document';
Expand Down
8 changes: 8 additions & 0 deletions src/models/SpecificationFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ export async function isURL(urlpath: string): Promise<boolean> {

export async function fileExists(name: string): Promise<boolean> {
try {
const extension = name.split('.')[1];

const allowedExtenstion=['yml','yaml','json'];

if (!allowedExtenstion.includes(extension)) {
throw new ErrorLoadingSpec('invalid file',name);
}

if ((await lstat(name)).isFile()) {
return true;
}
Expand Down

0 comments on commit 58b523d

Please sign in to comment.