openapi_validator 0.3.3 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ee3a6bfb7172dc7ba0758635a8a4303a5c4ea79c5548057340772ec3b41af5b
4
- data.tar.gz: 9bda0a6bde8325778ba45494c70030919ae96b7422fab576c57404483463e9c2
3
+ metadata.gz: 93502f2aa2c08a5117fc3415e972b5f6899e2b4314e4c08ce4e6c8255f1a97ad
4
+ data.tar.gz: 48687e1b4d85050e42c3d654fb05976d6b1c09c036988c24518e147b4d970612
5
5
  SHA512:
6
- metadata.gz: 9f3e2bad86407dd53b7fc0723b6e7149aa2b7f97ddb0b942e9e19407f0c8e3b064165373250eb12432255eed1374f19da9ee5dddbd89e13a15ce6f7a031143e9
7
- data.tar.gz: 0fc7db57b782c4727adcf3494412779c328038bba545d216370d52f5eeddb1c67f3bab7b0aee15a67c89bf045c762036fe4d9e9b8299a1a4a994bbd891ff921b
6
+ metadata.gz: b1fd3b961f82298c075bdd1f55de01dd8954eeca93a59844161e02d2552dcebeb95049626f8372af9d84db771977a633f732c9692e71ddef66a30e7b92767ab4
7
+ data.tar.gz: 87cc000fbf5151e40844ca29dc710418a5f8a5b7a1364447ee1634ad2e9f9a3444317200e2c73f58026a6fc0f37a89abeb0a22950d7ee3bc1f90b79fc665f11e
@@ -8,7 +8,7 @@ module OpenapiValidator
8
8
  end
9
9
 
10
10
  def path_key
11
- path[/(\/[-_\/\{\}\w]*)/]
11
+ path[%r{(/[-_/\{\}\w]*)}]
12
12
  end
13
13
 
14
14
  private
@@ -27,7 +27,9 @@ module OpenapiValidator
27
27
  if path_validator.empty_schema?
28
28
  @errors << "Path #{request.path} should return empty response." unless body.empty?
29
29
  else
30
- @errors += JsonValidator.fully_validate(validator.api_doc, body, fragment: path_validator.fragment, response: true)
30
+ @errors += OpenapiValidator::ResponseValidator.call(
31
+ request: request, schema: validator.api_doc, data: body, fragment: path_validator.fragment, response: true
32
+ ).errors
31
33
  end
32
34
 
33
35
  validator.remove_validated_path(path_validator.path) if @errors.empty?
@@ -0,0 +1,48 @@
1
+ require "openapi_validator/response_validators/json_validator"
2
+ require "openapi_validator/response_validators/image_validator"
3
+
4
+ module OpenapiValidator
5
+ class ResponseValidator
6
+
7
+ attr_reader :errors
8
+
9
+ def valid?
10
+ errors.empty?
11
+ end
12
+
13
+ def self.call(**params)
14
+ new(**params).call
15
+ end
16
+
17
+ def call
18
+ validate_response
19
+ self
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :request, :schema, :data, :fragment, :response
25
+
26
+ def initialize(request:, schema:, data:, fragment:, response:)
27
+ @request = request
28
+ @schema = schema
29
+ @data = data
30
+ @fragment = fragment
31
+ @response = response
32
+ @errors = []
33
+ end
34
+
35
+ def validate_response
36
+ @errors += validator.new(schema: schema, data: data, fragment: fragment, media_type: request.media_type, response: response).validate
37
+ end
38
+
39
+ def validator
40
+ case request.media_type
41
+ when 'application/json'
42
+ OpenapiValidator::ResponseValidator::JsonValidator
43
+ when %r{^image/[^/]*$}
44
+ OpenapiValidator::ResponseValidator::ImageValidator
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,41 @@
1
+ module OpenapiValidator
2
+ class ResponseValidator
3
+ class ImageValidator
4
+ def initialize(schema:, data:, fragment:, media_type:, response:)
5
+ @schema = schema
6
+ @data = data
7
+ @fragment = fragment
8
+ @media_type = media_type
9
+ @response = response
10
+ @property_name = JSON::Schema::Attribute.build_fragment([fragment])
11
+ @errors = []
12
+ end
13
+
14
+ def validate
15
+ validate_media_type
16
+ validate_schema
17
+
18
+ @errors
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :schema, :data, :fragment, :media_type, :response, :property_name
24
+
25
+ def validate_media_type
26
+ type, sub_type = media_type.split('/')
27
+ content = MimeMagic.by_magic(data)
28
+
29
+ if content&.mediatype != type && (content&.subtype == sub_type || sub_type == '*')
30
+ @errors << "Content-type of property '#{property_name}' did not match the following content-type: #{media_type}"
31
+ end
32
+ end
33
+
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}"
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,21 @@
1
+ module OpenapiValidator
2
+ class ResponseValidator
3
+ class JsonValidator
4
+ def initialize(schema:, data:, fragment:, media_type:, response:)
5
+ @schema = schema
6
+ @data = data
7
+ @fragment = fragment
8
+ @media_type = media_type
9
+ @response = response
10
+ end
11
+
12
+ def validate
13
+ OpenapiValidator::JsonValidator.fully_validate(schema, data, fragment: fragment, response: response)
14
+ end
15
+
16
+ private
17
+
18
+ attr_reader :schema, :data, :fragment, :media_type, :response, :errors
19
+ end
20
+ end
21
+ end
@@ -1,3 +1,3 @@
1
1
  module OpenapiValidator
2
- VERSION = "0.3.3"
2
+ VERSION = "0.4.0"
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.3.3
4
+ version: 0.4.0
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-06-30 00:00:00.000000000 Z
11
+ date: 2019-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema
@@ -83,6 +83,9 @@ files:
83
83
  - lib/openapi_validator/path_validator.rb
84
84
  - lib/openapi_validator/request.rb
85
85
  - lib/openapi_validator/request_validator.rb
86
+ - lib/openapi_validator/response_validator.rb
87
+ - lib/openapi_validator/response_validators/image_validator.rb
88
+ - lib/openapi_validator/response_validators/json_validator.rb
86
89
  - lib/openapi_validator/schema/json_validator.rb
87
90
  - lib/openapi_validator/schema/required_attribute.rb
88
91
  - lib/openapi_validator/schema/type_attribute.rb