Skip to content

Add ActiveModel::CompositeValidator - #42907

Open
jonathanhefner wants to merge 1 commit into
rails:mainfrom
jonathanhefner:add-composite_validator
Open

jonathanhefner wants to merge 1 commit into
rails:mainfrom
jonathanhefner:add-composite_validator

Conversation

@jonathanhefner

@jonathanhefner jonathanhefner commented Jul 29, 2021 •

Copy link
Copy Markdown
Member

CompositeValidator is a base class that can be used to compose existing validators. Subclasses define a compose method that calls validates* methods in the same way a model class would:

class MoneyValidator < ActiveModel::CompositeValidator
  def compose
    currencies = options[:in]

    validates *attributes, numericality: { only_integer: true }
    validates *attributes.map { |attribute| "#{attribute}_currency" }, inclusion: currencies
  end
end

class Account < ActiveRecord::Base
  validates :balance, money: ["JPY"]
end

Account.new(balance: 100, balance_currency: "JPY").valid?   # => true
Account.new(balance: 100.1, balance_currency: "JPY").valid? # => false
Account.new(balance: 100, balance_currency: "USD").valid?   # => false

This is based on the discussion in #41253.

@jonathanhefner
jonathanhefner force-pushed the add-composite_validator branch 2 times, most recently from 0640fc0 to 24b471f Compare January 20, 2022 23:03
@jonathanhefner
jonathanhefner marked this pull request as ready for review January 20, 2022 23:54
@rails-bot

rails-bot Bot commented Apr 21, 2022

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 21, 2022
@rails-bot rails-bot Bot closed this Apr 28, 2022
@rails-bot rails-bot Bot removed the stale label Apr 28, 2022
`CompositeValidator` is a base class that can be used to compose
existing validators.  Subclasses define a `compose` method that calls
`validates*` methods in the same way a model class would:

```ruby
class MoneyValidator < ActiveModel::CompositeValidator
  def compose
    currencies = options[:in]

    validates *attributes, numericality: { only_integer: true }
    validates *attributes.map { |attribute| "#{attribute}_currency" }, inclusion: currencies
  end
end

class Account < ActiveRecord::Base
  validates :balance, money: ["JPY"]
end

Account.new(balance: 100, balance_currency: "JPY").valid?   # => true
Account.new(balance: 100.1, balance_currency: "JPY").valid? # => false
Account.new(balance: 100, balance_currency: "USD").valid?   # => false
```
@jonathanhefner
jonathanhefner force-pushed the add-composite_validator branch from 24b471f to 97df780 Compare April 28, 2022 16:49
@jonathanhefner jonathanhefner changed the title Add ActiveModel::CompositeValidator Add ActiveModel::CompositeValidator Apr 28, 2022

This branch has not been deployed

No deployments
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.

1 participant