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::AlphanumericValidator do
4
6
  let(:validator) { HashValidator::Validator::AlphanumericValidator.new }
5
7
 
6
- context 'valid alphanumeric strings' do
7
- it 'validates letters only' do
8
+ context "valid alphanumeric strings" do
9
+ it "validates letters only" do
8
10
  errors = {}
9
- validator.validate('key', 'abc', {}, errors)
11
+ validator.validate("key", "abc", {}, errors)
10
12
  expect(errors).to be_empty
11
13
  end
12
14
 
13
- it 'validates numbers only' do
15
+ it "validates numbers only" 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 mixed letters and numbers' do
21
+ it "validates mixed letters and numbers" do
20
22
  errors = {}
21
- validator.validate('key', 'abc123', {}, errors)
23
+ validator.validate("key", "abc123", {}, errors)
22
24
  expect(errors).to be_empty
23
25
  end
24
26
 
25
- it 'validates uppercase letters' do
27
+ it "validates uppercase letters" do
26
28
  errors = {}
27
- validator.validate('key', 'ABC', {}, errors)
29
+ validator.validate("key", "ABC", {}, errors)
28
30
  expect(errors).to be_empty
29
31
  end
30
32
 
31
- it 'validates mixed case letters' do
33
+ it "validates mixed case letters" do
32
34
  errors = {}
33
- validator.validate('key', 'AbC123', {}, errors)
35
+ validator.validate("key", "AbC123", {}, errors)
34
36
  expect(errors).to be_empty
35
37
  end
36
38
 
37
- it 'validates single character' do
39
+ it "validates single character" do
38
40
  errors = {}
39
- validator.validate('key', 'a', {}, errors)
41
+ validator.validate("key", "a", {}, errors)
40
42
  expect(errors).to be_empty
41
43
  end
42
44
 
43
- it 'validates single digit' do
45
+ it "validates single digit" do
44
46
  errors = {}
45
- validator.validate('key', '1', {}, errors)
47
+ validator.validate("key", "1", {}, errors)
46
48
  expect(errors).to be_empty
47
49
  end
48
50
  end
49
51
 
50
- context 'invalid alphanumeric strings' do
51
- it 'rejects non-string values' do
52
+ context "invalid alphanumeric strings" do
53
+ it "rejects non-string values" do
52
54
  errors = {}
53
- validator.validate('key', 123, {}, errors)
54
- expect(errors['key']).to eq('must contain only letters and numbers')
55
+ validator.validate("key", 123, {}, errors)
56
+ expect(errors["key"]).to eq("must contain only letters and numbers")
55
57
  end
56
58
 
57
- it 'rejects nil values' do
59
+ it "rejects nil values" do
58
60
  errors = {}
59
- validator.validate('key', nil, {}, errors)
60
- expect(errors['key']).to eq('must contain only letters and numbers')
61
+ validator.validate("key", nil, {}, errors)
62
+ expect(errors["key"]).to eq("must contain only letters and numbers")
61
63
  end
62
64
 
63
- it 'rejects strings with spaces' do
65
+ it "rejects strings with spaces" do
64
66
  errors = {}
65
- validator.validate('key', 'abc 123', {}, errors)
66
- expect(errors['key']).to eq('must contain only letters and numbers')
67
+ validator.validate("key", "abc 123", {}, errors)
68
+ expect(errors["key"]).to eq("must contain only letters and numbers")
67
69
  end
68
70
 
69
- it 'rejects strings with special characters' do
71
+ it "rejects strings with special characters" do
70
72
  errors = {}
71
- validator.validate('key', 'abc!123', {}, errors)
72
- expect(errors['key']).to eq('must contain only letters and numbers')
73
+ validator.validate("key", "abc!123", {}, errors)
74
+ expect(errors["key"]).to eq("must contain only letters and numbers")
73
75
  end
74
76
 
75
- it 'rejects strings with hyphens' do
77
+ it "rejects strings with hyphens" do
76
78
  errors = {}
77
- validator.validate('key', 'abc-123', {}, errors)
78
- expect(errors['key']).to eq('must contain only letters and numbers')
79
+ validator.validate("key", "abc-123", {}, errors)
80
+ expect(errors["key"]).to eq("must contain only letters and numbers")
79
81
  end
80
82
 
81
- it 'rejects strings with underscores' do
83
+ it "rejects strings with underscores" do
82
84
  errors = {}
