Dry-rb for Ruby on Rails microservice part 2: Validations

Dry-rb for Ruby on Rails microservice part 2: Validations
Average rating: 0
(0 votes)

Thanks! You’ve rated this material!

validation-dogs-209x300This is the second tech post about less persistent microservices built on Ruby on Rails with help of DRY-rb utility framework.

In the first part we’ve discussed the usage of dry-types library. Now it’s turn to show how to validate parameters with the dry-validation. With its help we are going to mimic the ActiveModel validations API.

First of all, we need to implement the the same API as ActiveModel::Validations has, that’s how it looks like:

As you can see it implements the minimal methods we used before in the traditional Rails validations: `validate!`, `valid?`, and `errors`. For the simplicity we used the ActiveModel::Errors class in favour of implementing it by our own. 🙂

Now it’s time to show how dry-validations work together with dry-types. Welcome to the Box model:

The Box validates `packing`, `clothes`, and `drawings` properties with these: it must be an Integer within 0 and 100. It needs some proof, so:

If you like dry-rb, but don’t know how to use it within Rails application, that’s it!

Rate this article, if you like it

Thanks! You’ve rated this material!

Got a project? Let's discuss it!

Kyiv Sofiivska 1/2a, 01001, Kyiv, Ukraine
Dnipro Hlinky 2, of. 1003, 49000, Dnipro, Ukraine
Kharkiv Otakara Yarosha 22, 61000, Kharkiv, Ukraine
Email info@syndicode.com