hash_validator 1.0.0 → 1.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ddc15dcd023f389504598ac3895671ba65e79b8cc120af48db730c02552919ab
4
- data.tar.gz: e968308c55c754b88d4a0fb969e75832576f06429f93e29e0b354cf4c1fbd632
3
+ metadata.gz: 204abb59a31b60ea1e62d18d370ff1140f6190e83aa35a989f771fcd4986d5d8
4
+ data.tar.gz: 8369e8089adfe5d612bca0596d95594816fc8923216a58a50302ad8cc11a1c51
5
5
  SHA512:
6
- metadata.gz: 8251c3d9506f21e5f4fe6c5461ac45002b3bb48227619744734ccb7dd1a6c9ac7e8f443317f94845f93faf31b65695235d083f9a691b2dff7c6c26a9fef7cf79
7
- data.tar.gz: 972582fdda827e6a086956d38ef908dfff7fbfbe6e0d0c50dd9170d61965c6ee009ef60178aa384288361dc0144f513a1093c12406c2881db108d5d1ec585949
6
+ metadata.gz: 0663b18d2799515a852b71ef70c69f8153d80c521afbb5fd88904ce8b6d3502fa903c4cb598251eed41e0a287fd69915de535ab49f71c11b04827073625b2e37
7
+ data.tar.gz: 23d774f32abb66ad78a6bf63e26dc7533935884792146f6d29cfff50adf7517a8fe5d8d3b20922c4f23c45767a87de42dba2ca9cc56b14477d1302ec5f63ba62
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.1
data/.travis.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 2.0.0
4
- - 2.5.0
5
- - 2.6.3
4
+ - 2.6.6
5
+ - 2.7.2
@@ -18,8 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency 'bundler', '~> 1.3'
21
+ spec.add_development_dependency 'bundler', '~> 2.1'
22
22
  spec.add_development_dependency 'rake'
23
- spec.add_development_dependency 'rspec', '~> 3.5.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.10'
24
24
  spec.add_development_dependency 'coveralls'
25
25
  end
@@ -0,0 +1,66 @@
1
+ class HashValidator::Validator::ArrayValidator < HashValidator::Validator::Base
2
+ def initialize
3
+ super('__array__') # The name of the validator, underscored as it won't usually be directly invoked (invoked through use of validator)
4
+ end
5
+
6
+ def should_validate?(rhs)
7
+ return false unless rhs.is_a?(Array)
8
+ return false unless rhs.size > 0
9
+ return false unless rhs[0] == :array
10
+
11
+ return true
12
+ end
13
+
14
+ def validate(key, value, specification, errors)
15
+ # the first item in specification is always ":array"
16
+ unless specification[0] == :array
17
+ errors[key] = "Wrong array specification. The #{:array} is expected as first item."
18
+ return
19
+ end
20
+
21
+ if specification.size > 2
22
+ errors[key] = "Wrong size of array specification. Allowed is one or two items."
23
+ return
24
+ end
25
+
26
+ unless value.is_a?(Array)
27
+ errors[key] = "#{Array} required"
28
+ return
29
+ end
30
+
31
+ # second item is optional
32
+ return if specification.size < 2
33
+
34
+ array_spec = specification[1]
35
+ return if array_spec.nil? # array specification is optional
36
+
37
+ if array_spec.is_a?(Numeric)
38
+ array_spec = { size: array_spec }
39
+ end
40
+
41
+ unless array_spec.is_a?(Hash)
42
+ errors[key] = "Second item of array specification must be #{Hash} or #{Numeric}."
43
+ return
44
+ end
45
+
46
+ return if array_spec.empty?
47
+
48
+ size_spec = array_spec[:size]
49
+ if size_spec.present?
50
+ unless value.size == size_spec
51
+ errors[key] = "The required size of array is #{size_spec} but is #{value.size}."
52
+ return
53
+ end
54
+ end
55
+
56
+ allowed_keys = [:size]
57
+ wrong_keys = array_spec.keys - allowed_keys
58
+
59
+ return if wrong_keys.size < 1
60
+
61
+ errors[key] = "Not supported specification for array: #{wrong_keys.sort.join(", ")}."
62
+ return
63
+ end
64
+ end
65
+
66
+ HashValidator.append_validator(HashValidator::Validator::ArrayValidator.new)
@@ -7,10 +7,6 @@ class HashValidator::Validator::ClassValidator < HashValidator::Validator::Base
7
7
  rhs.is_a?(Class)
8
8
  end
9
9
 
10
- def presence_error_message
11
- 'value from list required'
12
- end
13
-
14
10
  def validate(key, value, klass, errors)
15
11
  unless value.is_a?(klass)
16
12
  errors[key] = "#{klass} required"
@@ -37,3 +37,4 @@ require 'hash_validator/validators/lambda_validator'
37
37
  require 'hash_validator/validators/optional_validator'
