simple_json_schema 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fd9171c82ee82f6182b25f9e8cb4d6f139dce7ff84d66131a9ea50c3c9728197
4
+ data.tar.gz: 4b9fbb3fe5dc2101a66dfe70a46d11d368ecd7811aa7056f18c1d4dc91f27b7d
5
+ SHA512:
6
+ metadata.gz: 557d9a88155358cffb6e13e2b4ca2b9036ad2d38f60f61ab4692080f72a4d19028d63981a4cb965aeb735def558d2b2697d00cb057577c93769200ee7f78e916
7
+ data.tar.gz: 8f57abf5721f17ff7695c5372554b1c35a6e91b273e85b3becffdbfda225e677130dc2eae5761e1d5fc269b5d9f5936f981a9eeb427e10a745e3b97dad99a2a7
@@ -0,0 +1,13 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = space
5
+ indent_size = 2
6
+ end_of_line = lf
7
+ charset = utf-8
8
+ trim_trailing_whitespace = true
9
+ insert_final_newline = true
10
+ max_line_length = 120
11
+
12
+ [*.md]
13
+ trim_trailing_whitespace = false
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
@@ -0,0 +1,30 @@
1
+ default:
2
+ image: ruby:2.7.2
3
+
4
+ variables:
5
+ GIT_SUBMODULE_STRATEGY: recursive
6
+ RUBY_ENV: test
7
+
8
+ cache:
9
+ key:
10
+ files:
11
+ - Gemfile.lock
12
+ paths:
13
+ - vendor/ruby
14
+
15
+ before_script:
16
+ - ruby -v # Print out ruby version for debugging
17
+ - bundle install -j $(nproc) --path vendor/ruby # Install dependencies into ./vendor/ruby
18
+
19
+ rubocop:
20
+ script:
21
+ - bundle exec rubocop
22
+
23
+ rspec:
24
+ coverage: '/LOC\s\(\d+\.\d+%\)\scovered/'
25
+ script:
26
+ - bundle exec rspec
27
+ artifacts:
28
+ paths:
29
+ - "coverage/"
30
+ name: "Pipeline $CI_PIPELINE_ID Coverage Report"
@@ -0,0 +1,3 @@
1
+ [submodule "spec/json-schema-test-suite"]
2
+ path = spec/json-schema-test-suite
3
+ url = https://github.com/json-schema-org/JSON-Schema-Test-Suite
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,29 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ NewCops: enable
7
+ SuggestExtensions: false
8
+ TargetRubyVersion: 2.6
9
+ Exclude:
10
+ - bin/**/*
11
+ - vendor/ruby/**/*
12
+ - .irbrc
13
+
14
+ Style/Documentation:
15
+ Enabled: false
16
+
17
+ Layout/LineLength:
18
+ Max: 120
19
+
20
+ Metrics/ClassLength:
21
+ Max: 300
22
+
23
+ Metrics/MethodLength:
24
+ Max: 25
25
+
26
+ Metrics/BlockLength:
27
+ Exclude:
28
+ - simple_json_schema.gemspec
29
+ - spec/**/*.rb
@@ -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 ggrocco@gmail.com. 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 [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in simple_json_schema.gemspec
6
+ gemspec
@@ -0,0 +1,103 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ simple_json_schema (0.1.3)
5
+ activesupport (~> 6.0)
6
+ addressable (~> 2.7)
7
+ ecma-re-validator (~> 0.3)
8
+ regexp_parser (~> 2.0)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ activesupport (6.1.0)
14
+ concurrent-ruby (~> 1.0, >= 1.0.2)
15
+ i18n (>= 1.6, < 2)
16
+ minitest (>= 5.1)
17
+ tzinfo (~> 2.0)
18
+ zeitwerk (~> 2.3)
19
+ addressable (2.7.0)
20
+ public_suffix (>= 2.0.2, < 5.0)
21
+ ast (2.4.1)
22
+ byebug (11.1.3)
23
+ coderay (1.1.3)
24
+ concurrent-ruby (1.1.7)
25
+ diff-lcs (1.4.4)
26
+ docile (1.3.3)
27
+ ecma-re-validator (0.3.0)
28
+ regexp_parser (~> 2.0)
29
+ i18n (1.8.5)
30
+ concurrent-ruby (~> 1.0)
31
+ method_source (1.0.0)
32
+ minitest (5.14.2)
33
+ parallel (1.20.1)
34
+ parser (2.7.2.0)
35
+ ast (~> 2.4.1)
36
+ pry (0.13.1)
37
+ coderay (~> 1.1)
38
+ method_source (~> 1.0)
39
+ pry-byebug (3.9.0)
40
+ byebug (~> 11.0)
41
+ pry (~> 0.13.0)
42
+ public_suffix (4.0.6)
43
+ rainbow (3.0.0)
44
+ rake (13.0.3)
45
+ regexp_parser (2.0.1)
46
+ rexml (3.2.4)
47
+ rspec (3.10.0)
48
+ rspec-core (~> 3.10.0)
49
+ rspec-expectations (~> 3.10.0)
50
+ rspec-mocks (~> 3.10.0)
51
+ rspec-core (3.10.0)
52
+ rspec-support (~> 3.10.0)
53
+ rspec-expectations (3.10.0)
54
+ diff-lcs (>= 1.2.0, < 2.0)
55
+ rspec-support (~> 3.10.0)
56
+ rspec-mocks (3.10.0)
57
+ diff-lcs (>= 1.2.0, < 2.0)
58
+ rspec-support (~> 3.10.0)
59
+ rspec-support (3.10.0)
60
+ rubocop (1.6.1)
61
+ parallel (~> 1.10)
62
+ parser (>= 2.7.1.5)
63
+ rainbow (>= 2.2.2, < 4.0)
64
+ regexp_parser (>= 1.8, < 3.0)
65
+ rexml
66
+ rubocop-ast (>= 1.2.0, < 2.0)
67
+ ruby-progressbar (~> 1.7)
68
+ unicode-display_width (>= 1.4.0, < 2.0)
69
+ rubocop-ast (1.3.0)
70
+ parser (>= 2.7.1.5)
71
+ rubocop-performance (1.9.1)
72
+ rubocop (>= 0.90.0, < 2.0)
73
+ rubocop-ast (>= 0.4.0)
74
+ rubocop-rspec (2.1.0)
75
+ rubocop (~> 1.0)
76
+ rubocop-ast (>= 1.1.0)
77
+ ruby-progressbar (1.10.1)
78
+ simplecov (0.20.0)
79
+ docile (~> 1.1)
80
+ simplecov-html (~> 0.11)
81
+ simplecov_json_formatter (~> 0.1)
82
+ simplecov-html (0.12.3)
83
+ simplecov_json_formatter (0.1.2)
84
+ tzinfo (2.0.4)
85
+ concurrent-ruby (~> 1.0)
86
+ unicode-display_width (1.7.0)
87
+ zeitwerk (2.4.2)
88
+
89
+ PLATFORMS
90
+ ruby
91
+
92
+ DEPENDENCIES
93
+ pry-byebug (~> 3.9)
94
+ rake (~> 13.0)
95
+ rspec (~> 3.10)
96
+ rubocop (~> 1.6)
97
+ rubocop-performance (~> 1.9)
98
+ rubocop-rspec (~> 2.1)
99
+ simple_json_schema!
100
+ simplecov (~> 0.20)
101
+
102
+ BUNDLED WITH
103
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Georgeo Rocco
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.
@@ -0,0 +1,88 @@
1
+ # SimpleJSONSchema
2
+
3
+ [![pipeline status](https://gitlab.com/vliper/simple_json_schema/badges/master/pipeline.svg)](https://gitlab.com/vliper/simple_json_schema/-/commits/master)
4
+ [![coverage report](https://gitlab.com/vliper/simple_json_schema/badges/master/coverage.svg)](https://gitlab.com/vliper/simple_json_schema/-/commits/master)
5
+ [![Gem Version](https://badge.fury.io/rb/simple_json_schema.svg)](https://badge.fury.io/rb/simple_json_schema)
6
+
7
+ This implementation of the JSON Schema validation is based on the [JSONSchemer](https://github.com/davishmcclurg/json_schemer) (whit is the recommended to be used), but only implement the Draft7, whit [limitations](https://gitlab.com/vliper/simple_json_schema/-/blob/master/spec/simple_json_schema/json_schema_test_suite_spec.rb#L15) and some feature embedded.
8
+
9
+ # Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'simple_json_schema'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install simple_json_schema
24
+
25
+ ## Usage
26
+ The use is based on two methods, the 'valid' and 'valid?'.
27
+
28
+ ```ruby
29
+
30
+ require 'simple_json_schema'
31
+
32
+ schema = {
33
+ type: 'object',
34
+ properties: {
35
+ abc: {
36
+ type: 'integer',
37
+ minimum: 11
38
+ }
39
+ }
40
+ options: { cast: true } # passing options whit the schema.
41
+ }
42
+
43
+ # true/false validation
44
+ SimpleJSONSchema.valid?({ 'abc' => 11 }, schema)
45
+ # => true
46
+
47
+ SimpleJSONSchema.valid?({ 'abc' => 10 }, schema)
48
+ # => false
49
+
50
+ # using options, with can be at the schema or passing as parameter.
51
+ # SimpleJSONSchema.valid?({ 'abc' => '10' }, schema, { cast: true } )
52
+ SimpleJSONSchema.valid?({ 'abc' => '11' }, schema)
53
+ # => true
54
+
55
+ # Return the errors if any.
56
+ SimpleJSONSchema.valid({ 'abc' => 10 }, schema)
57
+ # => [{:type=>:minimum, :segment=>{"type"=>"integer", "minimum"=>11}, :value=>10, :data_pointer=>"/abc", :schema_pointer=>"/properties/abc"}]
58
+ ```
59
+
60
+ #### Options:
61
+ Whit options is possible to improve the behavior.
62
+
63
+ | Name | Description | Default | Allowed values |
64
+ | ---- | ----------- | ------- | -------------- |
65
+ | after_property_validation | Call after validate a Property | nil | Proc to receive Scope object |
66
+ | before_property_validation | Call before validate a Property | nil | Proc to receive Scope object |
67
+ | cache | The object to do the cache for schema (future) and regex | {} per execution | any object whit implement fetch(name) { callback } |
68
+ | cast | Cast the values | false | true/false |
69
+ | insert_defaults | Insert the default property if not defined at the validation | false | true/false |
70
+
71
+ ## Development
72
+
73
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
74
+
75
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ggrocco/simple_json_schema. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/ggrocco/simple_json_schema/blob/master/CODE_OF_CONDUCT.md).
80
+
81
+
82
+ ## License
83
+
84
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
85
+
86
+ ## Code of Conduct
87
+
88
+ Everyone interacting in the Simple::Json::Schema project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ggrocco/simple_json_schema/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'simple_json_schema'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'simple_json_schema/version'
4
+
5
+ require 'active_support/core_ext/hash'
6
+ require 'addressable/template'
7
+ require 'ecma-re-validator'
8
+ require 'regexp_parser'
9
+
10
+ require 'simple_json_schema/validators/concerns/format'
11
+ require 'simple_json_schema/validators/base'
12
+ require 'simple_json_schema/validators/numeric'
13
+ require 'simple_json_schema/validators/boolean'
14
+ require 'simple_json_schema/validators/integer'
15
+ require 'simple_json_schema/validators/null'
16
+ require 'simple_json_schema/validators/number'
17
+ require 'simple_json_schema/validators/string'
18
+
19
+ require 'simple_json_schema/concerns/hash_acessor'
20
+ require 'simple_json_schema/checker'
21
+ require 'simple_json_schema/items_helper'
22
+ require 'simple_json_schema/properties_helper'
23
+ require 'simple_json_schema/regex_helper'
24
+ require 'simple_json_schema/scope'
25
+ require 'simple_json_schema/validator'
26
+
27
+ module SimpleJSONSchema
28
+ class UnsupportedMetaSchema < StandardError; end
29
+
30
+ DRAFT_BY_SCHEMA = {
31
+ 'http://json-schema.org/draft-07/schema#' => :draft7
32
+ }.freeze
33
+
34
+ DEFAULT_SCHEMA = 'http://json-schema.org/draft-07/schema#'
35
+
36
+ class << self
37
+ def valid?(data, schema, options = nil)
38
+ errors = valid(data, schema, options)
39
+ errors.none?
40
+ end
41
+
42
+ def valid(data, schema, options = nil)
43
+ if schema.is_a?(Hash)
44
+ schema = schema.with_indifferent_access
45
+ options ||= schema.delete(:options)
46
+ end
47
+
48
+ scope = Scope.new(data: data, schema: schema, draft: draft_class(schema), options: options)
49
+ Validator.validate(scope)
50
+ scope.errors
51
+ end
52
+
53
+ private
54
+
55
+ def draft_class(schema)
56
+ meta_schema = schema.is_a?(Hash) && schema.key?('$schema') ? schema['$schema'] : DEFAULT_SCHEMA
57
+ DRAFT_BY_SCHEMA[meta_schema] || raise(UnsupportedMetaSchema, meta_schema)
58
+ end
59
+ end
60
+ end