Skip to content

Commit 6b4f101

Browse files
authored
Reject negative days when testing dates (#34)
Date.valid_date? counts negative days from the end of the month, so the "day - 60" check for coordination numbers passed for regular days too. That made is_coord true for e.g. 750330-4631, and made valid? accept impossible dates like 750232-4630.
1 parent 8edc531 commit 6b4f101

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

‎lib/personnummer.rb‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ def get_parts(personnummer)
116116
# Checks if the given values form a valid date
117117
# (TrueClass/FalseClass)
118118
def test_date(year, month, day)
119-
Date.valid_date?(year, month, day)
119+
# Date.valid_date? counts negative days from the end of the month, so e.g. day 30 - 60 would pass.
120+
day > 0 && Date.valid_date?(year, month, day)
120121
end
121122

122123
# Checks if the Personnummer is valid

‎test/test_personnummer.rb‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ def test_coordination_numbers
4848
def test_wrong_coordination_numbers
4949
assert_equal false, Personnummer::valid?('900161-0017', false)
5050
assert_equal false, Personnummer::valid?('640893-3231')
51+
assert_equal false, Personnummer::valid?('750232-4630')
52+
end
53+
54+
def test_is_coord
55+
assert_equal true, Personnummer.parse('850769-9810').is_coord
56+
assert_equal false, Personnummer.parse('850709-9813').is_coord
57+
assert_equal false, Personnummer.parse('750330-4631').is_coord
5158
end
5259

5360
def test_format

0 commit comments

Comments
 (0)