mini_defender 0.1.4 → 0.1.6

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: efdaf5cef8d3ef0e28e077f3d2caf7f7bf067c09a3f4e5d23db5af62f6b95b5a
4
- data.tar.gz: caffb756c4fc342e22ce66fb7e71dd815ea1313cc69d3348c5726c7ac8bafcba
3
+ metadata.gz: c249768a094c4518199ffd28c11b23cedc2795f2fd7b314066dd1ab8735ad64a
4
+ data.tar.gz: 03db289c1919edc2d6cfffd6db6f5ae70011a679edb1af8ae5ee7b915faa06b2
5
5
  SHA512:
6
- metadata.gz: d6e690f9c86463a6929aa097812001af721786df391bfe78d70680683ac046943c3d676a63b69be20883ea826542797a22edfa8144cfb3256b042f8dfcbe66c5
7
- data.tar.gz: d9a09fd5c37e53a41cbd5c522fef92f487e3a7fb0a9e38fe2bb0d5d13c07aa08c7ae06a15d2182384f55609f6f623f0444851808142ad8c6a852b0f63102c24f
6
+ metadata.gz: bcb1491ce3eaef31ea2e713afd7651a5dd397ace80467eeab303086cda2ab1f23e8e044ace47f999ea49c62a31318650387044e4ae28c5e5d2204563c2746c33
7
+ data.tar.gz: 6bacb39daaf34e5e5d2685bb6e93629a8d390dc5dc0f7776495a4a1b003cb3e4939d9c67b2a3ccbe481758f24617707776b5768dc5b34b20d14995e1c33e67f4
data/.DS_Store ADDED
Binary file
@@ -74,4 +74,16 @@ class MiniDefender::Rule
74
74
  def message(attribute, value, validator)
75
75
  raise NotImplementedError, 'Use a concrete implementation.'
76
76
  end
77
+
78
+ def with_message(message)
79
+ @message = message
80
+ self
81
+ end
82
+
83
+ # @param [Object] attribute
84
+ # @param [Object] value
85
+ # @param [MiniDefender::Validator] validator
86
+ def error_message(attribute, value, validator)
87
+ @message || message(attribute, value, validator)
88
+ end
77
89
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ class MiniDefender::Rules::Hostname < MiniDefender::Rule
4
+ HOSTNAME_REGEX = /\A[a-zA-Z0-9][a-zA-Z0-9\-]{1,63}(\.[a-zA-Z0-9][a-zA-Z0-9\-]{1,63})+\.?\z/
5
+
6
+ def self.signature
7
+ 'hostname'
8
+ end
9
+
10
+ def coerce(value)
11
+ value.to_s.downcase.strip
12
+ end
13
+
14
+ def passes?(attribute, value, validator)
15
+ value = value.to_s.downcase.strip
16
+ return false if value.length > 255
17
+
18
+ value.match?(HOSTNAME_REGEX)
19
+ end
20
+
21
+ def message(attribute, value, validator)
22
+ 'Invalid hostname.'
23
+ end
24
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ class MiniDefender::Rules::Iban < MiniDefender::Rule
4
+ LENGTH_MATRIX = {
5
+ 'SA' => 24
6
+ }
7
+
8
+ def self.signature
9
+ 'iban'
10
+ end
11
+
12
+ def coerce(value)
13
+ value.to_s.upcase.gsub(/\s/, '')
14
+ end
15
+
16
+ def passes?(attribute, value, validator)
17
+ value = coerce(value)
18
+ value.match?(/[A-Z]{2}\d+/i) && valid_length?(value) && valid_checksum?(value)
19
+ end
20
+
21
+ def valid_checksum?(iban)
22
+ iban = "#{iban[4..]}#{letter_code(iban[0])}#{letter_code(iban[1])}#{iban[2..3]}".to_i
23
+ iban % 97 === 1
24
+ end
25
+
26
+ def valid_length?(iban)
27
+ iban.length == (LENGTH_MATRIX[iban[0..1]] || iban.length)
28
+ end
29
+
30
+ def message(attribute, value, validator)
31
+ "The value should be a valid IBAN."
32
+ end
33
+
34
+ private
35
+
36
+ def letter_code(letter)
37
+ # letter is assumed to be an upcase ASCII alphabet letter (A-Z)
38
+ letter.ord - 55
39
+ end
40
+ end
@@ -1,13 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class MiniDefender::Rules::Unique < MiniDefender::Rule
4
- def initialize(model, column)
5
- raise ArgumentError, 'model name must be a string or ActiveRecord::Base' unless model.is_a?(String)
6
- raise ArgumentError, 'Column name must be a string' unless column.is_a?(String)
4
+ def initialize(model, column = nil)
5
+ unless model.is_a?(String) || model.is_a?(Class) && model.ancestors.include?(ActiveRecord::Base)
6
+ raise ArgumentError, 'model name must be a string or ActiveRecord::Base'
7
+ end
7
8
 
