openapi3_parser 0.4.0 → 0.5.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 +4 -4
- data/.rubocop.yml +2 -0
- data/CHANGELOG.md +8 -0
- data/README.md +1 -1
- data/TODO.md +4 -4
- data/lib/openapi3_parser/array_sentence.rb +12 -0
- data/lib/openapi3_parser/cautious_dig.rb +39 -0
- data/lib/openapi3_parser/context.rb +53 -1
- data/lib/openapi3_parser/context/location.rb +1 -0
- data/lib/openapi3_parser/context/pointer.rb +67 -5
- data/lib/openapi3_parser/document.rb +45 -4
- data/lib/openapi3_parser/error.rb +9 -0
- data/lib/openapi3_parser/node/array.rb +14 -4
- data/lib/openapi3_parser/node/map.rb +45 -3
- data/lib/openapi3_parser/node/object.rb +25 -5
- data/lib/openapi3_parser/node_factory.rb +0 -150
- data/lib/openapi3_parser/node_factory/array.rb +198 -0
- data/lib/openapi3_parser/node_factory/callback.rb +24 -0
- data/lib/openapi3_parser/{node_factories → node_factory}/components.rb +24 -25
- data/lib/openapi3_parser/{node_factories → node_factory}/contact.rb +5 -6
- data/lib/openapi3_parser/{node_factories → node_factory}/discriminator.rb +6 -7
- data/lib/openapi3_parser/{node_factories → node_factory}/encoding.rb +6 -8
- data/lib/openapi3_parser/{node_factories → node_factory}/example.rb +4 -5
- data/lib/openapi3_parser/{node_factories → node_factory}/external_documentation.rb +4 -5
- data/lib/openapi3_parser/node_factory/field.rb +129 -0
- data/lib/openapi3_parser/node_factory/fields/reference.rb +54 -18
- data/lib/openapi3_parser/{node_factories → node_factory}/header.rb +4 -5
- data/lib/openapi3_parser/{node_factories → node_factory}/info.rb +6 -7
- data/lib/openapi3_parser/{node_factories → node_factory}/license.rb +4 -5
- data/lib/openapi3_parser/{node_factories → node_factory}/link.rb +6 -8
- data/lib/openapi3_parser/node_factory/map.rb +206 -21
- data/lib/openapi3_parser/{node_factories → node_factory}/media_type.rb +17 -16
- data/lib/openapi3_parser/{node_factories → node_factory}/oauth_flow.rb +2 -4
- data/lib/openapi3_parser/{node_factories → node_factory}/oauth_flows.rb +4 -6
- data/lib/openapi3_parser/node_factory/object.rb +66 -63
- data/lib/openapi3_parser/node_factory/object_factory/dsl.rb +50 -0
- data/lib/openapi3_parser/node_factory/object_factory/field_config.rb +88 -0
- data/lib/openapi3_parser/node_factory/object_factory/node_builder.rb +96 -0
- data/lib/openapi3_parser/node_factory/object_factory/validator.rb +172 -0
- data/lib/openapi3_parser/node_factory/openapi.rb +65 -0
- data/lib/openapi3_parser/node_factory/operation.rb +87 -0
- data/lib/openapi3_parser/node_factory/optional_reference.rb +7 -3
- data/lib/openapi3_parser/{node_factories → node_factory}/parameter.rb +16 -22
- data/lib/openapi3_parser/node_factory/parameter_like.rb +42 -0
- data/lib/openapi3_parser/{node_factories → node_factory}/path_item.rb +27 -29
- data/lib/openapi3_parser/{node_factories → node_factory}/paths.rb +21 -27
- data/lib/openapi3_parser/node_factory/recursive_pointer.rb +17 -0
- data/lib/openapi3_parser/node_factory/reference.rb +48 -0
- data/lib/openapi3_parser/{node_factories → node_factory}/request_body.rb +15 -19
- data/lib/openapi3_parser/{node_factories → node_factory}/response.rb +18 -21
- data/lib/openapi3_parser/node_factory/responses.rb +51 -0
- data/lib/openapi3_parser/{node_factories → node_factory}/schema.rb +33 -33
- data/lib/openapi3_parser/node_factory/security_requirement.rb +21 -0
- data/lib/openapi3_parser/{node_factories → node_factory}/security_scheme.rb +4 -6
- data/lib/openapi3_parser/{node_factories → node_factory}/server.rb +6 -8
- data/lib/openapi3_parser/{node_factories → node_factory}/server_variable.rb +10 -12
- data/lib/openapi3_parser/{node_factories → node_factory}/tag.rb +4 -6
- data/lib/openapi3_parser/node_factory/type_checker.rb +103 -0
- data/lib/openapi3_parser/{node_factories → node_factory}/xml.rb +4 -5
- data/lib/openapi3_parser/source/reference_resolver.rb +3 -3
- data/lib/openapi3_parser/validation/error.rb +9 -0
- data/lib/openapi3_parser/validation/input_validator.rb +18 -0
- data/lib/openapi3_parser/validation/validatable.rb +44 -0
- data/lib/openapi3_parser/validators/mutually_exclusive_fields.rb +121 -0
- data/lib/openapi3_parser/validators/required_fields.rb +37 -0
- data/lib/openapi3_parser/validators/unexpected_fields.rb +52 -0
- data/lib/openapi3_parser/version.rb +1 -1
- metadata +48 -38
- data/lib/openapi3_parser/node_factories/array.rb +0 -114
- data/lib/openapi3_parser/node_factories/callback.rb +0 -27
- data/lib/openapi3_parser/node_factories/map.rb +0 -120
- data/lib/openapi3_parser/node_factories/openapi.rb +0 -62
- data/lib/openapi3_parser/node_factories/operation.rb +0 -84
- data/lib/openapi3_parser/node_factories/parameter/parameter_like.rb +0 -41
- data/lib/openapi3_parser/node_factories/reference.rb +0 -35
- data/lib/openapi3_parser/node_factories/responses.rb +0 -60
- data/lib/openapi3_parser/node_factories/security_requirement.rb +0 -26
- data/lib/openapi3_parser/node_factory/field_config.rb +0 -88
- data/lib/openapi3_parser/node_factory/object/node_builder.rb +0 -97
- data/lib/openapi3_parser/node_factory/object/validator.rb +0 -176
@@ -1,176 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "openapi3_parser/context"
|
4
|
-
require "openapi3_parser/node_factory"
|
5
|
-
require "openapi3_parser/validation/error"
|
6
|
-
|
7
|
-
module Openapi3Parser
|
8
|
-
module NodeFactory
|
9
|
-
module Object
|
10
|
-
class Validator
|
11
|
-
def self.missing_required_fields(input, factory)
|
12
|
-
configs = factory.field_configs
|
13
|
-
configs.each_with_object([]) do |(name, field_config), memo|
|
14
|
-
f = input[name]
|
15
|
-
is_nil = f.respond_to?(:nil_input?) ? f.nil_input? : f.nil?
|
16
|
-
memo << name if field_config.required?(factory) && is_nil
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.unexpected_fields(input, factory)
|
21
|
-
unexpected_keys = input.keys - factory.field_configs.keys
|
22
|
-
if factory.allowed_extensions?
|
23
|
-
unexpected_keys.reject { |k| k =~ NodeFactory::EXTENSION_REGEX }
|
24
|
-
else
|
25
|
-
unexpected_keys
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def initialize(input, factory)
|
30
|
-
@input = input
|
31
|
-
@factory = factory
|
32
|
-
end
|
33
|
-
|
34
|
-
def errors
|
35
|
-
[
|
36
|
-
missing_required_fields_error,
|
37
|
-
unexpected_fields_error,
|
38
|
-
mututally_exclusive_fields_errors,
|
39
|
-
invalid_field_errors
|
40
|
-
].flatten.compact
|
41
|
-
end
|
42
|
-
|
43
|
-
private
|
44
|
-
|
45
|
-
attr_reader :input, :factory
|
46
|
-
|
47
|
-
def context
|
48
|
-
factory.context
|
49
|
-
end
|
50
|
-
|
51
|
-
def raw_input
|
52
|
-
context.input
|
53
|
-
end
|
54
|
-
|
55
|
-
def missing_required_fields_error
|
56
|
-
fields = self.class.missing_required_fields(input, factory)
|
57
|
-
return unless fields.any?
|
58
|
-
Validation::Error.new(
|
59
|
-
"Missing required fields: #{fields.join(', ')}",
|
60
|
-
context,
|
61
|
-
factory.class
|
62
|
-
)
|
63
|
-
end
|
64
|
-
|
65
|
-
def unexpected_fields_error
|
66
|
-
fields = self.class.unexpected_fields(input, factory)
|
67
|
-
return unless fields.any?
|
68
|
-
Validation::Error.new(
|
69
|
-
"Unexpected fields: #{fields.join(', ')}",
|
70
|
-
context,
|
71
|
-
factory.class
|
72
|
-
)
|
73
|
-
end
|
74
|
-
|
75
|
-
def invalid_field_errors
|
76
|
-
factory.field_configs.inject([]) do |memo, (name, field_config)|
|
77
|
-
memo + Array(field_errors(name, field_config))
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
def mututally_exclusive_fields_errors
|
82
|
-
MututallyExclusiveFieldErrors.call(factory, input)
|
83
|
-
end
|
84
|
-
|
85
|
-
def field_errors(name, field_config)
|
86
|
-
return if input[name].nil?
|
87
|
-
field = input[name]
|
88
|
-
|
89
|
-
if field.respond_to?(:errors) && !field.errors.empty?
|
90
|
-
field.errors.to_a
|
91
|
-
else
|
92
|
-
build_type_error(name, field_config) ||
|
93
|
-
build_validation_errors(name, field_config)
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
def build_type_error(name, field_config)
|
98
|
-
type_error = field_config.input_type_error(raw_input[name], factory)
|
99
|
-
return unless type_error
|
100
|
-
Validation::Error.new(
|
101
|
-
"Invalid type: #{type_error}",
|
102
|
-
Context.next_field(context, name),
|
103
|
-
factory.class
|
104
|
-
)
|
105
|
-
end
|
106
|
-
|
107
|
-
def build_validation_errors(name, field_config)
|
108
|
-
field_config.validation_errors(
|
109
|
-
raw_input[name], factory
|
110
|
-
).map do |error|
|
111
|
-
Validation::Error.new(
|
112
|
-
error,
|
113
|
-
Context.next_field(context, name),
|
114
|
-
factory.class
|
115
|
-
)
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
class MututallyExclusiveFieldErrors
|
120
|
-
def self.call(*args)
|
121
|
-
new.call(*args)
|
122
|
-
end
|
123
|
-
|
124
|
-
def call(factory, input)
|
125
|
-
factory
|
126
|
-
.mutually_exclusive_fields
|
127
|
-
.each_with_object([]) do |mutually_exclusive, memo|
|
128
|
-
error = determine_error(mutually_exclusive, input, factory)
|
129
|
-
memo << error if error
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
private
|
134
|
-
|
135
|
-
def determine_error(mutually_exclusive, input, factory)
|
136
|
-
fields = mutually_exclusive.fields
|
137
|
-
number_non_nil = count_non_nil_fields(fields, input)
|
138
|
-
|
139
|
-
if number_non_nil.zero? && mutually_exclusive.required
|
140
|
-
required_error(fields, factory)
|
141
|
-
elsif number_non_nil > 1
|
142
|
-
exclusive_error(fields, factory)
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
def count_non_nil_fields(fields, input)
|
147
|
-
fields.count do |field|
|
148
|
-
data = input[field]
|
149
|
-
data.respond_to?(:nil_input?) ? !data.nil_input? : !data.nil?
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
def required_error(fields, factory)
|
154
|
-
Validation::Error.new(
|
155
|
-
"one of #{sentence_fields(fields)} is required",
|
156
|
-
factory.context,
|
157
|
-
factory.class
|
158
|
-
)
|
159
|
-
end
|
160
|
-
|
161
|
-
def exclusive_error(fields, factory)
|
162
|
-
Validation::Error.new(
|
163
|
-
"#{sentence_fields(fields)} are mutually exclusive fields",
|
164
|
-
factory.context,
|
165
|
-
factory.class
|
166
|
-
)
|
167
|
-
end
|
168
|
-
|
169
|
-
def sentence_fields(fields)
|
170
|
-
fields[0..-2].join(", ") + " and " + fields[-1]
|
171
|
-
end
|
172
|
-
end
|
173
|
-
end
|
174
|
-
end
|
175
|
-
end
|
176
|
-
end
|