Skip to content

Commit 9b09bec

Browse files
committed
docs: clarify response data validation contract
1 parent ed1f1dd commit 9b09bec

4 files changed

Lines changed: 29 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All notable changes to this project are documented in this file.
44

5+
## [Unreleased]
6+
7+
### Changed
8+
- Clarified that response data must be a `Matrix` or array of arrays containing only integer `0`, integer `1`, or `nil`; floats, strings, booleans, and other values are rejected.
9+
10+
---
11+
512
## [0.3.0] - 2025-01-14
613

714
### Changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ result = model.fit
4747
puts "Abilities: #{result[:abilities]}"
4848
puts "Difficulties: #{result[:difficulties]}"
4949
```
50+
51+
Response data passed to model constructors must be either a `Matrix` or an
52+
array of arrays. Each response value must be the integer `0`, the integer `1`,
53+
or `nil` for missing data; floats such as `0.0`/`1.0`, strings, booleans, and
54+
other values are rejected.
55+
5056
### Using 2PL and 3PL Models
5157
```ruby
5258
two_pl_model = IrtRuby::TwoParameterModel.new(data)

lib/irt_ruby/response_data_validator.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
module IrtRuby
44
# Validates response data accepted by IRT model constructors.
5+
# @api private
56
module ResponseDataValidator
67
module_function
78

@@ -10,7 +11,7 @@ def validate!(data)
1011

1112
data_array = data.to_a
1213

13-
raise ArgumentError, "response data must have at least one row" unless data_array.is_a?(Array) && data_array.any?
14+
raise ArgumentError, "response data must have at least one row" unless data_array.any?
1415

1516
validate_rows!(data_array)
1617
validate_values!(data_array)
@@ -27,11 +28,12 @@ def validate_rows!(data_array)
2728
raise ArgumentError, "response data must have at least one column" if expected_columns.zero?
2829

2930
data_array.each_with_index do |row, index|
30-
raise ArgumentError, "response data row #{index} must be an Array" unless row.is_a?(Array)
31+
row_number = index + 1
32+
raise ArgumentError, "response data row #{row_number} must be an Array" unless row.is_a?(Array)
3133

3234
next if row.size == expected_columns
3335

34-
raise ArgumentError, "response data must be rectangular; row #{index} has #{row.size} columns, expected #{expected_columns}"
36+
raise ArgumentError, "response data must be rectangular; row #{row_number} has #{row.size} columns, expected #{expected_columns}"
3537
end
3638
end
3739

spec/spec_helper.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
end
1313

1414
it "rejects ragged rows" do
15-
expect { described_class.new([[1, 0], [1]]) }.to raise_error(ArgumentError, /rectangular/)
15+
expect { described_class.new([[1, 0], [1]]) }.to raise_error(ArgumentError, /rectangular; row 2/)
1616
end
1717

1818
it "rejects invalid response values" do
@@ -24,8 +24,16 @@
2424
expect { described_class.new([[1.0]]) }.to raise_error(ArgumentError, /invalid value 1\.0/)
2525
end
2626

27-
it "rejects non-numeric truthy, falsey, and string responses" do
28-
expect { described_class.new([[1, "1"], [false, nil]]) }.to raise_error(ArgumentError, /invalid value/)
27+
it "rejects string response values" do
28+
expect { described_class.new([["1"]]) }.to raise_error(ArgumentError, /invalid value "1"/)
29+
end
30+
31+
it "rejects false response values" do
32+
expect { described_class.new([[false]]) }.to raise_error(ArgumentError, /invalid value false/)
33+
end
34+
35+
it "rejects true response values" do
36+
expect { described_class.new([[true]]) }.to raise_error(ArgumentError, /invalid value true/)
2937
end
3038

3139
it "rejects hash input even when it can be converted to an array" do

0 commit comments

Comments
 (0)