83
- validator.validate('key', 'abc_123', {}, errors)
84
- expect(errors['key']).to eq('must contain only letters and numbers')
85
+ validator.validate("key", "abc_123", {}, errors)
86
+ expect(errors["key"]).to eq("must contain only letters and numbers")
85
87
  end
86
88
 
87
- it 'rejects empty strings' do
89
+ it "rejects empty strings" do
88
90
  errors = {}
89
- validator.validate('key', '', {}, errors)
90
- expect(errors['key']).to eq('must contain only letters and numbers')
91
+ validator.validate("key", "", {}, errors)
92
+ expect(errors["key"]).to eq("must contain only letters and numbers")
91
93
  end
92
94
 
93
- it 'rejects strings with periods' do
95
+ it "rejects strings with periods" do
94
96
  errors = {}
95
- validator.validate('key', 'abc.123', {}, errors)
96
- expect(errors['key']).to eq('must contain only letters and numbers')
97
+ validator.validate("key", "abc.123", {}, errors)
98
+ expect(errors["key"]).to eq("must contain only letters and numbers")
97
99
  end
98
100
  end
99
- end
101
+ end
@@ -1,181 +1,236 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
- describe 'Array validator' do
3
+ require "spec_helper"
4
+
5
+ describe "Array validator" do
4
6
  let(:validator) { HashValidator::Validator::ArrayValidator.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 array with first item ":array"' do
9
11
  expect(validator.should_validate?([:array])).to eq true
10
12
  end
11
13
 
12
- it 'should validate the array with empty specification' do
13
- expect(validator.should_validate?([:array, { }])).to eq true
14
+ it "should validate the array with empty specification" do
15
+ expect(validator.should_validate?([:array, {}])).to eq true
14
16
  end
15
17
 
16
- it 'should validate the array with size specified to nil' do
18
+ it "should validate the array with size specified to nil" do
17
19
  expect(validator.should_validate?([:array, { size: nil }])).to eq true
18
20
  end
19
21
 
20
- it 'should validate the array with non-sense specification' do
22
+ it "should validate the array with non-sense specification" do
21
23
  expect(validator.should_validate?([:array, { blah_blah_blah: false }])).to eq true
22
24
  end
23
25
 
24
- it 'should not validate the empty array' do
26
+ it "should not validate the empty array" do
25
27
  expect(validator.should_validate?([])).to eq false
26
28
  end
27
29
 
28
- it 'should not validate the array with nil item' do
30
+ it "should not validate the array with nil item" do
29
31
  expect(validator.should_validate?([nil])).to eq false
30
32
  end
31
33
 
32
- it 'should not validate other names' do
33
- expect(validator.should_validate?('string')).to eq false
34
- expect(validator.should_validate?('array')).to eq false
34
+ it "should not validate other names" do
35
+ expect(validator.should_validate?("string")).to eq false
36
+ expect(validator.should_validate?("array")).to eq false
35
37
  expect(validator.should_validate?(nil)).to eq false
36
38
  end
37
39
  end
38
40
 
39
- describe '#validate' do
40
- it 'should validate an empty array with true' do
41
+ describe "#validate" do
42
+ it "should validate an empty array with true" do
41
43
  validator.validate(:key, [], [:array], errors)
42
44
 
43
45
  expect(errors).to be_empty
44
46
  end
45
47
 
46
- it 'should validate an empty array with nil spec' do
48
+ it "should validate an empty array with nil spec" do
47
49
  validator.validate(:key, [], [:array, nil], errors)
48
50
 
49
51
  expect(errors).to be_empty
50
52
  end
51
53
 
52
- it 'should validate an empty array with empty spec' do
53
- validator.validate(:key, [], [:array, { }], errors)
54
+ it "should validate an empty array with empty spec" do
55
+ validator.validate(:key, [], [:array, {}], errors)
54
56
 
55
57
  expect(errors).to be_empty
56
58
  end
57
59
 
58
- it 'should validate an empty array with size spec = nil' do
60
+ it "should validate an empty array with size spec = nil" do
59
61
  validator.validate(:key, [], [:array, { size: nil }], errors)
60
62
 
61
63
  expect(errors).to be_empty
62
64
  end
63
65
 
64
- it 'should validate an empty array with size spec = 0' do
66
+ it "should validate an empty array with size spec = 0" do
65
67
  validator.validate(:key, [], [:array, { size: 0 }], errors)
66
68
 
67
69
  expect(errors).to be_empty
68
70
  end
69
71
 
70
- it 'should validate an empty array with spec = 0' do
72
+ it "should validate an empty array with spec = 0" do
71
73
  validator.validate(:key, [], [:array, 0], errors)
72
74
 
73
75
  expect(errors).to be_empty
74
76
  end
75
77
 
76
- it 'should validate an empty array with spec = 0.0' do
78
+ it "should validate an empty array with spec = 0.0" do
77
79
  validator.validate(:key, [], [:array, 0.0], errors)
78
80
 
79
81
  expect(errors).to be_empty
80
82
  end
81
83
 
82
- it 'should validate an array of one item with spec = 1' do
84
+ it "should validate an array of one item with spec = 1" do
83
85
  validator.validate(:key, [nil], [:array, 1], errors)
84
86
 
85
87
  expect(errors).to be_empty
86
88
  end
87
89
 
88
- it 'should validate an array of five items with {size: 5.0}' do
89
- my_array = ["one", 2, nil, ["f", "o", "u", "r"], {five: 5}]
90
- validator.validate(:key, my_array, [:array, {size: 5.0}], errors)
90
+ it "should validate an array of five items with {size: 5.0}" do
91
+ my_array = ["one", 2, nil, ["f", "o", "u", "r"], { five: 5 }]
92
+ validator.validate(:key, my_array, [:array, { size: 5.0 }], errors)
91
93
 
92
94
  expect(errors).to be_empty
93
95
  end
94
96
 
95
97
  # >>> NOT >>>
96
98
 
97
- it 'should not validate non array value' do
99
+ it "should not validate non array value" do
98
100
  validator.validate(:key, "I'm not array", [:array], errors)
99
101
 
100
102
  expect(errors).not_to be_empty
101
- expect(errors).to eq({ key: 'Array required' })
103
+ expect(errors).to eq({ key: "Array required" })
102
104
  end
103
105
 
104
- it 'should not validate an empty array with size spec = 1' do
106
+ it "should not validate an empty array with size spec = 1" do
105
107
  validator.validate(:key, [], [:array, { size: 1 }], errors)
106
108
 
107
109
  expect(errors).not_to be_empty
108
- expect(errors).to eq({ key: 'The required size of array is 1 but is 0.' })
110
+ expect(errors).to eq({ key: "The required size of array is 1 but is 0." })
109
111
  end
110
112
 
111
- it 'should not validate an empty array with size spec = 1' do
113
+ it "should not validate an empty array with size spec = 1" do
112
114
  validator.validate(:key, [], [:array, { size: 1 }], errors)
113
115
 
114
116
  expect(errors).not_to be_empty
115
- expect(errors).to eq({ key: 'The required size of array is 1 but is 0.' })
117
+ expect(errors).to eq({ key: "The required size of array is 1 but is 0." })
116
118
  end
117
119
 
118
- it 'should not validate an empty array with spec = 1' do
120
+ it "should not validate an empty array with spec = 1" do
119
121
  validator.validate(:key, [], [:array, 1], errors)
120
122
 
121
123
  expect(errors).not_to be_empty
122
- expect(errors).to eq({ key: 'The required size of array is 1 but is 0.' })
124
+ expect(errors).to eq({ key: "The required size of array is 1 but is 0." })
123
125
  end
124
126
 
125
127
  it 'should not validate an empty array with spec = "0" (string)' do
126
128
  validator.validate(:key, [], [:array, "0"], errors)
127
129
 
128
130
  expect(errors).not_to be_empty
129
- expect(errors).to eq({ key: 'Second item of array specification must be Hash or Numeric.' })
131
+ expect(errors).to eq({ key: "Second item of array specification must be Hash or Numeric." })
130
132
  end
131
133
 
132
134
  it 'should not validate an empty array with spec = "1" (string)' do
133
135
  validator.validate(:key, [], [:array, "1"], errors)
134
136
 
135
137
  expect(errors).not_to be_empty
136
- expect(errors).to eq({ key: 'Second item of array specification must be Hash or Numeric.' })
138
+ expect(errors).to eq({ key: "Second item of array specification must be Hash or Numeric." })
137
139
  end
138
140
 
139
- it 'should not validate an empty array with {min_size: 0} spec' do
141
+ it "should not validate an empty array with {min_size: 0} spec" do
140
142
  validator.validate(:key, [], [:array, { min_size: 0 }], errors)
141
143
 
142
144
  expect(errors).not_to be_empty
143
- expect(errors).to eq({ key: 'Not supported specification for array: min_size.' })
145
+ expect(errors).to eq({ key: "Not supported specification for array: min_size." })
144
146
  end
145
147
 
146
- it 'should not validate an empty array with {min_size: 0, max_size: 2} spec' do
148
+ it "should not validate an empty array with {min_size: 0, max_size: 2} spec" do
147
149
  validator.validate(:key, [], [:array, { min_size: 0, max_size: 2 }], errors)
148
150
 
149
151
  expect(errors).not_to be_empty
150
- expect(errors).to eq({ key: 'Not supported specification for array: max_size, min_size.' })
152
+ expect(errors).to eq({ key: "Not supported specification for array: max_size, min_size." })
151
153
  end
152
154
 
153
- it 'should not validate an array of four items with {size: 3} spec' do
155
+ it "should not validate an array of four items with {size: 3} spec" do
154
156
  validator.validate(:key, [0, 1, 2, 3], [:array, { size: 3 }], errors)
155
157
 
156
158
  expect(errors).not_to be_empty
157
- expect(errors).to eq({ key: 'The required size of array is 3 but is 4.' })
159
+ expect(errors).to eq({ key: "The required size of array is 3 but is 4." })
158
160
  end
159
161
 
160
- it 'should not validate an array of four items with {size: 5} spec' do
162
+ it "should not validate an array of four items with {size: 5} spec" do
161
163
  validator.validate(:key, [0, 1, 2, 3], [:array, { size: 5 }], errors)
162
164
 
163
165
  expect(errors).not_to be_empty
164
- expect(errors).to eq({ key: 'The required size of array is 5 but is 4.' })
166
+ expect(errors).to eq({ key: "The required size of array is 5 but is 4." })
165
167
  end
166
168
 
167
- it 'should not validate an empty array with invalid specification' do
169
+ it "should not validate an empty array with invalid specification" do
168
170
  validator.validate(:key, [], [:blah], errors)
169
171
 
170
172
  expect(errors).not_to be_empty
171
- expect(errors).to eq({ key: 'Wrong array specification. The array is expected as first item.' })
173
+ expect(errors).to eq({ key: "Wrong array specification. The array is expected as first item." })
172
174
  end
173
175
 
174
- it 'should not validate an empty array with to large specification' do
176
+ it "should not validate an empty array with to large specification" do
175
177
  validator.validate(:key, [], [:array, 0, "overlaping item"], errors)
176
178
 
177
179
  expect(errors).not_to be_empty
178
- expect(errors).to eq({ key: 'Wrong size of array specification. Allowed is one or two items.' })
180
+ expect(errors).to eq({ key: "Wrong size of array specification. Allowed is one or two items." })
181
+ end
182
+
183
+ # Edge cases that would trigger the original 'object' variable bug
184
+ it "should handle String size specification (non-empty)" do
185
+ validator.validate(:key, ["item"], [:array, { size: "non-empty" }], errors)
186
+
187
+ expect(errors).not_to be_empty
188
+ expect(errors).to eq({ key: "The required size of array is non-empty but is 1." })
189
+ end
190
+
191
+ it "should handle empty String size specification" do
192
+ validator.validate(:key, [], [:array, { size: "" }], errors)
193
+
194
+ expect(errors).to be_empty
195
+ end
196
+
197
+ it "should handle whitespace-only String size specification" do
198
+ validator.validate(:key, [], [:array, { size: " " }], errors)
199
+
200
+ expect(errors).to be_empty
201
+ end
202
+
203
+ it "should handle Array size specification (non-empty)" do
204
+ validator.validate(:key, ["item"], [:array, { size: [1, 2, 3] }], errors)
205
+
206
+ expect(errors).not_to be_empty
207
+ expect(errors).to eq({ key: "The required size of array is [1, 2, 3] but is 1." })
208
+ end
209
+
210
+ it "should handle empty Array size specification" do
211
+ validator.validate(:key, [], [:array, { size: [] }], errors)
212
+
213
+ expect(errors).to be_empty
214
+ end
215
+
216
+ it "should handle Hash size specification (non-empty)" do
217
+ validator.validate(:key, ["item"], [:array, { size: { a: 1 } }], errors)
218
+
219
+ expect(errors).not_to be_empty
220
+ expect(errors).to eq({ key: "The required size of array is {:a=>1} but is 1." })
221
+ end
222
+
223
+ it "should handle empty Hash size specification" do
224
+ validator.validate(:key, [], [:array, { size: {} }], errors)
225
+
226
+ expect(errors).to be_empty
227
+ end
228
+
229
+ it "should handle Symbol size specification using boolean coercion" do
230
+ validator.validate(:key, ["item"], [:array, { size: :symbol }], errors)
231
+
232
+ expect(errors).not_to be_empty
233
+ expect(errors).to eq({ key: "The required size of array is symbol but is 1." })
179
234
  end
180
235
  end
181
236
  end
@@ -1,23 +1,38 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
2
4
 
3
5
  describe HashValidator::Validator::Base do
4
- let(:name) { 'my_validator' }
6
+ let(:name) { "my_validator" }
5
7
 
6
8
 
7
- it 'allows a validator to be created with a valid name' do
9
+ it "allows a validator to be created with a valid name" do
8
10
  expect { HashValidator::Validator::Base.new(name) }.to_not raise_error
9
11
  end
10
12
 
11
- it 'does not allow a validator to be created with an invalid name' do
12
- expect { HashValidator::Validator::Base.new(nil) }.to raise_error(StandardError, 'Validator must be initialized with a valid name (length greater than zero)')
13
- expect { HashValidator::Validator::Base.new('') }.to raise_error(StandardError, 'Validator must be initialized with a valid name (length greater than zero)')
13
+ it "does not allow a validator to be created with an invalid name" do
14
+ expect { HashValidator::Validator::Base.new(nil) }.to raise_error(StandardError, "Validator must be initialized with a valid name (length greater than zero)")
15
+ expect { HashValidator::Validator::Base.new("") }.to raise_error(StandardError, "Validator must be initialized with a valid name (length greater than zero)")
14
16
  end
15
17
 
16
- describe '#validate' do
17
- let(:validator) { HashValidator::Validator::Base.new('test') }
18
+ describe "#validate" do
19
+ let(:validator) { HashValidator::Validator::Base.new("test") }
20
+
21
+ it "throws an exception as base validators must implement valid? or override validate" do
22
+ expect { validator.validate("key", "value", {}, {}) }.to raise_error(StandardError, "Validator must implement either valid? or override validate method")
23
+ end
24
+
25
+ it "throws an exception when valid? method has invalid arity" do
26
+ # Create a validator with a valid? method that accepts an invalid number of arguments (3)
27
+ invalid_arity_validator = Class.new(HashValidator::Validator::Base) do
28
+ def valid?(value, validations, extra_param)
29
+ true
30
+ end
31
+ end.new("invalid_arity")
18
32
 
19
- it 'throws an exception as base validators must implement valid? or override validate' do
20
- expect { validator.validate('key', 'value', {}, {}) }.to raise_error(StandardError, 'Validator must implement either valid? or override validate method')
33
+ expect {
34
+ invalid_arity_validator.validate("key", "value", {}, {})
35
+ }.to raise_error(StandardError, "valid? method must accept either 1 argument (value) or 2 arguments (value, validations)")
21
36
  end
22
37
  end
23
38
  end
@@ -1,46 +1,48 @@
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::BooleanValidator.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 "boolean"' do
9
- expect(validator.should_validate?('boolean')).to eq true
11
+ expect(validator.should_validate?("boolean")).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 a true boolean with true' do
21
+ describe "#validate" do
22
+ it "should validate a true boolean with true" do
21
23
  validator.validate(:key, true, {}, errors)
22
24
 
23
25
  expect(errors).to be_empty
24
26
  end
25
27
 
26
- it 'should validate a false boolean with true' do
28
+ it "should validate a false boolean with true" do
27
29
  validator.validate(:key, false, {}, errors)
28
30
 
29
31
  expect(errors).to be_empty
30
32
  end
31
33
 
32
- it 'should validate a nil with false' do
34
+ it "should validate a nil with false" do
33
35
  validator.validate(:key, nil, {}, errors)
34
36
 
35
37
  expect(errors).not_to be_empty
36
- expect(errors).to eq({ key: 'boolean required' })
38
+ expect(errors).to eq({ key: "boolean required" })
37
39
  end
38
40
 
39
- it 'should validate a number with false' do
41
+ it "should validate a number with false" do
40
42
  validator.validate(:key, 123, {}, errors)
41
43
 
42
44
  expect(errors).not_to be_empty
43
- expect(errors).to eq({ key: 'boolean required' })
45
+ expect(errors).to eq({ key: "boolean required" })
44
46
  end
45
47
  end
46
48
  end
@@ -1,52 +1,54 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
- describe 'Class validator' do
3
+ require "spec_helper"
4
+
5
+ describe "Class validator" do
4
6
  let(:errors) { Hash.new }
5
7
 
6
8
  {
7
9
  Array => {
8
- valid: [ [], [1], ['foo'], [1,['foo'],Time.now] ],
9
- invalid: [ nil, '', 123, '123', Time.now, '[1]' ]
10
+ valid: [ [], [1], ["foo"], [1, ["foo"], Time.now] ],
11
+ invalid: [ nil, "", 123, "123", Time.now, "[1]" ]
10
12
  },
11
13
  Complex => {
12
- valid: [ Complex(1), Complex(2, 3), Complex('2/3+3/4i'), 0.3.to_c ],
13
- invalid: [ nil, '', 123, '123', Time.now, '[1]', [1], '2/3+3/4i', Rational(2, 3) ]
14
+ valid: [ Complex(1), Complex(2, 3), Complex("2/3+3/4i"), 0.3.to_c ],
15
+ invalid: [ nil, "", 123, "123", Time.now, "[1]", [1], "2/3+3/4i", Rational(2, 3) ]
14
16
  },
15
17
  Float => {
16
18
  valid: [ 0.0, 1.1, 1.23, Float::INFINITY, Float::EPSILON ],
17
- invalid: [ nil, '', 0, 123, '123', Time.now, '[1]', '2013-03-04' ]
19
+ invalid: [ nil, "", 0, 123, "123", Time.now, "[1]", "2013-03-04" ]
18
20
  },
19
21
  Integer => {
20
22
  valid: [ 0, -1000000, 1000000 ],
21
- invalid: [ nil, '', 1.1, '123', Time.now, '[1]', '2013-03-04' ]
23
+ invalid: [ nil, "", 1.1, "123", Time.now, "[1]", "2013-03-04" ]
22
24
  },
23
25
  Numeric => {
24
26
  valid: [ 0, 123, 123.45 ],
25
- invalid: [ nil, '', '123', Time.now ]
27
+ invalid: [ nil, "", "123", Time.now ]
26
28
  },
27
29
  Range => {
28
- valid: [ 0..10, 'a'..'z', 5..0 ],
29
- invalid: [ nil, '', '123', Time.now ]
30
+ valid: [ 0..10, "a".."z", 5..0 ],
31
+ invalid: [ nil, "", "123", Time.now ]
30
32
  },
31
33
  Rational => {
32
34
  valid: [ Rational(1), Rational(2, 3), 3.to_r ],
33
- invalid: [ nil, '', 123, '123', Time.now, '[1]', [1], Complex(2, 3) ]
35
+ invalid: [ nil, "", 123, "123", Time.now, "[1]", [1], Complex(2, 3) ]
34
36
  },
35
37
  Regexp => {
36
- valid: [ /[a-z]+/, //, //i, Regexp.new('.*') ],
37
- invalid: [ nil, '', 123, '123', Time.now, '.*' ]
38
+ valid: [ /[a-z]+/, //, //i, Regexp.new(".*") ],
39
+ invalid: [ nil, "", 123, "123", Time.now, ".*" ]
38
40
  },
39
41
  String => {
40
- valid: [ '', 'Hello World', '12345' ],
42
+ valid: [ "", "Hello World", "12345" ],
41
43
  invalid: [ nil, 12345, Time.now ]
42
44
  },
43
45
  Symbol => {
44
- valid: [ :foo, :'', 'bar'.to_sym ],
45
- invalid: [ nil, '', 1.1, '123', Time.now, '[1]', '2013-03-04' ]
46
+ valid: [ :foo, :'', "bar".to_sym ],
47
+ invalid: [ nil, "", 1.1, "123", Time.now, "[1]", "2013-03-04" ]
46
48
  },
47
49
  Time => {
48
50
  valid: [ Time.now ],
49
- invalid: [ nil, '', 123, '123', "#{Time.now}" ]
51
+ invalid: [ nil, "", 123, "123", "#{Time.now}" ]
50
52
  }
51
53
  }.each do |type, data|
52
54
  describe type do