lite-validators 1.0.5 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +18 -3
  3. data/.travis.yml +1 -0
  4. data/CHANGELOG.md +20 -0
  5. data/Gemfile.lock +86 -78
  6. data/config/locales/en.yml +4 -0
  7. data/docs/ALPHA.md +3 -3
  8. data/docs/ALPHA_NUMERIC.md +3 -3
  9. data/docs/BASE64.md +1 -1
  10. data/docs/BOOLEAN.md +8 -1
  11. data/docs/COMPARE.md +1 -1
  12. data/docs/COORDINATE.md +2 -2
  13. data/docs/CREDIT_CARD.md +2 -2
  14. data/docs/CSV.md +3 -3
  15. data/docs/CURRENCY.md +1 -1
  16. data/docs/CUSIP.md +1 -1
  17. data/docs/EMAIL.md +3 -3
  18. data/docs/FILE_CONTENT_TYPE.md +3 -3
  19. data/docs/FILE_EXTENSION.md +2 -2
  20. data/docs/FILE_SIZE.md +3 -3
  21. data/docs/HEX.md +1 -1
  22. data/docs/IMEI.md +1 -1
  23. data/docs/IP_ADDRESS.md +2 -2
  24. data/docs/ISBN.md +1 -1
  25. data/docs/ISIN.md +1 -1
  26. data/docs/MAC_ADDRESS.md +1 -1
  27. data/docs/NAME.md +1 -1
  28. data/docs/PASSWORD.md +2 -2
  29. data/docs/PHONE_NUMBER.md +1 -1
  30. data/docs/SEDOL.md +1 -1
  31. data/docs/SLUG.md +1 -1
  32. data/docs/SSN.md +1 -1
  33. data/docs/TIME_ZONE.md +1 -1
  34. data/docs/TYPE.md +5 -5
  35. data/docs/URL.md +4 -4
  36. data/docs/USERNAME.md +2 -2
  37. data/docs/UUID.md +2 -2
  38. data/lib/lite/validators/alpha_validator.rb +1 -1
  39. data/lib/lite/validators/base64_validator.rb +1 -3
  40. data/lib/lite/validators/base_validator.rb +3 -3
  41. data/lib/lite/validators/boolean_validator.rb +24 -4
  42. data/lib/lite/validators/compare_validator.rb +1 -1
  43. data/lib/lite/validators/coordinate_validator.rb +1 -1
  44. data/lib/lite/validators/credit_card_validator.rb +2 -2
  45. data/lib/lite/validators/csv_validator.rb +4 -4
  46. data/lib/lite/validators/currency_validator.rb +1 -1
  47. data/lib/lite/validators/cusip_validator.rb +1 -1
  48. data/lib/lite/validators/ein_validator.rb +1 -1
  49. data/lib/lite/validators/email_validator.rb +2 -2
  50. data/lib/lite/validators/file_content_type_validator.rb +1 -1
  51. data/lib/lite/validators/file_extension_validator.rb +1 -3
  52. data/lib/lite/validators/file_size_validator.rb +4 -4
  53. data/lib/lite/validators/hex_validator.rb +1 -1
  54. data/lib/lite/validators/imei_validator.rb +1 -1
  55. data/lib/lite/validators/ip_address_validator.rb +1 -1
  56. data/lib/lite/validators/isbn_validator.rb +1 -1
  57. data/lib/lite/validators/isin_validator.rb +6 -6
  58. data/lib/lite/validators/mac_address_validator.rb +5 -5
  59. data/lib/lite/validators/name_validator.rb +1 -1
  60. data/lib/lite/validators/password_validator.rb +1 -1
  61. data/lib/lite/validators/phone_number_validator.rb +1 -1
  62. data/lib/lite/validators/railtie.rb +1 -1
  63. data/lib/lite/validators/sedol_validator.rb +2 -2
  64. data/lib/lite/validators/slug_validator.rb +1 -1
  65. data/lib/lite/validators/ssn_validator.rb +1 -1
  66. data/lib/lite/validators/type_validator.rb +1 -1
  67. data/lib/lite/validators/url_validator.rb +5 -5
  68. data/lib/lite/validators/username_validator.rb +1 -1
  69. data/lib/lite/validators/uuid_validator.rb +1 -1
  70. data/lib/lite/validators/version.rb +1 -1
  71. metadata +7 -7
