openapi_parser 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 356ab2602b24fbfed684e6b190a057e504905f9f3d988d37bf4451aa660b5b60
4
- data.tar.gz: 8aa7530f3bdbcb15de4d705af5eb8e8152e3f15425ae3c17bacaed1faa5654f0
3
+ metadata.gz: c0a5e0fc6c25502d4dab97d5fff4956daa05f5e543302328c394655f34a575ba
4
+ data.tar.gz: 7497efbb3bfe8921c34570614b653930c2e122e24f9ecbc57d86b4e766b29a2b
5
5
  SHA512:
6
- metadata.gz: dfab290ea662aa9a4384252f8529ac7d0e0d205aa681cc4112f9bbf9223a0c49806f555080a8b6db8acd1bcdf2eb7ef24b50e931ed4a5dd4261d8de3ac81aecc
7
- data.tar.gz: fa80f1e7a495b13199e860c7869fe0507174097e10ab3eaa3710d39e8d38724ae5e397b395bed63a16ef0031594fc0d5439e455526d5b62eda480ba44a3c8a30
6
+ metadata.gz: c97aa5cbdaade862ecb079cf8c89767c4cfadb0c81a591735892a4a2ba3f61955c1d41f10fddff8d1069f64e2d79eca5098267b64145f861e54ae979c4b58a5a
7
+ data.tar.gz: 06c3c526614c98ec489fa1d87237d44d428a9031bbd8fa405aa77d144226dcc2c6a03b44421a10edc94d049cde6e56fcb2cf8602a789f0a12bec3606f0c2776c
@@ -1,5 +1,8 @@
1
1
  ## Unreleased
2
2
 
3
+ ## 0.4.0 (2019-07-27)
4
+ * Add minimum and maximum checks for `integer` and `number` data types (#43)
5
+
3
6
  ## 0.3.1 (2019-06-04)
4
7
  * Add additionalProperties default value (#40)
5
8
 
@@ -110,6 +110,50 @@ module OpenAPIParser
110
110
  end
111
111
  end
112
112
 
113
+ class LessThanMinimum < OpenAPIError
114
+ def initialize(value, reference)
115
+ super(reference)
116
+ @value = value
117
+ end
118
+
119
+ def message
120
+ "#{@value} cannot be less than minimum value in #{@reference}"
121
+ end
122
+ end
123
+
124
+ class LessThanExclusiveMinimum < OpenAPIError
125
+ def initialize(value, reference)
126
+ super(reference)
127
+ @value = value
128
+ end
129
+
130
+ def message
131
+ "#{@value} cannot be less than or equal to exclusive minimum value in #{@reference}"
132
+ end
133
+ end
134
+
135
+ class MoreThanMaximum < OpenAPIError
136
+ def initialize(value, reference)
137
+ super(reference)
138
+ @value = value
139
+ end
140
+
141
+ def message
142
+ "#{@value} cannot be more than maximum value in #{@reference}"
143
+ end
144
+ end
145
+
146
+ class MoreThanExclusiveMaximum < OpenAPIError
147
+ def initialize(value, reference)
148
+ super(reference)
149
+ @value = value
150
+ end
151
+
152
+ def message
153
+ "#{@value} cannot be more than or equal to exclusive maximum value in #{@reference}"
154
+ end
155
+ end
156
+
113
157
  class InvalidPattern < OpenAPIError
114
158
  def initialize(value, pattern, reference)
115
159
  super(reference)
@@ -1,5 +1,6 @@
1
1
  require_relative 'schema_validators/options'
2
2
  require_relative 'schema_validators/enumable'
3
+ require_relative 'schema_validators/minimum_maximum'
3
4
  require_relative 'schema_validators/base'
4
5
  require_relative 'schema_validators/string_validator'
5
6
  require_relative 'schema_validators/integer_validator'
@@ -1,6 +1,7 @@
1
1
  class OpenAPIParser::SchemaValidator
2
2
  class FloatValidator < Base
3
3
  include ::OpenAPIParser::SchemaValidator::Enumable
4
+ include ::OpenAPIParser::SchemaValidator::MinimumMaximum
4
5
 
5
6
  # validate float value by schema
6
7
  # @param [Object] value
@@ -18,7 +19,10 @@ class OpenAPIParser::SchemaValidator
18
19
  def coercer_and_validate_numeric(value, schema)
19
20
  return OpenAPIParser::ValidateError.build_error_result(value, schema) unless value.kind_of?(Numeric)
20
21
 
21
- check_enum_include(value, schema)
22
+ value, err = check_enum_include(value, schema)
23
+ return [nil, err] if err
24
+
25
+ check_minimum_maximum(value, schema)
22
26
  end
23
27
 
24
28
  def coerce(value)
@@ -1,6 +1,7 @@
1
1
  class OpenAPIParser::SchemaValidator
2
2
  class IntegerValidator < Base
3
3
  include ::OpenAPIParser::SchemaValidator::Enumable
4
+ include ::OpenAPIParser::SchemaValidator::MinimumMaximum
4
5
 
5
6
  # validate integer value by schema
6
7
  # @param [Object] value
@@ -10,7 +11,10 @@ class OpenAPIParser::SchemaValidator
10
11
 
11
12
  return OpenAPIParser::ValidateError.build_error_result(value, schema) unless value.kind_of?(Integer)
12
13
 
13
- check_enum_include(value, schema)
14
+ value, err = check_enum_include(value, schema)
15
+ return [nil, err] if err
16
+
17
+ check_minimum_maximum(value, schema)
14
18
  end
15
19
 
16
20
  private
@@ -0,0 +1,38 @@
1
+ class OpenAPIParser::SchemaValidator
2
+ module MinimumMaximum
3
+ # check minimum and maximum value by schema
4
+ # @param [Object] value
5
+ # @param [OpenAPIParser::Schemas::Schema] schema
6
+ def check_minimum_maximum(value, schema)
7
+ include_min_max = schema.minimum || schema.maximum
8
+ return [value, nil] unless include_min_max
9
+
10
+ validate(value, schema)
11
+ [value, nil]
12
+ rescue OpenAPIParser::OpenAPIError => e
13
+ return [nil, e]
14
+ end
15
+
16
+ private
17
+
18
+ def validate(value, schema)
19
+ reference = schema.object_reference
20
+
21
+ if schema.minimum
22
+ if schema.exclusiveMinimum && value <= schema.minimum
23
+ raise OpenAPIParser::LessThanExclusiveMinimum.new(value, reference)
24
+ elsif value < schema.minimum
25
+ raise OpenAPIParser::LessThanMinimum.new(value, reference)
26
+ end
27
+ end
28
+
29
+ if schema.maximum
30
+ if schema.exclusiveMaximum && value >= schema.maximum
31
+ raise OpenAPIParser::MoreThanExclusiveMaximum.new(value, reference)
32
+ elsif value > schema.maximum
33
+ raise OpenAPIParser::MoreThanMaximum.new(value, reference)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  module OpenAPIParser
2
- VERSION = '0.3.1'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openapi_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ota42y
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-04 00:00:00.000000000 Z
11
+ date: 2019-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -178,6 +178,7 @@ files:
178
178
  - lib/openapi_parser/schema_validators/enumable.rb
179
179
  - lib/openapi_parser/schema_validators/float_validator.rb
180
180
  - lib/openapi_parser/schema_validators/integer_validator.rb
181
+ - lib/openapi_parser/schema_validators/minimum_maximum.rb
181
182
  - lib/openapi_parser/schema_validators/nil_validator.rb
182
183
  - lib/openapi_parser/schema_validators/object_validator.rb
183
184
  - lib/openapi_parser/schema_validators/one_of_validator.rb