How to list all validators in a Rails model
Rails models have a validators
class method. It comes in handy to review validation rules distributed over multiple classes and modules in a larger codebase.
Official example:
class Person
include ActiveModel::Validations
validates_with MyValidator
validates_with OtherValidator, on: :create
validates_with StrictValidator, strict: true
end
Person.validators
# => [
# #<MyValidator:0x007fbff403e808 @options={}>,
# #<OtherValidator:0x007fbff403d930 @options={on: :create}>,
# #<StrictValidator:0x007fbff3204a30 @options={strict:true}>
# ]
There is also #validators_on(:attribute)
to fetch attribute-specific validators.
References:
Somewhat related: