Skip to content

Commit 90b5dcc

Browse files
committed
fix: tighten response data validation
1 parent f2a4686 commit 90b5dcc

5 files changed

Lines changed: 17 additions & 4 deletions

File tree

lib/irt_ruby/rasch_model.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require "irt_ruby/response_data_validator"
4+
35
module IrtRuby
46
# A class representing the Rasch model for Item Response Theory (ability - difficulty).
57
# Incorporates:

lib/irt_ruby/response_data_validator.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
module IrtRuby
44
# Validates response data accepted by IRT model constructors.
55
module ResponseDataValidator
6-
VALID_RESPONSES = [0, 1, nil].freeze
7-
86
module_function
97

108
def validate!(data)
@@ -40,12 +38,16 @@ def validate_rows!(data_array)
4038
def validate_values!(data_array)
4139
data_array.each_with_index do |row, row_index|
4240
row.each_with_index do |value, column_index|
43-
next if VALID_RESPONSES.include?(value)
41+
next if valid_response?(value)
4442

4543
raise ArgumentError,
46-
"response data contains invalid value #{value.inspect} at row #{row_index}, column #{column_index}; allowed values are 0, 1, and nil"
44+
"response data contains invalid value #{value.inspect} at row #{row_index + 1}, column #{column_index + 1}; allowed values are 0, 1, and nil"
4745
end
4846
end
4947
end
48+
49+
def valid_response?(value)
50+
value.nil? || value.eql?(0) || value.eql?(1)
51+
end
5052
end
5153
end

lib/irt_ruby/three_parameter_model.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require "irt_ruby/response_data_validator"
4+
35
module IrtRuby
46
# A class representing the Three-Parameter model (3PL) for Item Response Theory.
57
# Incorporates:

lib/irt_ruby/two_parameter_model.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require "irt_ruby/response_data_validator"
4+
35
module IrtRuby
46
# A class representing the Two-Parameter model (2PL) for IRT.
57
# Incorporates:

spec/spec_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
expect { described_class.new([[1, 2], [0, nil]]) }.to raise_error(ArgumentError, /invalid value 2/)
2020
end
2121

22+
it "rejects float response values that compare equal to allowed integers" do
23+
expect { described_class.new([[0.0]]) }.to raise_error(ArgumentError, /invalid value 0\.0/)
24+
expect { described_class.new([[1.0]]) }.to raise_error(ArgumentError, /invalid value 1\.0/)
25+
end
26+
2227
it "rejects non-numeric truthy, falsey, and string responses" do
2328
expect { described_class.new([[1, "1"], [false, nil]]) }.to raise_error(ArgumentError, /invalid value/)
2429
end

0 commit comments

Comments
 (0)