hash_validator 2.0.0 → 2.0.1

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 (74) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +6 -1
  3. data/.rubocop.yml +340 -0
  4. data/Gemfile +3 -1
  5. data/README.md +2 -1
  6. data/Rakefile +6 -4
  7. data/hash_validator.gemspec +11 -5
  8. data/lib/hash_validator/base.rb +2 -0
  9. data/lib/hash_validator/configuration.rb +5 -3
  10. data/lib/hash_validator/validations/many.rb +2 -0
  11. data/lib/hash_validator/validations/multiple.rb +2 -0
  12. data/lib/hash_validator/validations/optional.rb +2 -0
  13. data/lib/hash_validator/validations.rb +5 -3
  14. data/lib/hash_validator/validators/alpha_validator.rb +5 -3
  15. data/lib/hash_validator/validators/alphanumeric_validator.rb +5 -3
  16. data/lib/hash_validator/validators/array_validator.rb +44 -48
  17. data/lib/hash_validator/validators/base.rb +6 -4
  18. data/lib/hash_validator/validators/boolean_validator.rb +3 -1
  19. data/lib/hash_validator/validators/class_validator.rb +3 -1
  20. data/lib/hash_validator/validators/digits_validator.rb +5 -3
  21. data/lib/hash_validator/validators/dynamic_func_validator.rb +8 -8
  22. data/lib/hash_validator/validators/dynamic_pattern_validator.rb +5 -3
  23. data/lib/hash_validator/validators/email_validator.rb +4 -2
  24. data/lib/hash_validator/validators/enumerable_validator.rb +4 -2
  25. data/lib/hash_validator/validators/hash_validator.rb +5 -3
  26. data/lib/hash_validator/validators/hex_color_validator.rb +5 -3
  27. data/lib/hash_validator/validators/ip_validator.rb +6 -4
  28. data/lib/hash_validator/validators/ipv4_validator.rb +5 -3
  29. data/lib/hash_validator/validators/ipv6_validator.rb +6 -4
  30. data/lib/hash_validator/validators/json_validator.rb +6 -4
  31. data/lib/hash_validator/validators/lambda_validator.rb +4 -2
  32. data/lib/hash_validator/validators/many_validator.rb +3 -1
  33. data/lib/hash_validator/validators/multiple_validator.rb +4 -2
  34. data/lib/hash_validator/validators/optional_validator.rb +3 -1
  35. data/lib/hash_validator/validators/presence_validator.rb +4 -2
  36. data/lib/hash_validator/validators/regex_validator.rb +4 -2
  37. data/lib/hash_validator/validators/simple_type_validators.rb +3 -1
  38. data/lib/hash_validator/validators/simple_validator.rb +2 -0
  39. data/lib/hash_validator/validators/url_validator.rb +6 -4
  40. data/lib/hash_validator/validators.rb +35 -33
  41. data/lib/hash_validator/version.rb +3 -1
  42. data/lib/hash_validator.rb +7 -5
  43. data/spec/configuration_spec.rb +76 -74
  44. data/spec/hash_validator_spec.rb +132 -113
  45. data/spec/hash_validator_spec_helper.rb +2 -0
  46. data/spec/spec_helper.rb +14 -4
  47. data/spec/validators/alpha_validator_spec.rb +43 -41
  48. data/spec/validators/alphanumeric_validator_spec.rb +44 -42
  49. data/spec/validators/array_spec.rb +102 -47
  50. data/spec/validators/base_spec.rb +25 -10
  51. data/spec/validators/boolean_spec.rb +15 -13
  52. data/spec/validators/class_spec.rb +20 -18
  53. data/spec/validators/digits_validator_spec.rb +46 -44
  54. data/spec/validators/dynamic_func_validator_spec.rb +90 -88
  55. data/spec/validators/dynamic_pattern_validator_spec.rb +65 -63
  56. data/spec/validators/email_spec.rb +15 -13
  57. data/spec/validators/hash_validator_spec.rb +39 -37
  58. data/spec/validators/hex_color_validator_spec.rb +49 -47
  59. data/spec/validators/in_enumerable_spec.rb +32 -30
  60. data/spec/validators/ip_validator_spec.rb +46 -44
  61. data/spec/validators/ipv4_validator_spec.rb +45 -43
  62. data/spec/validators/ipv6_validator_spec.rb +44 -42
  63. data/spec/validators/json_validator_spec.rb +38 -36
  64. data/spec/validators/lambda_spec.rb +20 -18
  65. data/spec/validators/many_spec.rb +25 -23
  66. data/spec/validators/multiple_spec.rb +13 -11
  67. data/spec/validators/optional_spec.rb +22 -20
  68. data/spec/validators/presence_spec.rb +16 -14
  69. data/spec/validators/regexp_spec.rb +14 -12
  70. data/spec/validators/simple_spec.rb +17 -15
  71. data/spec/validators/simple_types_spec.rb +21 -19
  72. data/spec/validators/url_validator_spec.rb +34 -32
  73. data/spec/validators/user_defined_spec.rb +29 -27
  74. metadata +59 -2
