Skip to content

Commit 1bb7bb3

Browse files
authored
Merge pull request #24 from hatamiarash7/domain-url-validation
Add `is_valid_url` and `is_valid_domain` functions
2 parents ef2377a + c8df3c4 commit 1bb7bb3

10 files changed

Lines changed: 1036 additions & 2 deletions

README.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ Table of Contents
3939
- [Normalize URL](#normalize-url)
4040
- [Domain Depth](#domain-depth)
4141
- [Base64 Encode / Decode](#base64-encode--decode)
42+
- [Validate URL](#validate-url)
43+
- [Validate Domain](#validate-domain)
4244
- [Get Extension Version](#get-extension-version)
4345
- [Build Requirements](#build-requirements)
4446
- [Debugging](#debugging)
@@ -731,6 +733,66 @@ D SELECT base64_decode(base64_encode('https://example.com')) AS roundtrip;
731733
└─────────────────────┘
732734
```
733735

736+
### Validate URL
737+
738+
The `is_valid_url` function checks whether a string is a well-formed URL. A valid URL must have a scheme (e.g., `http`, `https`, `ftp`), the `://` separator, and a non-empty host. Returns a `BOOLEAN`, `NULL` for `NULL` input.
739+
740+
```sql
741+
D SELECT is_valid_url('https://example.com') AS valid;
742+
┌─────────┐
743+
│ valid │
744+
boolean
745+
├─────────┤
746+
│ true │
747+
└─────────┘
748+
749+
D SELECT is_valid_url('example.com') AS valid;
750+
┌─────────┐
751+
│ valid │
752+
boolean
753+
├─────────┤
754+
│ false │
755+
└─────────┘
756+
757+
D SELECT is_valid_url('https://[::1]:8080/path') AS valid;
758+
┌─────────┐
759+
│ valid │
760+
boolean
761+
├─────────┤
762+
│ true │
763+
└─────────┘
764+
```
765+
766+
### Validate Domain
767+
768+
The `is_valid_domain` function validates a domain name against RFC 1035 / RFC 1123 rules. Requires at least two labels, alphanumeric and hyphens only (no start/end with hyphen), max 63 chars per label, max 253 chars total, and a non-numeric TLD. Returns a `BOOLEAN`, `NULL` for `NULL` input.
769+
770+
```sql
771+
D SELECT is_valid_domain('example.com') AS valid;
772+
┌─────────┐
773+
│ valid │
774+
boolean
775+
├─────────┤
776+
│ true │
777+
└─────────┘
778+
779+
D SELECT is_valid_domain('sub.example.co.uk') AS valid;
780+
┌─────────┐
781+
│ valid │
782+
boolean
783+
├─────────┤
784+
│ true │
785+
└─────────┘
786+
787+
D SELECT is_valid_domain('localhost') AS valid;
788+
┌─────────┐
789+
│ valid │
790+
boolean
791+
├─────────┤
792+
│ false │
793+
└─────────┘
794+
```
795+
734796
### Get Extension Version
735797

736798
You can use the `netquack_version` function to get the extension version.
@@ -773,12 +835,10 @@ Also, there will be stdout errors for background tasks like CURL.
773835
- [ ] Save Tranco data as Parquet
774836
- [ ] Implement GeoIP functionality
775837
- [ ] Return default value for `get_tranco_rank`
776-
- [ ] Implement `is_valid_url` function - Return whether a string is a well-formed URL
777838
- [ ] Implement `url_encode` / `url_decode` functions - Standalone percent-encoding and decoding
778839
- [ ] Implement `ip_in_range` function - Check if an IP falls within a given CIDR block
779840
- [ ] Support internationalized domain names (IDNs)
780841
- [ ] Implement `punycode_encode` / `punycode_decode` functions - Convert internationalized domain names to/from ASCII-compatible encoding
781-
- [ ] Implement `is_valid_domain` function - Validate a domain name against RFC rules
782842
- [ ] Implement `extract_path_segments` table function - Split a URL path into individual segment rows
783843

784844
## Contributing 🤝

docs/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* [Normalize URL](functions/normalize-url.md)
2525
* [Domain Depth](functions/domain-depth.md)
2626
* [Base64 Encode / Decode](functions/base64-functions.md)
27+
* [Validate URL](functions/is-valid-url.md)
28+
* [Validate Domain](functions/is-valid-domain.md)
2729
* [Tranco](functions/tranco/README.md)
2830
* [Get Tranco Rank](functions/tranco/get-tranco-rank.md)
2931
* [Download / Update Tranco](functions/tranco/download-update-tranco.md)

docs/functions/is-valid-domain.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
layout:
3+
title:
4+
visible: true
5+
description:
6+
visible: false
7+
tableOfContents:
8+
visible: true
9+
outline:
10+
visible: true
11+
pagination:
12+
visible: true
13+
---
14+
15+
# Validate Domain
16+
17+
The `is_valid_domain` function validates a domain name against RFC 1035 / RFC 1123 rules. Returns a `BOOLEAN`.
18+
19+
Validation rules:
20+
21+
- Total length: 1–253 characters
22+
- At least two labels separated by dots (e.g., `example.com`)
23+
- Each label: 1–63 characters, alphanumeric and hyphens only
24+
- Labels must not start or end with a hyphen
25+
- TLD must not be all-numeric (rejects IP addresses)
26+
- Optional trailing dot (FQDN notation) is allowed
27+
28+
```sql
29+
D SELECT is_valid_domain('example.com') AS valid;
30+
┌─────────┐
31+
│ valid │
32+
boolean
33+
├─────────┤
34+
│ true │
35+
└─────────┘
36+
```
37+
38+
```sql
39+
D SELECT is_valid_domain('sub.example.co.uk') AS valid;
40+
┌─────────┐
41+
│ valid │
42+
boolean
43+
├─────────┤
44+
│ true │
45+
└─────────┘
46+
```
47+
48+
```sql
49+
D SELECT is_valid_domain('localhost') AS valid;
50+
┌─────────┐
51+
│ valid │
52+
boolean
53+
├─────────┤
54+
│ false │
55+
└─────────┘
56+
```
57+
58+
```sql
59+
D SELECT is_valid_domain('-bad.com') AS valid;
60+
┌─────────┐
61+
│ valid │
62+
boolean
63+
├─────────┤
64+
│ false │
65+
└─────────┘
66+
```
67+
68+
Returns `NULL` for `NULL` input.

docs/functions/is-valid-url.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
layout:
3+
title:
4+
visible: true
5+
description:
6+
visible: false
7+
tableOfContents:
8+
visible: true
9+
outline:
10+
visible: true
11+
pagination:
12+
visible: true
13+
---
14+
15+
# Validate URL
16+
17+
The `is_valid_url` function checks whether a string is a well-formed URL with a scheme, authority (host), and optional port/path/query/fragment. Returns a `BOOLEAN`.
18+
19+
A valid URL must have:
20+
21+
- A scheme (e.g., `http`, `https`, `ftp`, or any valid scheme)
22+
- The `://` separator
23+
- A non-empty host (domain name, IPv4, or bracketed IPv6)
24+
25+
```sql
26+
D SELECT is_valid_url('https://example.com') AS valid;
27+
┌─────────┐
28+
│ valid │
29+
boolean
30+
├─────────┤
31+
│ true │
32+
└─────────┘
33+
```
34+
35+
```sql
36+
D SELECT is_valid_url('https://example.com:8080/path?q=1#frag') AS valid;
37+
┌─────────┐
38+
│ valid │
39+
boolean
40+
├─────────┤
41+
│ true │
42+
└─────────┘
43+
```
44+
45+
```sql
46+
D SELECT is_valid_url('not a url') AS valid;
47+
┌─────────┐
48+
│ valid │
49+
boolean
50+
├─────────┤
51+
│ false │
52+
└─────────┘
53+
```
54+
55+
```sql
56+
D SELECT is_valid_url('example.com') AS valid;
57+
┌─────────┐
58+
│ valid │
59+
boolean
60+
├─────────┤
61+
│ false │
62+
└─────────┘
63+
```
64+
65+
Returns `NULL` for `NULL` input.

0 commit comments

Comments
 (0)