Module: Easyop::Schema
- Defined in:
- lib/easyop/schema.rb
Overview
Optional typed schema DSL for Operation inputs and outputs.
When included (automatically when you use ‘params` or `result` in an Operation), it adds before/after hooks that validate ctx against the declared schema using the configured type adapter.
Usage:
params do
required :email, String
required :amount, Integer
optional :note, String
optional :retry, :boolean, default: false
end
result do
required :record, ActiveRecord::Base
optional :token, String
end
Type symbols (:boolean, :string, :integer, :float) are mapped to native Ruby classes. Pass an actual class (String, User, etc.) for strict is_a? checking. Type validation only happens if Easyop.config.type_adapter != :none.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#_validate_params! ⇒ Object
── Instance validation methods ───────────────────────────────────────────.
- #_validate_result! ⇒ Object
Class Method Details
.included(base) ⇒ Object
25 26 27 |
# File 'lib/easyop/schema.rb', line 25 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#_validate_params! ⇒ Object
── Instance validation methods ───────────────────────────────────────────
62 63 64 65 66 |
# File 'lib/easyop/schema.rb', line 62 def _validate_params! schema = self.class._param_schema return unless schema schema.validate!(ctx, phase: :params) end |
#_validate_result! ⇒ Object
68 69 70 71 72 |
# File 'lib/easyop/schema.rb', line 68 def _validate_result! schema = self.class._result_schema return unless schema && ctx.success? schema.validate!(ctx, phase: :result) end |