Introduce Input Data Validation Layer Using Dry-validations

The validation of the input data is a very common problem that we face almost every day.

The correct and expected input is very important in order to achieve proper and expected execution flow.

When it comes to the implementation of the data validators, we very often keep the logic of the input validation in the controllers. This is not something wrong but it may lead to the situations when the controller takes too much responsibilities and as a result becomes too large and mode difficult to maintain. In a perfect world, our controllers should only focus on supervising the process of handling the request. The controller should only know who it should call in order to achieve certain goals (not how to achieve those goals). In that case, it would be perfect if we could separate our data validation logic from the actual controller logic.

In my previous blog post, I presented you how to use the Dry stack to make our services even better. In fact, we can use the Dry stack to improve the process of the input data validation too. We can achieve it using the Dry-validation gem and in this blog post I’m going to show you how to use this gem in order to introduce a separate data validation layer.

Kamil Walkowiak

Kamil has obtained a Master’s degree in Computer Science at Poznań University of Technology. He is s a self-taught Ruby on Rails developer deeply interested in the web applications topic.

We're Netguru

At Netguru we specialize in designing, building, shipping and scaling beautiful, usable products with blazing-fast efficiency.

Let's talk business