Module: SimpleParams::Validations

Extended by:
ActiveModel::Validations
Included in:
Params
Defined in:
lib/simple_params/validations.rb

Instance Method Summary collapse

Instance Method Details

#valid?(context = nil) ⇒ Boolean

Overriding #valid? to provide recursive validating of

nested params

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/simple_params/validations.rb', line 9

def valid?(context = nil)
  current_context, self.validation_context = validation_context, context
  errors.clear
  run_validations!
  nested_hashes.each do |key, value|
    nested_class = send("#{key}") 
    nested_class.valid?
  end

  nested_arrays.each do |key, array|
    nested_array = send("#{key}") 
    nested_array.each { |a| a.valid? }
  end
  errors.empty?
ensure
  self.validation_context = current_context
end

#validate!Object



27
28
29
30
31
# File 'lib/simple_params/validations.rb', line 27

def validate!
  unless valid?
    raise SimpleParamsError, self.errors.to_hash.to_s
  end
end