Top Level Namespace
Defined Under Namespace
Modules: Botanalytics Classes: AmazonAlexa, Envoy, FacebookMessenger, Generic, GoogleAssistant, SlackEventApi, SlackRTMApi
Instance Method Summary collapse
-
#is_valid(payload, t, name, *keys) ⇒ Object
Validator method.
Instance Method Details
#is_valid(payload, t, name, *keys) ⇒ Object
Validator method
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/util/util.rb', line 5 def is_valid(payload, t, name, *keys) return { :ok=>false, :reason => 'nil payload is not accepted!', :err => Exception.new("payload is nil!") } if payload.nil? if keys.any? path = '' temp = nil keys.each { |key| if temp.nil? path += key return { :ok => false, :reason => 'Field does not exist', :err => Exception.new("Expected field <#{key}> can not be found in #{name}") } if payload[key.to_sym].nil? && payload[key].nil? temp = payload[key] || payload[key.to_sym] else path += '.'+key return { :ok => false, :reason => 'Field does not exist', :err => Exception.new("Expected field <#{path}> can not be found in #{name}") } if temp[key.to_sym].nil? && temp[key].nil? temp = temp[key] || temp[key.to_sym] end } temp.is_a?(t.class) ? {:ok => true} : { :ok => false, :reason => 'Unexpected format!', :err => Exception.new("Expected format for #{path} is #{t.class.name}, found #{temp.class.name}") } else payload.is_a?(t.class) ? {:ok => true} : { :ok => false, :reason => 'Unexpected format!', :err => Exception.new("Expected format for #{name} is #{t.class.name}, found #{payload.class.name}") } end end |