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 13, 2023
1 parent 15835b7 commit feded76
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
24 changes: 11 additions & 13 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@
"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"
},
"dependencies": {
"@apidevtools/swagger-parser": "^10.1.0",
Expand All @@ -47,7 +46,6 @@
"micromatch": "^4.0.5",
"openapi-types": "^12.1.3",
"openapi3-ts": "^3.2.0",
"swagger2openapi": "^7.0.8",
"validator": "^13.11.0"
"swagger2openapi": "^7.0.8"
}
}
14 changes: 14 additions & 0 deletions packages/core/src/utils/assertion.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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('http://localhost/test.json')).toBeTruthy();
expect(isUrl('http://localhost:6001/swagger/v1/swagger.json')).toBeTruthy();
expect(isUrl('D:/a/test.txt')).toBeFalsy();
expect(isUrl('./file.txt')).toBeFalsy();
});
});
9 changes: 7 additions & 2 deletions packages/core/src/utils/assertion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ReferenceObject, SchemaObject } from 'openapi3-ts';
import validatorIsUrl from 'validator/lib/isURL';
import { SchemaType, Verbs } from '../types';
import { extname } from './path';

Expand Down Expand Up @@ -81,5 +80,11 @@ export const isRootKey = (specKey: string, target: string) => {
};

export const isUrl = (str: string) => {
return validatorIsUrl(str, { require_tld: false });
let givenURL;
try {
givenURL = new URL(str);
} catch (error) {
return false;
}
return givenURL.protocol === 'http:' || givenURL.protocol === 'https:';
};

0 comments on commit feded76

Please sign in to comment.