module Kraftwerk::Endpoint::Validatable

Public Class Methods

prepended(base) click to toggle source
# File lib/kraftwerk/endpoint/validatable.rb, line 23
def self.prepended(base)
  base.class_eval do
    class_attribute :validation_class
    extend ClassMethods
  end
end

Public Instance Methods

call(params) click to toggle source

Unlike in Hanami, where we usually want to validate params manually and, if they are invalid, take a proper action, in Kraftwerk if validation is not passed, the request should not happen at all. Instead, error messages are returned.

Calls superclass method
# File lib/kraftwerk/endpoint/validatable.rb, line 10
def call(params)
  validation_class = self.class.validation_class
  return super if validation_class.nil?

  result = validation_class.new.call(params.to_h)
  if result.success?
    # pass only whitelisted parameters down
    super(result.to_h)
  else
    Response.new(code: 400, body: result.errors.to_h)
  end
end