Skip to content

Commit

Permalink
fix: improve isurl regex
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Nov 10, 2023
1 parent 15835b7 commit bec2e1f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
22 changes: 11 additions & 11 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
"test": "vitest --global test.ts"
},
"devDependencies": {
"@types/debug": "^4.1.10",
"@types/fs-extra": "^11.0.3",
"@types/inquirer": "^9.0.6",
"@types/lodash.get": "^4.4.8",
"@types/lodash.isempty": "^4.4.8",
"@types/lodash.omit": "^4.5.8",
"@types/lodash.uniq": "^4.5.8",
"@types/lodash.uniqby": "^4.7.8",
"@types/lodash.uniqwith": "^4.5.8",
"@types/micromatch": "^4.0.4",
"@types/validator": "^13.11.5"
"@types/debug": "^4.1.12",
"@types/fs-extra": "^11.0.4",
"@types/inquirer": "^9.0.7",
"@types/lodash.get": "^4.4.9",
"@types/lodash.isempty": "^4.4.9",
"@types/lodash.omit": "^4.5.9",
"@types/lodash.uniq": "^4.5.9",
"@types/lodash.uniqby": "^4.7.9",
"@types/lodash.uniqwith": "^4.5.9",
"@types/micromatch": "^4.0.5",
"@types/validator": "^13.11.6"
},
"dependencies": {
"@apidevtools/swagger-parser": "^10.1.0",
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/utils/assertion.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { describe, expect, it } from 'vitest';
import { isUrl } from './assertion';

describe('assertion testing', () => {
it('check for valid URLs', () => {
expect(isUrl('http://my-docker-service/docs.json')).toBeTruthy();
expect(isUrl('https://www.example.com')).toBeTruthy();
expect(isUrl('http://localhost:8080/docs/spec.yaml')).toBeTruthy();
expect(isUrl('D:/a/test.txt')).toBeFalsy();
expect(isUrl('./file.txt')).toBeFalsy();
});
});
4 changes: 3 additions & 1 deletion packages/core/src/utils/assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export const isRootKey = (specKey: string, target: string) => {
return specKey === target;
};

const LOCALHOST_REGEX = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/i;

export const isUrl = (str: string) => {
return validatorIsUrl(str, { require_tld: false });
return validatorIsUrl(str) || LOCALHOST_REGEX.test(str);
};

0 comments on commit bec2e1f

Please sign in to comment.