This topic is for the [Extract Validator] exercise in the [Refactoring] trail. Post any questions, corrections, or pointers you have to share with other Upcase subscribers.
[Extract Validator]: https://exercises.upcase.com/exercises/extract-validator
[Refactoring]: Refactoring Code | Code Refactoring Online Tutorial by thoughtbot
What’s the difference between ActiveModel::Validator
and ActiveModel::EachValidator
?
We can use both of them to implement this custom validator.
Hi @rckv, the difference is that ActiveModel::Validator
subclasses will have a validate
method that takes a whole record, while ActiveModel::EachValidator
will have a validate_each
method which takes all of record, attribute, value
as params. The EachValidator is described as:
The easiest way to add custom validators for validating individual attributes is with the convenient
ActiveModel::EachValidator
You can read more here: Active Record Validations — Ruby on Rails Guides