IOS VIPER: where to enter the form verification code?

I read a lot of articles about the clean architecture of VIPER iOS, I understand that the main puropose: separation of problems.

I am currently using it for my project. I have modules, each of which is divided into representations, interactors, presenters, objects and routers (with layout).

I have a module: Address and Add submodule for an additional address page.

So, I have my View protocol implemented by my UIViewController. The view controller contains all weak labels and text fields of IBOutlet (for the new address form).

The address form contains several fields, such as:

  • user name and surname
  • postcode
  • Country
  • state
  • telephone
  • Email
  • etc...

In my case, the facilitator simply relies on user interaction with the interactor who makes the API call.

But before making the API call, I want to pre-check the form to avoid using a useless network resource.

I need to check for example:

  • contents of the country and tell the point of view that the field is required if empty ...
  • email format and inform the viewer that this field is invalid ...

My question is where can I put my form validation code?

Which VIPER component should fill this job?

Thank you in advance!

+5
source share
4 answers

One of the key benefits of VIPER is the separation of problems, as the information is encapsulated in an appropriate element.

Interactor deals with β€œbusiness logic”, which includes most of the verification issues (part of this verification may be carried out by the Court itself). Thus, the presentation would pass on its data to the Presenter, who will refer to Interactor, who will verify his business and ask Entity to inform him about the consistency of the data.

However, using libraries to speed up your development can force you to exchange encapsulation for ease of use. For example, SwiftValidator offers fairly extensive validation rules, but you need to pass your UITextField the Validator component.

So the choice is yours, between the best encapsulated architecture based on Interactor (possibly using a library like Validators ), or a more MVVM-oriented tool like SwiftValidator .

+4
source

Since this is related to business logic, I would say that validation should move on to interoperability. You can even create a worker called form interactiveor - if your check is too large, but an interactor is used for this. With delegates, you can notify the user if something is wrong, what exactly is wrong, an error message, etc.

I would call http://clean-swift.com/clean-swift-ios-architecture/ .

+2
source

It seems you should consider using SOA architecture for this purpose.

In general, the pure application architecture using VIPER modules can be divided into the following layers:

  • Assembly

    There is all the code associated with creating the components of the viper module.

  • Presentation

    You should store components such as Animator, LayoutPerformer, Presenter, View, ViewController, Router.

  • Business logic

    It is designed for Interactor and service bundles. Ideally, each service is stateless and uses classes from the Core Components layer. For example, a service encapsulates some network client and requests and produces some models as output.

  • Main components

    The purpose of this layer is related to your question. This is the place for things like Response Validator, Object Mapper, Network Client, etc. Thus, the answer is placed in the main component layer.

  • Model level

    This is the deepest layer that all entities must contain.

Well, that sounds good, but how can I structure the project in this case?

You can use, for example, the structure below.

 --Modules ----Module ------Interactor ------Presenter ------Assembly ------Router ------View --Services --Core ----Validators ----Mappers --Models 
+1
source

You can check this template ( https://github.com/CheesecakeLabs/Boilerplate_iOS_VIPER ) from this post ( https://www.ckl.io/blog/best-practices-viper-architecture ).

Presenter is responsible for validating the form, but you should not leave code in Presenter ifself. Instead, call FormValidationHelper() on the presenter.

0
source

All Articles