Skip to content

Commit ed1f1dd

Browse files
committed
fix: restrict response data input types
1 parent 90b5dcc commit ed1f1dd

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

lib/irt_ruby/response_data_validator.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module ResponseDataValidator
66
module_function
77

88
def validate!(data)
9-
raise ArgumentError, "response data must be a Matrix or array of arrays" unless data.respond_to?(:to_a)
9+
raise ArgumentError, "response data must be a Matrix or array of arrays" unless valid_data_container?(data)
1010

1111
data_array = data.to_a
1212

@@ -49,5 +49,9 @@ def validate_values!(data_array)
4949
def valid_response?(value)
5050
value.nil? || value.eql?(0) || value.eql?(1)
5151
end
52+
53+
def valid_data_container?(data)
54+
data.is_a?(Array) || (defined?(::Matrix) && data.is_a?(::Matrix))
55+
end
5256
end
5357
end

spec/spec_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
it "rejects non-numeric truthy, falsey, and string responses" do
2828
expect { described_class.new([[1, "1"], [false, nil]]) }.to raise_error(ArgumentError, /invalid value/)
2929
end
30+
31+
it "rejects hash input even when it can be converted to an array" do
32+
expect { described_class.new({ [0] => 1 }) }.to raise_error(ArgumentError, /Matrix or array of arrays/)
33+
end
3034
end
3135

3236
RSpec.configure do |config|

0 commit comments

Comments
 (0)