hash_validator 1.2.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 (75) 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 +175 -13
  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 +18 -0
  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 +8 -14
  15. data/lib/hash_validator/validators/alphanumeric_validator.rb +8 -14
  16. data/lib/hash_validator/validators/array_validator.rb +45 -49
  17. data/lib/hash_validator/validators/base.rb +31 -4
  18. data/lib/hash_validator/validators/boolean_validator.rb +6 -6
  19. data/lib/hash_validator/validators/class_validator.rb +4 -2
  20. data/lib/hash_validator/validators/digits_validator.rb +8 -14
  21. data/lib/hash_validator/validators/dynamic_func_validator.rb +26 -0
  22. data/lib/hash_validator/validators/dynamic_pattern_validator.rb +25 -0
  23. data/lib/hash_validator/validators/email_validator.rb +8 -8
  24. data/lib/hash_validator/validators/enumerable_validator.rb +8 -8
  25. data/lib/hash_validator/validators/hash_validator.rb +7 -5
  26. data/lib/hash_validator/validators/hex_color_validator.rb +8 -14
  27. data/lib/hash_validator/validators/ip_validator.rb +24 -0
  28. data/lib/hash_validator/validators/ipv4_validator.rb +20 -0
  29. data/lib/hash_validator/validators/ipv6_validator.rb +24 -0
  30. data/lib/hash_validator/validators/json_validator.rb +9 -14
  31. data/lib/hash_validator/validators/lambda_validator.rb +9 -10
  32. data/lib/hash_validator/validators/many_validator.rb +6 -4
  33. data/lib/hash_validator/validators/multiple_validator.rb +5 -3
  34. data/lib/hash_validator/validators/optional_validator.rb +4 -2
  35. data/lib/hash_validator/validators/presence_validator.rb +8 -8
  36. data/lib/hash_validator/validators/regex_validator.rb +8 -8
  37. data/lib/hash_validator/validators/simple_type_validators.rb +4 -2
  38. data/lib/hash_validator/validators/simple_validator.rb +4 -4
  39. data/lib/hash_validator/validators/url_validator.rb +9 -14
  40. data/lib/hash_validator/validators.rb +63 -25
  41. data/lib/hash_validator/version.rb +3 -1
  42. data/lib/hash_validator.rb +7 -4
  43. data/spec/configuration_spec.rb +191 -0
  44. data/spec/hash_validator_spec.rb +135 -116
  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 +254 -0
  55. data/spec/validators/dynamic_pattern_validator_spec.rb +152 -0
  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 +107 -0
  61. data/spec/validators/ipv4_validator_spec.rb +101 -0
  62. data/spec/validators/ipv6_validator_spec.rb +101 -0
  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 +31 -29
  74. metadata +77 -3
  75. data/Plan.md +0 -309
