Lightweight Login form validator using Combine
Since I have written an article for implementing a validator for the login form in RxSwift, I decided to write one for the same idea using Apple Combine framework.
Almost all mobile apps require a login form and you need to validate the user credentials before you proceed with the login process.
Let’s start implementing our reactive validator in a simple way.
First, let’s import Combine
and Foundation
and define our errors enum to handle the expected validation errors:
Second, let’s create LoginFormValidator
class then add a private Constants
enum and another enum ValidationType
for the validation types.
We have created validate
function with 2 parameters text
and type
and it will manage the validation process inside the class and return a type of AnyPublisher<Void, ValidationError>
to return Void
in case of success or ValidationError
in failure.
The next step is to create our private functions to validate the email and password.
We have created our private functions validateEmail
and ValidatePassword
to keep our code clean.
We use Future
to manage the state and return the result and we added Deferred
here to wait for the implementation of Future
block.
Finally, We are going to implement the usage of LoginFormValidator
class in real-time.
Now our validator is ready to use and you can add more validation cases depending on your app requirements.
I tried to keep it simple to explain the idea easily and you get used to it.
You can find the implementation of the Login form validator using RxSwift
in the following story.