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,296 +1,315 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
2
4
 
3
5
  describe HashValidator do
4
- describe 'Adding validators' do
5
- let(:new_validator1) { HashValidator::Validator::SimpleValidator.new('my_type1', lambda { |v| true }) }
6
- let(:new_validator2) { HashValidator::Validator::SimpleValidator.new('my_type2', lambda { |v| true }) }
6
+ describe "Adding validators" do
7
+ let(:new_validator1) { HashValidator::Validator::SimpleValidator.new("my_type1", lambda { |v| true }) }
8
+ let(:new_validator2) { HashValidator::Validator::SimpleValidator.new("my_type2", lambda { |v| true }) }
7
9
 
8
- it 'allows validators with unique names' do
10
+ it "allows validators with unique names" do
9
11
  expect {
10
12
  HashValidator.add_validator(new_validator1)
11
13
  }.to_not raise_error
12
14
  end
13
15
 
14
- it 'does not allow validators with conflicting names' do
16
+ it "does not allow validators with conflicting names" do
15
17
  expect {
16
18
  HashValidator.add_validator(new_validator2)
17
19
  HashValidator.add_validator(new_validator2)
18
- }.to raise_error(StandardError, 'validators need to have unique names')
20
+ }.to raise_error(StandardError, "validators need to have unique names")
21
+ end
22
+
23
+ it "does not allow validators that do not inherit from the base validator class" do
24
+ expect {
25
+ HashValidator.add_validator("Not a validator")
26
+ }.to raise_error(StandardError, "validators need to inherit from HashValidator::Validator::Base")
19
27
  end
20
28
 
21
- it 'does not allow validators that do not inherit from the base validator class' do
29
+ it "raises ArgumentError when options hash is missing :pattern or :func key" do
22
30
  expect {
23
- HashValidator.add_validator('Not a validator')
24
- }.to raise_error(StandardError, 'validators need to inherit from HashValidator::Validator::Base')
31
+ HashValidator.add_validator("test_validator", { invalid_key: "value" })
32
+ }.to raise_error(ArgumentError, "Options hash must contain either :pattern or :func key")
33
+ end
34
+
35
+ it "raises ArgumentError when second argument is not a hash" do
36
+ expect {
37
+ HashValidator.add_validator("test_validator", "not_a_hash")
38
+ }.to raise_error(ArgumentError, "Second argument must be an options hash with :pattern or :func key")
39
+ end
40
+
41
+ it "raises ArgumentError when wrong number of arguments are provided" do
42
+ expect {
43
+ HashValidator.add_validator("test", "arg2", "arg3")
44
+ }.to raise_error(ArgumentError, "add_validator expects 1 argument (validator instance) or 2 arguments (name, options)")
25
45
  end
26
46
  end
27
47
 
28
- describe '#validate' do
29
- describe 'individual type validations' do
30
- it 'should validate hash' do
48
+ describe "#validate" do
49
+ describe "individual type validations" do
50
+ it "should validate hash" do
31
51
  expect(validate({ v: {} }, { v: {} }).valid?).to eq true
32
52
 
33
- expect(validate({ v: '' }, { v: {} }).valid?).to eq false
34
- expect(validate({ v: '' }, { v: {} }).errors).to eq({ v: 'hash required' })
53
+ expect(validate({ v: "" }, { v: {} }).valid?).to eq false
54
+ expect(validate({ v: "" }, { v: {} }).errors).to eq({ v: "hash required" })
35
55
  end
36
56
 
37
- it 'should validate presence' do
38
- expect(validate({ v: 'test' }, { v: 'required' }).valid?).to eq true
39
- expect(validate({ v: 1234 }, { v: 'required' }).valid?).to eq true
57
+ it "should validate presence" do
58
+ expect(validate({ v: "test" }, { v: "required" }).valid?).to eq true
59
+ expect(validate({ v: 1234 }, { v: "required" }).valid?).to eq true
40
60
 
41
- expect(validate({ v: nil }, { v: 'required' }).valid?).to eq false
42
- expect(validate({ v: nil }, { v: 'required' }).errors).to eq({ v: 'is required' })
61
+ expect(validate({ v: nil }, { v: "required" }).valid?).to eq false
62
+ expect(validate({ v: nil }, { v: "required" }).errors).to eq({ v: "is required" })
43
63
 
44
- expect(validate({ x: 'test' }, { v: 'required' }).valid?).to eq false
45
- expect(validate({ x: 'test' }, { v: 'required' }).errors).to eq({ v: 'is required' })
64
+ expect(validate({ x: "test" }, { v: "required" }).valid?).to eq false
65
+ expect(validate({ x: "test" }, { v: "required" }).errors).to eq({ v: "is required" })
46
66
 
47
- expect(validate({ x: 1234 }, { v: 'required' }).valid?).to eq false
48
- expect(validate({ x: 1234 }, { v: 'required' }).errors).to eq({ v: 'is required' })
67
+ expect(validate({ x: 1234 }, { v: "required" }).valid?).to eq false
68
+ expect(validate({ x: 1234 }, { v: "required" }).errors).to eq({ v: "is required" })
49
69
  end
50
70
 
51
- it 'should validate string' do
52
- expect(validate({ v: 'test' }, { v: 'string' }).valid?).to eq true
71
+ it "should validate string" do
72
+ expect(validate({ v: "test" }, { v: "string" }).valid?).to eq true
53
73
 
54
- expect(validate({ v: 123456 }, { v: 'string' }).valid?).to eq false
55
- expect(validate({ v: 123456 }, { v: 'string' }).errors).to eq({ v: 'string required' })
74
+ expect(validate({ v: 123456 }, { v: "string" }).valid?).to eq false
75
+ expect(validate({ v: 123456 }, { v: "string" }).errors).to eq({ v: "string required" })
56
76
  end
57
77
 
58
- it 'should validate numeric' do
59
- expect(validate({ v: 1234 }, { v: 'numeric' }).valid?).to eq true
60
- expect(validate({ v: '12' }, { v: 'numeric' }).valid?).to eq false
78
+ it "should validate numeric" do
79
+ expect(validate({ v: 1234 }, { v: "numeric" }).valid?).to eq true
80
+ expect(validate({ v: "12" }, { v: "numeric" }).valid?).to eq false
61
81
  end
62
82
 
63
- it 'should validate array' do
64
- expect(validate({ v: [ 1,2,3 ] }, { v: 'array' }).valid?).to eq true
65
- expect(validate({ v: ' 1,2,3 ' }, { v: 'array' }).valid?).to eq false
83
+ it "should validate array" do
84
+ expect(validate({ v: [ 1, 2, 3 ] }, { v: "array" }).valid?).to eq true
85
+ expect(validate({ v: " 1,2,3 " }, { v: "array" }).valid?).to eq false
66
86
  end
67
87
 
68
- it 'should validate time' do
69
- expect(validate({ v: Time.now }, { v: 'time' }).valid?).to eq true
70
- expect(validate({ v: '2013-04-12 13:18:05 +0930' }, { v: 'time' }).valid?).to eq false
88
+ it "should validate time" do
89
+ expect(validate({ v: Time.now }, { v: "time" }).valid?).to eq true
90
+ expect(validate({ v: "2013-04-12 13:18:05 +0930" }, { v: "time" }).valid?).to eq false
71
91
  end
72
92
  end
73
93
 
74
- describe 'validator syntax' do
75
- it 'should allow strings as validator names' do
76
- expect(validate({ v: 'test' }, { v: 'string' }).valid?).to eq true
94
+ describe "validator syntax" do
95
+ it "should allow strings as validator names" do
96
+ expect(validate({ v: "test" }, { v: "string" }).valid?).to eq true
77
97
  end
78
98
 
79
- it 'should allow symbols as validator names' do
80
- expect(validate({ v: 'test' }, { v: :string }).valid?).to eq true
99
+ it "should allow symbols as validator names" do
100
+ expect(validate({ v: "test" }, { v: :string }).valid?).to eq true
81
101
  end
82
102
  end
83
103
 
84
- describe 'full validations' do
85
- let(:empty_hash) {{}}
104
+ describe "full validations" do
105
+ let(:empty_hash) { {} }
86
106
 
