https://github.com/notus-sh/active_model_validations_reflection
An ActiveModel extension for more expressive validations reflection
https://github.com/notus-sh/active_model_validations_reflection
activerecord rails ruby validations
Last synced: 4 months ago
JSON representation
An ActiveModel extension for more expressive validations reflection
- Host: GitHub
- URL: https://github.com/notus-sh/active_model_validations_reflection
- Owner: notus-sh
- License: other
- Created: 2021-05-06T05:14:05.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2025-11-24T20:28:09.000Z (7 months ago)
- Last Synced: 2025-11-28T08:13:32.216Z (7 months ago)
- Topics: activerecord, rails, ruby, validations
- Language: Ruby
- Size: 50.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# ActiveModel::Validations::Reflection
[](https://github.com/notus-sh/active_model_validations_reflection/actions/workflows/unit-tests.yml)
[](https://badge.fury.io/rb/active_model_validations_reflection)
`ActiveModel::Validations::Reflection` extends `ActiveModel` reflection capabilities on validations.
## Installation
`ActiveModel::Validations::Reflection` is distributed as a gem and available on [rubygems.org](https://rubygems.org/gems/active_model_validations_reflection) so you can add it to your `Gemfile` or install it manually with:
```ruby
gem install active_model_validations_reflection
```
## Usage
`ActiveModel`, at least since 3.0, provide a basic mechanism to inspect validations on a model through [`ActiveModel::Validations::ClassMethods#validators`](https://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validators) and [`ActiveModel::Validations::ClassMethods#validators_on`](https://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validators_on).
This extends it with easier filtering on validators on class level and contextual filtering on instance level.
### `.validators_of_kinds(*kinds)` & `.validators_on_of_kinds(attribute, *kinds)`
`.validators_of_kinds` & `.validators_on_of_kinds` are class methods to filter validators by kind, respectively on all or a single attribute.
Any kind supported by [`validates`](https://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates) (`:presence` , `:inclusion`, `:uniqueness`, or custom validator's kind) can be used to filter validators.
```ruby
class Article < ApplicationRecord
include ActiveModel::Validations::Reflection
validates :date,
presence: true,
timeliness: { type: :date }
end
Article.validators_of_kinds(:presence)
# => [#]
Article.validators_on_of_kinds(:date, :timeliness)
# => [#]
```
### `.flat_validators_of_kinds(*kinds)` & `.flat_validators_on_of_kinds(attribute, *kinds)`
Same as the above two but only return validators without conditions (`:if` / `:unless`).
```ruby
class Article < ApplicationRecord
include ActiveModel::Validations::Reflection
enum :status, %i[draft published]
validates :date,
presence: true,
timeliness: { type: :date, if: :published? }
end
Article.validators_of_kinds(:presence)
# => [#]
Article.validators_on_of_kinds(:date, :timeliness)
# => []
```
### `#relevant_validators(*kinds)` & `#relevant_validators_on(attribute, *kinds)`
Returns validator that will be applied considering the instance current state: flat validators and conditional validators whose condition is met.
```ruby
class Article < ApplicationRecord
include ActiveModel::Validations::Reflection
enum :status, %i[draft published]
validates :date,
presence: true,
timeliness: { type: :date, if: :published? }
end
article = Article.new
article.relevant_validators
# => [#]
article.relevant_validators_on(:date)
# => [#]
article.status = :published
article.relevant_validators_on(:date)
# => [#, #]
```
## Contributing
Bug reports and pull requests are welcome on GitHub at .