Module: JSONRPC2::Types
- Defined in:
- lib/jsonrpc2/types.rb
Overview
Types are checked against textual descriptions of the contents:
-
String - A string
-
Number - Any kind of number
-
Integer - Integer value
-
Boolean - true or false
-
true - True value
-
false - False value
-
null - nil value
-
Object - An object
-
Array - An array
-
Array [Type] - An array of type Type
-
Value (or Any or void) - Any value of any type
-
CustomType - A defined custom object type
Constant Summary collapse
- DateTimeRegex =
%r"([0-9]{4})(-([0-9]{2})(-([0-9]{2})(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?"
- DateRegex =
%r'\A\d{4}-\d{2}-\d{2}\z'
- TimeRegex =
%r'\A\d{2}:\d{2}(?:\.\d{1,4})?\z'
Class Method Summary collapse
-
.valid?(interface, type_string, object) ⇒ Boolean
Checks that object is of given type (and that any custom types comply with interface).
-
.valid_params?(interface, method, data) ⇒ Boolean
Checks that param hash is valid for API call.
-
.valid_result?(interface, method, value) ⇒ Boolean
Checks that result is valid for API call.
Class Method Details
.valid?(interface, type_string, object) ⇒ Boolean
Checks that object is of given type (and that any custom types comply with interface)
separated if more than one
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/jsonrpc2/types.rb', line 32 def valid?(interface, type_string, object) res = type_string.split(/,/).any? do |type| case type when 'String' object.is_a?(String) when 'Number' object.kind_of?(Numeric) when 'true' object == true when 'false' object == false when 'Boolean' object == true || object == false when 'null' object.nil? when 'Integer' object.kind_of?(Numeric) && (object.to_i.to_f == object.to_f) when 'Object' object.is_a?(Hash) when 'Array' object.is_a?(Array) when 'Date' object.is_a?(String) && DateRegex.match(object) || object.is_a?(Date) when 'Time' object.is_a?(String) && TimeRegex.match(object) || object.is_a?(Time) when 'DateTime' object.is_a?(String) && DateTimeRegex.match(object) || object.is_a?(Time) when /\AArray\[(.*)\]\z/ object.is_a?(Array) && object.all? { |value| valid?(interface, $1, value) } when 'Value', 'Any', 'void' true else # Custom type subset = (type[-1] == ?*) type = type[0...-1] if subset custom = interface.types[type] if custom custom.valid_object?(interface, object, subset) else raise "Invalid/unknown type: #{type} for #{interface.name}" end end end res end |
.valid_params?(interface, method, data) ⇒ Boolean
Checks that param hash is valid for API call
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/jsonrpc2/types.rb', line 84 def valid_params?(interface, method, data) about = interface.about[method.to_s] return true if about.nil? # No defined params params = (about[:params] || []) param_names = params.map { |param| param[:name] } if params.empty? && (data.nil? or data.empty?) return true end raise "Params should not be nil" if data.nil? extra_keys = data.keys - param_names unless extra_keys.empty? raise "Extra parameters #{extra_keys.inspect} for #{method}." end params.each do |param| if data.has_key?(param[:name]) value = data[param[:name].to_s] unless valid?(interface, param[:type], value) raise "'#{param[:name]}' should be of type #{param[:type]}, was #{value.class.name}" end elsif ! param[:required] next true else raise "Missing parameter: '#{param[:name]}' of type #{param[:type]} for #{method}" end end end |
.valid_result?(interface, method, value) ⇒ Boolean
Checks that result is valid for API call
122 123 124 125 126 127 128 129 130 |
# File 'lib/jsonrpc2/types.rb', line 122 def valid_result?(interface, method, value) about = interface.about[method.to_s] return true if about.nil? # Undefined if about[:returns].nil? return value.nil? end valid?(interface, about[:returns][:type], value) or raise "Invalid return type: should have been #{about[:returns][:type]}, was #{value.class.name}" end |