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
# 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
  errors.empty?
ensure
  self.validation_context = current_context
end

#validate!Object



22
23
24
25
26
# File 'lib/simple_params/validations.rb', line 22

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