@@ -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
@@ -0,0 +1,254 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe HashValidator::Validator::DynamicFuncValidator do
6
+ describe "Function-based validator" do
7
+ let(:odd_func_validator) do
8
+ HashValidator::Validator::DynamicFuncValidator.new(
9
+ "odd_func",
10
+ ->(n) { n.is_a?(Integer) && n.odd? },
11
+ "is not an odd integer"
12
+ )
13
+ end
14
+
15
+ let(:range_validator) do
16
+ HashValidator::Validator::DynamicFuncValidator.new(
17
+ "in_range",
18
+ ->(n) { n.is_a?(Numeric) && n >= 18 && n <= 65 },
19
+ "must be between 18 and 65"
20
+ )
21
+ end
22
+
23
+ let(:custom_validator) do
24
+ HashValidator::Validator::DynamicFuncValidator.new(
25
+ "custom",
26
+ proc { |v| v.to_s.length > 5 },
27
+ "must be longer than 5 characters"
28
+ )
29
+ end
30
+
31
+ let(:errors) { Hash.new }
32
+
33
+ describe "#initialize" do
34
+ it "requires a callable object" do
35
+ expect {
36
+ HashValidator::Validator::DynamicFuncValidator.new("test", "not_callable")
37
+ }.to raise_error(ArgumentError, "Function must be callable (proc or lambda)")
38
+ end
39
+
40
+ it "accepts a lambda" do
41
+ validator = HashValidator::Validator::DynamicFuncValidator.new(
42
+ "test",
43
+ ->(v) { true }
44
+ )
45
+ expect(validator.func).to respond_to(:call)
46
+ end
47
+
48
+ it "accepts a proc" do
49
+ validator = HashValidator::Validator::DynamicFuncValidator.new(
50
+ "test",
51
+ proc { |v| true }
52
+ )
53
+ expect(validator.func).to respond_to(:call)
54
+ end
55
+
56
+ it "accepts a custom error message" do
57
+ validator = HashValidator::Validator::DynamicFuncValidator.new(
58
+ "test",
59
+ ->(v) { true },
60
+ "custom error"
61
+ )
62
+ expect(validator.error_message).to eq("custom error")
63
+ end
64
+
65
+ it "uses default error message when none provided" do
66
+ validator = HashValidator::Validator::DynamicFuncValidator.new(
67
+ "test",
68
+ ->(v) { true }
69
+ )
70
+ expect(validator.error_message).to eq("test required")
71
+ end
72
+ end
73
+
74
+ describe "#should_validate?" do
75
+ it "validates the correct name" do
76
+ expect(odd_func_validator.should_validate?("odd_func")).to eq true
77
+ end
78
+
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
82
+ end
83
+ end
84
+
85
+ describe "#validate" do
86
+ context "with odd number function" do
87
+ it "validates odd integers correctly" do
88
+ odd_func_validator.validate(:key, 1, {}, errors)
89
+ expect(errors).to be_empty
90
+
91
+ errors.clear
92
+ odd_func_validator.validate(:key, 13, {}, errors)
93
+ expect(errors).to be_empty
94
+
95
+ errors.clear
96
+ odd_func_validator.validate(:key, -7, {}, errors)
97
+ expect(errors).to be_empty
98
+ end
99
+
100
+ it "rejects even integers" do
101
+ odd_func_validator.validate(:key, 2, {}, errors)
102
+ expect(errors).to eq({ key: "is not an odd integer" })
103
+
104
+ errors.clear
105
+ odd_func_validator.validate(:key, 100, {}, errors)
106
+ expect(errors).to eq({ key: "is not an odd integer" })
107
+ end
108
+
109
+ it "rejects non-integers" do
110
+ odd_func_validator.validate(:key, 1.5, {}, errors)
111
+ expect(errors).to eq({ key: "is not an odd integer" })
112
+
113
+ errors.clear
114
+ odd_func_validator.validate(:key, "1", {}, errors)
115
+ expect(errors).to eq({ key: "is not an odd integer" })
116
+ end
117
+ end
118
+
119
+ context "with range validator" do
120
+ it "validates numbers in range" do
121
+ range_validator.validate(:key, 18, {}, errors)
122
+ expect(errors).to be_empty
123
+
124
+ errors.clear
125
+ range_validator.validate(:key, 30, {}, errors)
126
+ expect(errors).to be_empty
127
+
128
+ errors.clear
129
+ range_validator.validate(:key, 65, {}, errors)
130
+ expect(errors).to be_empty
131
+
132
+ errors.clear
133
+ range_validator.validate(:key, 25.5, {}, errors)
134
+ expect(errors).to be_empty
135
+ end
136
+
137
+ it "rejects numbers outside range" do
138
+ range_validator.validate(:key, 17, {}, errors)
139
+ expect(errors).to eq({ key: "must be between 18 and 65" })
140
+
141
+ errors.clear
142
+ range_validator.validate(:key, 66, {}, errors)
143
+ expect(errors).to eq({ key: "must be between 18 and 65" })
144
+
145
+ errors.clear
146
+ range_validator.validate(:key, -5, {}, errors)
147
+ expect(errors).to eq({ key: "must be between 18 and 65" })
148
+ end
149
+
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" })
153
+ end
154
+ end
155
+
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)
159
+ expect(errors).to be_empty
160
+
161
+ errors.clear
162
+ custom_validator.validate(:key, "testing", {}, errors)
163
+ expect(errors).to be_empty
164
+ end
165
+
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" })
169
+
170
+ errors.clear
171
+ custom_validator.validate(:key, "hi", {}, errors)
172
+ expect(errors).to eq({ key: "must be longer than 5 characters" })
173
+ end
174
+
175
+ it "converts values to strings" do
176
+ custom_validator.validate(:key, 123456, {}, errors)
177
+ expect(errors).to be_empty
178
+
179
+ errors.clear
180
+ custom_validator.validate(:key, 12345, {}, errors)
181
+ expect(errors).to eq({ key: "must be longer than 5 characters" })
182
+ end
183
+ end
184
+
185
+ context "with function that raises errors" do
186
+ let(:error_func_validator) do
187
+ HashValidator::Validator::DynamicFuncValidator.new(
188
+ "error_func",
189
+ ->(v) { raise "intentional error" },
190
+ "validation failed"
191
+ )
192
+ end
193
+
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" })
197
+ end
198
+ end
199
+ end
200
+
201
+ describe "Integration with HashValidator.add_validator" do
202
+ before do
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",
208
+ func: proc { |s| s.to_s == s.to_s.reverse },
209
+ error_message: "must be a palindrome")
210
+ end
211
+
212
+ after do
213
+ HashValidator.remove_validator("adult_age")
214
+ HashValidator.remove_validator("palindrome")
215
+ end
216
+
217
+ it "can register lambda-based validators" do
218
+ validator = HashValidator.validate(
219
+ { age: 25 },
220
+ { age: "adult_age" }
221
+ )
222
+ expect(validator.valid?).to eq true
223
+ expect(validator.errors).to be_empty
224
+ end
225
+
226
+ it "returns errors for invalid lambda validations" do
227
+ validator = HashValidator.validate(
228
+ { age: 16 },
229
+ { age: "adult_age" }
230
+ )
231
+ expect(validator.valid?).to eq false
232
+ expect(validator.errors).to eq({ age: "must be 18 or older" })
233
+ end
234
+
235
+ it "can register proc-based validators" do
236
+ validator = HashValidator.validate(
237
+ { word: "racecar" },
238
+ { word: "palindrome" }
239
+ )
240
+ expect(validator.valid?).to eq true
241
+ expect(validator.errors).to be_empty
242
+ end
243
+
244
+ it "returns errors for invalid proc validations" do
245
+ validator = HashValidator.validate(
246
+ { word: "hello" },
247
+ { word: "palindrome" }
248
+ )
249
+ expect(validator.valid?).to eq false
250
+ expect(validator.errors).to eq({ word: "must be a palindrome" })
251
+ end
252
+ end
253
+ end
254
+ end
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe HashValidator::Validator::DynamicPatternValidator do
6
+ describe "Pattern-based validator" do
7
+ let(:odd_pattern_validator) do
8
+ HashValidator::Validator::DynamicPatternValidator.new(
9
+ "odd_pattern",
10
+ /\A\d*[13579]\z/,
11
+ "is not an odd number"
12
+ )
13
+ end
14
+
15
+ let(:postal_code_validator) do
16
+ HashValidator::Validator::DynamicPatternValidator.new(
17
+ "us_postal",
18
+ /\A\d{5}(-\d{4})?\z/,
19
+ "is not a valid US postal code"
20
+ )
21
+ end
22
+
23
+ let(:errors) { Hash.new }
24
+
25
+ describe "#initialize" do
26
+ it "requires a valid regular expression" do
27
+ expect {
28
+ HashValidator::Validator::DynamicPatternValidator.new("test", "not_a_regex")
29
+ }.to raise_error(ArgumentError, "Pattern must be a regular expression")
30
+ end
31
+
32
+ it "accepts a custom error message" do
33
+ validator = HashValidator::Validator::DynamicPatternValidator.new(
34
+ "test",
35
+ /test/,
36
+ "custom error"
37
+ )
38
+ expect(validator.error_message).to eq("custom error")
39
+ end
40
+
41
+ it "uses default error message when none provided" do
42
+ validator = HashValidator::Validator::DynamicPatternValidator.new("test", /test/)
43
+ expect(validator.error_message).to eq("test required")
44
+ end
45
+ end
46
+
47
+ describe "#should_validate?" do
48
+ it "validates the correct name" do
49
+ expect(odd_pattern_validator.should_validate?("odd_pattern")).to eq true
50
+ end
51
+
52
+ it "does not validate other names" do
53
+ expect(odd_pattern_validator.should_validate?("even_pattern")).to eq false
54
+ expect(odd_pattern_validator.should_validate?("string")).to eq false
55
+ end
56
+ end
57
+
58
+ describe "#validate" do
59
+ context "with odd number pattern" do
60
+ it "validates odd numbers correctly" do
61
+ odd_pattern_validator.validate(:key, "1", {}, errors)
62
+ expect(errors).to be_empty
63
+
64
+ errors.clear
65
+ odd_pattern_validator.validate(:key, "13", {}, errors)
66
+ expect(errors).to be_empty
67
+
68
+ errors.clear
69
+ odd_pattern_validator.validate(:key, "999", {}, errors)
70
+ expect(errors).to be_empty
71
+ end
72
+
73
+ it "rejects even numbers" do
74
+ odd_pattern_validator.validate(:key, "2", {}, errors)
75
+ expect(errors).to eq({ key: "is not an odd number" })
76
+
77
+ errors.clear
78
+ odd_pattern_validator.validate(:key, "100", {}, errors)
79
+ expect(errors).to eq({ key: "is not an odd number" })
80
+ end
81
+
82
+ it "rejects non-numeric strings" do
83
+ odd_pattern_validator.validate(:key, "abc", {}, errors)
84
+ expect(errors).to eq({ key: "is not an odd number" })
85
+ end
86
+
87
+ it "converts non-string values to strings" do
88
+ odd_pattern_validator.validate(:key, 13, {}, errors)
89
+ expect(errors).to be_empty
90
+
91
+ errors.clear
92
+ odd_pattern_validator.validate(:key, 14, {}, errors)
93
+ expect(errors).to eq({ key: "is not an odd number" })
94
+ end
95
+ end
96
+
97
+ context "with postal code pattern" do
98
+ it "validates correct postal codes" do
99
+ postal_code_validator.validate(:key, "12345", {}, errors)
100
+ expect(errors).to be_empty
101
+
102
+ errors.clear
103
+ postal_code_validator.validate(:key, "12345-6789", {}, errors)
104
+ expect(errors).to be_empty
105
+ end
106
+
107
+ it "rejects invalid postal codes" do
108
+ postal_code_validator.validate(:key, "1234", {}, errors)
109
+ expect(errors).to eq({ key: "is not a valid US postal code" })
110
+
111
+ errors.clear
112
+ postal_code_validator.validate(:key, "12345-678", {}, errors)
113
+ expect(errors).to eq({ key: "is not a valid US postal code" })
114
+
115
+ errors.clear
116
+ postal_code_validator.validate(:key, "ABCDE", {}, errors)
117
+ expect(errors).to eq({ key: "is not a valid US postal code" })
118
+ end
119
+ end
120
+ end
121
+
122
+ describe "Integration with HashValidator.add_validator" do
123
+ before do
124
+ HashValidator.add_validator("phone_number",
125
+ pattern: /\A\d{3}-\d{3}-\d{4}\z/,
126
+ error_message: "is not a valid phone number")
127
+ end
128
+
129
+ after do
130
+ HashValidator.remove_validator("phone_number")
131
+ end
132
+
133
+ it "can be registered and used via add_validator" do
134
+ validator = HashValidator.validate(
135
+ { phone: "555-123-4567" },
136
+ { phone: "phone_number" }
137
+ )
138
+ expect(validator.valid?).to eq true
139
+ expect(validator.errors).to be_empty
140
+ end
141
+
142
+ it "returns errors for invalid values" do
143
+ validator = HashValidator.validate(
144
+ { phone: "5551234567" },
145
+ { phone: "phone_number" }
146
+ )
147
+ expect(validator.valid?).to eq false
148
+ expect(validator.errors).to eq({ phone: "is not a valid phone number" })
149
+ end
150
+ end
151
+ end
152
+ end
@@ -1,40 +1,42 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
2
4
 
