bulletproof-nodejs icon indicating copy to clipboard operation
bulletproof-nodejs copied to clipboard

Question: Form/Input Validation in Controller or Service Layer?

Open austinckl opened this issue 4 years ago • 2 comments

Hi Santiq,

Good Day!

Great work! I've been learning on this pattern and applying on a test project. But now I'm struggling about which layer should the form/input validation located at. I saw your example that you place the validation in the express middleware, but isn't these validation considered as business logic? Which mean shouldn't we place the validation at the service layer? Hope you can help me to understand on this part.

Thanks.

austinckl avatar Sep 09 '19 14:09 austinckl

Hi @austinckl, Good day for you too!

In my opinion, there are different types of validations, the API validations just make sure that the required data is sent, and make sure it is in the correct type.

And then you have the business logic validations:

  • is this user owner of this resource?
  • does this user belong to this organization?
  • does this user has the amount of money requested to transfer to the other user?

Also, the way I see the Service/Business layer is like a command program that you invoke with arguments. The API layer is just a way to invoke this program through the internet but should be easy to create a library or npm package from it.

What do you think?

santiq avatar Sep 11 '19 09:09 santiq

Make sense to me now. Thanks for your explanation!

austinckl avatar Sep 12 '19 13:09 austinckl