Module: Tama::Params

Defined in:
lib/tama/params.rb

Class Method Summary collapse

Class Method Details

.hash(value, name) ⇒ Object



7
8
9
10
11
12
# File 'lib/tama/params.rb', line 7

def hash(value, name)
  value = value.to_h if value.respond_to?(:to_h) && !value.is_a?(Hash)
  raise Error::ValidationError.new("#{name} must be a hash", errors: {name => "must be a hash"}) unless value.is_a?(Hash)

  value.transform_keys(&:to_s)
end

.optional_string!(data, key) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/tama/params.rb', line 21

def optional_string!(data, key)
  value = data[key]
  return if value.nil?
  return value if value.is_a?(String)

  raise Error::ValidationError.new("#{key} is invalid", errors: {key => "must be a string"})
end

.require_string!(data, key) ⇒ Object



14
15
16
17
18
19
# File 'lib/tama/params.rb', line 14

def require_string!(data, key)
  value = data[key]
  return value if value.is_a?(String) && !value.empty?

  raise Error::ValidationError.new("#{key} is required", errors: {key => "must be a non-empty string"})
end