hash_validator 0.8.0 → 1.0.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: ddc15dcd023f389504598ac3895671ba65e79b8cc120af48db730c02552919ab
4
+ data.tar.gz: e968308c55c754b88d4a0fb969e75832576f06429f93e29e0b354cf4c1fbd632
5
5
  SHA512:
6
- metadata.gz: 44fc5928ebd086077dac64d7a53e91a3c14b144e9831b07389aa5b086ffdd63f6271659ae3fc3bc695e73d6d06767077147e48748ddcf32bb149b82aa1fdbe97
7
- data.tar.gz: 2589013673495b01ac126c5a250528059e69c5f0610003d9db0ab5c852d3b837a51efd2eb18d22f3b81058a2b07acf292db0c199ad9da637a363ddc5ff171fc1
6
+ metadata.gz: 8251c3d9506f21e5f4fe6c5461ac45002b3bb48227619744734ccb7dd1a6c9ac7e8f443317f94845f93faf31b65695235d083f9a691b2dff7c6c26a9fef7cf79
7
+ data.tar.gz: 972582fdda827e6a086956d38ef908dfff7fbfbe6e0d0c50dd9170d61965c6ee009ef60178aa384288361dc0144f513a1093c12406c2881db108d5d1ec585949
@@ -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.5.0
5
+ - 2.6.3
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.
@@ -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]
@@ -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'
@@ -0,0 +1,21 @@
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 presence_error_message
11
+ 'value from list required'
12
+ end
13
+
14
+ def validate(key, value, klass, errors)
15
+ unless value.is_a?(klass)
16
+ errors[key] = "#{klass} required"
17
+ end
18
+ end
19
+ end
20
+
21
+ HashValidator.append_validator(HashValidator::Validator::ClassValidator.new)
@@ -1,3 +1,3 @@
1
1
  module HashValidator
2
- VERSION = '0.8.0'
2
+ VERSION = '1.0.0'
3
3
  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.0.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: 2017-06-22 00:00:00.000000000 Z
11
+ date: 2019-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -90,6 +90,7 @@ files:
90
90
  - lib/hash_validator/validators.rb
91
91
  - lib/hash_validator/validators/base.rb
92
92
  - lib/hash_validator/validators/boolean_validator.rb
93
+ - lib/hash_validator/validators/class_validator.rb
93
94
  - lib/hash_validator/validators/email_validator.rb
94
95
  - lib/hash_validator/validators/enumerable_validator.rb
95
96
  - lib/hash_validator/validators/hash_validator.rb
@@ -107,6 +108,7 @@ files:
107
108
  - spec/spec_helper.rb
108
109
  - spec/validators/base_spec.rb
109
110
  - spec/validators/boolean_spec.rb
111
+ - spec/validators/class_spec.rb
110
112
  - spec/validators/email_spec.rb
111
113
  - spec/validators/in_enumerable_spec.rb
112
114
  - spec/validators/lambda_spec.rb
@@ -137,8 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
139
  - !ruby/object:Gem::Version
138
140
  version: '0'
139
141
  requirements: []
140
- rubyforge_project:
141
- rubygems_version: 2.5.2
142
+ rubygems_version: 3.0.3
142
143
  signing_key:
143
144
  specification_version: 4
144
145
  summary: Ruby library to validate hashes (Hash) against user-defined requirements
@@ -148,6 +149,7 @@ test_files:
148
149
  - spec/spec_helper.rb
149
150
  - spec/validators/base_spec.rb
150
151
  - spec/validators/boolean_spec.rb
152
+ - spec/validators/class_spec.rb
151
153
  - spec/validators/email_spec.rb
152
154
  - spec/validators/in_enumerable_spec.rb
153
155
  - spec/validators/lambda_spec.rb