-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ba2ec2
commit d625dfd
Showing
4 changed files
with
121 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,83 @@ | ||
# frozen_string_literal: true | ||
|
||
class MiniDefender::Rules::Integer < MiniDefender::Rule | ||
attr_reader :mode | ||
attr_reader :parsed | ||
|
||
DIGIT_MAP = { | ||
# Arabic-Indic Digits | ||
"\u0660" => '0', | ||
"\u0661" => '1', | ||
"\u0662" => '2', | ||
"\u0663" => '3', | ||
"\u0664" => '4', | ||
"\u0665" => '5', | ||
"\u0666" => '6', | ||
"\u0667" => '7', | ||
"\u0668" => '8', | ||
"\u0669" => '9', | ||
|
||
# Extended Arabic-Indic Digits | ||
"\u06F0" => '0', | ||
"\u06F1" => '1', | ||
"\u06F2" => '2', | ||
"\u06F3" => '3', | ||
"\u06F4" => '4', | ||
"\u06F5" => '5', | ||
"\u06F6" => '6', | ||
"\u06F7" => '7', | ||
"\u06F8" => '8', | ||
"\u06F9" => '9', | ||
} | ||
|
||
def initialize(mode = 'strict') | ||
@mode = mode | ||
end | ||
|
||
def self.signature | ||
'integer' | ||
end | ||
|
||
def self.make(args) | ||
new(args[0] || 'strict') | ||
end | ||
|
||
def coerce(value) | ||
Integer(value) | ||
@parsed | ||
end | ||
|
||
def passes?(attribute, value, validator) | ||
Integer(value.to_s) | ||
# Avoid converting integers to string and back | ||
if value.is_a?(Integer) | ||
@parsed = value | ||
return true | ||
end | ||
|
||
value = value.to_s | ||
|
||
if @mode == 'relaxed' | ||
value = normalize_digits(value) | ||
end | ||
|
||
@parsed = Integer(value) | ||
rescue | ||
false | ||
end | ||
end | ||
|
||
def message(attribute, value, validator) | ||
"The value must be an integer." | ||
end | ||
|
||
def normalize_digits(data) | ||
# Check if arabic digits exist or avoid expensive string creation operation | ||
unless data.match?(/[\u0660-\u0669\u06F0-\u06F9]/) | ||
return data | ||
end | ||
|
||
DIGIT_MAP.each do |k, v| | ||
data = data.gsub(k, v) | ||
end | ||
|
||
data | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module MiniDefender | ||
VERSION = "0.5.8" | ||
VERSION = "0.6.0" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class ExpiryMonthTest < Minitest::Test | ||
def setup | ||
@rule = MiniDefender::Rules::ExpiryMonth.new | ||
end | ||
|
||
def test_accept_integer_value | ||
assert @rule.passes?('month', 1, nil) | ||
end | ||
|
||
def test_accept_string_digits | ||
assert @rule.passes?('month', '2', nil) | ||
end | ||
|
||
def test_accept_single_integer_value | ||
assert @rule.passes?('month', '3', nil) | ||
end | ||
|
||
def test_accept_string_with_leading_zero | ||
assert @rule.passes?('month', '04', nil) | ||
end | ||
|
||
def test_accept_one_to_twelve | ||
(1..12).each do |month| | ||
assert @rule.passes?('month', month.to_s.rjust(2, '0'), nil) | ||
end | ||
end | ||
|
||
def test_rejects_invalid_month | ||
refute @rule.passes?('month', '13', nil) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters