Skip to content

Commit

Permalink
feat: added case for the extension to validate the file (#807)
Browse files Browse the repository at this point in the history
Co-authored-by: Aayush <[email protected]>%0ACo-authored-by: souvik <[email protected]>
  • Loading branch information
AayushSaini101 and Souvikns authored Sep 19, 2023
1 parent c147a7a commit 6e96bba
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
10 changes: 7 additions & 3 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,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) {
Expand Down
9 changes: 9 additions & 0 deletions src/models/SpecificationFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ export async function fileExists(name: string): Promise<boolean> {
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);
Expand Down

0 comments on commit 6e96bba

Please sign in to comment.