I was doing a project calculating the BMI value through an API endpoint. For that, I created a plain ruby class BMI and wanted to validate the parameters sent via API.

Then I found a simple way to validate using ActiveModel::Validations. Here is the article that I read to implement it.

To do this you have to first add the activemodel gem to your Gemfile. Like this:

gem "activemodel", require: "active_model"

Then run:

$ bundle install

After this you include the ActiveModel::Validations module in your ruby class. Like I did in my API project:

class Bmi
  include ActiveModel::Validations

  attr_reader :height, :weight

  def initialize(height:, weight:)
    @height = height
    @weight = weight
  end

ActiveModel::Validations provides you with the full standard validation stack that you know from ActiveRecord. So, I could include the validations that I wanted, like this:

class Bmi
  include ActiveModel::Validations

  attr_reader :height, :weight

  validates :height, :weight, presence: true
  validates :height, numericality: {greater_than: 0}
  validates :weight, numericality: {greater_than: 0}

  def initialize(height:, weight:)
    @height = height
    @weight = weight
  end

The project repo that I used this: thaisantero/imc-calculator

Stay tuned, in the next post I will talk about how to add I18n translations to your validations errors in PORO.