Module: Kraftwerk::Endpoint::Validatable
- Defined in:
- lib/kraftwerk/endpoint/validatable.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#call(params) ⇒ Object
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.
Class Method Details
.prepended(base) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/kraftwerk/endpoint/validatable.rb', line 23 def self.prepended(base) base.class_eval do class_attribute :validation_class extend ClassMethods end end |
Instance Method Details
#call(params) ⇒ Object
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.
10 11 12 13 14 15 16 17 18 19 20 21 |
# 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 |