Tokopedia May 30, 2022

Validating Social Media URLs in Swift

Article Summary

Alvin Matthew Pratama from Tokopedia tackles a deceptively complex problem: validating social media URLs when each platform has wildly different URL structures. Regex alone won't cut it.

When Tokopedia launched their affiliate program, they needed users to register social media accounts. The challenge? YouTube alone has three different valid URL patterns for user pages. The team needed a flexible, maintainable solution that wouldn't become a regex nightmare.

Key Takeaways

Critical Insight

Tokopedia open sourced SuperValidator, an option-based Swift library that validates social media URLs by breaking them into customizable components instead of brittle regex patterns.

The library also validates emails, phone numbers, and FQDNs, with plans to integrate directly with UITextField and RxSwift.

About This Article

Problem

Tokopedia's affiliate program needed to validate social media URLs from different platforms. Each platform had its own domain structure and URL format, which made regex solutions hard to maintain and prone to errors.

Solution

Alvin Matthew Pratama's team built SuperValidator, a Swift library that breaks URL validation into separate, customizable parts. Instead of writing complex regex patterns, developers can now configure protocols, paths, domain whitelists and blacklists, and FQDN checks as needed.

Impact

SuperValidator is now an open-source library that handles Instagram, TikTok, Twitter, and other platforms. It includes built-in validators for each social media service, so developers can validate platform-specific URLs with a single function call and get detailed error types to show users.