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!