Repository navigation
Add ActiveModel::CompositeValidator - #42907
Open
jonathanhefner wants to merge 1 commit into
Open
jonathanhefner wants to merge 1 commit into
jonathanhefner wants to merge 1 commit into
Conversation
jonathanhefner
force-pushed
the
add-composite_validator
branch
2 times, most recently
from
January 20, 2022 23:03
0640fc0 to
24b471f
Compare
jonathanhefner
marked this pull request as ready for review
January 20, 2022 23:54
|
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. |
`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
force-pushed
the
add-composite_validator
branch
from
April 28, 2022 16:49
24b471f to
97df780
Compare
ActiveModel::CompositeValidator
4 tasks done
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
CompositeValidatoris a base class that can be used to compose existing validators. Subclasses define acomposemethod that callsvalidates*methods in the same way a model class would:This is based on the discussion in #41253.