Class: Trailblazer::Macro::Contract::Validate
- Inherits:
-
Object
- Object
- Trailblazer::Macro::Contract::Validate
- Defined in:
- lib/trailblazer/macro/contract/validate.rb
Defined Under Namespace
Classes: Extract
Instance Method Summary collapse
-
#call(ctx) ⇒ Object
Task: Validates contract ‘:name`.
-
#initialize(name: "default", contract_path:, representer: false, params_path: nil) ⇒ Validate
constructor
A new instance of Validate.
- #validate!(ctx, representer: false, from: :document, params_path: nil) ⇒ Object
Constructor Details
#initialize(name: "default", contract_path:, representer: false, params_path: nil) ⇒ Validate
Returns a new instance of Validate.
62 63 64 |
# File 'lib/trailblazer/macro/contract/validate.rb', line 62 def initialize(name: "default", contract_path:, representer: false, params_path: nil) @name, @representer, @params_path, @contract_path = name, representer, params_path, contract_path end |
Instance Method Details
#call(ctx) ⇒ Object
Task: Validates contract ‘:name`.
67 68 69 70 71 72 73 |
# File 'lib/trailblazer/macro/contract/validate.rb', line 67 def call(ctx, **) validate!( ctx, representer: ctx[:"representer.#{@name}.class"] ||= @representer, # FIXME: maybe @representer should use DI. params_path: @params_path ) end |
#validate!(ctx, representer: false, from: :document, params_path: nil) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/trailblazer/macro/contract/validate.rb', line 75 def validate!(ctx, representer: false, from: :document, params_path: nil) contract = ctx[@contract_path] # grab contract instance from "contract.default" (usually set in {Contract::Build()}) # this is for 1.1-style compatibility and should be removed once we have Deserializer in place: ctx[:"result.#{@contract_path}"] = result = if representer # use :document as the body and let the representer deserialize to the contract. # this will be simplified once we have Deserializer. # translates to contract.("{document: bla}") { MyRepresenter.new(contract).from_json .. } contract.(ctx[from]) { |document| representer.new(contract).parse(document) } else # let Reform handle the deserialization. contract.(ctx[params_path]) end result.success? end |