38
38
  require 'hash_validator/validators/many_validator'
39
39
  require 'hash_validator/validators/multiple_validator'
40
+ require 'hash_validator/validators/array_validator'
@@ -1,3 +1,3 @@
1
1
  module HashValidator
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -0,0 +1,181 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Array validator' do
4
+ let(:validator) { HashValidator::Validator::ArrayValidator.new }
5
+ let(:errors) { Hash.new }
6
+
7
+ describe '#should_validate?' do
8
+ it 'should validate the array with first item ":array"' do
9
+ expect(validator.should_validate?([:array])).to eq true
10
+ end
11
+
12
+ it 'should validate the array with empty specification' do
13
+ expect(validator.should_validate?([:array, { }])).to eq true
14
+ end
15
+
16
+ it 'should validate the array with size specified to nil' do
17
+ expect(validator.should_validate?([:array, { size: nil }])).to eq true
18
+ end
19
+
20
+ it 'should validate the array with non-sense specification' do
21
+ expect(validator.should_validate?([:array, { blah_blah_blah: false }])).to eq true
22
+ end
23
+
24
+ it 'should not validate the empty array' do
25
+ expect(validator.should_validate?([])).to eq false
26
+ end
27
+
28
+ it 'should not validate the array with nil item' do
29
+ expect(validator.should_validate?([nil])).to eq false
30
+ end
31
+
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
35
+ expect(validator.should_validate?(nil)).to eq false
36
+ end
37
+ end
38
+
39
+ describe '#validate' do
40
+ it 'should validate an empty array with true' do
41
+ validator.validate(:key, [], [:array], errors)
42
+
43
+ expect(errors).to be_empty
44
+ end
45
+
46
+ it 'should validate an empty array with nil spec' do
47
+ validator.validate(:key, [], [:array, nil], errors)
48
+
49
+ expect(errors).to be_empty
50
+ end
51
+
52
+ it 'should validate an empty array with empty spec' do
53
+ validator.validate(:key, [], [:array, { }], errors)
54
+
55
+ expect(errors).to be_empty
56
+ end
57
+
58
+ it 'should validate an empty array with size spec = nil' do
59
+ validator.validate(:key, [], [:array, { size: nil }], errors)
60
+
61
+ expect(errors).to be_empty
62
+ end
63
+
64
+ it 'should validate an empty array with size spec = 0' do
65
+ validator.validate(:key, [], [:array, { size: 0 }], errors)
66
+
67
+ expect(errors).to be_empty
68
+ end
69
+
70
+ it 'should validate an empty array with spec = 0' do
71
+ validator.validate(:key, [], [:array, 0], errors)
72
+
73
+ expect(errors).to be_empty
74
+ end
75
+
76
+ it 'should validate an empty array with spec = 0.0' do
77
+ validator.validate(:key, [], [:array, 0.0], errors)
78
+
79
+ expect(errors).to be_empty
80
+ end
81
+
82
+ it 'should validate an array of one item with spec = 1' do
83
+ validator.validate(:key, [nil], [:array, 1], errors)
84
+
85
+ expect(errors).to be_empty
86
+ end
87
+
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)
91
+
92
+ expect(errors).to be_empty
93
+ end
94
+
95
+ # >>> NOT >>>
96
+
97
+ it 'should not validate non array value' do
98
+ validator.validate(:key, "I'm not array", [:array], errors)
99
+
100
+ expect(errors).not_to be_empty
101
+ expect(errors).to eq({ key: 'Array required' })
102
+ end
103
+
104
+ it 'should not validate an empty array with size spec = 1' do
105
+ validator.validate(:key, [], [:array, { size: 1 }], errors)
106
+
107
+ expect(errors).not_to be_empty
108
+ expect(errors).to eq({ key: 'The required size of array is 1 but is 0.' })
109
+ end
110
+
111
+ it 'should not validate an empty array with size spec = 1' do
112
+ validator.validate(:key, [], [:array, { size: 1 }], errors)
113
+
114
+ expect(errors).not_to be_empty
115
+ expect(errors).to eq({ key: 'The required size of array is 1 but is 0.' })
116
+ end
117
+
118
+ it 'should not validate an empty array with spec = 1' do
119
+ validator.validate(:key, [], [:array, 1], errors)
120
+
121
+ expect(errors).not_to be_empty
122
+ expect(errors).to eq({ key: 'The required size of array is 1 but is 0.' })
123
+ end
124
+
125
+ it 'should not validate an empty array with spec = "0" (string)' do
126
+ validator.validate(:key, [], [:array, "0"], errors)
127
+
128
+ expect(errors).not_to be_empty
129
+ expect(errors).to eq({ key: 'Second item of array specification must be Hash or Numeric.' })
130
+ end
131
+
132
+ it 'should not validate an empty array with spec = "1" (string)' do
133
+ validator.validate(:key, [], [:array, "1"], errors)
134
+
135
+ expect(errors).not_to be_empty
136
+ expect(errors).to eq({ key: 'Second item of array specification must be Hash or Numeric.' })
137
+ end
138
+
139
+ it 'should not validate an empty array with {min_size: 0} spec' do
140
+ validator.validate(:key, [], [:array, { min_size: 0 }], errors)
141
+
142
+ expect(errors).not_to be_empty
143
+ expect(errors).to eq({ key: 'Not supported specification for array: min_size.' })
144
+ end
145
+
146
+ it 'should not validate an empty array with {min_size: 0, max_size: 2} spec' do
147
+ validator.validate(:key, [], [:array, { min_size: 0, max_size: 2 }], errors)
148
+
149
+ expect(errors).not_to be_empty
150
+ expect(errors).to eq({ key: 'Not supported specification for array: max_size, min_size.' })
151
+ end
152
+
153
+ it 'should not validate an array of four items with {size: 3} spec' do
154
+ validator.validate(:key, [0, 1, 2, 3], [:array, { size: 3 }], errors)
155
+
156
+ expect(errors).not_to be_empty
157
+ expect(errors).to eq({ key: 'The required size of array is 3 but is 4.' })
158
+ end
159
+
160
+ it 'should not validate an array of four items with {size: 5} spec' do
161
+ validator.validate(:key, [0, 1, 2, 3], [:array, { size: 5 }], errors)
162
+
163
+ expect(errors).not_to be_empty
164
+ expect(errors).to eq({ key: 'The required size of array is 5 but is 4.' })
165
+ end
166
+
167
+ it 'should not validate an empty array with invalid specification' do
168
+ validator.validate(:key, [], [:blah], errors)
169
+
170
+ expect(errors).not_to be_empty
171
+ expect(errors).to eq({ key: 'Wrong array specification. The array is expected as first item.' })
172
+ end
173
+
174
+ it 'should not validate an empty array with to large specification' do
175
+ validator.validate(:key, [], [:array, 0, "overlaping item"], errors)
176
+
177
+ expect(errors).not_to be_empty
178
+ expect(errors).to eq({ key: 'Wrong size of array specification. Allowed is one or two items.' })
179
+ end
180
+ end
181
+ end
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: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Brooks
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-18 00:00:00.000000000 Z
11
+ date: 2022-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.3'
19
+ version: '2.1'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.3'
26
+ version: '2.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 3.5.0
47
+ version: '3.10'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 3.5.0
54
+ version: '3.10'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: coveralls
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -75,6 +75,7 @@ extra_rdoc_files: []
75
75
  files:
76
76
  - ".gitignore"
77
77
  - ".rspec"
78
+ - ".ruby-version"
78
79
  - ".travis.yml"
79
80
  - Gemfile
80
81
  - LICENSE.txt
@@ -88,6 +89,7 @@ files:
88
89
  - lib/hash_validator/validations/multiple.rb
89
90
  - lib/hash_validator/validations/optional.rb
90
91
  - lib/hash_validator/validators.rb
92
+ - lib/hash_validator/validators/array_validator.rb
91
93
  - lib/hash_validator/validators/base.rb
92
94
  - lib/hash_validator/validators/boolean_validator.rb
93
95
  - lib/hash_validator/validators/class_validator.rb
@@ -106,6 +108,7 @@ files:
106
108
  - spec/hash_validator_spec.rb
107
109
  - spec/hash_validator_spec_helper.rb
108
110
  - spec/spec_helper.rb
111
+ - spec/validators/array_spec.rb
109
112
  - spec/validators/base_spec.rb
110
113
  - spec/validators/boolean_spec.rb
111
114
  - spec/validators/class_spec.rb
@@ -124,7 +127,7 @@ homepage: https://github.com/JamesBrooks/hash_validator
124
127
  licenses:
125
128
  - MIT
126
129
  metadata: {}
127
- post_install_message:
130
+ post_install_message:
128
131
  rdoc_options: []
129
132
  require_paths:
130
133
  - lib
@@ -139,14 +142,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
142
  - !ruby/object:Gem::Version
140
143
  version: '0'
141
144
  requirements: []
142
- rubygems_version: 3.0.3
143
- signing_key:
145
+ rubygems_version: 3.1.2
146
+ signing_key:
144
147
  specification_version: 4
145
148
  summary: Ruby library to validate hashes (Hash) against user-defined requirements
146
149
  test_files:
147
150
  - spec/hash_validator_spec.rb
148
151
  - spec/hash_validator_spec_helper.rb
149
152
  - spec/spec_helper.rb
153
+ - spec/validators/array_spec.rb
150
154
  - spec/validators/base_spec.rb
151
155
  - spec/validators/boolean_spec.rb
152
156
  - spec/validators/class_spec.rb