hash_validator 0.8.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d3f14e426a06042aa90d83cd7addfc6453f759e4
4
- data.tar.gz: 7fd7178c50fc0491bf276696b2c42b1cca24d3a7
2
+ SHA256:
3
+ metadata.gz: 204abb59a31b60ea1e62d18d370ff1140f6190e83aa35a989f771fcd4986d5d8
4
+ data.tar.gz: 8369e8089adfe5d612bca0596d95594816fc8923216a58a50302ad8cc11a1c51
5
5
  SHA512:
6
- metadata.gz: 44fc5928ebd086077dac64d7a53e91a3c14b144e9831b07389aa5b086ffdd63f6271659ae3fc3bc695e73d6d06767077147e48748ddcf32bb149b82aa1fdbe97
7
- data.tar.gz: 2589013673495b01ac126c5a250528059e69c5f0610003d9db0ab5c852d3b837a51efd2eb18d22f3b81058a2b07acf292db0c199ad9da637a363ddc5ff171fc1
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,7 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 2.0.0
4
- - 2.1.0
5
- - 2.2.0
6
- - 2.3.0
7
- - 2.4.0
4
+ - 2.6.6
5
+ - 2.7.2
data/README.md CHANGED
@@ -1,10 +1,9 @@
1
1
  # Hash Validator
2
2
 
3
- [![Gem](https://img.shields.io/gem/v/hash_validator.svg)]()
3
+ [![Gem](https://img.shields.io/gem/v/hash_validator.svg)](https://rubygems.org/gems/hash_validator)
4
4
  [![Travis](https://travis-ci.org/jamesbrooks/hash_validator.svg)](https://travis-ci.org/jamesbrooks/hash_validator)
5
5
  [![Coveralls](https://img.shields.io/coveralls/jamesbrooks/hash_validator.svg)](https://coveralls.io/r/jamesbrooks/hash_validator)
6
- [![Code Climate](https://img.shields.io/codeclimate/github/JamesBrooks/hash_validator.svg)](https://codeclimate.com/github/JamesBrooks/hash_validator)
7
- [![Gemnasium](https://img.shields.io/gemnasium/jamesbrooks/hash_validator.svg)](https://gemnasium.com/github.com/JamesBrooks/hash_validator)
6
+ [![Maintainability](https://api.codeclimate.com/v1/badges/dc6edc1b240860c5f5d9/maintainability)](https://codeclimate.com/github/JamesBrooks/hash_validator/maintainability)
8
7
 
9
8
  Ruby library to validate hashes (Hash) against user-defined requirements
10
9
 
@@ -28,7 +27,7 @@ Or install it yourself as:
28
27
  # Validations hash
29
28
  validations = {
30
29
  user: {
31
- first_name: 'string',
30
+ first_name: String,
32
31
  last_name: 'string',
33
32
  age: 'numeric',
34
33
  likes: 'array'
@@ -80,6 +79,8 @@ Define a validation hash which will be used to validate. This has can be nested
80
79
  * `required`: just requires any value to be present for the designated key.
81
80
  * hashes are validates by nesting validations, or if just the presence of a hash is required `{}` can be used.
82
81
 
82
+ On top of the pre-defined simple types, classes can be used directly (e.g. String) to validate the presence of a value of a desired class.
83
+
83
84
  Additional validations exist to validate beyond simple typing, such as:
84
85
 
85
86
  * An Enumerable instance: validates that the value is contained within the supplied enumerable.
@@ -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
@@ -6,7 +6,7 @@ class HashValidator::Base
6
6
  self.errors = {}
7
7
  self.hash = hash
8
8
  self.validations = validations
9
-
9
+
10
10
  validate
11
11
  end
12
12
 
@@ -18,12 +18,13 @@ class HashValidator::Base
18
18
  @strict = strict
19
19
  new(hash, validations)
20
20
  end
21
-
21
+
22
22
  def self.strict?
23
23
  @strict
24
24
  end
25
-
26
- private
25
+
26
+
27
+ private
27
28
  def validate
28
29
  HashValidator.validator_for(hash).validate(:base, self.hash, self.validations, self.errors)
29
30
  self.errors = errors[:base]
@@ -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)
@@ -0,0 +1,17 @@
1
+ class HashValidator::Validator::ClassValidator < HashValidator::Validator::Base
2
+ def initialize
3
+ super('_class') # 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
+ rhs.is_a?(Class)
8
+ end
9
+
10
+ def validate(key, value, klass, errors)
11
+ unless value.is_a?(klass)
12
+ errors[key] = "#{klass} required"
13
+ end
14
+ end
15
+ end
16
+
17
+ HashValidator.append_validator(HashValidator::Validator::ClassValidator.new)
@@ -25,6 +25,7 @@ end
25
25
  # Load validators
26
26
  require 'hash_validator/validators/base'
27
27
  require 'hash_validator/validators/simple_validator'
28
+ require 'hash_validator/validators/class_validator'
28
29
  require 'hash_validator/validators/hash_validator'
29
30
  require 'hash_validator/validators/presence_validator'
30
31
  require 'hash_validator/validators/simple_type_validators'
@@ -36,3 +37,4 @@ require 'hash_validator/validators/lambda_validator'
36
37
  require 'hash_validator/validators/optional_validator'
37
38
  require 'hash_validator/validators/many_validator'
38
39
  require 'hash_validator/validators/multiple_validator'
40
+ require 'hash_validator/validators/array_validator'
@@ -1,3 +1,3 @@
1
1
  module HashValidator
2
- VERSION = '0.8.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
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Class validator' do
4
+ let(:errors) { Hash.new }
5
+
6
+ {
7
+ Array => {
8
+ valid: [ [], [1], ['foo'], [1,['foo'],Time.now] ],
9
+ invalid: [ nil, '', 123, '123', Time.now, '[1]' ]
10
+ },
11
+ 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
+ },
15
+ Float => {
16
+ valid: [ 0.0, 1.1, 1.23, Float::INFINITY, Float::EPSILON ],
17
+ invalid: [ nil, '', 0, 123, '123', Time.now, '[1]', '2013-03-04' ]
18
+ },
19
+ Integer => {
20
+ valid: [ 0, -1000000, 1000000 ],
21
+ invalid: [ nil, '', 1.1, '123', Time.now, '[1]', '2013-03-04' ]
22
+ },
23
+ Numeric => {
24
+ valid: [ 0, 123, 123.45 ],
25
+ invalid: [ nil, '', '123', Time.now ]
26
+ },
27
+ Range => {
28
+ valid: [ 0..10, 'a'..'z', 5..0 ],
29
+ invalid: [ nil, '', '123', Time.now ]
30
+ },
31
+ Rational => {
32
+ valid: [ Rational(1), Rational(2, 3), 3.to_r ],
33
+ invalid: [ nil, '', 123, '123', Time.now, '[1]', [1], Complex(2, 3) ]
34
+ },
35
+ Regexp => {
36
+ valid: [ /[a-z]+/, //, //i, Regexp.new('.*') ],
37
+ invalid: [ nil, '', 123, '123', Time.now, '.*' ]
38
+ },
39
+ String => {
40
+ valid: [ '', 'Hello World', '12345' ],
41
+ invalid: [ nil, 12345, Time.now ]
42
+ },
43
+ Symbol => {
44
+ valid: [ :foo, :'', 'bar'.to_sym ],
45
+ invalid: [ nil, '', 1.1, '123', Time.now, '[1]', '2013-03-04' ]
46
+ },
47
+ Time => {
48
+ valid: [ Time.now ],
49
+ invalid: [ nil, '', 123, '123', "#{Time.now}" ]
50
+ }
51
+ }.each do |type, data|
52
+ describe type do
53
+ data[:valid].each do |value|
54
+ it "validates '#{value}' successful" do
55
+ validator = HashValidator.validate({ v: value }, { v: type })
56
+
57
+ expect(validator.valid?).to eq true
58
+ expect(validator.errors).to be_empty
59
+ end
60
+ end
61
+
62
+ data[:invalid].each do |value|
63
+ it "validates '#{value}' with failure" do
64
+ validator = HashValidator.validate({ v: value }, { v: type })
65
+
66
+ expect(validator.valid?).to eq false
67
+ expect(validator.errors).to eq({ v: "#{type} required" })
68
+ end
69
+ end
70
+ end
71
+ end
72
+ 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: 0.8.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: 2017-06-22 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,8 +89,10 @@ 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
95
+ - lib/hash_validator/validators/class_validator.rb
93
96
  - lib/hash_validator/validators/email_validator.rb
94
97
  - lib/hash_validator/validators/enumerable_validator.rb
95
98
  - lib/hash_validator/validators/hash_validator.rb
@@ -105,8 +108,10 @@ files:
105
108
  - spec/hash_validator_spec.rb
106
109
  - spec/hash_validator_spec_helper.rb
107
110
  - spec/spec_helper.rb
111
+ - spec/validators/array_spec.rb
108
112
  - spec/validators/base_spec.rb
109
113
  - spec/validators/boolean_spec.rb
114
+ - spec/validators/class_spec.rb
110
115
  - spec/validators/email_spec.rb
111
116
  - spec/validators/in_enumerable_spec.rb
112
117
  - spec/validators/lambda_spec.rb
@@ -122,7 +127,7 @@ homepage: https://github.com/JamesBrooks/hash_validator
122
127
  licenses:
123
128
  - MIT
124
129
  metadata: {}
125
- post_install_message:
130
+ post_install_message:
126
131
  rdoc_options: []
127
132
  require_paths:
128
133
  - lib
@@ -137,17 +142,18 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
142
  - !ruby/object:Gem::Version
138
143
  version: '0'
139
144
  requirements: []
140
- rubyforge_project:
141
- rubygems_version: 2.5.2
142
- signing_key:
145
+ rubygems_version: 3.1.2
146
+ signing_key:
143
147
  specification_version: 4
144
148
  summary: Ruby library to validate hashes (Hash) against user-defined requirements
145
149
  test_files:
146
150
  - spec/hash_validator_spec.rb
147
151
  - spec/hash_validator_spec_helper.rb
148
152
  - spec/spec_helper.rb
153
+ - spec/validators/array_spec.rb
149
154
  - spec/validators/base_spec.rb
150
155
  - spec/validators/boolean_spec.rb
156
+ - spec/validators/class_spec.rb
151
157
  - spec/validators/email_spec.rb
152
158
  - spec/validators/in_enumerable_spec.rb
153
159
  - spec/validators/lambda_spec.rb