@@ -18,9 +18,9 @@ exclude | array, regexp, string, symbol | |
18
18
  ```ruby
19
19
  class User < ActiveRecord::Base
20
20
 
21
- validates :input_0, file_content_type: { include: 'text/csv' }
22
- validates :input_1, file_content_type: { exclude: %w[image/png text/plain] }
23
- validates :input_2, file_content_type: { include: /^text\/.*/, exclude: 'text/csv' }
21
+ validates :input0, file_content_type: { include: 'text/csv' }
22
+ validates :input1, file_content_type: { exclude: %w[image/png text/plain] }
23
+ validates :input2, file_content_type: { include: /^text\/.*/, exclude: 'text/csv' }
24
24
 
25
25
  end
26
26
  ```
@@ -18,8 +18,8 @@ exclude | array, string, symbol | |
18
18
  ```ruby
19
19
  class User < ActiveRecord::Base
20
20
 
21
- validates :input_0, file_extension: { include: 'csv' }
22
- validates :input_1, file_extension: { exclude: [:png, 'text'] }
21
+ validates :input0, file_extension: { include: 'csv' }
22
+ validates :input1, file_extension: { exclude: [:png, 'text'] }
23
23
 
24
24
  end
25
25
  ```
data/docs/FILE_SIZE.md CHANGED
@@ -30,9 +30,9 @@ not_equal_to | numeric | |
30
30
  ```ruby
31
31
  class User < ActiveRecord::Base
32
32
 
33
- validates :input_0, file_size: { less_than: 100 }
34
- validates :input_1, file_size: { greater_than_or_equal_to: 75, less_than: 100 }
35
- validates :input_0, file_size: { in: (50..100) }
33
+ validates :input0, file_size: { less_than: 100 }
34
+ validates :input1, file_size: { greater_than_or_equal_to: 75, less_than: 100 }
35
+ validates :input0, file_size: { in: (50..100) }
36
36
 
37
37
  end
38
38
  ```
data/docs/HEX.md CHANGED
@@ -12,7 +12,7 @@
12
12
  ```ruby
13
13
  class User < ActiveRecord::Base
14
14
 
15
- validates :input_0, hex: true
15
+ validates :input0, hex: true
16
16
 
17
17
  end
18
18
  ```
data/docs/IMEI.md CHANGED
@@ -14,7 +14,7 @@
14
14
  ```ruby
15
15
  class User < ActiveRecord::Base
16
16
 
17
- validates :input_0, imei: true
17
+ validates :input0, imei: true
18
18
 
19
19
  end
20
20
  ```
data/docs/IP_ADDRESS.md CHANGED
@@ -19,8 +19,8 @@ protocol | symbol | any, ipv4, ipv6 | any
19
19
  ```ruby
20
20
  class User < ActiveRecord::Base
21
21
 
22
- validates :input_0, ip_address: true
23
- validates :input_1, ip_address: { protocol: :ipv4 }
22
+ validates :input0, ip_address: true
23
+ validates :input1, ip_address: { protocol: :ipv4 }
24
24
 
25
25
  end
26
26
  ```
data/docs/ISBN.md CHANGED
@@ -12,7 +12,7 @@
12
12
  ```ruby
13
13
  class User < ActiveRecord::Base
14
14
 
15
- validates :input_0, isbn: true
15
+ validates :input0, isbn: true
16
16
 
17
17
  end
18
18
  ```
data/docs/ISIN.md CHANGED
@@ -14,7 +14,7 @@
14
14
  ```ruby
15
15
  class User < ActiveRecord::Base
16
16
 
17
- validates :input_0, isin: true
17
+ validates :input0, isin: true
18
18
 
19
19
  end
20
20
  ```
data/docs/MAC_ADDRESS.md CHANGED
@@ -15,7 +15,7 @@
15
15
  ```ruby
16
16
  class User < ActiveRecord::Base
17
17
 
18
- validates :input_0, mac_address: true
18
+ validates :input0, mac_address: true
19
19
 
20
20
  end
21
21
  ```