8
- @model = model.camelcase.constantize
9
+ raise ArgumentError, 'Column name must be a string' unless column.is_a?(String) || column.nil?
10
+
11
+ @model = model
12
+ @model = @model.camelcase.constantize if model.is_a?(String)
9
13
  @column = column
10
14
  @ignore = nil
15
+ @additional_scope = nil
11
16
  end
12
17
 
13
18
  def self.signature
@@ -19,6 +24,11 @@ class MiniDefender::Rules::Unique < MiniDefender::Rule
19
24
  self
20
25
  end
21
26
 
27
+ def scope_by(callback)
28
+ @additional_scope = callback
29
+ self
30
+ end
31
+
22
32
  def self.make(args)
23
33
  raise ArgumentError, 'Model and column are required.' unless args.length == 2
24
34
 
@@ -26,9 +36,13 @@ class MiniDefender::Rules::Unique < MiniDefender::Rule
26
36
  end
27
37
 
28
38
  def passes?(attribute, value, validator)
39
+ @column = attribute.split('.')[-1] if @column.nil?
40
+
29
41
  query = @model.where(@column => value)
42
+ query = @additional_scope.call(query) unless @additional_scope.nil?
30
43
  query = query.where.not(@column, @ignore) unless @ignore.nil?
31
- query.exists?
44
+
45
+ !query.exists?
32
46
  end
33
47
 
34
48
  def message(attribute, value, validator)
@@ -57,7 +57,7 @@ module MiniDefender
57
57
  value_included &= !rule.excluded?(self)
58
58
 
59
59
  unless rule.passes?(k, value, self)
60
- @errors[k] << rule.message(k, value, self)
60
+ @errors[k] << rule.error_message(k, value, self)
61
61
  end
62
62
  end
63
63
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MiniDefender
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.6"
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.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ali Alhoshaiyan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-11-26 00:00:00.000000000 Z
11
+ date: 2022-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -74,6 +74,7 @@ executables: []
74
74
  extensions: []
75
75
  extra_rdoc_files: []
76
76
  files:
77
+ - ".DS_Store"
77
78
  - CHANGELOG.md
78
79
  - Gemfile
79
80
  - LICENSE.md
@@ -126,6 +127,8 @@ files:
126
127
  - lib/mini_defender/rules/greater_than.rb
127
128
  - lib/mini_defender/rules/greater_than_or_equal.rb
128
129
  - lib/mini_defender/rules/hash.rb
130
+ - lib/mini_defender/rules/hostname.rb
131
+ - lib/mini_defender/rules/iban.rb
129
132
  - lib/mini_defender/rules/image.rb
130
133
  - lib/mini_defender/rules/in.rb
131
134
  - lib/mini_defender/rules/in_field.rb
@@ -197,7 +200,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
197
200
  - !ruby/object:Gem::Version
198
201
  version: '0'
199
202
  requirements: []
200
- rubygems_version: 3.3.7
203
+ rubygems_version: 3.3.26
201
204
  signing_key:
202
205
  specification_version: 4
203
206
  summary: A small and efficient validation library for Rails and anything that uses