Module: Trailblazer::Operation::Contract::Validate

Defined in:
lib/trailblazer/operation/validate.rb

Class Method Summary collapse

Class Method Details

.Call(name: "default", representer: false, params_path: nil) ⇒ Object

Macro: Validates contract ‘:name`.



38
39
40
41
42
43
44
45
46
# File 'lib/trailblazer/operation/validate.rb', line 38

def self.Call(name:"default", representer:false, params_path:nil)
  step = ->(input, options) {
    validate!(options, name: name, representer: options["representer.#{name}.class"], params_path: params_path)
  }

  step = Pipetree::Step.new( step, "representer.#{name}.class" => representer )

  [ step, name: "contract.#{name}.call" ]
end

.Extract(key: nil, params_path: nil) ⇒ Object

Macro: extract the contract’s input from params by reading ‘:key`.



28
29
30
31
32
33
34
35
# File 'lib/trailblazer/operation/validate.rb', line 28

def self.Extract(key:nil, params_path:nil)
  # TODO: introduce nested pipes and pass composed input instead.
  step = ->(input, options) do
    options[params_path] = key ? options["params"][key] : options["params"]
  end

  [ step, name: params_path ]
end

.validate!(options, name: nil, representer: false, from: "document", params_path: nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/trailblazer/operation/validate.rb', line 48

def self.validate!(options, name:nil, representer:false, from: "document", params_path:nil)
  path     = "contract.#{name}"
  contract = options[path]

  # this is for 1.1-style compatibility and should be removed once we have Deserializer in place:
  options["result.#{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.(options[from]) { |document| representer.new(contract).parse(document) }
    else
      # let Reform handle the deserialization.
      contract.(options[params_path])
    end

  result.success?
end