3
5
  describe HashValidator::Validator::Base do
4
6
  let(:validator) { HashValidator::Validator::EmailValidator.new }
5
7
  let(:errors) { Hash.new }
6
8
 
7
- describe '#should_validate?' do
9
+ describe "#should_validate?" do
8
10
  it 'should validate the name "email"' do
9
- expect(validator.should_validate?('email')).to eq true
11
+ expect(validator.should_validate?("email")).to eq true
10
12
  end
11
13
 
12
- it 'should not validate other names' do
13
- expect(validator.should_validate?('string')).to eq false
14
- expect(validator.should_validate?('array')).to eq false
14
+ it "should not validate other names" do
15
+ expect(validator.should_validate?("string")).to eq false
16
+ expect(validator.should_validate?("array")).to eq false
15
17
  expect(validator.should_validate?(nil)).to eq false
16
18
  end
17
19
  end
18
20
 
19
- describe '#validate' do
20
- it 'should validate an email with true' do
21
+ describe "#validate" do
22
+ it "should validate an email with true" do
21
23
  validator.validate(:key, "johndoe@gmail.com", {}, errors)
22
24
 
23
25
  expect(errors).to be_empty
24
26
  end
25
27
 
26
- it 'should validate a string without an @ symbol with false' do
27
- validator.validate(:key, 'test', {}, errors)
28
+ it "should validate a string without an @ symbol with false" do
29
+ validator.validate(:key, "test", {}, errors)
28
30
 
29
31
  expect(errors).not_to be_empty
30
- expect(errors).to eq({ key: 'is not a valid email' })
32
+ expect(errors).to eq({ key: "is not a valid email" })
31
33
  end
32
34
 
33
- it 'should validate a number with false' do
35
+ it "should validate a number with false" do
34
36
  validator.validate(:key, 123, {}, errors)
35
37
 
36
38
  expect(errors).not_to be_empty
37
- expect(errors).to eq({ key: 'is not a valid email' })
39
+ expect(errors).to eq({ key: "is not a valid email" })
38
40
  end
39
41
  end
40
42
  end