json-schema-oas 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.
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'json-schema-oas/schema/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'json-schema-oas'
7
+ spec.version = JSON::Oas::Schema::VERSION
8
+ spec.authors = ['Guillaume Dt']
9
+ spec.email = ['Deuteu@users.noreply.github.com']
10
+ spec.license = 'Apache-2.0'
11
+
12
+ spec.summary = 'Ruby JSON Schema Validator with OAS helper'
13
+ spec.homepage = 'https://github.com/Deuteu/json-schema-oas'
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_runtime_dependency 'json-schema', '~> 2.8', '>= 2.8.1'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.16'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ spec.add_development_dependency 'rubocop', '~> 0.65'
30
+ spec.add_development_dependency 'simplecov', '~> 0.16'
31
+ end
@@ -0,0 +1,5 @@
1
+ require 'json-schema-oas/schema/version'
2
+ require 'json-schema-oas/error'
3
+ require 'json-schema-oas/version'
4
+ require 'json-schema-oas/fragment'
5
+ require 'json-schema-oas/validator'
@@ -0,0 +1,9 @@
1
+ module JSON
2
+ module Oas
3
+ # Error raised in JSON::Oas
4
+ class Error < ::StandardError
5
+ INVALID_SCHEMA_ERROR = 'Invalid schema'.freeze
6
+ UNKNOWN_VERSION_ERROR = 'Unknown version'.freeze
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,57 @@
1
+ module JSON
2
+ module Oas
3
+ # This is a workaround for json-schema's fragment validation
4
+ # which does not allow to contain forward slashes
5
+ # due to an attempt split('/')
6
+ class Fragment < Array
7
+ class << self
8
+ def response_schema_for(version, path_or_name, method = nil, code = nil)
9
+ case version
10
+ when Version::OAS2
11
+ raise ArgumentError unless method && code
12
+
13
+ new(['#', 'paths', path_or_name, method.to_s, 'responses', code.to_s, 'schema'])
14
+ when Version::OAS3
15
+ v3_response_schema_for(path_or_name, method, code)
16
+ else
17
+ raise Error, Error::UNKNOWN_VERSION_ERROR
18
+ end
19
+ end
20
+
21
+ def schema_for(version, name)
22
+ case version
23
+ when Version::OAS2
24
+ new(['#', 'definitions', name.to_s])
25
+ when Version::OAS3
26
+ new(['#', 'components', 'schemas', name.to_s])
27
+ else
28
+ raise Error, Error::UNKNOWN_VERSION_ERROR
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ # @path_or_name String To avoid https://github.com/ruby-json-schema/json-schema/issues/229
35
+ def v3_response_schema_for(path_or_name, method = nil, code = nil)
36
+ unless method && code
37
+ raise ArgumentError unless path_or_name
38
+
39
+ return new(['#', 'components', 'responses', path_or_name, 'content', 'application/json', 'schema'])
40
+ end
41
+
42
+ v3_paths_response_schema_for(path_or_name, method, code)
43
+ end
44
+
45
+ def v3_paths_response_schema_for(path, method, code)
46
+ raise ArgumentError unless method && code && path
47
+
48
+ new(['#', 'paths', path, method.to_s, 'responses', code.to_s, 'content', 'application/json', 'schema'])
49
+ end
50
+ end
51
+
52
+ def split(_options = nil)
53
+ dup
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,7 @@
1
+ module JSON
2
+ module Oas
3
+ class Schema
4
+ VERSION = '0.1.0'.freeze
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,65 @@
1
+ require 'json-schema'
2
+
3
+ module JSON
4
+ module Oas
5
+ OAS2_SCHEMA_PATH = File.expand_path('../../data/specifications/oas2.json', __dir__)
6
+ OAS3_SCHEMA_PATH = File.expand_path('../../data/specifications/oas3.json', __dir__)
7
+
8
+ # Validator to validate data against a OpenAPI schema
9
+ class Validator < JSON::Validator
10
+ class << self
11
+ def valid_schema?(schema, version)
12
+ specs = case version
13
+ when Version::OAS2
14
+ OAS2_SCHEMA_PATH
15
+ when Version::OAS3
16
+ OAS3_SCHEMA_PATH
17
+ else
18
+ raise Error, Error::UNKNOWN_VERSION_ERROR
19
+ end
20
+ JSON::Validator.fully_validate(specs, schema).empty?
21
+ end
22
+
23
+ def compute_fragment(version, opts)
24
+ options = Hash(opts)
25
+ return options if options[:fragment]
26
+
27
+ if options[:with_schema]
28
+ options[:fragment] = Fragment.schema_for(version, options[:with_schema].to_s)
29
+ elsif options[:with_response]
30
+ options[:fragment] = Fragment.response_schema_for(version, *Array(options[:with_response]))
31
+ end
32
+
33
+ options
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def initialize(schema_data, data, opts = {})
40
+ @original_schema = schema_data.dup
41
+
42
+ @oas_version = opts[:oas_version] || Oas::Version::DEFAULT_VERSION
43
+ raise Error, Error::UNKNOWN_VERSION_ERROR unless Oas::Version::VERSIONS.include?(@oas_version)
44
+
45
+ options = self.class.compute_fragment(@oas_version, opts)
46
+
47
+ super(schema_data, data, options)
48
+
49
+ validate_schema!
50
+ end
51
+
52
+ def initialize_schema(schema)
53
+ schema = super(schema)
54
+ @original_schema = schema.dup
55
+ schema
56
+ end
57
+
58
+ def validate_schema!
59
+ return true if self.class.valid_schema?(@original_schema.schema, @oas_version)
60
+
61
+ raise Error, Error::INVALID_SCHEMA_ERROR
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,11 @@
1
+ module JSON
2
+ module Oas
3
+ class Version
4
+ OAS2 = '2.0'.freeze
5
+ OAS3 = '3.0'.freeze
6
+
7
+ DEFAULT_VERSION = OAS3
8
+ VERSIONS = [OAS2, OAS3].freeze
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json-schema-oas
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Guillaume Dt
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json-schema
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.8'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.8.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.8'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.8.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.16'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.16'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '10.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rubocop
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '0.65'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '0.65'
89
+ - !ruby/object:Gem::Dependency
90
+ name: simplecov
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '0.16'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '0.16'
103
+ description:
104
+ email:
105
+ - Deuteu@users.noreply.github.com
106
+ executables: []
107
+ extensions: []
108
+ extra_rdoc_files: []
109
+ files:
110
+ - ".gitignore"
111
+ - ".rspec"
112
+ - ".rubocop.yml"
113
+ - ".travis.yml"
114
+ - Gemfile
115
+ - Gemfile.lock
116
+ - README.md
117
+ - Rakefile
118
+ - bin/console
119
+ - bin/setup
120
+ - data/example/error.json
121
+ - data/example/invalid/error.json
122
+ - data/example/invalid/pet.json
123
+ - data/example/pet.json
124
+ - data/schema/invalid/empty.yml
125
+ - data/schema/invalid/petstore.oas2.yml
126
+ - data/schema/invalid/petstore.oas3.yml
127
+ - data/schema/petstore.oas2.yml
128
+ - data/schema/petstore.oas3.yml
129
+ - data/specifications/oas2.json
130
+ - data/specifications/oas3.json
131
+ - json-schema-oas.gemspec
132
+ - lib/json-schema-oas.rb
133
+ - lib/json-schema-oas/error.rb
134
+ - lib/json-schema-oas/fragment.rb
135
+ - lib/json-schema-oas/schema/version.rb
136
+ - lib/json-schema-oas/validator.rb
137
+ - lib/json-schema-oas/version.rb
138
+ homepage: https://github.com/Deuteu/json-schema-oas
139
+ licenses:
140
+ - Apache-2.0
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.7.6
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: Ruby JSON Schema Validator with OAS helper
162
+ test_files: []