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,99 +1,101 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
2
4
 
3
5
  describe HashValidator::Validator::DigitsValidator do
4
6
  let(:validator) { HashValidator::Validator::DigitsValidator.new }
5
7
 
6
- context 'valid digit strings' do
7
- it 'validates single digit' do
8
+ context "valid digit strings" do
9
+ it "validates single digit" do
8
10
  errors = {}
9
- validator.validate('key', '1', {}, errors)
11
+ validator.validate("key", "1", {}, errors)
10
12
  expect(errors).to be_empty
11
13
  end
12
14
 
13
- it 'validates multiple digits' do
15
+ it "validates multiple digits" do
14
16
  errors = {}
15
- validator.validate('key', '123', {}, errors)
17
+ validator.validate("key", "123", {}, errors)
16
18
  expect(errors).to be_empty
17
19
  end
18
20
 
19
- it 'validates zero' do
21
+ it "validates zero" do
20
22
  errors = {}
21
- validator.validate('key', '0', {}, errors)
23
+ validator.validate("key", "0", {}, errors)
22
24
  expect(errors).to be_empty
23
25
  end
24
26
 
25
- it 'validates long number strings' do
27
+ it "validates long number strings" do
26
28
  errors = {}
27
- validator.validate('key', '1234567890', {}, errors)
29
+ validator.validate("key", "1234567890", {}, errors)
28
30
  expect(errors).to be_empty
29
31
  end
30
32
 
31
- it 'validates leading zeros' do
33
+ it "validates leading zeros" do
32
34
  errors = {}
33
- validator.validate('key', '0123', {}, errors)
35
+ validator.validate("key", "0123", {}, errors)
34
36
  expect(errors).to be_empty
35
37
  end
36
38
  end
37
39
 
38
- context 'invalid digit strings' do
39
- it 'rejects non-string values' do
40
+ context "invalid digit strings" do
41
+ it "rejects non-string values" do
40
42
  errors = {}
41
- validator.validate('key', 123, {}, errors)
42
- expect(errors['key']).to eq('must contain only digits')
43
+ validator.validate("key", 123, {}, errors)
44
+ expect(errors["key"]).to eq("must contain only digits")
43
45
  end
44
46
 
45
- it 'rejects nil values' do
47
+ it "rejects nil values" do
46
48
  errors = {}
47
- validator.validate('key', nil, {}, errors)
48
- expect(errors['key']).to eq('must contain only digits')
49
+ validator.validate("key", nil, {}, errors)
50
+ expect(errors["key"]).to eq("must contain only digits")
49
51
  end
50
52
 
51
- it 'rejects strings with letters' do
53
+ it "rejects strings with letters" do
52
54
  errors = {}
53
- validator.validate('key', '123abc', {}, errors)
54
- expect(errors['key']).to eq('must contain only digits')
55
+ validator.validate("key", "123abc", {}, errors)
56
+ expect(errors["key"]).to eq("must contain only digits")
55
57
  end
56
58
 
57
- it 'rejects strings with spaces' do
59
+ it "rejects strings with spaces" do
58
60
  errors = {}
59
- validator.validate('key', '123 456', {}, errors)
60
- expect(errors['key']).to eq('must contain only digits')
61
+ validator.validate("key", "123 456", {}, errors)
62
+ expect(errors["key"]).to eq("must contain only digits")
61
63
  end
62
64
 
63
- it 'rejects strings with special characters' do
65
+ it "rejects strings with special characters" do
64
66
  errors = {}
65
- validator.validate('key', '123!', {}, errors)
66
- expect(errors['key']).to eq('must contain only digits')
67
+ validator.validate("key", "123!", {}, errors)
68
+ expect(errors["key"]).to eq("must contain only digits")
67
69
  end
68
70
 
69
- it 'rejects strings with hyphens' do
71
+ it "rejects strings with hyphens" do
70
72
  errors = {}
71
- validator.validate('key', '123-456', {}, errors)
72
- expect(errors['key']).to eq('must contain only digits')
73
+ validator.validate("key", "123-456", {}, errors)
74
+ expect(errors["key"]).to eq("must contain only digits")
73
75
  end
74
76
 
75
- it 'rejects strings with periods' do
77
+ it "rejects strings with periods" do
76
78
  errors = {}
77
- validator.validate('key', '123.456', {}, errors)
78
- expect(errors['key']).to eq('must contain only digits')
79
+ validator.validate("key", "123.456", {}, errors)
80
+ expect(errors["key"]).to eq("must contain only digits")
79
81
  end
80
82
 
81
- it 'rejects empty strings' do
83
+ it "rejects empty strings" do
82
84
  errors = {}
83
- validator.validate('key', '', {}, errors)
84
- expect(errors['key']).to eq('must contain only digits')
85
+ validator.validate("key", "", {}, errors)
86
+ expect(errors["key"]).to eq("must contain only digits")
85
87
  end
86
88
 
87
- it 'rejects negative signs' do
89
+ it "rejects negative signs" do
88
90
  errors = {}
89
- validator.validate('key', '-123', {}, errors)
90
- expect(errors['key']).to eq('must contain only digits')
91
+ validator.validate("key", "-123", {}, errors)
92
+ expect(errors["key"]).to eq("must contain only digits")
91
93
  end
92
94
 
93
- it 'rejects positive signs' do
95
+ it "rejects positive signs" do
94
96
  errors = {}
95
- validator.validate('key', '+123', {}, errors)
96
- expect(errors['key']).to eq('must contain only digits')
97
+ validator.validate("key", "+123", {}, errors)
98
+ expect(errors["key"]).to eq("must contain only digits")
97
99
  end
98
100
  end
99
- end
101
+ end
@@ -1,88 +1,90 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
2
4
 
3
5
  describe HashValidator::Validator::DynamicFuncValidator do
4
- describe 'Function-based validator' do
6
+ describe "Function-based validator" do
5
7
  let(:odd_func_validator) do
6
8
  HashValidator::Validator::DynamicFuncValidator.new(
7
- 'odd_func',
9
+ "odd_func",
8
10
  ->(n) { n.is_a?(Integer) && n.odd? },
9
- 'is not an odd integer'
11
+ "is not an odd integer"
10
12
  )
11
13
  end
12
14
 
13
15
  let(:range_validator) do
14
16
  HashValidator::Validator::DynamicFuncValidator.new(
15
- 'in_range',
17
+ "in_range",
16
18
  ->(n) { n.is_a?(Numeric) && n >= 18 && n <= 65 },
17
- 'must be between 18 and 65'
19
+ "must be between 18 and 65"
18
20
  )
19
21
  end
20
22
 
21
23
  let(:custom_validator) do
22
24
  HashValidator::Validator::DynamicFuncValidator.new(
23
- 'custom',
25
+ "custom",
24
26
  proc { |v| v.to_s.length > 5 },
25
- 'must be longer than 5 characters'
27
+ "must be longer than 5 characters"
26
28
  )
27
29
  end
28
30
 
29
31
  let(:errors) { Hash.new }
30
32
 
31
- describe '#initialize' do
32
- it 'requires a callable object' do
33
+ describe "#initialize" do
34
+ it "requires a callable object" do
33
35
  expect {
34
- HashValidator::Validator::DynamicFuncValidator.new('test', 'not_callable')
35
- }.to raise_error(ArgumentError, 'Function must be callable (proc or lambda)')
36
+ HashValidator::Validator::DynamicFuncValidator.new("test", "not_callable")
37
+ }.to raise_error(ArgumentError, "Function must be callable (proc or lambda)")
36
38
  end
37
39
 
38
- it 'accepts a lambda' do
40
+ it "accepts a lambda" do
39
41
  validator = HashValidator::Validator::DynamicFuncValidator.new(
40
- 'test',
42
+ "test",
41
43
  ->(v) { true }
42
44
  )
43
45
  expect(validator.func).to respond_to(:call)
44
46
  end
45
47
 
46
- it 'accepts a proc' do
48
+ it "accepts a proc" do
47
49
  validator = HashValidator::Validator::DynamicFuncValidator.new(
48
- 'test',
50
+ "test",
49
51
  proc { |v| true }
50
52
  )
51
53
  expect(validator.func).to respond_to(:call)
52
54
  end
53
55
 
54
- it 'accepts a custom error message' do
56
+ it "accepts a custom error message" do
55
57
  validator = HashValidator::Validator::DynamicFuncValidator.new(
56
- 'test',
58
+ "test",
57
59
  ->(v) { true },
58
- 'custom error'
60
+ "custom error"
59
61
  )
60
- expect(validator.error_message).to eq('custom error')
62
+ expect(validator.error_message).to eq("custom error")
61
63
  end
62
64
 
63
- it 'uses default error message when none provided' do
65
+ it "uses default error message when none provided" do
64
66
  validator = HashValidator::Validator::DynamicFuncValidator.new(
65
- 'test',
67
+ "test",
66
68
  ->(v) { true }
67
69
  )
68
- expect(validator.error_message).to eq('test required')
70
+ expect(validator.error_message).to eq("test required")
69
71
  end
70
72
  end
71
73
 
72
- describe '#should_validate?' do
73
- it 'validates the correct name' do
74
- expect(odd_func_validator.should_validate?('odd_func')).to eq true
74
+ describe "#should_validate?" do
75
+ it "validates the correct name" do
76
+ expect(odd_func_validator.should_validate?("odd_func")).to eq true
75
77
  end
76
78
 
77
- it 'does not validate other names' do
78
- expect(odd_func_validator.should_validate?('even_func')).to eq false
79
- expect(odd_func_validator.should_validate?('string')).to eq false
79
+ it "does not validate other names" do
80
+ expect(odd_func_validator.should_validate?("even_func")).to eq false
81
+ expect(odd_func_validator.should_validate?("string")).to eq false
80
82
  end
81
83
  end
82
84
 
83
- describe '#validate' do
84
- context 'with odd number function' do
85
- it 'validates odd integers correctly' do
85
+ describe "#validate" do
86
+ context "with odd number function" do
87
+ it "validates odd integers correctly" do
86
88
  odd_func_validator.validate(:key, 1, {}, errors)
87
89
  expect(errors).to be_empty
88
90
 
@@ -95,27 +97,27 @@ describe HashValidator::Validator::DynamicFuncValidator do
95
97
  expect(errors).to be_empty
96
98
  end
97
99
 
98
- it 'rejects even integers' do
100
+ it "rejects even integers" do
99
101
  odd_func_validator.validate(:key, 2, {}, errors)
100
- expect(errors).to eq({ key: 'is not an odd integer' })
102
+ expect(errors).to eq({ key: "is not an odd integer" })
101
103
 
102
104
  errors.clear
103
105
  odd_func_validator.validate(:key, 100, {}, errors)
104
- expect(errors).to eq({ key: 'is not an odd integer' })
106
+ expect(errors).to eq({ key: "is not an odd integer" })
105
107
  end
106
108
 
107
- it 'rejects non-integers' do
109
+ it "rejects non-integers" do
108
110
  odd_func_validator.validate(:key, 1.5, {}, errors)
109
- expect(errors).to eq({ key: 'is not an odd integer' })
111
+ expect(errors).to eq({ key: "is not an odd integer" })
110
112
 
111
113
  errors.clear
112
- odd_func_validator.validate(:key, '1', {}, errors)
113
- expect(errors).to eq({ key: 'is not an odd integer' })
114
+ odd_func_validator.validate(:key, "1", {}, errors)
115
+ expect(errors).to eq({ key: "is not an odd integer" })
114
116
  end
115
117
  end
116
118
 
117
- context 'with range validator' do
118
- it 'validates numbers in range' do
119
+ context "with range validator" do
120
+ it "validates numbers in range" do
119
121
  range_validator.validate(:key, 18, {}, errors)
120
122
  expect(errors).to be_empty
121
123
 
@@ -132,121 +134,121 @@ describe HashValidator::Validator::DynamicFuncValidator do
132
134
  expect(errors).to be_empty
133
135
  end
134
136
 
135
- it 'rejects numbers outside range' do
137
+ it "rejects numbers outside range" do
136
138
  range_validator.validate(:key, 17, {}, errors)
137
- expect(errors).to eq({ key: 'must be between 18 and 65' })
139
+ expect(errors).to eq({ key: "must be between 18 and 65" })
138
140
 
139
141
  errors.clear
140
142
  range_validator.validate(:key, 66, {}, errors)
141
- expect(errors).to eq({ key: 'must be between 18 and 65' })
143
+ expect(errors).to eq({ key: "must be between 18 and 65" })
142
144
 
143
145
  errors.clear
144
146
  range_validator.validate(:key, -5, {}, errors)
145
- expect(errors).to eq({ key: 'must be between 18 and 65' })
147
+ expect(errors).to eq({ key: "must be between 18 and 65" })
146
148
  end
147
149
 
148
- it 'rejects non-numeric values' do
149
- range_validator.validate(:key, 'twenty', {}, errors)
150
- expect(errors).to eq({ key: 'must be between 18 and 65' })
150
+ it "rejects non-numeric values" do
151
+ range_validator.validate(:key, "twenty", {}, errors)
152
+ expect(errors).to eq({ key: "must be between 18 and 65" })
151
153
  end
152
154
  end
153
155
 
154
- context 'with custom validator using proc' do
155
- it 'validates strings longer than 5 chars' do
156
- custom_validator.validate(:key, 'hello world', {}, errors)
156
+ context "with custom validator using proc" do
157
+ it "validates strings longer than 5 chars" do
158
+ custom_validator.validate(:key, "hello world", {}, errors)
157
159
  expect(errors).to be_empty
158
160
 
159
161
  errors.clear
160
- custom_validator.validate(:key, 'testing', {}, errors)
162
+ custom_validator.validate(:key, "testing", {}, errors)
161
163
  expect(errors).to be_empty
162
164
  end
163
165
 
164
- it 'rejects strings 5 chars or shorter' do
165
- custom_validator.validate(:key, 'hello', {}, errors)
166
- expect(errors).to eq({ key: 'must be longer than 5 characters' })
166
+ it "rejects strings 5 chars or shorter" do
167
+ custom_validator.validate(:key, "hello", {}, errors)
168
+ expect(errors).to eq({ key: "must be longer than 5 characters" })
167
169
 
168
170
  errors.clear
169
- custom_validator.validate(:key, 'hi', {}, errors)
170
- expect(errors).to eq({ key: 'must be longer than 5 characters' })
171
+ custom_validator.validate(:key, "hi", {}, errors)
172
+ expect(errors).to eq({ key: "must be longer than 5 characters" })
171
173
  end
172
174
 
173
- it 'converts values to strings' do
175
+ it "converts values to strings" do
174
176
  custom_validator.validate(:key, 123456, {}, errors)
175
177
  expect(errors).to be_empty
176
178
 
177
179
  errors.clear
178
180
  custom_validator.validate(:key, 12345, {}, errors)
179
- expect(errors).to eq({ key: 'must be longer than 5 characters' })
181
+ expect(errors).to eq({ key: "must be longer than 5 characters" })
180
182
  end
181
183
  end
182
184
 
183
- context 'with function that raises errors' do
185
+ context "with function that raises errors" do
184
186
  let(:error_func_validator) do
185
187
  HashValidator::Validator::DynamicFuncValidator.new(
186
- 'error_func',
187
- ->(v) { raise 'intentional error' },
188
- 'validation failed'
188
+ "error_func",
189
+ ->(v) { raise "intentional error" },
190
+ "validation failed"
189
191
  )
190
192
  end
191
193
 
192
- it 'treats exceptions as validation failures' do
193
- error_func_validator.validate(:key, 'any value', {}, errors)
194
- expect(errors).to eq({ key: 'validation failed' })
194
+ it "treats exceptions as validation failures" do
195
+ error_func_validator.validate(:key, "any value", {}, errors)
196
+ expect(errors).to eq({ key: "validation failed" })
195
197
  end
196
198
  end
197
199
  end
198
200
 
199
- describe 'Integration with HashValidator.add_validator' do
201
+ describe "Integration with HashValidator.add_validator" do
200
202
  before do
201
- HashValidator.add_validator('adult_age',
202
- func: ->(age) { age.is_a?(Integer) && age >= 18 },
203
- error_message: 'must be 18 or older')
204
-
205
- HashValidator.add_validator('palindrome',
203
+ HashValidator.add_validator("adult_age",
204
+ func: ->(age) { age.is_a?(Integer) && age >= 18 },
205
+ error_message: "must be 18 or older")
206
+
207
+ HashValidator.add_validator("palindrome",
206
208
  func: proc { |s| s.to_s == s.to_s.reverse },
207
- error_message: 'must be a palindrome')
209
+ error_message: "must be a palindrome")
208
210
  end
209
211
 
210
212
  after do
211
- HashValidator.remove_validator('adult_age')
212
- HashValidator.remove_validator('palindrome')
213
+ HashValidator.remove_validator("adult_age")
214
+ HashValidator.remove_validator("palindrome")
213
215
  end
214
216
 
215
- it 'can register lambda-based validators' do
217
+ it "can register lambda-based validators" do
216
218
  validator = HashValidator.validate(
217
219
  { age: 25 },
218
- { age: 'adult_age' }
220
+ { age: "adult_age" }
219
221
  )
220
222
  expect(validator.valid?).to eq true
221
223
  expect(validator.errors).to be_empty
222
224
  end
223
225
 
224
- it 'returns errors for invalid lambda validations' do
226
+ it "returns errors for invalid lambda validations" do
225
227
  validator = HashValidator.validate(
226
228
  { age: 16 },
227
- { age: 'adult_age' }
229
+ { age: "adult_age" }
228
230
  )
229
231
  expect(validator.valid?).to eq false
230
- expect(validator.errors).to eq({ age: 'must be 18 or older' })
232
+ expect(validator.errors).to eq({ age: "must be 18 or older" })
231
233
  end
232
234
 
233
- it 'can register proc-based validators' do
235
+ it "can register proc-based validators" do
234
236
  validator = HashValidator.validate(
235
- { word: 'racecar' },
236
- { word: 'palindrome' }
237
+ { word: "racecar" },
238
+ { word: "palindrome" }
237
239
  )
238
240
  expect(validator.valid?).to eq true
239
241
  expect(validator.errors).to be_empty
240
242
  end
241
243
 
242
- it 'returns errors for invalid proc validations' do
244
+ it "returns errors for invalid proc validations" do
243
245
  validator = HashValidator.validate(
244
- { word: 'hello' },
245
- { word: 'palindrome' }
246
+ { word: "hello" },
247
+ { word: "palindrome" }
246
248
  )
247
249
  expect(validator.valid?).to eq false
248
- expect(validator.errors).to eq({ word: 'must be a palindrome' })
250
+ expect(validator.errors).to eq({ word: "must be a palindrome" })
249
251
  end
250
252
  end
251
253
  end
252
- end
254
+ end