@@ -1,25 +1,27 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
- describe 'HashValidator.configure' do
4
- describe 'configuration DSL' do
3
+ require "spec_helper"
4
+
5
+ describe "HashValidator.configure" do
6
+ describe "configuration DSL" do
5
7
  after(:each) do
6
8
  # Clean up any validators added during tests
7
- HashValidator.remove_validator('test_pattern')
8
- HashValidator.remove_validator('test_func')
9
- HashValidator.remove_validator('bulk_odd')
10
- HashValidator.remove_validator('bulk_even')
11
- HashValidator.remove_validator('bulk_palindrome')
12
- HashValidator.remove_validator('custom_configured')
9
+ HashValidator.remove_validator("test_pattern")
10
+ HashValidator.remove_validator("test_func")
11
+ HashValidator.remove_validator("bulk_odd")
12
+ HashValidator.remove_validator("bulk_even")
13
+ HashValidator.remove_validator("bulk_palindrome")
14
+ HashValidator.remove_validator("custom_configured")
13
15
  end
14
16
 
15
- it 'allows adding instance-based validators' do
17
+ it "allows adding instance-based validators" do
16
18
  class HashValidator::Validator::CustomConfiguredValidator < HashValidator::Validator::Base
17
19
  def initialize
18
- super('custom_configured')
20
+ super("custom_configured")
19
21
  end
20
-
22
+
21
23
  def valid?(value)
22
- value == 'configured'
24
+ value == "configured"
23
25
  end
24
26
  end
25
27
 
@@ -28,162 +30,162 @@ describe 'HashValidator.configure' do
28
30
  end
29
31
 
30
32
  validator = HashValidator.validate(
31
- { status: 'configured' },
32
- { status: 'custom_configured' }
33
+ { status: "configured" },
34
+ { status: "custom_configured" }
33
35
  )
34
36
  expect(validator.valid?).to eq true
35
37
  end
36
38
 
37
- it 'allows adding pattern-based validators' do
39
+ it "allows adding pattern-based validators" do
38
40
  HashValidator.configure do |config|
39
- config.add_validator 'test_pattern',
40
- pattern: /\A[A-Z]{3}\z/,
41
- error_message: 'must be three uppercase letters'
41
+ config.add_validator "test_pattern",
42
+ pattern: /\A[A-Z]{3}\z/,
43
+ error_message: "must be three uppercase letters"
42
44
  end
43
45
 
44
46
  validator = HashValidator.validate(
45
- { code: 'ABC' },
46
- { code: 'test_pattern' }
47
+ { code: "ABC" },
48
+ { code: "test_pattern" }
47
49
  )
48
50
  expect(validator.valid?).to eq true
49
51
 
50
52
  validator = HashValidator.validate(
51
- { code: 'abc' },
52
- { code: 'test_pattern' }
53
+ { code: "abc" },
54
+ { code: "test_pattern" }
53
55
  )
54
56
  expect(validator.valid?).to eq false
55
- expect(validator.errors).to eq({ code: 'must be three uppercase letters' })
57
+ expect(validator.errors).to eq({ code: "must be three uppercase letters" })
56
58
  end
57
59
 
58
- it 'allows adding function-based validators' do
60
+ it "allows adding function-based validators" do
59
61
  HashValidator.configure do |config|
60
- config.add_validator 'test_func',
62
+ config.add_validator "test_func",
61
63
  func: ->(v) { v.is_a?(Integer) && v > 100 },
62
- error_message: 'must be an integer greater than 100'
64
+ error_message: "must be an integer greater than 100"
63
65
  end
64
66
 
65
67
  validator = HashValidator.validate(
66
68
  { score: 150 },
67
- { score: 'test_func' }
69
+ { score: "test_func" }
68
70
  )
69
71
  expect(validator.valid?).to eq true
70
72
 
71
73
  validator = HashValidator.validate(
72
74
  { score: 50 },
73
- { score: 'test_func' }
75
+ { score: "test_func" }
74
76
  )
75
77
  expect(validator.valid?).to eq false
76
- expect(validator.errors).to eq({ score: 'must be an integer greater than 100' })
78
+ expect(validator.errors).to eq({ score: "must be an integer greater than 100" })
77
79
  end
78
80
 
79
- it 'allows adding multiple validators in one configure block' do
81
+ it "allows adding multiple validators in one configure block" do
80
82
  HashValidator.configure do |config|
81
- config.add_validator 'bulk_odd',
83
+ config.add_validator "bulk_odd",
82
84
  pattern: /\A\d*[13579]\z/,
83
- error_message: 'must be odd'
84
-
85
- config.add_validator 'bulk_even',
85
+ error_message: "must be odd"
86
+
87
+ config.add_validator "bulk_even",
86
88
  pattern: /\A\d*[02468]\z/,
87
- error_message: 'must be even'
88
-
89
- config.add_validator 'bulk_palindrome',
89
+ error_message: "must be even"
90
+
91
+ config.add_validator "bulk_palindrome",
90
92
  func: proc { |s| s.to_s == s.to_s.reverse },
91
- error_message: 'must be a palindrome'
93
+ error_message: "must be a palindrome"
92
94
  end
93
95
 
94
96
  # Test odd validator
95
97
  validator = HashValidator.validate(
96
- { num: '13' },
97
- { num: 'bulk_odd' }
98
+ { num: "13" },
99
+ { num: "bulk_odd" }
98
100
  )
99
101
  expect(validator.valid?).to eq true
100
102
 
101
103
  # Test even validator
102
104
  validator = HashValidator.validate(
103
- { num: '24' },
104
- { num: 'bulk_even' }
105
+ { num: "24" },
106
+ { num: "bulk_even" }
105
107
  )
106
108
  expect(validator.valid?).to eq true
107
109
 
108
110
  # Test palindrome validator
109
111
  validator = HashValidator.validate(
110
- { word: 'level' },
111
- { word: 'bulk_palindrome' }
112
+ { word: "level" },
113
+ { word: "bulk_palindrome" }
112
114
  )
113
115
  expect(validator.valid?).to eq true
114
116
  end
115
117
 
116
- it 'allows removing validators within configure block' do
118
+ it "allows removing validators within configure block" do
117
119
  # First add a validator
118
- HashValidator.add_validator 'test_pattern',
120
+ HashValidator.add_validator "test_pattern",
119
121
  pattern: /test/,
120
- error_message: 'test'
122
+ error_message: "test"
121
123
 
122
124
  # Then remove it in configure
123
125
  HashValidator.configure do |config|
124
- config.remove_validator 'test_pattern'
126
+ config.remove_validator "test_pattern"
125
127
  end
126
128
 
127
129
  # Should raise error as validator no longer exists
128
130
  expect {
129
- HashValidator.validate({ value: 'test' }, { value: 'test_pattern' })
131
+ HashValidator.validate({ value: "test" }, { value: "test_pattern" })
130
132
  }.to raise_error(StandardError, /Could not find valid validator/)
131
133
  end
132
134
 
133
- it 'works without a block' do
135
+ it "works without a block" do
134
136
  expect {
135
137
  HashValidator.configure
136
138
  }.not_to raise_error
137
139
  end
138
140
 
139
- it 'yields a Configuration instance' do
141
+ it "yields a Configuration instance" do
140
142
  HashValidator.configure do |config|
141
143
  expect(config).to be_a(HashValidator::Configuration)
142
144
  end
143
145
  end
144
146
  end
145
147
 
146
- describe 'Rails-style initializer example' do
148
+ describe "Rails-style initializer example" do
147
149
  after(:each) do
148
- HashValidator.remove_validator('phone')
149
- HashValidator.remove_validator('postal_code')
150
- HashValidator.remove_validator('age_range')
150
+ HashValidator.remove_validator("phone")
151
+ HashValidator.remove_validator("postal_code")
152
+ HashValidator.remove_validator("age_range")
151
153
  end
152
154
 
153
- it 'can be used like a Rails initializer' do
155
+ it "can be used like a Rails initializer" do
154
156
  # This would typically be in config/initializers/hash_validator.rb
155
157
  HashValidator.configure do |config|
156
158
  # Add pattern validators
157
- config.add_validator 'phone',
159
+ config.add_validator "phone",
158
160
  pattern: /\A\+?[1-9]\d{1,14}\z/,
159
- error_message: 'must be a valid international phone number'
160
-
161
- config.add_validator 'postal_code',
161
+ error_message: "must be a valid international phone number"
162
+
163
+ config.add_validator "postal_code",
162
164
  pattern: /\A[A-Z0-9]{3,10}\z/i,
163
- error_message: 'must be a valid postal code'
164
-
165
+ error_message: "must be a valid postal code"
166
+
165
167
  # Add function validator
166
- config.add_validator 'age_range',
168
+ config.add_validator "age_range",
167
169
  func: ->(age) { age.is_a?(Integer) && age.between?(0, 120) },
168
- error_message: 'must be between 0 and 120'
170
+ error_message: "must be between 0 and 120"
169
171
  end
170
172
 
171
173
  # Use the configured validators
172
174
  validator = HashValidator.validate(
173
- {
174
- phone: '+14155551234',
175
- postal_code: 'SW1A1AA',
175
+ {
176
+ phone: "+14155551234",
177
+ postal_code: "SW1A1AA",
176
178
  age: 30
177
179
  },
178
- {
179
- phone: 'phone',
180
- postal_code: 'postal_code',
181
- age: 'age_range'
180
+ {
181
+ phone: "phone",
182
+ postal_code: "postal_code",
183
+ age: "age_range"
182
184
  }
183
185
  )
184
-
186
+
185
187
  expect(validator.valid?).to eq true
186
188
  expect(validator.errors).to be_empty
187
189
  end
188
190
  end
189
- end
191
+ end