data/docs/NAME.md CHANGED
@@ -12,7 +12,7 @@
12
12
  ```ruby
13
13
  class User < ActiveRecord::Base
14
14
 
15
- validates :input_0, name: true
15
+ validates :input0, name: true
16
16
 
17
17
  end
18
18
  ```
data/docs/PASSWORD.md CHANGED
@@ -19,8 +19,8 @@ strength | symbol | strong, weak | weak
19
19
  ```ruby
20
20
  class User < ActiveRecord::Base
21
21
 
22
- validates :input_0, password: true
23
- validates :input_1, password: { strength: :strong }
22
+ validates :input0, password: true
23
+ validates :input1, password: { strength: :strong }
24
24
 
25
25
  end
26
26
  ```
data/docs/PHONE_NUMBER.md CHANGED
@@ -12,7 +12,7 @@
12
12
  ```ruby
13
13
  class User < ActiveRecord::Base
14
14
 
15
- validates :input_0, phone_number: true
15
+ validates :input0, phone_number: true
16
16
 
17
17
  end
18
18
  ```
data/docs/SEDOL.md CHANGED
@@ -14,7 +14,7 @@
14
14
  ```ruby
15
15
  class User < ActiveRecord::Base
16
16
 
17
- validates :input_0, sedol: true
17
+ validates :input0, sedol: true
18
18
 
19
19
  end
20
20
  ```
data/docs/SLUG.md CHANGED
@@ -12,7 +12,7 @@
12
12
  ```ruby
13
13
  class User < ActiveRecord::Base
14
14
 
15
- validates :input_0, slug: true
15
+ validates :input0, slug: true
16
16
 
17
17
  end
18
18
  ```
data/docs/SSN.md CHANGED
@@ -12,7 +12,7 @@
12
12
  ```ruby
13
13
  class User < ActiveRecord::Base
14
14
 
15
- validates :input_0, ssn: true
15
+ validates :input0, ssn: true
16
16
 
17
17
  end
18
18
  ```
data/docs/TIME_ZONE.md CHANGED
@@ -11,7 +11,7 @@
11
11
  ```ruby
12
12
  class User < ActiveRecord::Base
13
13
 
14
- validates :input_0, time_zone: true
14
+ validates :input0, time_zone: true
15
15
 
16
16
  end
17
17
  ```
data/docs/TYPE.md CHANGED
@@ -18,11 +18,11 @@ is_not | class | |
18
18
  ```ruby
19
19
  class User < ActiveRecord::Base
20
20
 
21
- validates :input_0, type: { is: Array }
22
- validates :input_1, type: { is: Boolean } #=> Checks both TrueClass and FalseClass
23
- validates :input_2, type: { is: [Array, Hash] }
24
- validates :input_3, type: { is_not: CustomKlass } #=> Any Class based objects
25
- validates :input_4, type: { is_not: Integer }
21
+ validates :input0, type: { is: Array }
22
+ validates :input1, type: { is: Boolean } #=> Checks both TrueClass and FalseClass
23
+ validates :input2, type: { is: [Array, Hash] }
24
+ validates :input3, type: { is_not: CustomKlass } #=> Any Class based objects
25
+ validates :input4, type: { is_not: Integer }
26
26
 
27
27
  end
28
28
  ```
data/docs/URL.md CHANGED
@@ -19,10 +19,10 @@ scheme | array, string, symbol | |
19
19
  ```ruby
20
20
  class User < ActiveRecord::Base
21
21
 
22
- validates :input_0, url: true
23
- validates :input_1, url: { domain: [:com, 'org'] }
24
- validates :input_2, url: { scheme: :https }
25
- validates :input_3, url: { root_only: true }
22
+ validates :input0, url: true
23
+ validates :input1, url: { domain: [:com, 'org'] }
24
+ validates :input2, url: { scheme: :https }
25
+ validates :input3, url: { root_only: true }
26
26
 
27
27
  end
28
28
  ```
data/docs/USERNAME.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  ```ruby
6
6
  # 1. Pattern
7
- /^[a-z0-9_-]{1,255}$/
7
+ /^[A-Za-z0-9._-]{1,255}$/
8
8
  ```
9
9
 
10
10
  #### Usage
