Skip to content

Commit 72a84ba

Browse files
committed
Added url validation
1 parent 58b5c9b commit 72a84ba

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ npm install validate-functions
1919
- **IP Validation**: Validate IPv4 and IPv6 formats.
2020
- **Hex Color Validation**: Validate #RRGGBB or #RGB color formats.
2121
- **Postal/ZIP Code Validation**: Validate based on country-specific formats. It supports top 25 countries based on ISO 3166-1 alpha-2 codes namely: US, CN, IN, ID, PK, BR, NG, BD, RU, MX, JP, ET, PH, EG, VN, CD, TR, IR, DE, TH, GB, FR, IT, TZ, ZA.
22+
- **URL Validation**: Validate structure and it supports URLs with and without protocols, and it handles query parameters and fragments.
2223

2324

2425
Usage details are available [here](./USAGE_DETAILS.md).

USAGE_DETAILS.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,14 @@ import { isValid_Postal_Code, CountryCode } from 'validate-functions/postal-zip'
7474
// India
7575
const indianPostalCode = '110001';
7676
console.log(isValid_Postal_Code(indianPostalCode, CountryCode.IN)); // Output: true
77-
```
77+
```
78+
79+
- **isValid_URL**
80+
81+
```typescript
82+
import { isValid_URL } from 'validate-functions/url';
83+
84+
// Example of a URL with query parameters
85+
const urlWithQuery = 'http://example.com?name=value';
86+
console.log(isValid_URL(urlWithQuery)); // Output: true
87+
```

src/__tests__/url.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { isValid_URL } from '../url';
2+
3+
describe('URL Validation', () => {
4+
it('should return true for a valid HTTP URL', () => {
5+
expect(isValid_URL('http://example.com')).toBe(true);
6+
});
7+
8+
it('should return true for a valid HTTPS URL', () => {
9+
expect(isValid_URL('https://example.com')).toBe(true);
10+
});
11+
12+
it('should return true for a valid URL without protocol', () => {
13+
expect(isValid_URL('example.com')).toBe(true);
14+
});
15+
16+
it('should return false for a URL with invalid characters', () => {
17+
expect(isValid_URL('http://example.com/<>')).toBe(false);
18+
});
19+
20+
it('should return true for a URL with query parameters', () => {
21+
expect(isValid_URL('http://example.com?name=value')).toBe(true);
22+
});
23+
24+
it('should return true for a URL with a fragment', () => {
25+
expect(isValid_URL('http://example.com#section')).toBe(true);
26+
});
27+
28+
it('should return false for an empty string', () => {
29+
expect(isValid_URL('')).toBe(false);
30+
});
31+
32+
it('should return false for a URL with only protocol', () => {
33+
expect(isValid_URL('http://')).toBe(false);
34+
});
35+
});

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './ip';
55
export * from './hex-color';
66
export * from './postal-zip'
77
export * from './ssn';
8+
export * from './url';

src/url.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Validate structure and optionally check if it's HTTPS.
3+
* @param url - The URL to validate.
4+
* @returns True if the URL is valid, otherwise false.
5+
*/
6+
export const isValid_URL = (url: string): boolean => {
7+
const urlRegex = /^(https?:\/\/)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(:\d+)?(\/[^\s<>]*)?(\?[^\s<>]*)?(#[^\s<>]*)?$/i;
8+
return urlRegex.test(url);
9+
};

0 commit comments

Comments
 (0)