Class: SwaggerShield::BeforeAction

Inherits:
Object
  • Object
show all
Defined in:
lib/swagger_shield/before_action.rb

Instance Method Summary collapse

Constructor Details

#initialize(shield) ⇒ BeforeAction

Returns a new instance of BeforeAction.



3
4
5
# File 'lib/swagger_shield/before_action.rb', line 3

def initialize(shield)
  @shield = shield
end

Instance Method Details

#before(controller) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/swagger_shield/before_action.rb', line 7

def before(controller)
  request = controller.request
  params = controller.params

  errors = shield.validate(
    request.path,
    request.method,
    params.to_unsafe_h
  )

  unless errors.empty?
    formatted = errors.map { |error|
      {
        status: '422',
        detail: error[:message],
        source: { pointer: error[:fragment] }
      }
    }
    controller.render json: { errors: formatted }, status: :unprocessable_entity
  end
end