Skip to content

Document creating validators by combining existing ones - #41253

Closed
eliotsykes wants to merge 1 commit into
rails:mainfrom
eliotsykes:how-to-combine-validators
Closed

eliotsykes wants to merge 1 commit into
rails:mainfrom
eliotsykes:how-to-combine-validators

Conversation

@eliotsykes

@eliotsykes eliotsykes commented Jan 27, 2021 •

Copy link
Copy Markdown
Contributor

Documents how to compose new validators by combining existing validators, e.g.

class Product < ApplicationRecord
  validates :price, money: true
  # Above line replaces these two lines:
  # validates :price, numericality: { only_integer: true }
  # validates :price_currency, inclusion: { in: %w(USD CNY JPY) }
end

# Validator composed of numericality and inclusion validators.
class MoneyValidator < ActiveModel::Validator
  def initialize(options={})
    super
    options[:attributes].each do |attr_name|
      options[:class].validates attr_name, numericality: { only_integer: true }
      options[:class].validates "#{attr_name}_currency", inclusion: { in: %w(USD CNY JPY) }
    end
  end

  def validate(_record)
    # Method does nothing as all validation is currently delegated to validators in constructor.
  end
end

@rails-bot

rails-bot Bot commented Apr 27, 2021

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot Bot added the stale label Apr 27, 2021
@eliotsykes

Copy link
Copy Markdown
Contributor Author

Please keep open/fresh, I'm hoping to get some feedback 🤞

@rails-bot rails-bot Bot removed the stale label Apr 27, 2021
@jonathanhefner

Copy link
Copy Markdown
Member

Something feels a little off about the code here. Even though initialize is only invoked once per application, it still gives me pause seeing a class macro invoked in an instance method. Also, an empty (but required!) validate method isn't so pretty.

I wonder if we could add a new ActiveModel::CompositeValidator to inherit from to make this easier? It could provide a validates method that delegates to options[:class], and it could invoke a compose method (which subclasses override) as part of initialize. So MoneyValidator would look like:

class MoneyValidator < ActiveModel::CompositeValidator
  def compose(options)
    options[:attributes].each do |attr_name|
      validates attr_name, numericality: { only_integer: true }
      validates "#{attr_name}_currency", inclusion: { in: %w(USD CNY JPY) }
    end
  end
end

@eliotsykes

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback. Agreed the CompositeValidator suggested would be an improvement. Please consider opening a new PR with it to gather feedback on its design.

@rafaelfranca

Copy link
Copy Markdown
Member

Thank you for the PR. I think we should implement the CompositeValidator and in that case it is better to already document it instead of changing the current documentation to tell how to compose validators with the current implementation. Feel free to open a PR implementing the CompositeValidator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants