hash_validator 1.0.0 → 1.1.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 +27 -0
- data/.ruby-version +1 -0
- data/README.md +1 -2
- data/hash_validator.gemspec +7 -7
- data/lib/hash_validator/validators/array_validator.rb +80 -0
- data/lib/hash_validator/validators/class_validator.rb +0 -4
- data/lib/hash_validator/validators.rb +1 -0
- data/lib/hash_validator/version.rb +1 -1
- data/spec/spec_helper.rb +1 -4
- data/spec/validators/array_spec.rb +181 -0
- metadata +16 -26
- data/.travis.yml +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4074014e1480acf0709f7a6c5c4f444e7a9b740bdb58eb04345fc930e1528f90
|
4
|
+
data.tar.gz: 196e0be47b751403869cf47a48f946e63bfd949d88fda63f4f04e57327b1541e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6f121a0d98d4c6e9c825290951cec72985e169a5fd9ef8e69fda5216554a1ae17405a397a0c82041677ae96881c24104fcdf04dc76f9ccf91647207766e739ea
|
7
|
+
data.tar.gz: 67b9c1941c04c075c247496d9b69c509c272eaa2f5b633af74fb94e1c9b135149cf76cd8f6a344ad41424804ff224662f353fd65070cc157517a19d4f70ca049
|
@@ -0,0 +1,27 @@
|
|
1
|
+
name: Test
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ "master" ]
|
6
|
+
pull_request:
|
7
|
+
branches: [ "master" ]
|
8
|
+
|
9
|
+
permissions:
|
10
|
+
contents: read
|
11
|
+
|
12
|
+
jobs:
|
13
|
+
spec:
|
14
|
+
name: "RSpec / Ruby ${{ matrix.ruby }}"
|
15
|
+
runs-on: ubuntu-24.04
|
16
|
+
strategy:
|
17
|
+
fail-fast: false
|
18
|
+
matrix:
|
19
|
+
ruby: ["2.3", "2.4", "2.5", "2.6", "2.7", "3.0", "3.1", "3.2", "3.3"]
|
20
|
+
steps:
|
21
|
+
- run: sudo apt-get install libcurl4-openssl-dev
|
22
|
+
- uses: actions/checkout@v4
|
23
|
+
- uses: ruby/setup-ruby@v1
|
24
|
+
with:
|
25
|
+
ruby-version: ${{ matrix.ruby }}
|
26
|
+
bundler-cache: true
|
27
|
+
- run: bundle exec rake spec
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.7.1
|
data/README.md
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
# Hash Validator
|
2
2
|
|
3
3
|
[](https://rubygems.org/gems/hash_validator)
|
4
|
-
|
5
|
-
[](https://coveralls.io/r/jamesbrooks/hash_validator)
|
4
|
+

|
6
5
|
[](https://codeclimate.com/github/JamesBrooks/hash_validator/maintainability)
|
7
6
|
|
8
7
|
Ruby library to validate hashes (Hash) against user-defined requirements
|
data/hash_validator.gemspec
CHANGED
@@ -13,13 +13,13 @@ Gem::Specification.new do |spec|
|
|
13
13
|
spec.homepage = "https://github.com/JamesBrooks/hash_validator"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
|
-
spec.
|
17
|
-
spec.
|
18
|
-
spec.
|
19
|
-
spec.
|
16
|
+
spec.required_ruby_version = ">= 2.0.0"
|
17
|
+
spec.files = `git ls-files`.split($/)
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
20
21
|
|
21
|
-
spec.add_development_dependency 'bundler', '~> 1
|
22
|
+
spec.add_development_dependency 'bundler', '~> 2.1'
|
22
23
|
spec.add_development_dependency 'rake'
|
23
|
-
spec.add_development_dependency 'rspec', '~> 3.
|
24
|
-
spec.add_development_dependency 'coveralls'
|
24
|
+
spec.add_development_dependency 'rspec', '~> 3.10'
|
25
25
|
end
|
@@ -0,0 +1,80 @@
|
|
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
|
+
|
50
|
+
size_spec_present = case size_spec
|
51
|
+
when String
|
52
|
+
!object.strip.empty?
|
53
|
+
when NilClass
|
54
|
+
false
|
55
|
+
when Numeric
|
56
|
+
true
|
57
|
+
when Array, Hash
|
58
|
+
!object.empty?
|
59
|
+
else
|
60
|
+
!!object
|
61
|
+
end
|
62
|
+
|
63
|
+
if size_spec_present
|
64
|
+
unless value.size == size_spec
|
65
|
+
errors[key] = "The required size of array is #{size_spec} but is #{value.size}."
|
66
|
+
return
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
allowed_keys = [:size]
|
71
|
+
wrong_keys = array_spec.keys - allowed_keys
|
72
|
+
|
73
|
+
return if wrong_keys.size < 1
|
74
|
+
|
75
|
+
errors[key] = "Not supported specification for array: #{wrong_keys.sort.join(", ")}."
|
76
|
+
return
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
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'
|
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,3 @@
|
|
1
|
-
require 'coveralls'
|
2
|
-
Coveralls.wear!
|
3
|
-
|
4
1
|
require 'rubygems'
|
5
2
|
require 'hash_validator'
|
6
3
|
require 'hash_validator_spec_helper'
|
@@ -9,6 +6,6 @@ RSpec.configure do |config|
|
|
9
6
|
config.run_all_when_everything_filtered = true
|
10
7
|
config.filter_run :focus
|
11
8
|
config.order = 'random'
|
12
|
-
|
9
|
+
|
13
10
|
config.include HashValidatorSpecHelper
|
14
11
|
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.
|
4
|
+
version: 1.1.1
|
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:
|
11
|
+
date: 2025-01-14 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
|
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
|
26
|
+
version: '2.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -44,28 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 3.
|
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.
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: coveralls
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
54
|
+
version: '3.10'
|
69
55
|
description: Ruby library to validate hashes (Hash) against user-defined requirements
|
70
56
|
email:
|
71
57
|
- james@gooddogdesign.com
|
@@ -73,9 +59,10 @@ executables: []
|
|
73
59
|
extensions: []
|
74
60
|
extra_rdoc_files: []
|
75
61
|
files:
|
62
|
+
- ".github/workflows/ruby.yml"
|
76
63
|
- ".gitignore"
|
77
64
|
- ".rspec"
|
78
|
-
- ".
|
65
|
+
- ".ruby-version"
|
79
66
|
- Gemfile
|
80
67
|
- LICENSE.txt
|
81
68
|
- README.md
|
@@ -88,6 +75,7 @@ files:
|
|
88
75
|
- lib/hash_validator/validations/multiple.rb
|
89
76
|
- lib/hash_validator/validations/optional.rb
|
90
77
|
- lib/hash_validator/validators.rb
|
78
|
+
- lib/hash_validator/validators/array_validator.rb
|
91
79
|
- lib/hash_validator/validators/base.rb
|
92
80
|
- lib/hash_validator/validators/boolean_validator.rb
|
93
81
|
- lib/hash_validator/validators/class_validator.rb
|
@@ -106,6 +94,7 @@ files:
|
|
106
94
|
- spec/hash_validator_spec.rb
|
107
95
|
- spec/hash_validator_spec_helper.rb
|
108
96
|
- spec/spec_helper.rb
|
97
|
+
- spec/validators/array_spec.rb
|
109
98
|
- spec/validators/base_spec.rb
|
110
99
|
- spec/validators/boolean_spec.rb
|
111
100
|
- spec/validators/class_spec.rb
|
@@ -124,7 +113,7 @@ homepage: https://github.com/JamesBrooks/hash_validator
|
|
124
113
|
licenses:
|
125
114
|
- MIT
|
126
115
|
metadata: {}
|
127
|
-
post_install_message:
|
116
|
+
post_install_message:
|
128
117
|
rdoc_options: []
|
129
118
|
require_paths:
|
130
119
|
- lib
|
@@ -132,21 +121,22 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
132
121
|
requirements:
|
133
122
|
- - ">="
|
134
123
|
- !ruby/object:Gem::Version
|
135
|
-
version:
|
124
|
+
version: 2.0.0
|
136
125
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
126
|
requirements:
|
138
127
|
- - ">="
|
139
128
|
- !ruby/object:Gem::Version
|
140
129
|
version: '0'
|
141
130
|
requirements: []
|
142
|
-
rubygems_version: 3.
|
143
|
-
signing_key:
|
131
|
+
rubygems_version: 3.5.23
|
132
|
+
signing_key:
|
144
133
|
specification_version: 4
|
145
134
|
summary: Ruby library to validate hashes (Hash) against user-defined requirements
|
146
135
|
test_files:
|
147
136
|
- spec/hash_validator_spec.rb
|
148
137
|
- spec/hash_validator_spec_helper.rb
|
149
138
|
- spec/spec_helper.rb
|
139
|
+
- spec/validators/array_spec.rb
|
150
140
|
- spec/validators/base_spec.rb
|
151
141
|
- spec/validators/boolean_spec.rb
|
152
142
|
- spec/validators/class_spec.rb
|