Module: StructuredParams::Validations
- Extended by:
- ActiveSupport::Concern
- Included in:
- Params
- Defined in:
- lib/structured_params/validations.rb,
sig/structured_params/validations.rbs
Overview
Provides validates_raw which validates raw parameter values before type casting.
Internally delegates to ActiveModel's validates on the _before_type_cast
attribute, then remaps errors back to the original attribute name.
This means all standard ActiveModel validators (format, numericality, etc.)
work as-is on the raw input value.
Example:
class UserParams < StructuredParams::Params
attribute :age, :integer
validates_raw :age, format: { with: /\A\d+\z/, message: 'must be numeric string' }
end
params = UserParams.new(age: "abc")
params.valid? # => false
params.errors[:age] # => ["must be numeric string"]
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#validates_raw_remap_errors ⇒ void
: () -> void.
Instance Method Details
#validates_raw_remap_errors ⇒ void
This method returns an undefined value.
: () -> void
63 64 65 66 67 68 69 70 |
# File 'lib/structured_params/validations.rb', line 63 def validates_raw_remap_errors self.class.validates_raw_btc_map.each do |attr, btc| next if errors.where(btc).none? errors.where(btc).dup.each { |e| errors.import(e, attribute: attr) } errors.delete(btc) end end |