schema_validator 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +54 -0
- data/Rakefile +26 -0
- data/lib/schema_validator.rb +11 -0
- data/lib/schema_validator/schema/base.rb +13 -0
- data/lib/schema_validator/version.rb +3 -0
- data/spec/dummy/app/models/product.rb +2 -0
- data/spec/dummy/app/models/product/schema.rb +25 -0
- data/spec/lib/schema/base_spec.rb +9 -0
- data/spec/models/product/schema_spec.rb +38 -0
- data/spec/spec_helper.rb +107 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 014747701ba67637e5e989074b3ed9aa6ecfb3fb
|
4
|
+
data.tar.gz: aecae5997c01f64b0923ddb5d809fc6d2cd27333
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bf3ad748562558bf0725bc71dd97aef14c8bf528897824106925db4109f35f0b872f5007f7bb7c62f3bf27ebcff9241e2141838e38efed1d9a1eb0cde8e551c0
|
7
|
+
data.tar.gz: 7e8ffacf9ffbe31e651d662d31458ebe8a4a0fa962dff2a052a134a86e61a9ee86095bc4093a92b3da35ee28915abc18191001b3ee4687ce7c53de680e27c9ec
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 Patrice Lebel
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# SchemaValidator
|
2
|
+
|
3
|
+
### Usage
|
4
|
+
|
5
|
+
Just define a json schema definition and assign it to any constant within the class definition:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
# app/schemas/any_schema.rb
|
9
|
+
|
10
|
+
class AnySchema < Schema::Base
|
11
|
+
SCHEMA_NAME = ::IceNine.deep_freeze(
|
12
|
+
type: 'object', properties: {
|
13
|
+
nested_blocks_attributes: {
|
14
|
+
type: 'array', minItems: 1, items: {
|
15
|
+
type: 'object', properties: {
|
16
|
+
date: { type: 'string', format: 'date-time' },
|
17
|
+
values: {
|
18
|
+
type: 'object', properties: {
|
19
|
+
attr_1: { type: 'array', items: { type: ['number', 'integer'] }, minItems: 5, maxItems: 5 },
|
20
|
+
attr_2: { type: 'array', items: { type: ['number'] }, minItems: 4, maxItems: 4 },
|
21
|
+
attr_3: { type: 'array', items: { type: ['integer'], minimum: 0, maximum: 1 }, minItems: 64, maxItems: 64 },
|
22
|
+
attr_4: { type: 'array', items: { type: ['integer'] }, minItems: 4, maxItems: 4 },
|
23
|
+
}
|
24
|
+
}
|
25
|
+
}
|
26
|
+
}
|
27
|
+
}
|
28
|
+
}
|
29
|
+
)
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
Then, you just need to call either `validate`, `validate!` or `fully_validate`:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
AnySchema.validate! params_hash
|
37
|
+
```
|
38
|
+
|
39
|
+
### Options
|
40
|
+
|
41
|
+
It's possible to have multiple schema definitions within the same class, the name must be specified in that case:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
AnySchema.validate! params_hash, schema: 'OTHER_SCHEMA_NAME'
|
45
|
+
```
|
46
|
+
|
47
|
+
Other options are the one in use with the [json-schema](https://github.com/ruby-json-schema/json-schema) gem.
|
48
|
+
|
49
|
+
Default options already set are `strict: true, parse_data: false, insert_defaults: true`.
|
50
|
+
|
51
|
+
Since most schema definitions are static configurations, `IceNine` deep_freeze is recommended and included within the gemspec.
|
52
|
+
|
53
|
+
|
54
|
+
This project rocks and uses MIT-LICENSE.
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'SchemaValidator'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
|
21
|
+
load 'rails/tasks/statistics.rake'
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
require 'bundler/gem_tasks'
|
26
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Schema
|
2
|
+
class Base
|
3
|
+
%i[ validate validate! fully_validate ].each do |method_name|
|
4
|
+
define_singleton_method method_name do |params, **options|
|
5
|
+
JSON::Validator.send(method_name,
|
6
|
+
const_get(options.delete(:schema) || constants.first),
|
7
|
+
params,
|
8
|
+
options.reverse_merge(strict: true, parse_data: false, insert_defaults: true)
|
9
|
+
)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class Product::Schema < Schema::Base
|
2
|
+
COMMENTS = ::IceNine.deep_freeze(
|
3
|
+
type: 'object', properties: {
|
4
|
+
comments_attributes: {
|
5
|
+
type: 'array', minItems: 1, items: {
|
6
|
+
type: 'object', properties: {
|
7
|
+
comment: { type: 'string' },
|
8
|
+
timestamp: { type: 'string', format: 'date-time' },
|
9
|
+
tags: { type: 'array', items: { type: ['integer'] }, minItems: 1, maxItems: 2, default: [0] },
|
10
|
+
}
|
11
|
+
}
|
12
|
+
}
|
13
|
+
}
|
14
|
+
)
|
15
|
+
|
16
|
+
TAGS = ::IceNine.deep_freeze(
|
17
|
+
type: 'object', properties: {
|
18
|
+
tags_attributes: {
|
19
|
+
type: 'array', items: {
|
20
|
+
type: 'string'
|
21
|
+
}
|
22
|
+
}
|
23
|
+
}
|
24
|
+
)
|
25
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
describe Product::Schema do
|
2
|
+
describe '#validate' do
|
3
|
+
context 'with COMMENTS schema' do
|
4
|
+
let(:product_params) {
|
5
|
+
{ comments_attributes: [
|
6
|
+
{ comment: 'Comment', timestamp: Time.now.iso8601, tags: [1, 2] },
|
7
|
+
]
|
8
|
+
}
|
9
|
+
}
|
10
|
+
|
11
|
+
it { expect(Product::Schema.validate product_params).to be_truthy }
|
12
|
+
|
13
|
+
context 'with missing attribute and strict true' do
|
14
|
+
before { product_params[:comments_attributes][0].delete(:comment) }
|
15
|
+
|
16
|
+
it { expect(Product::Schema.validate product_params).to be_falsey }
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'with insert defaults true' do
|
20
|
+
before { product_params[:comments_attributes][0].delete(:tags) }
|
21
|
+
|
22
|
+
it { expect(Product::Schema.validate product_params).to be_truthy }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'with TAGS schema' do
|
27
|
+
let(:product_params) { { tags_attributes: ['tag'] } }
|
28
|
+
|
29
|
+
it { expect(Product::Schema.validate product_params, schema: :TAGS).to be_truthy }
|
30
|
+
|
31
|
+
context 'with invalid schema' do
|
32
|
+
before { product_params[:tags_attributes] << 1 }
|
33
|
+
|
34
|
+
it { expect(Product::Schema.validate product_params, schema: :TAGS).to be_falsey }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'schema_validator'
|
2
|
+
require 'require_all'
|
3
|
+
require_all 'spec/dummy/**/*.rb'
|
4
|
+
|
5
|
+
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
|
6
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
7
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
8
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
9
|
+
# files.
|
10
|
+
#
|
11
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
12
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
13
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
14
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
15
|
+
# a separate helper file that requires the additional dependencies and performs
|
16
|
+
# the additional setup, and require it from the spec files that actually need
|
17
|
+
# it.
|
18
|
+
#
|
19
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
20
|
+
# users commonly want.
|
21
|
+
#
|
22
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
23
|
+
RSpec.configure do |config|
|
24
|
+
# rspec-expectations config goes here. You can use an alternate
|
25
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
26
|
+
# assertions if you prefer.
|
27
|
+
config.expect_with :rspec do |expectations|
|
28
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
29
|
+
# and `failure_message` of custom matchers include text for helper methods
|
30
|
+
# defined using `chain`, e.g.:
|
31
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
32
|
+
# # => "be bigger than 2 and smaller than 4"
|
33
|
+
# ...rather than:
|
34
|
+
# # => "be bigger than 2"
|
35
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
36
|
+
end
|
37
|
+
|
38
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
39
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
40
|
+
config.mock_with :rspec do |mocks|
|
41
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
42
|
+
# a real object. This is generally recommended, and will default to
|
43
|
+
# `true` in RSpec 4.
|
44
|
+
mocks.verify_partial_doubles = true
|
45
|
+
end
|
46
|
+
|
47
|
+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
48
|
+
# have no way to turn it off -- the option exists only for backwards
|
49
|
+
# compatibility in RSpec 3). It causes shared context metadata to be
|
50
|
+
# inherited by the metadata hash of host groups and examples, rather than
|
51
|
+
# triggering implicit auto-inclusion in groups with matching metadata.
|
52
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
53
|
+
|
54
|
+
# The settings below are suggested to provide a good initial experience
|
55
|
+
# with RSpec, but feel free to customize to your heart's content.
|
56
|
+
=begin
|
57
|
+
# This allows you to limit a spec run to individual examples or groups
|
58
|
+
# you care about by tagging them with `:focus` metadata. When nothing
|
59
|
+
# is tagged with `:focus`, all examples get run. RSpec also provides
|
60
|
+
# aliases for `it`, `describe`, and `context` that include `:focus`
|
61
|
+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
62
|
+
config.filter_run_when_matching :focus
|
63
|
+
|
64
|
+
# Allows RSpec to persist some state between runs in order to support
|
65
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
66
|
+
# you configure your source control system to ignore this file.
|
67
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
68
|
+
|
69
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
70
|
+
# recommended. For more details, see:
|
71
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
72
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
73
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
74
|
+
config.disable_monkey_patching!
|
75
|
+
|
76
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
77
|
+
# be too noisy due to issues in dependencies.
|
78
|
+
config.warnings = true
|
79
|
+
|
80
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
81
|
+
# file, and it's useful to allow more verbose output when running an
|
82
|
+
# individual spec file.
|
83
|
+
if config.files_to_run.one?
|
84
|
+
# Use the documentation formatter for detailed output,
|
85
|
+
# unless a formatter has already been configured
|
86
|
+
# (e.g. via a command-line flag).
|
87
|
+
config.default_formatter = 'doc'
|
88
|
+
end
|
89
|
+
|
90
|
+
# Print the 10 slowest examples and example groups at the
|
91
|
+
# end of the spec run, to help surface which specs are running
|
92
|
+
# particularly slow.
|
93
|
+
config.profile_examples = 10
|
94
|
+
|
95
|
+
# Run specs in random order to surface order dependencies. If you find an
|
96
|
+
# order dependency and want to debug it, you can fix the order by providing
|
97
|
+
# the seed, which is printed after each run.
|
98
|
+
# --seed 1234
|
99
|
+
config.order = :random
|
100
|
+
|
101
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
102
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
103
|
+
# test failures related to randomization by passing the same `--seed` value
|
104
|
+
# as the one that triggered the failure.
|
105
|
+
Kernel.srand config.seed
|
106
|
+
=end
|
107
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: schema_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Patrice Lebel
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '6'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4.2'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '6'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: ice_nine
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0.11'
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0.11'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: json-schema
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '2.6'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '2.6'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rspec
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: require_all
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
description: SchemaValidator
|
90
|
+
email:
|
91
|
+
- patleb@users.noreply.github.com
|
92
|
+
executables: []
|
93
|
+
extensions: []
|
94
|
+
extra_rdoc_files: []
|
95
|
+
files:
|
96
|
+
- MIT-LICENSE
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- lib/schema_validator.rb
|
100
|
+
- lib/schema_validator/schema/base.rb
|
101
|
+
- lib/schema_validator/version.rb
|
102
|
+
- spec/dummy/app/models/product.rb
|
103
|
+
- spec/dummy/app/models/product/schema.rb
|
104
|
+
- spec/lib/schema/base_spec.rb
|
105
|
+
- spec/models/product/schema_spec.rb
|
106
|
+
- spec/spec_helper.rb
|
107
|
+
homepage: https://github.com/patleb/schema_validator
|
108
|
+
licenses:
|
109
|
+
- MIT
|
110
|
+
metadata: {}
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
require_paths:
|
114
|
+
- lib
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
requirements: []
|
126
|
+
rubyforge_project:
|
127
|
+
rubygems_version: 2.5.1
|
128
|
+
signing_key:
|
129
|
+
specification_version: 4
|
130
|
+
summary: SchemaValidator
|
131
|
+
test_files:
|
132
|
+
- spec/models/product/schema_spec.rb
|
133
|
+
- spec/lib/schema/base_spec.rb
|
134
|
+
- spec/spec_helper.rb
|
135
|
+
- spec/dummy/app/models/product.rb
|
136
|
+
- spec/dummy/app/models/product/schema.rb
|
137
|
+
has_rdoc:
|