Validating Social Media URLs in Swift

Background

In February 2022, Tokopedia introduced an affiliate program to all Tokopedia users and the public. To become an affiliate, users need to register their social media accounts by providing the social media URLs. As each social media URL has a different domain and URL path, the application needs to validate the URL according to the social media.

We agree that regex is one of the easiest ways to solve many parsing problems However, this case is complex due to several rules for each social media and will be challenging to customize in the future. Some social media have more than one URL path for the user page. For example, Youtube has several paths as follows:

https://www.youtube.com/user/example
https://www.youtube.com/c/coldplay
https://www.youtube.com/channel/UC9zY...

One way to solve this is by creating a reusable validator that allows validating specific social media URLs in the simplest way. This article will use and introduce SuperValidator as our swift validator library to solve this problem.

SuperValidator

SuperValidator is an option-based validator and an open-source library managed by Tokopedia iOS Engineers. The validator provides options to customize the validation according to your needs. Let’s go through this article to know more about this!

URL Validator

If we look at the URL anatomy, we can create separate validation for each of the URL’s components.

SuperValidator has split up the components as parameters such as the following:

protocols: [String] = ["https", "http", "ftp"],
requireProtocol: Bool = false,
requireValidProtocol: Bool = true,
paths: [String] = [],
allowQueryComponents: Bool = true,
domainWhitelist: [String] = [],
domainBlacklist: [String] = [],
fqdn: FQDN = .init() // Fully Qualified Domain Name

protocols

An array of strings for valid protocols, such as http or https. If we store https only then it will only allow https.

requireProtocol

If set to true, then the URL must have a protocol.

requireValidProtocol

If set to true, then the URL protocol must be the same as one of the valid protocols listed in the protocols options.

paths

An array of strings for URL paths /shop/{shopID}.
There are two types, which include fixed and value.

/shop/{shopID}

The path will be separated by /,
shop is a fixed type, which means the URL path inputted must be exactly the same.
{shopID} is a value type, curly braces {} indicate the content inside is the value type.

allowQueryComponents

If set to false, the query component and anchor are not allowed.

domainWhitelist

An array of strings for the whitelist domain. It can also use regex.

domainBlacklist

An array of strings for the blacklist domain. It can also use regex.

FQDN

FQDN stands for Fully Qualified Domain Name. It will check if the domain is good to go.

Basic Usage

We can just call isURL function to return a boolean that indicates a valid URL.

If you want to customize the validator, you can set the options parameters covered in the previous section.

You can also use validateURL function to return success or error result. You can use the error type provided by SuperValidator to generate an error message.

Use Cases

Let’s break down the users’ social media URLs. They have different domains and paths, and some of them have multiple valid paths. Cases like this can also happen in other URLs. We can create an enum to store options for every social media.

Then you can use isURL or validateURL function from SuperValidator.

SuperValidator.shared.isURL(
"https://instagram.com/myuser",
options: SocialMediaURL.instagram.options
)

Fortunately, you don’t need to create a new function for social media URL validation, because SuperValidator already covers this. You can call isURL or validateURL with socialMedia parameters.

You can use validateURL to get the error reasons and give it to the user.

For more test cases you can see this link.

Other Validators

Besides URL, there are several validators that SuperValidator already covers:

FQDN

Help validate fully qualified domain names. (Wikipedia)

Email

Help validate email with options such as length limit of the local part, whitelist, and blacklist domain.

Phone number

Help validate phone numbers with International, North American, and Extensible Provisioning Protocol formats, validate country code or the national prefix.

Future Plans

Contribute🚨

Calling contributors!! SuperValidator is open-source, and we’re excited to hear and learn from you. Your experiences will benefit others who use SuperValidator. Let’s make it even better!

https://github.com/tokopedia/ios-supervalidator

Credits

Thanks to Christopher Teddy for helping with the development. Kudos to Superman squad and all developers who helped make this happen.

As always, we have an opening at Tokopedia.
We are an Indonesian technology company with a mission to democratize commerce through technology, and our vision is to build an ecosystem where everyone can start and discover anything.
Find your Dream Job with us in Tokopedia!
https://www.tokopedia.com/careers/

--

--

--

Story from people who build Tokopedia

Recommended from Medium

Looping Through Python For…

Building my ITX PC, DIY-style

Closure in Python

Generate timebase UUID in swift

What are Vanity Metrics and How Can They Be Identified?

LeetCode — Intersection of Two Arrays

A Layman’s Introduction to Version Control System

My Take: Github or Gitlab

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alvin Matthew Pratama

Alvin Matthew Pratama

More from Medium

Access Control in Swift: A beginner’s guide

Xcode Debugging tips and tricks

Multiple API calls with Dispatch Group in Swift

Factory Design Pattern in Swift for Beginners