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.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +6 -1
- data/.rubocop.yml +340 -0
- data/Gemfile +3 -1
- data/README.md +2 -1
- data/Rakefile +6 -4
- data/hash_validator.gemspec +11 -5
- data/lib/hash_validator/base.rb +2 -0
- data/lib/hash_validator/configuration.rb +5 -3
- data/lib/hash_validator/validations/many.rb +2 -0
- data/lib/hash_validator/validations/multiple.rb +2 -0
- data/lib/hash_validator/validations/optional.rb +2 -0
- data/lib/hash_validator/validations.rb +5 -3
- data/lib/hash_validator/validators/alpha_validator.rb +5 -3
- data/lib/hash_validator/validators/alphanumeric_validator.rb +5 -3
- data/lib/hash_validator/validators/array_validator.rb +44 -48
- data/lib/hash_validator/validators/base.rb +6 -4
- data/lib/hash_validator/validators/boolean_validator.rb +3 -1
- data/lib/hash_validator/validators/class_validator.rb +3 -1
- data/lib/hash_validator/validators/digits_validator.rb +5 -3
- data/lib/hash_validator/validators/dynamic_func_validator.rb +8 -8
- data/lib/hash_validator/validators/dynamic_pattern_validator.rb +5 -3
- data/lib/hash_validator/validators/email_validator.rb +4 -2
- data/lib/hash_validator/validators/enumerable_validator.rb +4 -2
- data/lib/hash_validator/validators/hash_validator.rb +5 -3
- data/lib/hash_validator/validators/hex_color_validator.rb +5 -3
- data/lib/hash_validator/validators/ip_validator.rb +6 -4
- data/lib/hash_validator/validators/ipv4_validator.rb +5 -3
- data/lib/hash_validator/validators/ipv6_validator.rb +6 -4
- data/lib/hash_validator/validators/json_validator.rb +6 -4
- data/lib/hash_validator/validators/lambda_validator.rb +4 -2
- data/lib/hash_validator/validators/many_validator.rb +3 -1
- data/lib/hash_validator/validators/multiple_validator.rb +4 -2
- data/lib/hash_validator/validators/optional_validator.rb +3 -1
- data/lib/hash_validator/validators/presence_validator.rb +4 -2
- data/lib/hash_validator/validators/regex_validator.rb +4 -2
- data/lib/hash_validator/validators/simple_type_validators.rb +3 -1
- data/lib/hash_validator/validators/simple_validator.rb +2 -0
- data/lib/hash_validator/validators/url_validator.rb +6 -4
- data/lib/hash_validator/validators.rb +35 -33
- data/lib/hash_validator/version.rb +3 -1
- data/lib/hash_validator.rb +7 -5
- data/spec/configuration_spec.rb +76 -74
- data/spec/hash_validator_spec.rb +132 -113
- data/spec/hash_validator_spec_helper.rb +2 -0
- data/spec/spec_helper.rb +14 -4
- data/spec/validators/alpha_validator_spec.rb +43 -41
- data/spec/validators/alphanumeric_validator_spec.rb +44 -42
- data/spec/validators/array_spec.rb +102 -47
- data/spec/validators/base_spec.rb +25 -10
- data/spec/validators/boolean_spec.rb +15 -13
- data/spec/validators/class_spec.rb +20 -18
- data/spec/validators/digits_validator_spec.rb +46 -44
- data/spec/validators/dynamic_func_validator_spec.rb +90 -88
- data/spec/validators/dynamic_pattern_validator_spec.rb +65 -63
- data/spec/validators/email_spec.rb +15 -13
- data/spec/validators/hash_validator_spec.rb +39 -37
- data/spec/validators/hex_color_validator_spec.rb +49 -47
- data/spec/validators/in_enumerable_spec.rb +32 -30
- data/spec/validators/ip_validator_spec.rb +46 -44
- data/spec/validators/ipv4_validator_spec.rb +45 -43
- data/spec/validators/ipv6_validator_spec.rb +44 -42
- data/spec/validators/json_validator_spec.rb +38 -36
- data/spec/validators/lambda_spec.rb +20 -18
- data/spec/validators/many_spec.rb +25 -23
- data/spec/validators/multiple_spec.rb +13 -11
- data/spec/validators/optional_spec.rb +22 -20
- data/spec/validators/presence_spec.rb +16 -14
- data/spec/validators/regexp_spec.rb +14 -12
- data/spec/validators/simple_spec.rb +17 -15
- data/spec/validators/simple_types_spec.rb +21 -19
- data/spec/validators/url_validator_spec.rb +34 -32
- data/spec/validators/user_defined_spec.rb +29 -27
- metadata +59 -2
data/spec/hash_validator_spec.rb
CHANGED
@@ -1,296 +1,315 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
2
4
|
|
3
5
|
describe HashValidator do
|
4
|
-
describe
|
5
|
-
let(:new_validator1) { HashValidator::Validator::SimpleValidator.new(
|
6
|
-
let(:new_validator2) { HashValidator::Validator::SimpleValidator.new(
|
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
|
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
|
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,
|
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
|
29
|
+
it "raises ArgumentError when options hash is missing :pattern or :func key" do
|
22
30
|
expect {
|
23
|
-
HashValidator.add_validator(
|
24
|
-
}.to raise_error(
|
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
|
29
|
-
describe
|
30
|
-
it
|
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:
|
34
|
-
expect(validate({ v:
|
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
|
38
|
-
expect(validate({ v:
|
39
|
-
expect(validate({ v: 1234 }, { v:
|
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:
|
42
|
-
expect(validate({ v: nil }, { v:
|
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:
|
45
|
-
expect(validate({ x:
|
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:
|
48
|
-
expect(validate({ x: 1234 }, { v:
|
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
|
52
|
-
expect(validate({ v:
|
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:
|
55
|
-
expect(validate({ v: 123456 }, { v:
|
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
|
59
|
-
expect(validate({ v: 1234 }, { v:
|
60
|
-
expect(validate({ v:
|
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
|
64
|
-
expect(validate({ v: [ 1,2,3 ] }, { v:
|
65
|
-
expect(validate({ v:
|
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
|
69
|
-
expect(validate({ v: Time.now }, { v:
|
70
|
-
expect(validate({ v:
|
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
|
75
|
-
it
|
76
|
-
expect(validate({ v:
|
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
|
80
|
-
expect(validate({ v:
|
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
|
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:
|
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:
|
119
|
+
bar: "baz",
|
100
120
|
user: {
|
101
|
-
first_name:
|
102
|
-
last_name:
|
121
|
+
first_name: "James",
|
122
|
+
last_name: "Brooks",
|
103
123
|
age: 27,
|
104
|
-
likes: [
|
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:
|
113
|
-
last_name:
|
114
|
-
likes:
|
132
|
+
first_name: "James",
|
133
|
+
last_name: "Brooks",
|
134
|
+
likes: "Ruby, Kendo, Board Games"
|
115
135
|
}
|
116
136
|
}}
|
117
137
|
|
118
|
-
describe
|
119
|
-
let(:validations) {{}}
|
138
|
+
describe "no validations" do
|
139
|
+
let(:validations) { {} }
|
120
140
|
|
121
|
-
it
|
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
|
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
|
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
|
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
|
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
|
153
|
-
let(:validations) {{ foo:
|
172
|
+
describe "simple validations" do
|
173
|
+
let(:validations) { { foo: "numeric", bar: "string" } }
|
154
174
|
|
155
|
-
it
|
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:
|
178
|
+
expect(v.errors).to eq({ foo: "numeric required", bar: "string required" })
|
159
179
|
end
|
160
180
|
|
161
|
-
it
|
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
|
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:
|
190
|
+
expect(v.errors).to eq({ bar: "string required" })
|
171
191
|
end
|
172
192
|
|
173
|
-
it
|
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
|
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:
|
202
|
+
expect(v.errors).to eq({ bar: "string required" })
|
183
203
|
end
|
184
204
|
end
|
185
205
|
|
186
|
-
describe
|
187
|
-
let(:validations) {{
|
188
|
-
foo:
|
189
|
-
bar:
|
206
|
+
describe "nested validations" do
|
207
|
+
let(:validations) { {
|
208
|
+
foo: "numeric",
|
209
|
+
bar: "string",
|
190
210
|
user: {
|
191
|
-
first_name:
|
211
|
+
first_name: "string",
|
192
212
|
last_name: /^(Brooks|Smith)$/,
|
193
|
-
age:
|
194
|
-
likes:
|
213
|
+
age: "required",
|
214
|
+
likes: "array"
|
195
215
|
}
|
196
216
|
}}
|
197
217
|
|
198
|
-
it
|
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
|
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:
|
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
|
212
|
-
let(:validations) {{ foo:
|
231
|
+
describe "optional validations" do
|
232
|
+
let(:validations) { { foo: "numeric", bar: HashValidator.optional("string") } }
|
213
233
|
|
214
|
-
it
|
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
|
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:
|
243
|
+
expect(v.errors).to eq({ bar: "string required" })
|
224
244
|
end
|
225
245
|
end
|
226
246
|
|
227
|
-
describe
|
228
|
-
let(:validations) {{ foo:
|
247
|
+
describe "many validations" do
|
248
|
+
let(:validations) { { foo: "numeric", bar: "string", user: { first_name: "string", likes: HashValidator.many("string") } } }
|
229
249
|
|
230
|
-
it
|
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
|
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:
|
259
|
+
expect(v.errors).to eq({ bar: "string required", user: { likes: "enumerable required" } })
|
240
260
|
end
|
241
261
|
end
|
242
262
|
|
243
|
-
describe
|
244
|
-
let(:validations) {{ foo:
|
263
|
+
describe "multiple validations" do
|
264
|
+
let(:validations) { { foo: "numeric", user: { age: HashValidator.multiple("numeric", 1..100) } } }
|
245
265
|
|
246
|
-
it
|
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
|
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:
|
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
|
263
|
-
let(:simple_hash) { { 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:
|
269
|
-
last_name:
|
288
|
+
first_name: "James",
|
289
|
+
last_name: "Brooks",
|
270
290
|
age: 27,
|
271
|
-
likes: [
|
291
|
+
likes: [ "Ruby", "Kendo", "Board Games" ]
|
272
292
|
}
|
273
293
|
}}
|
274
294
|
|
275
|
-
let(:validations) { { foo:
|
295
|
+
let(:validations) { { foo: "string" } }
|
276
296
|
|
277
|
-
let(:complex_validations) {{
|
278
|
-
foo:
|
297
|
+
let(:complex_validations) { {
|
298
|
+
foo: "integer",
|
279
299
|
user: {
|
280
|
-
first_name:
|
300
|
+
first_name: "string", age: "integer"
|
281
301
|
}
|
282
302
|
}}
|
283
303
|
|
284
|
-
it
|
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:
|
307
|
+
expect(v.errors).to eq({ bar: "key not expected" })
|
288
308
|
end
|
289
309
|
|
290
|
-
it
|
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:
|
313
|
+
expect(v.errors).to eq(user: { last_name: "key not expected", likes: "key not expected" })
|
294
314
|
end
|
295
|
-
|
296
315
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,11 +1,21 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
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 =
|
18
|
+
config.order = "random"
|
9
19
|
|
10
20
|
config.include HashValidatorSpecHelper
|
11
21
|
end
|
@@ -1,93 +1,95 @@
|
|
1
|
-
|
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
|
7
|
-
it
|
8
|
+
context "valid alpha strings" do
|
9
|
+
it "validates lowercase letters" do
|
8
10
|
errors = {}
|
9
|
-
validator.validate(
|
11
|
+
validator.validate("key", "abc", {}, errors)
|
10
12
|
expect(errors).to be_empty
|
11
13
|
end
|
12
14
|
|
13
|
-
it
|
15
|
+
it "validates uppercase letters" do
|
14
16
|
errors = {}
|
15
|
-
validator.validate(
|
17
|
+
validator.validate("key", "ABC", {}, errors)
|
16
18
|
expect(errors).to be_empty
|
17
19
|
end
|
18
20
|
|
19
|
-
it
|
21
|
+
it "validates mixed case letters" do
|
20
22
|
errors = {}
|
21
|
-
validator.validate(
|
23
|
+
validator.validate("key", "AbC", {}, errors)
|
22
24
|
expect(errors).to be_empty
|
23
25
|
end
|
24
26
|
|
25
|
-
it
|
27
|
+
it "validates single letter" do
|
26
28
|
errors = {}
|
27
|
-
validator.validate(
|
29
|
+
validator.validate("key", "a", {}, errors)
|
28
30
|
expect(errors).to be_empty
|
29
31
|
end
|
30
32
|
|
31
|
-
it
|
33
|
+
it "validates long strings" do
|
32
34
|
errors = {}
|
33
|
-
validator.validate(
|
35
|
+
validator.validate("key", "HelloWorld", {}, errors)
|
34
36
|
expect(errors).to be_empty
|
35
37
|
end
|
36
38
|
end
|
37
39
|
|
38
|
-
context
|
39
|
-
it
|
40
|
+
context "invalid alpha strings" do
|
41
|
+
it "rejects non-string values" do
|
40
42
|
errors = {}
|
41
|
-
validator.validate(
|
42
|
-
expect(errors[
|
43
|
+
validator.validate("key", 123, {}, errors)
|
44
|
+
expect(errors["key"]).to eq("must contain only letters")
|
43
45
|
end
|
44
46
|
|
45
|
-
it
|
47
|
+
it "rejects nil values" do
|
46
48
|
errors = {}
|
47
|
-
validator.validate(
|
48
|
-
expect(errors[
|
49
|
+
validator.validate("key", nil, {}, errors)
|
50
|
+
expect(errors["key"]).to eq("must contain only letters")
|
49
51
|
end
|
50
52
|
|
51
|
-
it
|
53
|
+
it "rejects strings with numbers" do
|
52
54
|
errors = {}
|
53
|
-
validator.validate(
|
54
|
-
expect(errors[
|
55
|
+
validator.validate("key", "abc123", {}, errors)
|
56
|
+
expect(errors["key"]).to eq("must contain only letters")
|
55
57
|
end
|
56
58
|
|
57
|
-
it
|
59
|
+
it "rejects strings with spaces" do
|
58
60
|
errors = {}
|
59
|
-
validator.validate(
|
60
|
-
expect(errors[
|
61
|
+
validator.validate("key", "abc def", {}, errors)
|
62
|
+
expect(errors["key"]).to eq("must contain only letters")
|
61
63
|
end
|
62
64
|
|
63
|
-
it
|
65
|
+
it "rejects strings with special characters" do
|
64
66
|
errors = {}
|
65
|
-
validator.validate(
|
66
|
-
expect(errors[
|
67
|
+
validator.validate("key", "abc!", {}, errors)
|
68
|
+
expect(errors["key"]).to eq("must contain only letters")
|
67
69
|
end
|
68
70
|
|
69
|
-
it
|
71
|
+
it "rejects strings with hyphens" do
|
70
72
|
errors = {}
|
71
|
-
validator.validate(
|
72
|
-
expect(errors[
|
73
|
+
validator.validate("key", "abc-def", {}, errors)
|
74
|
+
expect(errors["key"]).to eq("must contain only letters")
|
73
75
|
end
|
74
76
|
|
75
|
-
it
|
77
|
+
it "rejects strings with underscores" do
|
76
78
|
errors = {}
|
77
|
-
validator.validate(
|
78
|
-
expect(errors[
|
79
|
+
validator.validate("key", "abc_def", {}, errors)
|
80
|
+
expect(errors["key"]).to eq("must contain only letters")
|
79
81
|
end
|
80
82
|
|
81
|
-
it
|
83
|
+
it "rejects empty strings" do
|
82
84
|
errors = {}
|
83
|
-
validator.validate(
|
84
|
-
expect(errors[
|
85
|
+
validator.validate("key", "", {}, errors)
|
86
|
+
expect(errors["key"]).to eq("must contain only letters")
|
85
87
|
end
|
86
88
|
|
87
|
-
it
|
89
|
+
it "rejects strings with periods" do
|
88
90
|
errors = {}
|
89
|
-
validator.validate(
|
90
|
-
expect(errors[
|
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
|