87
- let(:simple_hash) {{
107
+ let(:simple_hash) { {
88
108
  foo: 1,
89
- bar: 'baz'
109
+ bar: "baz"
90
110
  }}
91
111
 
92
- let(:invalid_simple_hash) {{
112
+ let(:invalid_simple_hash) { {
93
113
  foo: 1,
94
114
  bar: 2
95
115
  }}
96
116
 
97
- let(:complex_hash) {{
117
+ let(:complex_hash) { {
98
118
  foo: 1,
99
- bar: 'baz',
119
+ bar: "baz",
100
120
  user: {
101
- first_name: 'James',
102
- last_name: 'Brooks',
121
+ first_name: "James",
122
+ last_name: "Brooks",
103
123
  age: 27,
104
- likes: [ 'Ruby', 'Kendo', 'Board Games' ]
124
+ likes: [ "Ruby", "Kendo", "Board Games" ]
105
125
  }
106
126
  }}
107
127
 
108
- let(:invalid_complex_hash) {{
128
+ let(:invalid_complex_hash) { {
109
129
  foo: 1,
110
130
  bar: 2,
111
131
  user: {
112
- first_name: 'James',
113
- last_name: 'Brooks',
114
- likes: 'Ruby, Kendo, Board Games'
132
+ first_name: "James",
133
+ last_name: "Brooks",
134
+ likes: "Ruby, Kendo, Board Games"
115
135
  }
116
136
  }}
117
137
 
118
- describe 'no validations' do
119
- let(:validations) {{}}
138
+ describe "no validations" do
139
+ let(:validations) { {} }
120
140
 
121
- it 'should validate an empty hash' do
141
+ it "should validate an empty hash" do
122
142
  v = validate(empty_hash, validations)
123
143
  expect(v.valid?).to eq true
124
144
  expect(v.errors).to be_empty
125
145
  end
126
146
 
127
- it 'should validate a simple hash' do
147
+ it "should validate a simple hash" do
128
148
  v = validate(simple_hash, validations)
129
149
  expect(v.valid?).to eq true
130
150
  expect(v.errors).to be_empty
131
151
  end
132
152
 
133
- it 'should validate a simple hash 2' do
153
+ it "should validate a simple hash 2" do
134
154
  v = validate(invalid_simple_hash, validations)
135
155
  expect(v.valid?).to eq true
136
156
  expect(v.errors).to be_empty
137
157
  end
138
158
 
139
- it 'should validate a complex hash' do
159
+ it "should validate a complex hash" do
140
160
  v = validate(complex_hash, validations)
141
161
  expect(v.valid?).to eq true
142
162
  expect(v.errors).to be_empty
143
163
  end
144
164
 
145
- it 'should validate a complex hash 2' do
165
+ it "should validate a complex hash 2" do
146
166
  v = validate(invalid_complex_hash, validations)
147
167
  expect(v.valid?).to eq true
148
168
  expect(v.errors).to be_empty
149
169
  end
150
170
  end
151
171
 
152
- describe 'simple validations' do
153
- let(:validations) {{ foo: 'numeric', bar: 'string' }}
172
+ describe "simple validations" do
173
+ let(:validations) { { foo: "numeric", bar: "string" } }
154
174
 
155
- it 'should not validate an empty hash (stating missing with required)' do
175
+ it "should not validate an empty hash (stating missing with required)" do
156
176
  v = validate(empty_hash, validations)
157
177
  expect(v.valid?).to eq false
158
- expect(v.errors).to eq({ foo: 'numeric required', bar: 'string required' })
178
+ expect(v.errors).to eq({ foo: "numeric required", bar: "string required" })
159
179
  end
160
180
 
161
- it 'should validate a simple hash' do
181
+ it "should validate a simple hash" do
162
182
  v = validate(simple_hash, validations)
163
183
  expect(v.valid?).to eq true
164
184
  expect(v.errors).to be_empty
165
185
  end
166
186
 
167
- it 'should not validate a simple hash 2' do
187
+ it "should not validate a simple hash 2" do
168
188
  v = validate(invalid_simple_hash, validations)
169
189
  expect(v.valid?).to eq false
170
- expect(v.errors).to eq({ bar: 'string required' })
190
+ expect(v.errors).to eq({ bar: "string required" })
171
191
  end
172
192
 
173
- it 'should validate a complex hash' do
193
+ it "should validate a complex hash" do
174
194
  v = validate(complex_hash, validations)
175
195
  expect(v.valid?).to eq true
176
196
  expect(v.errors).to be_empty
177
197
  end
178
198
 
179
- it 'should not validate a complex hash 2' do
199
+ it "should not validate a complex hash 2" do
180
200
  v = validate(invalid_complex_hash, validations)
181
201
  expect(v.valid?).to eq false
182
- expect(v.errors).to eq({ bar: 'string required' })
202
+ expect(v.errors).to eq({ bar: "string required" })
183
203
  end
184
204
  end
185
205
 
186
- describe 'nested validations' do
187
- let(:validations) {{
188
- foo: 'numeric',
189
- bar: 'string',
206
+ describe "nested validations" do
207
+ let(:validations) { {
208
+ foo: "numeric",
209
+ bar: "string",
190
210
  user: {
191
- first_name: 'string',
211
+ first_name: "string",
192
212
  last_name: /^(Brooks|Smith)$/,
193
- age: 'required',
194
- likes: 'array'
213
+ age: "required",
214
+ likes: "array"
195
215
  }
196
216
  }}
197
217
 
198
- it 'should validate a complex hash' do
218
+ it "should validate a complex hash" do
199
219
  v = validate(complex_hash, validations)
200
220
  expect(v.valid?).to eq true
201
221
  expect(v.errors).to be_empty
202
222
  end
203
223
 
204
- it 'should not validate a complex hash 2' do
224
+ it "should not validate a complex hash 2" do
205
225
  v = validate(invalid_complex_hash, validations)
206
226
  expect(v.valid?).to eq false
207
- expect(v.errors).to eq({ bar: 'string required', user: { age: 'is required', likes: 'array required' } })
227
+ expect(v.errors).to eq({ bar: "string required", user: { age: "is required", likes: "array required" } })
208
228
  end
209
229
  end
210
230
 
211
- describe 'optional validations' do
212
- let(:validations) {{ foo: 'numeric', bar: HashValidator.optional('string') }}
231
+ describe "optional validations" do
232
+ let(:validations) { { foo: "numeric", bar: HashValidator.optional("string") } }
213
233
 
214
- it 'should validate a complex hash' do
234
+ it "should validate a complex hash" do
215
235
  v = validate(complex_hash, validations)
216
236
  expect(v.valid?).to eq true
217
237
  expect(v.errors).to be_empty
218
238
  end
219
239
 
220
- it 'should not validate a complex hash 2' do
240
+ it "should not validate a complex hash 2" do
221
241
  v = validate(invalid_complex_hash, validations)
222
242
  expect(v.valid?).to eq false
223
- expect(v.errors).to eq({ bar: 'string required' })
243
+ expect(v.errors).to eq({ bar: "string required" })
224
244
  end
225
245
  end
226
246
 
227
- describe 'many validations' do
228
- let(:validations) {{ foo: 'numeric', bar: 'string', user: { first_name: 'string', likes: HashValidator.many('string') } }}
247
+ describe "many validations" do
248
+ let(:validations) { { foo: "numeric", bar: "string", user: { first_name: "string", likes: HashValidator.many("string") } } }
229
249
 
230
- it 'should validate a complex hash' do
250
+ it "should validate a complex hash" do
231
251
  v = validate(complex_hash, validations)
232
252
  expect(v.valid?).to eq true
233
253
  expect(v.errors).to be_empty
234
254
  end
235
255
 
236
- it 'should not validate a complex hash 2' do
256
+ it "should not validate a complex hash 2" do
237
257
  v = validate(invalid_complex_hash, validations)
238
258
  expect(v.valid?).to eq false
239
- expect(v.errors).to eq({ bar: 'string required', user: { likes: 'enumerable required' } })
259
+ expect(v.errors).to eq({ bar: "string required", user: { likes: "enumerable required" } })
240
260
  end
241
261
  end
242
262
 
243
- describe 'multiple validations' do
244
- let(:validations) {{ foo: 'numeric', user: { age: HashValidator.multiple('numeric', 1..100) } }}
263
+ describe "multiple validations" do
264
+ let(:validations) { { foo: "numeric", user: { age: HashValidator.multiple("numeric", 1..100) } } }
245
265
 
246
- it 'should validate a complex hash' do
266
+ it "should validate a complex hash" do
247
267
  v = validate(complex_hash, validations)
248
268
  expect(v.valid?).to eq true
249
269
  expect(v.errors).to be_empty
250
270
  end
251
271
 
252
- it 'should not validate a complex hash 2' do
272
+ it "should not validate a complex hash 2" do
253
273
  v = validate(invalid_complex_hash, validations)
254
274
  expect(v.valid?).to eq false
255
- expect(v.errors).to eq({ user: { age: 'numeric required, value from list required' } })
275
+ expect(v.errors).to eq({ user: { age: "numeric required, value from list required" } })
256
276
  end
257
277
  end
258
278
  end
259
279
  end
260
280
  end
261
281
 
262
- describe 'Strict Validation' do
263
- let(:simple_hash) { { foo: 'bar', bar: 'foo' } }
282
+ describe "Strict Validation" do
283
+ let(:simple_hash) { { foo: "bar", bar: "foo" } }
264
284
 
265
- let(:complex_hash) {{
285
+ let(:complex_hash) { {
266
286
  foo: 1,
267
287
  user: {
268
- first_name: 'James',
269
- last_name: 'Brooks',
288
+ first_name: "James",
289
+ last_name: "Brooks",
270
290
  age: 27,
271
- likes: [ 'Ruby', 'Kendo', 'Board Games' ]
291
+ likes: [ "Ruby", "Kendo", "Board Games" ]
272
292
  }
273
293
  }}
274
294
 
275
- let(:validations) { { foo: 'string' } }
295
+ let(:validations) { { foo: "string" } }
276
296
 
277
- let(:complex_validations) {{
278
- foo: 'integer',
297
+ let(:complex_validations) { {
298
+ foo: "integer",
279
299
  user: {
280
- first_name: 'string', age: 'integer'
300
+ first_name: "string", age: "integer"
281
301
  }
282
302
  }}
283
303
 
284
- it 'reports which keys are not expected for a simple hash' do
304
+ it "reports which keys are not expected for a simple hash" do
285
305
  v = validate(simple_hash, validations, true)
286
306
  expect(v.valid?).to eq false
287
- expect(v.errors).to eq({ bar: 'key not expected' })
307
+ expect(v.errors).to eq({ bar: "key not expected" })
288
308
  end
289
309
 
290
- it 'reports which keys are not expected for a complex hash' do
310
+ it "reports which keys are not expected for a complex hash" do
291
311
  v = validate(complex_hash, complex_validations, true)
292
312
  expect(v.valid?).to eq false
293
- expect(v.errors).to eq(user: { last_name: 'key not expected', likes: 'key not expected' })
313
+ expect(v.errors).to eq(user: { last_name: "key not expected", likes: "key not expected" })
294
314
  end
295
-
296
315
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module HashValidatorSpecHelper
2
4
  def validate(hash, validations, strict = false)
3
5
  HashValidator.validate(hash, validations, strict)
data/spec/spec_helper.rb CHANGED
@@ -1,11 +1,21 @@
1
- require 'rubygems'
2
- require 'hash_validator'
3
- require 'hash_validator_spec_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "rubygems"
4
+ require "simplecov"
5
+ require "simplecov_json_formatter"
6
+
7
+ SimpleCov.start do
8
+ formatter SimpleCov::Formatter::JSONFormatter
9
+ add_filter "/spec/"
10
+ end
11
+
12
+ require "hash_validator"
13
+ require "hash_validator_spec_helper"
4
14
 
5
15
  RSpec.configure do |config|
6
16
  config.run_all_when_everything_filtered = true
7
17
  config.filter_run :focus
8
- config.order = 'random'
18
+ config.order = "random"
9
19
 
10
20
  config.include HashValidatorSpecHelper
11
21
  end
@@ -1,93 +1,95 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
2
4
 
3
5
  describe HashValidator::Validator::AlphaValidator do
4
6
  let(:validator) { HashValidator::Validator::AlphaValidator.new }
5
7
 
6
- context 'valid alpha strings' do
7
- it 'validates lowercase letters' do
8
+ context "valid alpha strings" do
9
+ it "validates lowercase letters" 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 uppercase letters' do
15
+ it "validates uppercase letters" do
14
16
  errors = {}
15
- validator.validate('key', 'ABC', {}, errors)
17
+ validator.validate("key", "ABC", {}, errors)
16
18
  expect(errors).to be_empty
17
19
  end
18
20
 
19
- it 'validates mixed case letters' do
21
+ it "validates mixed case letters" do
20
22
  errors = {}
21
- validator.validate('key', 'AbC', {}, errors)
23
+ validator.validate("key", "AbC", {}, errors)
22
24
  expect(errors).to be_empty
23
25
  end
24
26
 
25
- it 'validates single letter' do
27
+ it "validates single letter" do
26
28
  errors = {}
27
- validator.validate('key', 'a', {}, errors)
29
+ validator.validate("key", "a", {}, errors)
28
30
  expect(errors).to be_empty
29
31
  end
30
32
 
31
- it 'validates long strings' do
33
+ it "validates long strings" do
32
34
  errors = {}
33
- validator.validate('key', 'HelloWorld', {}, errors)
35
+ validator.validate("key", "HelloWorld", {}, errors)
34
36
  expect(errors).to be_empty
35
37
  end
36
38
  end
37
39
 
38
- context 'invalid alpha strings' do
39
- it 'rejects non-string values' do
40
+ context "invalid alpha 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 letters')
43
+ validator.validate("key", 123, {}, errors)
44
+ expect(errors["key"]).to eq("must contain only letters")
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 letters')
49
+ validator.validate("key", nil, {}, errors)
50
+ expect(errors["key"]).to eq("must contain only letters")
49
51
  end
50
52
 
51
- it 'rejects strings with numbers' do
53
+ it "rejects strings with numbers" do
52
54
  errors = {}
53
- validator.validate('key', 'abc123', {}, errors)
54
- expect(errors['key']).to eq('must contain only letters')
55
+ validator.validate("key", "abc123", {}, errors)
56
+ expect(errors["key"]).to eq("must contain only letters")
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', 'abc def', {}, errors)
60
- expect(errors['key']).to eq('must contain only letters')
61
+ validator.validate("key", "abc def", {}, errors)
62
+ expect(errors["key"]).to eq("must contain only letters")
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', 'abc!', {}, errors)
66
- expect(errors['key']).to eq('must contain only letters')
67
+ validator.validate("key", "abc!", {}, errors)
68
+ expect(errors["key"]).to eq("must contain only letters")
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', 'abc-def', {}, errors)
72
- expect(errors['key']).to eq('must contain only letters')
73
+ validator.validate("key", "abc-def", {}, errors)
74
+ expect(errors["key"]).to eq("must contain only letters")
73
75
  end
74
76
 
75
- it 'rejects strings with underscores' do
77
+ it "rejects strings with underscores" do
76
78
  errors = {}
77
- validator.validate('key', 'abc_def', {}, errors)
78
- expect(errors['key']).to eq('must contain only letters')
79
+ validator.validate("key", "abc_def", {}, errors)
80
+ expect(errors["key"]).to eq("must contain only letters")
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 letters')
85
+ validator.validate("key", "", {}, errors)
86
+ expect(errors["key"]).to eq("must contain only letters")
85
87
  end
86
88
 
87
- it 'rejects strings with periods' do
89
+ it "rejects strings with periods" do
88
90
  errors = {}
89
- validator.validate('key', 'abc.def', {}, errors)
90
- expect(errors['key']).to eq('must contain only letters')
91
+ validator.validate("key", "abc.def", {}, errors)
92
+ expect(errors["key"]).to eq("must contain only letters")
91
93
  end
92
94
  end
93
- end
95
+ end