@@ -12,7 +12,7 @@
12
12
  ```ruby
13
13
  class User < ActiveRecord::Base
14
14
 
15
- validates :input_0, username: true
15
+ validates :input0, username: true
16
16
 
17
17
  end
18
18
  ```
data/docs/UUID.md CHANGED
@@ -21,8 +21,8 @@ version | symbol, integer | any, 3, 4, 5 | any
21
21
  ```ruby
22
22
  class User < ActiveRecord::Base
23
23
 
24
- validates :input_0, uuid: true
25
- validates :input_1, uuid: { version: 3 }
24
+ validates :input0, uuid: true
25
+ validates :input1, uuid: { version: 3 }
26
26
 
27
27
  end
28
28
  ```
@@ -2,7 +2,7 @@
2
2
 
3
3
  class AlphaValidator < BaseValidator
4
4
 
5
- CASES ||= {
5
+ CASES = {
6
6
  lower: 'a-z',
7
7
  upper: 'A-Z',
8
8
  any: 'A-Za-z'
@@ -2,8 +2,6 @@
2
2
 
3
3
  class Base64Validator < BaseValidator
4
4
 
5
- # rubocop:disable Metrics/LineLength
6
- REGEXP ||= /^(?:[A-Za-z0-9+$]{4})*(?:[A-Za-z0-9+$]{2}==|[A-Za-z0-9+$]{3}=|[A-Za-z0-9+$]{4})$/.freeze
7
- # rubocop:enable Metrics/LineLength
5
+ REGEXP = /^(?:[A-Za-z0-9+$]{4})*(?:[A-Za-z0-9+$]{2}==|[A-Za-z0-9+$]{3}=|[A-Za-z0-9+$]{4})$/.freeze
8
6
 
9
7
  end
@@ -19,17 +19,17 @@ class BaseValidator < ActiveModel::EachValidator
19
19
  @value = value
20
20
  end
21
21
 
22
- # rubocop:disable Metrics/LineLength
22
+ # rubocop:disable Layout/LineLength
23
23
  def assert_valid_option!(name, collection, option: nil)
24
24
  option ||= send(name)
25
25
 
26
- [*option].each do |option_value|
26
+ Array(option).each do |option_value|
27
27
  next if collection.include?(option_value)
28
28
 
29
29
  raise ArgumentError, "Unknown #{name}: #{option_value.inspect}. Valid options are: #{collection.map(&:inspect).join(', ')}"
30
30
  end
31
31
  end
32
- # rubocop:enable Metrics/LineLength
32
+ # rubocop:enable Layout/LineLength
33
33
 
34
34
  def error_message
35
35
  [options[:message] || :invalid]
@@ -2,14 +2,34 @@
2
2
 
3
3
  class BooleanValidator < BaseValidator
4
4
 
5
- VALUES ||= %w[
6
- 1 0 t f true false y n yes no on off
7
- ].freeze
5
+ CHECKS = {
6
+ all: nil,
7
+ false_only: %w[0 f false n no off],
8
+ true_only: %w[1 t true y yes on]
9
+ }.freeze
10
+
11
+ def validate_each(record, attribute, value)
12
+ assert_valid_check!
13
+ super
14
+ end
8
15
 
9
16
  private
10
17
 
18
+ def assert_valid_check!
19
+ assert_valid_option!(:check, CHECKS.keys)
20
+ end
21
+
22
+ def check
23
+ options[:check] || :all
24
+ end
25
+
26
+ def error_message
27
+ [options[:message] || I18n.t("errors.messages.boolean.#{check}")]
28
+ end
29
+
11
30
  def valid_attr?
12
- VALUES.include?(value.to_s.downcase)
31
+ checks = CHECKS[check] || [].concat(CHECKS[:false_only], CHECKS[:true_only])
32
+ checks.include?(value.to_s.downcase)
13
33
  end
14
34
 
15
35
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  class CompareValidator < BaseValidator
4
4
 
5
- CHECKS ||= {
5
+ CHECKS = {
6
6
  less_than: :<,
7
7
  less_than_or_equal_to: :<=,
8
8
  greater_than: :>,
@@ -2,7 +2,7 @@
2
2
 
3
3
  class CoordinateValidator < BaseValidator
4
4
 
5
- BOUNDARIES ||= {
5
+ BOUNDARIES = {
6
6
  latitude: 90.0,
7
7
  longitude: 180.0
8
8
  }.freeze
@@ -4,7 +4,7 @@ class CreditCardValidator < BaseValidator
4
4
 
5
5
  # NOTE: https://en.wikipedia.org/wiki/Payment_card_value#Issuer_identification_value_.28IIN.29
6
6
 
7
- PROVIDERS ||= {
7
+ PROVIDERS = {
8
8
  american_express: {
9
9
  sizes: [15],
10
10
  prefixes: [34, 37]
@@ -88,7 +88,7 @@ class CreditCardValidator < BaseValidator
88
88
 
89
89
  def checksum(value)
90
90
  values = digits(value).reverse.map.with_index { |n, i| i.even? ? n * 2 : n }
91
- total = values.reverse.inject(0) { |a, b| a + digits(b).inject(:+) }
91
+ total = values.reverse.inject(0) { |a, b| a + digits(b).sum }
92
92
  checksum = 10 - (total % 10)
93
93
  checksum == 10 ? 0 : checksum
94
94
  end
@@ -4,7 +4,7 @@ require 'csv'
4
4
 
5
5
  class CsvValidator < FileSizeValidator
6
6
 
7
- DIMENSIONS ||= %i[
7
+ DIMENSIONS = %i[
8
8
  columns rows
9
9
  ].freeze
10
10
 
@@ -19,7 +19,7 @@ class CsvValidator < FileSizeValidator
19
19
 
20
20
  private
21
21
 
22
- # rubocop:disable Metrics/LineLength, Style/GuardClause
22
+ # rubocop:disable Layout/LineLength, Style/GuardClause
23
23
  def assert_valid_dimensions!
24
24
  if dimensions.empty?
25
25
  raise ArgumentError, "Missing atleast one dimension of #{DIMENSIONS.map(&:inspect).join(', ')} attribute for comparison."
@@ -27,7 +27,7 @@ class CsvValidator < FileSizeValidator
27
27
  assert_valid_option!(:dimensions, DIMENSIONS)
28
28
  end
29
29
  end
30
- # rubocop:enable Metrics/LineLength, Style/GuardClause
30
+ # rubocop:enable Layout/LineLength, Style/GuardClause
31
31
 
32
32
  def csv_dimensions(path)
33
33
  dimension = { columns: 0, rows: 0 }
@@ -52,7 +52,7 @@ class CsvValidator < FileSizeValidator
52
52
  def error_message_for(dimension, check, check_value)
53
53
  options[:message] || I18n.t(
54
54
  "errors.messages.csv.#{check}",
55
- error_options(check_value).merge(dimension: dimension)
55
+ **error_options(check_value).merge(dimension: dimension)
56
56
  )
57
57
  end
58
58
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  class CurrencyValidator < BaseValidator
4
4
 
5
- REGEXP ||= /^\d*+(\.\d{1,2})$/.freeze
5
+ REGEXP = /^\d*+(\.\d{1,2})$/.freeze
6
6
 
7
7
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  class CusipValidator < BaseValidator
4
4
 
5
- REGEXP ||= /^[0-9A-Z]{9}$/.freeze
5
+ REGEXP = /^[0-9A-Z]{9}$/.freeze
6
6
 
7
7
  private
8
8
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  class EinValidator < BaseValidator
4
4
 
5
- REGEXP ||= /^[1-9]\d?-\d{7}$/.freeze
5
+ REGEXP = /^[1-9]\d?-\d{7}$/.freeze
6
6
 
7
7
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  class EmailValidator < BaseValidator
4
4
 
5
- REGEXP ||= /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i.freeze
5
+ REGEXP = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i.freeze
6
6
 
7
7
  private
8
8
 
@@ -13,7 +13,7 @@ class EmailValidator < BaseValidator
13
13
  def valid_domain?
14
14
  return true unless options.key?(:domain)
15
15
 
16
- [options[:domain]].flatten.any? { |domain| value.downcase.end_with?(".#{domain.downcase}") }
16
+ Array(options[:domain]).any? { |domain| value.downcase.end_with?(".#{domain.downcase}") }
17
17
  end
18
18
 
19
19
  end