hash_validator 0.5.0 → 0.6.0
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/.travis.yml +3 -0
- data/README.md +1 -1
- data/lib/hash_validator/validators/base.rb +5 -5
- data/lib/hash_validator/version.rb +1 -1
- data/spec/hash_validator_spec.rb +16 -6
- data/spec/validators/base_spec.rb +2 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf1be444ed797f5eb29cf69a168d6954c06ecfe9
|
4
|
+
data.tar.gz: 4bd3fc6857245aacb1f60e046e5fbb200ab0c5ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dfd2fb4cd260de7e6c5af4c303fef12cc6787707cb2048317a790acdbbebbbbc7f9e8e9ada6603c99b540ceeb995a5f7141b933e0a030c1bead07f5e1d1d2793
|
7
|
+
data.tar.gz: 0fe4873c5a5fd271ed2fc79d28db3fa90523d1f835845932096faaa23ded1cda2c9f9d301cf4e2d7efca13b87bb090f8d2d69f90e64d1783dbb57fc2a1991c58
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
[](https://travis-ci.org/jamesbrooks/hash_validator)
|
5
5
|
[](https://coveralls.io/r/jamesbrooks/hash_validator)
|
6
6
|
[](https://codeclimate.com/github/JamesBrooks/hash_validator)
|
7
|
-
[](https://gemnasium.com/github.com/JamesBrooks/hash_validator)
|
8
8
|
|
9
9
|
Ruby library to validate hashes (Hash) against user-defined requirements
|
10
10
|
|
@@ -3,15 +3,15 @@ class HashValidator::Validator::Base
|
|
3
3
|
|
4
4
|
|
5
5
|
def initialize(name)
|
6
|
-
|
7
|
-
raise StandardError.new('Validator must be initialized with a valid name (string with length greater than zero)')
|
8
|
-
end
|
6
|
+
self.name = name.to_s
|
9
7
|
|
10
|
-
self.name
|
8
|
+
unless self.name.size > 0
|
9
|
+
raise StandardError.new('Validator must be initialized with a valid name (length greater than zero)')
|
10
|
+
end
|
11
11
|
end
|
12
12
|
|
13
13
|
def should_validate?(name)
|
14
|
-
self.name == name
|
14
|
+
self.name == name.to_s
|
15
15
|
end
|
16
16
|
|
17
17
|
def presence_error_message
|
data/spec/hash_validator_spec.rb
CHANGED
@@ -71,6 +71,16 @@ describe HashValidator do
|
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
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
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should allow symbols as validator names' do
|
80
|
+
expect(validate({ v: 'test' }, { v: :string }).valid?).to eq true
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
74
84
|
describe 'full validations' do
|
75
85
|
let(:empty_hash) {{}}
|
76
86
|
|
@@ -226,7 +236,7 @@ end
|
|
226
236
|
|
227
237
|
describe 'Strict Validation' do
|
228
238
|
let(:simple_hash) { { foo: 'bar', bar: 'foo' } }
|
229
|
-
|
239
|
+
|
230
240
|
let(:complex_hash) {{
|
231
241
|
foo: 1,
|
232
242
|
user: {
|
@@ -236,26 +246,26 @@ describe 'Strict Validation' do
|
|
236
246
|
likes: [ 'Ruby', 'Kendo', 'Board Games' ]
|
237
247
|
}
|
238
248
|
}}
|
239
|
-
|
249
|
+
|
240
250
|
let(:validations) { { foo: 'string' } }
|
241
|
-
|
251
|
+
|
242
252
|
let(:complex_validations) {{
|
243
253
|
foo: 'integer',
|
244
254
|
user: {
|
245
255
|
first_name: 'string', age: 'integer'
|
246
256
|
}
|
247
257
|
}}
|
248
|
-
|
258
|
+
|
249
259
|
it 'reports which keys are not expected for a simple hash' do
|
250
260
|
v = validate(simple_hash, validations, true)
|
251
261
|
expect(v.valid?).to eq false
|
252
262
|
expect(v.errors).to eq({ bar: 'key not expected' })
|
253
263
|
end
|
254
|
-
|
264
|
+
|
255
265
|
it 'reports which keys are not expected for a complex hash' do
|
256
266
|
v = validate(complex_hash, complex_validations, true)
|
257
267
|
expect(v.valid?).to eq false
|
258
268
|
expect(v.errors).to eq(user: { last_name: 'key not expected', likes: 'key not expected' })
|
259
269
|
end
|
260
|
-
|
270
|
+
|
261
271
|
end
|
@@ -9,9 +9,8 @@ describe HashValidator::Validator::Base do
|
|
9
9
|
end
|
10
10
|
|
11
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 (
|
13
|
-
expect { HashValidator::Validator::Base.new(
|
14
|
-
expect { HashValidator::Validator::Base.new('') }.to raise_error(StandardError, 'Validator must be initialized with a valid name (string with length greater than zero)')
|
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)')
|
15
14
|
end
|
16
15
|
|
17
16
|
describe '#validate' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Brooks
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|