-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathconfiguration.rb
More file actions
55 lines (46 loc) · 1.67 KB
/
configuration.rb
File metadata and controls
55 lines (46 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true
module OpenapiFirst
# Global configuration. Currently only used for the request validation middleware.
class Configuration
HOOKS = %i[
after_request_validation
after_response_validation
after_request_parameter_property_validation
after_request_body_property_validation
].freeze
def initialize
@request_validation_error_response = OpenapiFirst.find_error_response(:default)
@request_validation_raise_error = false
@response_validation_raise_error = true
@hooks = HOOKS.to_h { [_1, Set.new] }
@path = nil
end
def register(path_or_definition, as: :default)
OpenapiFirst.register(path_or_definition, as:)
end
attr_reader :request_validation_error_response, :hooks
attr_accessor :request_validation_raise_error, :response_validation_raise_error, :path
# Return a child configuration that still receives updates of global hooks.
def child
ChildConfiguration.new(parent: self)
end
# @visibility private
def clone
raise NoMethodError, 'OpenapiFirst::Configuration#clone was removed. You want to call #child instead'
end
HOOKS.each do |hook|
define_method(hook) do |&block|
return hooks[hook] if block.nil?
hooks[hook] << block
block
end
end
def request_validation_error_response=(mod)
@request_validation_error_response = if mod.is_a?(Symbol)
OpenapiFirst.find_error_response(mod)
else
mod
end
end
end
end