mini_defender 0.5.7 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 42cffd5eb5cd6e4e6735ac05f335fd0d2cb856430b666893e8b4477c5ac65262
4
- data.tar.gz: 2af39ecec258011f4f4f7d20b447fcf984d239a27b6db4d4f26acd185bfe1031
3
+ metadata.gz: 4afc89ccb1f98c186e927a88b6bd85e9a5194f4dd74c7b45653f8a63feeb8f79
4
+ data.tar.gz: 53c21490530615efe4e8ba51c52ac26e2a448f8b3c555af66744dd7c0ce238df
5
5
  SHA512:
6
- metadata.gz: 06c12dedec9bcf79df20573f7de5553f5b5eeff4f00f6c39132f3f4b901009b4903403821f1fbec828ef49f65c6d0e5e0843eef06ee7db06163505055ebc47bc
7
- data.tar.gz: af62aaeffc7200becfeec360a4192f0fd184dede43c16f953a1ab0e4ff3b51dc434e22d0744906d3e2e6062511d284eafa739822646285d69220759918bf12b6
6
+ metadata.gz: 9cd0c033f42766e22cb7900b0903d275f3454df9293c553a5f2766154ed73760d7c28cd392ccd051d80bffa4c09027687ef7ca361d375b6f4037d706f26d6435
7
+ data.tar.gz: 9a50f0cc9ab6e9cd751d978cd5e37ec48ee2c742f80b5d058af6e82db2c1d2d9fdbf2de155c967fcf5baf1e7079d6fb77ad01dbd7e1d3ed025e0f3a5f0916501
@@ -1,19 +1,83 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class MiniDefender::Rules::Integer < MiniDefender::Rule
4
+ attr_reader :mode
5
+ attr_reader :parsed
6
+
7
+ DIGIT_MAP = {
8
+ # Arabic-Indic Digits
9
+ "\u0660" => '0',
10
+ "\u0661" => '1',
11
+ "\u0662" => '2',
12
+ "\u0663" => '3',
13
+ "\u0664" => '4',
14
+ "\u0665" => '5',
15
+ "\u0666" => '6',
16
+ "\u0667" => '7',
17
+ "\u0668" => '8',
18
+ "\u0669" => '9',
19
+
20
+ # Extended Arabic-Indic Digits
21
+ "\u06F0" => '0',
22
+ "\u06F1" => '1',
23
+ "\u06F2" => '2',
24
+ "\u06F3" => '3',
25
+ "\u06F4" => '4',
26
+ "\u06F5" => '5',
27
+ "\u06F6" => '6',
28
+ "\u06F7" => '7',
29
+ "\u06F8" => '8',
30
+ "\u06F9" => '9',
31
+ }
32
+
33
+ def initialize(mode = 'strict')
34
+ @mode = mode
35
+ end
36
+
4
37
  def self.signature
5
38
  'integer'
6
39
  end
7
40
 
41
+ def self.make(args)
42
+ new(args[0] || 'strict')
43
+ end
44
+
8
45
  def coerce(value)
9
- value.to_i
46
+ @parsed
10
47
  end
11
48
 
12
49
  def passes?(attribute, value, validator)
13
- value.is_a?(Integer) || value.is_a?(String) && value.match?(/^\d+$/)
50
+ # Avoid converting integers to string and back
51
+ if value.is_a?(Integer)
52
+ @parsed = value
53
+ return true
54
+ end
55
+
56
+ value = value.to_s
57
+
58
+ if @mode == 'relaxed'
59
+ value = normalize_digits(value)
60
+ end
61
+
62
+ @parsed = Integer(value)
63
+ rescue
64
+ false
14
65
  end
15
66
 
16
67
  def message(attribute, value, validator)
17
68
  "The value must be an integer."
18
69
  end
70
+
71
+ def normalize_digits(data)
72
+ # Check if arabic digits exist or avoid expensive string creation operation
73
+ unless data.match?(/[\u0660-\u0669\u06F0-\u06F9]/)
74
+ return data
75
+ end
76
+
77
+ DIGIT_MAP.each do |k, v|
78
+ data = data.gsub(k, v)
79
+ end
80
+
81
+ data
82
+ end
19
83
  end
@@ -33,9 +33,22 @@ module MiniDefender
33
33
  end
34
34
 
35
35
  def array_patterns(rules)
36
- rules
37
- .filter { |key, _| key.include?('*') }
38
- .map { |key, _| Regexp.compile('\A' + key.gsub(/\*/, '\d+') + '\Z') }
36
+ unless rules.is_a?(Hash)
37
+ raise ArgumentError, 'Rules must be a Hash'
38
+ end
39
+
40
+ result = rules.keys.filter { |key| key.include?('*') }
41
+
42
+ # If the user hasn't add a rule for each of the array elements, add it manually
43
+ rules.keys.each do |key|
44
+ current = []
45
+ key.split('.').each do |section|
46
+ current << section
47
+ result << current.join('.') if section == '*'
48
+ end
49
+ end
50
+
51
+ result.sort.uniq.map { |key| Regexp.compile('\A' + key.gsub(/\*/, '\d+') + '\Z') }
39
52
  end
40
53
  end
41
54
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MiniDefender
4
- VERSION = "0.5.7"
4
+ VERSION = "0.6.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_defender
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.7
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ali Alhoshaiyan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-10 00:00:00.000000000 Z
11
+ date: 2024-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport