Permalink
Browse files

Fail early with a clear message when a params definition is invalid.

  • Loading branch information...
1 parent d51dda0 commit 0c9cc8fe6e2b7c3d62e230b984cb06f52821e1f5 @myronmarston myronmarston committed Sep 13, 2012
Showing with 36 additions and 1 deletion.
  1. +16 −1 lib/interpol/request_params_parser.rb
  2. +20 −0 spec/unit/interpol/request_params_parser_spec.rb
@@ -69,7 +69,19 @@ def description
end
def property_defs_from(meth)
- @endpoint_definition.send(meth).fetch('properties')
+ schema = @endpoint_definition.send(meth)
+
+ unless schema['type'] == 'object'
+ raise InvalidParamsDefinitionError,
+ "The #{meth} of #{@endpoint_definition.description} " +
+ "is not typed as an object expected."
+ end
+
+ schema.fetch('properties') do
+ raise InvalidParamsDefinitionError,
+ "The #{meth} of #{@endpoint_definition.description} " +
+ "does not contain 'properties' as required."
+ end
end
def build_params_schema
@@ -243,6 +255,9 @@ def initialize(*invalid_params)
# Raised when a parameter value cannot be parsed.
CannotBeParsedError = Class.new(ArgumentError)
+
+ # Raised when a params definition is invalid.
+ InvalidParamsDefinitionError = Class.new(ArgumentError)
end
end
@@ -28,6 +28,26 @@ def endpoint_definition
expect { parser }.to raise_error(/#{type} params are not supported/)
end
end
+
+ %w[ path_params query_params ].each do |param_type|
+ it "raises an error if #{param_type} does not have 'type: object'" do
+ endpoint_definition.send(param_type)['type'] = 'array'
+ expect { parser }.to raise_error { |error|
+ error.message.should include(param_type)
+ error.message.should include(endpoint_definition.endpoint_name)
+ error.message.should include("object")
+ }
+ end
+
+ it "raises an error if #{param_type} lacks property definitions" do
+ endpoint_definition.send(param_type).delete('properties')
+ expect { parser }.to raise_error { |error|
+ error.message.should include(param_type)
+ error.message.should include(endpoint_definition.endpoint_name)
+ error.message.should include("properties")
+ }
+ end
+ end
end
describe '#validate!' do

0 comments on commit 0c9cc8f

Please sign in to comment.