openapi_validator 0.4.1 → 0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d4bd8846a30fce3dc1dd1503a6b21989251d86a21869ffa7e080067d23dd275
4
- data.tar.gz: 8a0f95184f4de1a96d2c36fe80f8abfb867f3f85993a50b11b3743ad54792f02
3
+ metadata.gz: 421695ee7a834a974d8564fbd30da8f3bc21ade39aeca6bb67dd81bf5394d5c9
4
+ data.tar.gz: ee0caa0e6210f8c7f3a11eac1d744c906a886b8868c402da52082804dcd3a848
5
5
  SHA512:
6
- metadata.gz: 874600b81ceb661d8df2d6d55cc881a7202ddea8e3d9ed87d6b9eb54be7dbf48638cf3789e03dcd5faed275655cb0aaa58f8b48c49546adac31e04ed55162a03
7
- data.tar.gz: d690efa10ec2dae997b6c5f0ce3d785b375a06909cb913d4b75b23d7f49f3f0e8c0c122c2e2fc672fe4d86510247fabb9560d8e995a661a8cbf4dfa49b286609
6
+ metadata.gz: ea58fb690c20e18076d9bacea5d563cdbf6ab7951c1e7a7ddc9967efe0f5251a3409e3aaf38d4077b870eef01fa7b2693a6b34fc776aba0cf3904f1f8d942adc
7
+ data.tar.gz: 2b14e4bac49769407d1e0b9d0bb8a82428c17e182b52755e74b6479da0a54a641f1434ad52d54f68fb88e8ae433bc22dfc979634ce7f9b6aaab9c169fce539a6
data/README.md CHANGED
@@ -7,6 +7,7 @@
7
7
  [![Build Status](https://travis-ci.org/medsolutions/openapi_validator.svg?branch=master)][travis]
8
8
  [![Code Climate](https://codeclimate.com/github/medsolutions/openapi_validator/badges/gpa.svg)][codeclimate]
9
9
  [![Test Coverage](https://codeclimate.com/github/medsolutions/openapi_validator/badges/coverage.svg)][codeclimate]
10
+ [![Ruby Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://github.com/testdouble/standard)
10
11
 
11
12
  Validator used for [openapi_rspec](https://github.com/medsolutions/openapi_rspec)
12
13
 
@@ -43,3 +44,5 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/medsol
43
44
  ## License
44
45
 
45
46
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
47
+
48
+ This project includes the [OpenAPI 3.0 Schema](https://github.com/googleapis/gnostic/blob/master/OpenAPIv3/openapi-3.0.json) as `data/openapi-3.0.json`. Copyright 2017, Google Inc. Released under the Apache 2.0 license.
@@ -4,7 +4,6 @@ require "openapi_validator/schema/json_validator"
4
4
 
5
5
  module OpenapiValidator
6
6
  class DocumentationValidator
7
-
8
7
  attr_reader :errors
9
8
 
10
9
  # @param [Hash] api_doc parsed openapi documentation
@@ -7,13 +7,13 @@ module OpenapiValidator
7
7
  class ExtendedSchema < JSON::Schema::Draft4
8
8
  def initialize
9
9
  super
10
- @attributes['type'] = TypeAttribute
11
- @attributes['required'] = RequiredAttribute
12
- @uri = URI.parse('http://example.com/extended_schema')
13
- @names = ['http://example.com/extended_schema']
10
+ @attributes["type"] = TypeAttribute
11
+ @attributes["required"] = RequiredAttribute
12
+ @uri = URI.parse("http://example.com/extended_schema")
13
+ @names = ["http://example.com/extended_schema"]
14
14
  end
15
15
 
16
- JsonValidator.register_validator(self.new)
17
- JsonValidator.register_default_validator(self.new)
16
+ JsonValidator.register_validator(new)
17
+ JsonValidator.register_default_validator(new)
18
18
  end
19
19
  end
@@ -3,7 +3,6 @@ require "yaml"
3
3
 
4
4
  module OpenapiValidator
5
5
  class FileLoader
6
-
7
6
  # @param [String] path path to file
8
7
  # @return [Hash] parsed file
9
8
  def self.call(path)
@@ -1,4 +1,4 @@
1
- require 'forwardable'
1
+ require "forwardable"
2
2
 
3
3
  module OpenapiValidator
4
4
  class PathValidator
@@ -14,8 +14,8 @@ module OpenapiValidator
14
14
  [path_key, method, @schema_code]
15
15
  end
16
16
 
17
- def fragment
18
- build_fragment.tap do |array|
17
+ def fragment(media_type: nil)
18
+ build_fragment(media_type: media_type).tap do |array|
19
19
  array.define_singleton_method(:split) do |_|
20
20
  self
21
21
  end
@@ -93,7 +93,7 @@ module OpenapiValidator
93
93
  raise(Error, "OpenAPI documentation does not have a documented path for #{path_key}")
94
94
  end
95
95
 
96
- def build_fragment
96
+ def build_fragment(media_type: nil)
97
97
  fragment =
98
98
  if @fragment_path
99
99
  @fragment_path.split("/")
@@ -101,7 +101,7 @@ module OpenapiValidator
101
101
  ["#", "paths", path_key, method, "responses", @schema_code]
102
102
  end
103
103
 
104
- fragment += ["content", media_type, "schema"] unless @empty_schema
104
+ fragment += ["content", media_type || self.media_type, "schema"] unless @empty_schema
105
105
 
106
106
  fragment
107
107
  end
@@ -1,6 +1,5 @@
1
1
  module OpenapiValidator
2
2
  class Request
3
-
4
3
  attr_reader :path, :method, :code, :media_type
5
4
 
6
5
  def self.call(**params)
@@ -1,10 +1,9 @@
1
- require 'openapi_validator/path_validator'
2
- require 'openapi_validator/response_validator'
3
- require 'openapi_validator/schema/json_validator'
1
+ require "openapi_validator/path_validator"
2
+ require "openapi_validator/response_validator"
3
+ require "openapi_validator/schema/json_validator"
4
4
 
5
5
  module OpenapiValidator
6
6
  class RequestValidator
7
-
8
7
  attr_reader :errors, :api_doc, :path_validator
9
8
 
10
9
  def valid?
@@ -20,7 +19,7 @@ module OpenapiValidator
20
19
  self
21
20
  end
22
21
 
23
- def validate_response(body:, code:)
22
+ def validate_response(body:, code:, media_type: request.media_type)
24
23
  if request.code != code.to_s
25
24
  @errors << "Path #{request.path} did not respond with expected status code. Expected #{request.code} got #{code}"
26
25
  end
@@ -29,7 +28,8 @@ module OpenapiValidator
29
28
  @errors << "Path #{request.path} should return empty response." unless body.empty?
30
29
  else
31
30
  @errors += OpenapiValidator::ResponseValidator.call(
32
- request: request, schema: validator.api_doc, data: body, fragment: path_validator.fragment, response: true
31
+ request: request, schema: validator.api_doc, data: body, media_type: media_type,
32
+ fragment: path_validator.fragment(media_type: media_type), response: true
33
33
  ).errors
34
34
  end
35
35
 
@@ -3,7 +3,6 @@ require "openapi_validator/response_validators/image_validator"
3
3
 
4
4
  module OpenapiValidator
5
5
  class ResponseValidator
6
-
7
6
  attr_reader :errors
8
7
 
9
8
  def valid?
@@ -21,27 +20,28 @@ module OpenapiValidator
21
20
 
22
21
  private
23
22
 
24
- attr_reader :request, :schema, :data, :fragment, :response
23
+ attr_reader :request, :schema, :data, :fragment, :response, :media_type
25
24
 
26
- def initialize(request:, schema:, data:, fragment:, response:)
25
+ def initialize(request:, schema:, data:, fragment:, response:, media_type:)
27
26
  @request = request
28
27
  @schema = schema
29
28
  @data = data
29
+ @media_type = media_type
30
30
  @fragment = fragment
31
31
  @response = response
32
32
  @errors = []
33
33
  end
34
34
 
35
35
  def validate_response
36
- @errors += validator.new(schema: schema, data: data, fragment: fragment, media_type: request.media_type, response: response).validate
36
+ @errors += validator.new(schema: schema, data: data, fragment: fragment, media_type: media_type, response: response).validate
37
37
  end
38
38
 
39
39
  def validator
40
- case request.media_type
41
- when 'application/json'
42
- OpenapiValidator::ResponseValidator::JsonValidator
40
+ case media_type
43
41
  when %r{^image/[^/]*$}
44
42
  OpenapiValidator::ResponseValidator::ImageValidator
43
+ else
44
+ OpenapiValidator::ResponseValidator::JsonValidator
45
45
  end
46
46
  end
47
47
  end
@@ -23,17 +23,17 @@ module OpenapiValidator
23
23
  attr_reader :schema, :data, :fragment, :media_type, :response, :property_name
24
24
 
25
25
  def validate_media_type
26
- type, sub_type = media_type.split('/')
26
+ type, sub_type = media_type.split("/")
27
27
  content = MimeMagic.by_magic(data)
28
28
 
29
- if content&.mediatype != type && (content&.subtype == sub_type || sub_type == '*')
29
+ if content&.mediatype != type && (content&.subtype == sub_type || sub_type == "*")
30
30
  @errors << "Content-type of property '#{property_name}' did not match the following content-type: #{media_type}"
31
31
  end
32
32
  end
33
33
 
34
34
  def validate_schema
35
- unless JSON::Schema::Attribute.data_valid_for_type?(data, 'string')
36
- @errors << "The property '#{property_name}' did not match the following type: #{type}"
35
+ unless JSON::Schema::Attribute.data_valid_for_type?(data, "string")
36
+ @errors << "The property '#{property_name}' did not match the following type: #{type}"
37
37
  end
38
38
  end
39
39
  end
@@ -1,4 +1,3 @@
1
-
2
1
  module OpenapiValidator
3
2
  class JsonValidator < JSON::Validator
4
3
  attr_reader :options
@@ -6,17 +6,17 @@ module OpenapiValidator
6
6
  return unless data.is_a?(Hash)
7
7
 
8
8
  schema = current_schema.schema
9
- defined_properties = schema['properties']
9
+ defined_properties = schema["properties"]
10
10
 
11
- schema['required'].each do |property, _property_schema|
12
- next if data.has_key?(property.to_s)
11
+ schema["required"].each do |property, _property_schema|
12
+ next if data.key?(property.to_s)
13
13
  prop_defaults = options[:insert_defaults] &&
14
14
  defined_properties &&
15
15
  defined_properties[property] &&
16
16
  !defined_properties[property]["default"].nil? &&
17
17
  !defined_properties[property]["readonly"]
18
18
 
19
- skip_error = processor.options[:response] && defined_properties[property]["writeOnly"]
19
+ skip_error = processor.options[:response] && defined_properties.dig(property, "writeOnly")
20
20
 
21
21
  if !prop_defaults && !skip_error
22
22
  message = "The property '#{build_fragment(fragments)}' did not contain a required property of '#{property}'"
@@ -6,7 +6,6 @@ require "openapi_validator/request_validator"
6
6
 
7
7
  module OpenapiValidator
8
8
  class Validator
9
-
10
9
  attr_reader :api_base_path, :unvalidated_requests, :api_doc
11
10
 
12
11
  # @return [DocumentationValidator] validation result
@@ -41,11 +40,12 @@ module OpenapiValidator
41
40
  def build_unvalidated_requests
42
41
  http_methods = %w[get put post delete options head patch trace]
43
42
  requests = []
44
- api_doc["paths"] && api_doc["paths"].each do |path, methods|
43
+
44
+ api_doc["paths"]&.each do |path, methods|
45
45
  methods.each do |method, values|
46
46
  next unless http_methods.include?(method)
47
47
 
48
- values["responses"] && values["responses"].each_key do |code|
48
+ values["responses"]&.each_key do |code|
49
49
  requests << [path, method, code]
50
50
  end
51
51
  end
@@ -1,3 +1,3 @@
1
1
  module OpenapiValidator
2
- VERSION = "0.4.1"
2
+ VERSION = "0.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openapi_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: '0.5'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Svyatoslav Kryukov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-05 00:00:00.000000000 Z
11
+ date: 2019-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema