objecheck 0.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 +7 -0
- data/.circleci/config.yml +81 -0
- data/.gitignore +15 -0
- data/.rspec +3 -0
- data/.rubocop.yml +80 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +13 -0
- data/README.md +57 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/objecheck/validator/collector.rb +40 -0
- data/lib/objecheck/validator/type_rule.rb +27 -0
- data/lib/objecheck/validator.rb +42 -0
- data/lib/objecheck.rb +22 -0
- data/objecheck.gemspec +23 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c34dee3669dc8ba5d6da6601f2691dfa1cdf596b23cdb575ce99fec568e84283
|
4
|
+
data.tar.gz: 5a190c9ad765f40ae422a87462134a2066c48497acb63518f2cc63f1cded79e1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 57c538a3674c233bbf7ba84a6bd0a454b8f0532306d62530adce569b6965143180c60be60e3899064eb671f293d31886ae515d65b6418641e13c66d6ac36f2e0
|
7
|
+
data.tar.gz: df4388871daec2f26406fa2bd91e0c9e9779d70fbef3a8c32a11187bd6effb43f90130dab742c4e094ced95f182356e058a5c2cfbc23b2f8bab1b6a6c20fa3de
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# Ruby CircleCI 2.0 configuration file
|
2
|
+
#
|
3
|
+
# Check https://circleci.com/docs/2.0/language-ruby/ for more details
|
4
|
+
#
|
5
|
+
version: 2
|
6
|
+
jobs:
|
7
|
+
build:
|
8
|
+
docker:
|
9
|
+
# specify the version you desire here
|
10
|
+
- image: circleci/ruby:2.4.2-node-browsers
|
11
|
+
working_directory: ~/repo
|
12
|
+
|
13
|
+
steps:
|
14
|
+
- checkout
|
15
|
+
|
16
|
+
- run:
|
17
|
+
name: install dependencies
|
18
|
+
command: |
|
19
|
+
bundle install --jobs=4 --retry=3 --path vendor/bundle
|
20
|
+
|
21
|
+
# Build gem
|
22
|
+
- run:
|
23
|
+
name: build gem
|
24
|
+
command: |
|
25
|
+
bundle exec rake build
|
26
|
+
|
27
|
+
- save_cache:
|
28
|
+
paths:
|
29
|
+
- ./vendor/bundle
|
30
|
+
key: v1-dependencies-{{ checksum "Gemfile.lock" }}
|
31
|
+
|
32
|
+
# run unit tests
|
33
|
+
- run:
|
34
|
+
name: run unit tests
|
35
|
+
command: |
|
36
|
+
bundle exec rspec --format documentation
|
37
|
+
|
38
|
+
# run rubocop
|
39
|
+
- run: bundle exec rubocop -D
|
40
|
+
|
41
|
+
# pass workspace to next job
|
42
|
+
- persist_to_workspace:
|
43
|
+
root: .
|
44
|
+
paths:
|
45
|
+
- .
|
46
|
+
|
47
|
+
publish:
|
48
|
+
docker:
|
49
|
+
# specify the version you desire here
|
50
|
+
- image: circleci/ruby:2.4.2-node-browsers
|
51
|
+
working_directory: ~/repo
|
52
|
+
steps:
|
53
|
+
- attach_workspace:
|
54
|
+
at: .
|
55
|
+
# configure to publish
|
56
|
+
- run:
|
57
|
+
name: configure
|
58
|
+
command: |
|
59
|
+
mkdir ~/.gem && echo -e ":rubygems_api_key: $RUBYGEMS_API_KEY" > ~/.gem/credentials && chmod 0600 /home/circleci/.gem/credentials
|
60
|
+
# publish to rubygems.org
|
61
|
+
- run:
|
62
|
+
name: publish
|
63
|
+
command: |
|
64
|
+
gem push pkg/objecheck-*.gem
|
65
|
+
|
66
|
+
workflows:
|
67
|
+
version: 2
|
68
|
+
test-and-deploy:
|
69
|
+
jobs:
|
70
|
+
- build:
|
71
|
+
filters:
|
72
|
+
tags:
|
73
|
+
only: /.*/
|
74
|
+
- publish:
|
75
|
+
requires:
|
76
|
+
- build
|
77
|
+
filters:
|
78
|
+
tags:
|
79
|
+
only: /^v[0-9]+(\.[0-9]+){2}.*/
|
80
|
+
branches:
|
81
|
+
ignore: /.*/
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.5
|
3
|
+
Exclude:
|
4
|
+
- bin/*
|
5
|
+
- config/**/*.rb
|
6
|
+
- db/migrate/*.rb
|
7
|
+
- db/schema.rb
|
8
|
+
- db/seeds.rb
|
9
|
+
- vendor/bundle/**/*
|
10
|
+
|
11
|
+
Metrics/LineLength:
|
12
|
+
Max: 120
|
13
|
+
|
14
|
+
Style/AsciiComments:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Style/BracesAroundHashParameters:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
Style/ClassAndModuleChildren:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
Style/MutableConstant:
|
24
|
+
AutoCorrect: false
|
25
|
+
|
26
|
+
Layout/EndAlignment:
|
27
|
+
EnforcedStyleAlignWith: variable
|
28
|
+
|
29
|
+
Style/NegatedIf:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
Style/NegatedWhile:
|
33
|
+
Enabled: false
|
34
|
+
|
35
|
+
Style/DoubleNegation:
|
36
|
+
Enabled: false
|
37
|
+
|
38
|
+
Metrics/ParameterLists:
|
39
|
+
Exclude:
|
40
|
+
- spec/**/*
|
41
|
+
|
42
|
+
Metrics/MethodLength:
|
43
|
+
Max: 15
|
44
|
+
Exclude:
|
45
|
+
- apps/**/views/**/*.rb
|
46
|
+
|
47
|
+
Metrics/BlockLength:
|
48
|
+
Exclude:
|
49
|
+
- apps/**/views/**/*.rb
|
50
|
+
- spec/**/*.rb
|
51
|
+
|
52
|
+
Metrics/AbcSize:
|
53
|
+
Max: 20
|
54
|
+
|
55
|
+
Metrics/PerceivedComplexity:
|
56
|
+
Max: 8
|
57
|
+
|
58
|
+
Metrics/CyclomaticComplexity:
|
59
|
+
Max: 7
|
60
|
+
|
61
|
+
Performance/RedundantMerge:
|
62
|
+
Enabled: false
|
63
|
+
|
64
|
+
Style/FrozenStringLiteralComment:
|
65
|
+
Enabled: false
|
66
|
+
|
67
|
+
Style/SingleLineBlockParams:
|
68
|
+
Enabled: false
|
69
|
+
|
70
|
+
Style/EmptyMethod:
|
71
|
+
Enabled: false
|
72
|
+
|
73
|
+
Style/NumericPredicate:
|
74
|
+
Enabled: false
|
75
|
+
|
76
|
+
Bundler/OrderedGems:
|
77
|
+
Enabled: false
|
78
|
+
|
79
|
+
Gemspec/OrderedDependencies:
|
80
|
+
Enabled: false
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Copyright (C) 2018 Akira Tanimura (@AuToPP)
|
2
|
+
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
you may not use this file except in compliance with the License.
|
5
|
+
You may obtain a copy of the License at
|
6
|
+
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
See the License for the specific language governing permissions and
|
13
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# Objecheck
|
2
|
+
|
3
|
+
[](https://circleci.com/gh/autopp/objecheck/tree/master)
|
4
|
+
|
5
|
+
Objecheck provides a simple and extensible object validator
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'objecheck'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install objecheck
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'objecheck'
|
27
|
+
|
28
|
+
# Create a validator with rules
|
29
|
+
validator = Objecheck::Validator.new({
|
30
|
+
type: Hash
|
31
|
+
})
|
32
|
+
|
33
|
+
# Validator#validate checks the given object and returns error messages as a array
|
34
|
+
p validator.validate({ a: 1, b: 2 }) # => []
|
35
|
+
p validator.validate([1, 2]) # => ["root: the type should be a Hash (got Array)"]
|
36
|
+
```
|
37
|
+
|
38
|
+
## Builtin rules
|
39
|
+
|
40
|
+
### `type`
|
41
|
+
|
42
|
+
`type` checks that the object is a given class (by `is_a?`).
|
43
|
+
|
44
|
+
Example schema:
|
45
|
+
```ruby
|
46
|
+
{
|
47
|
+
type: Hash
|
48
|
+
}
|
49
|
+
```
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/autopp/objecheck.
|
54
|
+
|
55
|
+
## License
|
56
|
+
|
57
|
+
[Apache License 2.0](LICENSE.txt)
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'objecheck'
|
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
@@ -0,0 +1,40 @@
|
|
1
|
+
#
|
2
|
+
# Objecheck
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
#
|
16
|
+
|
17
|
+
# Collector is used in Validator#validate to aggregate errors
|
18
|
+
#
|
19
|
+
class Objecheck::Validator::Collector
|
20
|
+
def initialize(validator)
|
21
|
+
@validator = validator
|
22
|
+
@prefix_stack = []
|
23
|
+
@errors = []
|
24
|
+
end
|
25
|
+
|
26
|
+
def add_prefix_in(prefix)
|
27
|
+
@prefix_stack.push(prefix)
|
28
|
+
yield
|
29
|
+
ensure
|
30
|
+
@prefix_stack.pop
|
31
|
+
end
|
32
|
+
|
33
|
+
def add_error(msg)
|
34
|
+
@errors << "#{@prefix_stack.join('')}: #{msg}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def errors
|
38
|
+
@errors.dup
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
#
|
2
|
+
# Objecheck
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
#
|
16
|
+
|
17
|
+
# TypeRule validates type of a target
|
18
|
+
#
|
19
|
+
class Objecheck::Validator::TypeRule
|
20
|
+
def initialize(type)
|
21
|
+
@type = type
|
22
|
+
end
|
23
|
+
|
24
|
+
def validate(target, collector)
|
25
|
+
collector.add_error("the type should be a #{@type} (got #{target.class})") if !target.is_a?(@type)
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
#
|
2
|
+
# Objecheck
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
#
|
16
|
+
|
17
|
+
# Validator checks a object with given schema and report errors
|
18
|
+
#
|
19
|
+
class Objecheck::Validator
|
20
|
+
require 'objecheck/validator/type_rule'
|
21
|
+
DEFAULT_RULES = {
|
22
|
+
type: TypeRule
|
23
|
+
}.freeze
|
24
|
+
|
25
|
+
def initialize(schema, rules = DEFAULT_RULES)
|
26
|
+
@rules = schema.map do |rule_name, param|
|
27
|
+
rules[rule_name].new(param)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def validate(target)
|
32
|
+
collector = Collector.new(self)
|
33
|
+
collector.add_prefix_in('root') do
|
34
|
+
@rules.each do |rule|
|
35
|
+
rule.validate(target, collector)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
collector.errors
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
require 'objecheck/validator/collector'
|
data/lib/objecheck.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#
|
2
|
+
# Objecheck
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
#
|
16
|
+
|
17
|
+
# Objecheck is a top level module of objecheck
|
18
|
+
#
|
19
|
+
module Objecheck
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'objecheck/validator'
|
data/objecheck.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = 'objecheck'
|
3
|
+
spec.version = '0.1.0'
|
4
|
+
spec.authors = ['autopp']
|
5
|
+
spec.email = ['autopp.inc@gmail.com']
|
6
|
+
|
7
|
+
spec.summary = 'schema and validator for a Ruby object'
|
8
|
+
spec.description = spec.summary
|
9
|
+
spec.homepage = 'https://github.com/autopp/objecheck'
|
10
|
+
spec.license = 'Apache-2.0'
|
11
|
+
|
12
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
13
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
14
|
+
end
|
15
|
+
spec.bindir = 'exe'
|
16
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
17
|
+
spec.require_paths = ['lib']
|
18
|
+
|
19
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
20
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
21
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
22
|
+
spec.add_development_dependency 'rubocop'
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: objecheck
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- autopp
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-09-18 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
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: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
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'
|
69
|
+
description: schema and validator for a Ruby object
|
70
|
+
email:
|
71
|
+
- autopp.inc@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".circleci/config.yml"
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".rubocop.yml"
|
80
|
+
- CHANGELOG.md
|
81
|
+
- Gemfile
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/objecheck.rb
|
88
|
+
- lib/objecheck/validator.rb
|
89
|
+
- lib/objecheck/validator/collector.rb
|
90
|
+
- lib/objecheck/validator/type_rule.rb
|
91
|
+
- objecheck.gemspec
|
92
|
+
homepage: https://github.com/autopp/objecheck
|
93
|
+
licenses:
|
94
|
+
- Apache-2.0
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.7.3
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: schema and validator for a Ruby object
|
116
|
+
test_files: []
|