hval 0.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 +7 -0
- data/.gitignore +11 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +40 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/hval.gemspec +25 -0
- data/lib/hval.rb +9 -0
- data/lib/hval/result.rb +35 -0
- data/lib/hval/schema.rb +26 -0
- data/lib/hval/validator.rb +41 -0
- data/lib/hval/validators.rb +186 -0
- data/lib/hval/version.rb +3 -0
- metadata +102 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: b83187f62263478bc58819593b0e7a1a380c7a74
|
|
4
|
+
data.tar.gz: 60ec151b5e44e23273175a854dad4a45f4128800
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 90d3b069dfc9aa4989409ed2f29d5d3cbf68e0b73a2b902092c48e0fffb7b2bc54b420b3372b7db572dfd9a15fedae3cfadbbdea4b9a51d862a527a26931326f
|
|
7
|
+
data.tar.gz: fe1b6024f4f0d11f89dbde205e03714c6aaa1f24dd414770c860508185caf91c97c53d6f0264251a2ed4c78b9a2fa36dff9513658728e36a48c0fef6d5b893da
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at juan.repetti@tecnocratica.net. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: http://contributor-covenant.org
|
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Juan Repetti
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Hval 🐳
|
|
2
|
+
|
|
3
|
+
Hval is a Hash Validation gem.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Not available in rubygems (yet) so you can clone the repo and build the gem from source.
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
$ gem build hval.gemspec
|
|
11
|
+
$ gem install hval-0.1.0.gem
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```ruby
|
|
17
|
+
schema = {name: {type?: String, format?: /brush/}, age: {type?: Integer }
|
|
18
|
+
validator = Hval::Validator.new(schema)
|
|
19
|
+
validator.call({name: "guybrush", age: 45})
|
|
20
|
+
validator.valid?
|
|
21
|
+
=> true
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
## Development
|
|
26
|
+
|
|
27
|
+
Hval is swimming under water, and will suffer a lot of changes. The gem approach is very naive and not fully tested, so if you
|
|
28
|
+
use it be carefull. Critics, fixes, suggestions are more than welcome.
|
|
29
|
+
|
|
30
|
+
## Todo
|
|
31
|
+
|
|
32
|
+
* Return errors with messages and in a friendly format. Now, the return of the #call method return information about the result of the validation.
|
|
33
|
+
|
|
34
|
+
## License
|
|
35
|
+
|
|
36
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
|
37
|
+
|
|
38
|
+
## Code of Conduct
|
|
39
|
+
|
|
40
|
+
Everyone interacting in the Hval project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/jmrepetti/hval/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "hval"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/hval.gemspec
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "hval/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "hval"
|
|
8
|
+
spec.version = Hval::VERSION
|
|
9
|
+
spec.authors = ["Juan Repetti"]
|
|
10
|
+
spec.email = ["jmrepetti@github.com"]
|
|
11
|
+
spec.summary = %q{Hval 🐳 is hash validation gem }
|
|
12
|
+
spec.homepage = "http://github.com/jmrepetti/hval"
|
|
13
|
+
spec.license = "MIT"
|
|
14
|
+
|
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
|
17
|
+
end
|
|
18
|
+
spec.bindir = "exe"
|
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
20
|
+
spec.require_paths = ["lib"]
|
|
21
|
+
|
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
24
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
|
25
|
+
end
|
data/lib/hval.rb
ADDED
data/lib/hval/result.rb
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module Hval
|
|
2
|
+
class Result
|
|
3
|
+
|
|
4
|
+
def initialize(result)
|
|
5
|
+
@success = reduce_to_boolean(result)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def success?
|
|
9
|
+
@success
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
private
|
|
13
|
+
def reduce_to_boolean(res)
|
|
14
|
+
case res
|
|
15
|
+
when TrueClass, FalseClass
|
|
16
|
+
return res
|
|
17
|
+
when Array
|
|
18
|
+
res.all? do |result|
|
|
19
|
+
if result.size == 2 #[key, [results]]
|
|
20
|
+
_, results = result
|
|
21
|
+
reduce_to_boolean(results)
|
|
22
|
+
elsif result.size == 4 #[name, spec, result, given]
|
|
23
|
+
_, _, results, _ = result
|
|
24
|
+
reduce_to_boolean(results)
|
|
25
|
+
elsif
|
|
26
|
+
raise "Result size #{result.size} not expected: #{result.inspect}"
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
else
|
|
30
|
+
raise "Result must be TrueClass, FalseClass or Array: #{res.inspect}"
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
end
|
|
35
|
+
end
|
data/lib/hval/schema.rb
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module Hval
|
|
2
|
+
class Schema
|
|
3
|
+
include Hval::Validators
|
|
4
|
+
|
|
5
|
+
def initialize(schema)
|
|
6
|
+
@validators = schema.collect do |op, spec|
|
|
7
|
+
method("validate_#{op}").call(spec)
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def validators
|
|
12
|
+
@validators
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def call(value)
|
|
16
|
+
@validators.collect do |validator|
|
|
17
|
+
validator.call(value)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def match?(value)
|
|
22
|
+
Hval::Result.new(self.call(value)).success?
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
module Hval
|
|
2
|
+
class Validator
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
# Return a Validator
|
|
6
|
+
#
|
|
7
|
+
# ==== Example
|
|
8
|
+
#
|
|
9
|
+
# schema = {name: {type?: String, format?: /brush/}, age: {type?: Integer }
|
|
10
|
+
# validator = Hval::Validator.new(schema)
|
|
11
|
+
def initialize(hash)
|
|
12
|
+
@validators = hash.collect do |k, schema|
|
|
13
|
+
[k, Hval::Schema.new(schema)]
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Return an Array with the results of the validation
|
|
18
|
+
#
|
|
19
|
+
# ==== Example
|
|
20
|
+
#
|
|
21
|
+
# schema = {name: {type?: String, format?: /brush/}, age: {type?: Integer }
|
|
22
|
+
# validator = Hval::Validator.new(schema)
|
|
23
|
+
# validator.call({name: "guybrush", age: "45"})
|
|
24
|
+
# => [[:name, [[:type, String, true, "guybrush"], [:format, /brush/, true, "guybrush"]]], [:age, [[:type, Integer, false, "45"]]]]
|
|
25
|
+
def call(hash)
|
|
26
|
+
@result = @validators.collect do |key, validator|
|
|
27
|
+
[key, validator.call(hash[key])]
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def valid?
|
|
32
|
+
return false if @result.nil?
|
|
33
|
+
Hval::Result.new(@result).success?
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def errors(processor=Hval::Errors)
|
|
37
|
+
processor.process(@result)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
module Hval
|
|
2
|
+
module Validators
|
|
3
|
+
|
|
4
|
+
# Validators return a Proc to be called with a value to test,
|
|
5
|
+
# this call return a result which is an array of 4 elements with:
|
|
6
|
+
# - a name (validator name, eg.: :type)
|
|
7
|
+
# - an expected value (eg.: String)
|
|
8
|
+
# - a result which can be true, false or another result array
|
|
9
|
+
# - the given value
|
|
10
|
+
#
|
|
11
|
+
# ==== Example
|
|
12
|
+
#
|
|
13
|
+
# irb> include Hval::Validators
|
|
14
|
+
# irb> validate_format?(/guy/).call("guybrush")
|
|
15
|
+
# => [:format, /guy/, true, "guybrush"]
|
|
16
|
+
# irb> validate_type?(String).call(10)
|
|
17
|
+
# => [:type, String, false, 10]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# Validates the format of a value with a regular expression
|
|
21
|
+
#
|
|
22
|
+
# ==== Example
|
|
23
|
+
#
|
|
24
|
+
# irb> include Hval::Validators
|
|
25
|
+
# irb> validate_format?(/guy/).call("guybrush")
|
|
26
|
+
# => [:format, /guy/, true, "guybrush"]
|
|
27
|
+
def validate_format?(spec)
|
|
28
|
+
-> (value) {
|
|
29
|
+
res = if value.respond_to? :match?
|
|
30
|
+
value.match?(spec)
|
|
31
|
+
else
|
|
32
|
+
false
|
|
33
|
+
end
|
|
34
|
+
[ :format, spec, res, value ]
|
|
35
|
+
}
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Validates the minimal size of a value with an Number
|
|
39
|
+
#
|
|
40
|
+
# ==== Example
|
|
41
|
+
#
|
|
42
|
+
# irb> include Hval::Validators
|
|
43
|
+
# irb> validate_min_size?(10).call(13)
|
|
44
|
+
# => [:min_size, 10, true, 13]
|
|
45
|
+
def validate_min_size?(spec)
|
|
46
|
+
-> (value) {
|
|
47
|
+
res = case value
|
|
48
|
+
when Enumerable, String
|
|
49
|
+
value.size >= spec
|
|
50
|
+
when Numeric
|
|
51
|
+
value >= spec
|
|
52
|
+
end
|
|
53
|
+
[ :min_size, spec, res, value ]
|
|
54
|
+
}
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Validates the maximal size of a value with an Number
|
|
58
|
+
#
|
|
59
|
+
# ==== Example
|
|
60
|
+
#
|
|
61
|
+
# irb> include Hval::Validators
|
|
62
|
+
# irb> validate_max_size?(10).call(9)
|
|
63
|
+
# => [:max_size, 10, true, 9]
|
|
64
|
+
def validate_max_size?(spec)
|
|
65
|
+
-> (value) {
|
|
66
|
+
res = case value
|
|
67
|
+
when Enumerable, String
|
|
68
|
+
value.size <= spec
|
|
69
|
+
when Numeric
|
|
70
|
+
value <= spec
|
|
71
|
+
end
|
|
72
|
+
[ :max_size, spec, res, value ]
|
|
73
|
+
}
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Validates a type of a given value
|
|
77
|
+
#
|
|
78
|
+
# ==== Example
|
|
79
|
+
#
|
|
80
|
+
# irb> include Hval::Validators
|
|
81
|
+
# irb> validate_type?(String).call(10)
|
|
82
|
+
# => [:type, String, false, 10]
|
|
83
|
+
def validate_type?(spec)
|
|
84
|
+
-> (value) {
|
|
85
|
+
[ :type, spec, value.is_a?(spec), value ]
|
|
86
|
+
}
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Validates that a value is included in a list
|
|
90
|
+
#
|
|
91
|
+
# ==== Example
|
|
92
|
+
#
|
|
93
|
+
# irb> include Hval::Validators
|
|
94
|
+
# irb> validate_enum?([1,2,3,4]).call(2)
|
|
95
|
+
# => [:enum, [1, 2, 3, 4], true, 2]
|
|
96
|
+
def validate_enum?(spec)
|
|
97
|
+
-> (value) {
|
|
98
|
+
[ :enum, spec, spec.include?(value), value ]
|
|
99
|
+
}
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Validates a value with a block. The block must return the result
|
|
103
|
+
# in the array format [name, spec, result, given]
|
|
104
|
+
#
|
|
105
|
+
# ==== Example
|
|
106
|
+
#
|
|
107
|
+
# irb> include Hval::Validators
|
|
108
|
+
# irb> _proc = ->(val){ [:proc, :greater_than_ten, val > 10, val] }
|
|
109
|
+
# irb> validate_proc?(_proc).(10)
|
|
110
|
+
# => [:proc, :greater_than_ten, false, 10]
|
|
111
|
+
def validate_proc?(spec)
|
|
112
|
+
-> (value) {
|
|
113
|
+
spec.call(value)
|
|
114
|
+
}
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# Validates that the elements of an Array conform the schema
|
|
118
|
+
#
|
|
119
|
+
# ==== Example
|
|
120
|
+
#
|
|
121
|
+
# irb> include Hval::Validators
|
|
122
|
+
# irb> validate_array?({type?: Integer}).([1,2,3,4,5])
|
|
123
|
+
# => [:array, {:type?=>Integer}, true, [1, 2, 3, 4, 5]]
|
|
124
|
+
# irb> validate_array?({type?: String}).([1,2,3,4,5])
|
|
125
|
+
# => [:array, {:type?=>String}, false, [1, 2, 3, 4, 5]]
|
|
126
|
+
def validate_array?(spec)
|
|
127
|
+
-> (values) {
|
|
128
|
+
res = if values.is_a? Array
|
|
129
|
+
values.all? { |value|
|
|
130
|
+
Hval::Schema.new(spec).match?(value)
|
|
131
|
+
}
|
|
132
|
+
else
|
|
133
|
+
false
|
|
134
|
+
end
|
|
135
|
+
[ :array, spec, res, values ]
|
|
136
|
+
}
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# Validates that a value conform a schema only if provided. Nil or blank are allowed.
|
|
140
|
+
#
|
|
141
|
+
# ==== Example
|
|
142
|
+
#
|
|
143
|
+
# irb> include Hval::Validators
|
|
144
|
+
# irb> validate_optional?({type?: Integer}).(1)
|
|
145
|
+
# => [:optional, {:type?=>Integer}, true, 1]
|
|
146
|
+
# irb> validate_optional?({type?: Integer}).(nil)
|
|
147
|
+
# => [:optional, {:type?=>Integer}, true, nil]
|
|
148
|
+
# irb> validate_optional?({type?: Integer}).("asdasd")
|
|
149
|
+
# => [:optional, {:type?=>Integer}, false, "asdasd"]
|
|
150
|
+
def validate_optional?(spec)
|
|
151
|
+
-> (value) {
|
|
152
|
+
res = case value
|
|
153
|
+
when NilClass, ""
|
|
154
|
+
true
|
|
155
|
+
else
|
|
156
|
+
Hval::Schema.new(spec).match?(value)
|
|
157
|
+
end
|
|
158
|
+
[ :optional, spec, res, value ]
|
|
159
|
+
}
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# Validates a Hash against a schema.
|
|
163
|
+
#
|
|
164
|
+
# The result on hashes also contains the key with the results associated with the key. As many results as validators defined.
|
|
165
|
+
#
|
|
166
|
+
# ==== Example
|
|
167
|
+
#
|
|
168
|
+
# irb> include Hval::Validators
|
|
169
|
+
# irb> validate_hash?({name: {type?: String}}).({name: "carlos"})
|
|
170
|
+
# => [:hash, {:name=>{:type?=>String}}, [[:name, [[:type, String, true, "carlos"]]]], {:name=>"carlos"}]
|
|
171
|
+
# irb> validate_hash?({name: {type?: String}}).({name: 123})
|
|
172
|
+
# => [:hash, {:name=>{:type?=>String}}, [[:name, [[:type, String, false, 123]]]], {:name=>123}]
|
|
173
|
+
# irb> validate_hash?({name: {type?: String, format?: /carl/}}).({name: 123})
|
|
174
|
+
# => [:hash, {:name=>{:type?=>String, :format?=>/carl/}}, [[:name, [[:type, String, false, 123], [:format, /carl/, false, 123]]]], {:name=>123}]
|
|
175
|
+
def validate_hash?(spec)
|
|
176
|
+
-> (value) {
|
|
177
|
+
res = if value.is_a? Hash
|
|
178
|
+
Hval::Validator.new(spec).call(value)
|
|
179
|
+
else
|
|
180
|
+
false
|
|
181
|
+
end
|
|
182
|
+
[ :hash, spec, res, value ]
|
|
183
|
+
}
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
end
|
data/lib/hval/version.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: hval
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Juan Repetti
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2017-08-14 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.15'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.15'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: minitest
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '5.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '5.0'
|
|
55
|
+
description:
|
|
56
|
+
email:
|
|
57
|
+
- jmrepetti@github.com
|
|
58
|
+
executables: []
|
|
59
|
+
extensions: []
|
|
60
|
+
extra_rdoc_files: []
|
|
61
|
+
files:
|
|
62
|
+
- ".gitignore"
|
|
63
|
+
- ".travis.yml"
|
|
64
|
+
- CODE_OF_CONDUCT.md
|
|
65
|
+
- Gemfile
|
|
66
|
+
- LICENSE.txt
|
|
67
|
+
- README.md
|
|
68
|
+
- Rakefile
|
|
69
|
+
- bin/console
|
|
70
|
+
- bin/setup
|
|
71
|
+
- hval.gemspec
|
|
72
|
+
- lib/hval.rb
|
|
73
|
+
- lib/hval/result.rb
|
|
74
|
+
- lib/hval/schema.rb
|
|
75
|
+
- lib/hval/validator.rb
|
|
76
|
+
- lib/hval/validators.rb
|
|
77
|
+
- lib/hval/version.rb
|
|
78
|
+
homepage: http://github.com/jmrepetti/hval
|
|
79
|
+
licenses:
|
|
80
|
+
- MIT
|
|
81
|
+
metadata: {}
|
|
82
|
+
post_install_message:
|
|
83
|
+
rdoc_options: []
|
|
84
|
+
require_paths:
|
|
85
|
+
- lib
|
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
87
|
+
requirements:
|
|
88
|
+
- - ">="
|
|
89
|
+
- !ruby/object:Gem::Version
|
|
90
|
+
version: '0'
|
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '0'
|
|
96
|
+
requirements: []
|
|
97
|
+
rubyforge_project:
|
|
98
|
+
rubygems_version: 2.6.11
|
|
99
|
+
signing_key:
|
|
100
|
+
specification_version: 4
|
|
101
|
+
summary: "Hval \U0001F433 is hash validation gem"
|
|
102
|
+
test_files: []
|