Skip to content

Commit

Permalink
better URL validation
Browse files Browse the repository at this point in the history
  • Loading branch information
undergroundwires committed Jan 6, 2020
1 parent 8d05b03 commit aff463d
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/application/ApplicationParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,29 +74,22 @@ export class ApplicationParser {
}

private static parseDocUrls(documentable: YamlDocumentable): ReadonlyArray<string> {
if (!documentable.docs) {
const docs = documentable.docs;
if (!docs) {
return [];
}
const docs = documentable.docs;
const result = new Array<string>();
const addDoc = (doc: string) => {
if (!doc) {
throw new Error('Documentiton url is null or empty');
}
if (doc.includes('\n')) {
throw new Error('Documentation url cannot be multi-lined.');
}
result.push(doc);
};
if (docs instanceof Array) {
for (const doc of docs) {
if (typeof doc !== 'string') {
throw new Error('Docs field (documentation url) must be an array of strings');
}
addDoc(doc as string);
this.validateUrl(doc);
result.push(doc);
}
} else if (typeof docs === 'string') {
addDoc(docs as string);
this.validateUrl(docs);
result.push(docs);
} else {
throw new Error('Docs field (documentation url) must a string or array of strings');
}
Expand All @@ -110,4 +103,18 @@ export class ApplicationParser {
private static isCategory(categoryOrScript: any): boolean {
return categoryOrScript.category && categoryOrScript.category.length > 0;
}

private static validateUrl(docUrl: string): void {
if (!docUrl) {
throw new Error('Documentation url is null or empty');
}
if (docUrl.includes('\n')) {
throw new Error('Documentation url cannot be multi-lined.');
}
const res = docUrl.match(
/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);
if (res == null) {
throw new Error(`Invalid documentation url: ${docUrl}`);
}
}
}

0 comments on commit aff463d

Please sign in to comment.